Skip to content

Commit

Permalink
Add an IdentityStorageStrategy.
Browse files Browse the repository at this point in the history
  • Loading branch information
renggli committed Dec 17, 2023
1 parent 031fc8a commit 7f28976
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/src/graph/strategy.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'strategy/identity.dart';
import 'strategy/integer.dart';
import 'strategy/object.dart';
import 'strategy/positive_integer.dart';
Expand All @@ -10,6 +11,9 @@ abstract class StorageStrategy<T> {
/// Returns a strategy using canonical collection objects.
factory StorageStrategy.object() = ObjectStorageStrategy;

/// Returns a strategy using canonical collection objects and object identity.
factory StorageStrategy.identity() = IdentityStorageStrategy;

/// Returns a strategy for [int] objects.
static StorageStrategy<int> integer() => IntegerStorageStrategy();

Expand Down
12 changes: 12 additions & 0 deletions lib/src/graph/strategy/identity.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import '../strategy.dart';

/// Strategy using canonical collection objects and identity comparison.
class IdentityStorageStrategy<T> implements StorageStrategy<T> {
const IdentityStorageStrategy();

@override
Set<T> createSet() => Set<T>.identity();

@override
Map<T, R> createMap<R>() => Map<T, R>.identity();
}
21 changes: 20 additions & 1 deletion test/graph_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -148,15 +148,34 @@ void main() {
test('set', () {
final set = strategy.createSet();
set.addAll(['foo', 'bar', 'foo']);
expect(set, ['foo', 'bar']);
expect(set, hasLength(2));
expect(set, unorderedEquals(['foo', 'bar']));
});
test('map', () {
final map = strategy.createMap<int>();
map['foo'] = 42;
map['bar'] = 43;
expect(map, hasLength(2));
expect(map, {'foo': 42, 'bar': 43});
});
});
group('identity', () {
final strategy = StorageStrategy<Point<int>>.identity();
final first = const Point(1, 2),
second = const Point(2, 3) - const Point(1, 1);
test('set', () {
final set = strategy.createSet();
set.addAll([first, second]);
expect(set, unorderedEquals([first, second]));
});
test('map', () {
final map = strategy.createMap<int>();
map[first] = 42;
map[second] = 43;
expect(map.keys, unorderedEquals([first, second]));
expect(map.values, unorderedEquals([42, 43]));
});
});
group('integer', () {
final strategy = StorageStrategy.integer();
test('set', () {
Expand Down

0 comments on commit 7f28976

Please sign in to comment.