-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add EqualityMap and EqualitySet. (dart-archive/collection#35)
- Loading branch information
Showing
7 changed files
with
157 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:collection'; | ||
|
||
import 'equality.dart'; | ||
import 'wrappers.dart'; | ||
|
||
/// A [Map] whose key equality is determined by an [Equality] object. | ||
class EqualityMap<K, V> extends DelegatingMap<K, V> { | ||
/// Creates a map with equality based on [equality]. | ||
EqualityMap(Equality<K> equality) | ||
: super(new LinkedHashMap( | ||
equals: equality.equals, | ||
hashCode: equality.hash, | ||
isValidKey: equality.isValidKey)); | ||
|
||
/// Creates a map with equality based on [equality] that contains all | ||
/// key-value pairs of [other]. | ||
/// | ||
/// If [other] has multiple keys that are equivalent according to [equality], | ||
/// the last one reached during iteration takes precedence. | ||
EqualityMap.from(Equality<K> equality, Map<K, V> other) | ||
: super(new LinkedHashMap( | ||
equals: equality.equals, | ||
hashCode: equality.hash, | ||
isValidKey: equality.isValidKey)) { | ||
addAll(other); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'dart:collection'; | ||
|
||
import 'equality.dart'; | ||
import 'wrappers.dart'; | ||
|
||
/// A [Map] whose key equality is determined by an [Equality] object. | ||
class EqualitySet<E> extends DelegatingSet<E> { | ||
/// Creates a set with equality based on [equality]. | ||
EqualitySet(Equality<E> equality) | ||
: super(new LinkedHashSet( | ||
equals: equality.equals, | ||
hashCode: equality.hash, | ||
isValidKey: equality.isValidKey)); | ||
|
||
/// Creates a set with equality based on [equality] that contains all | ||
/// elements in [other]. | ||
/// | ||
/// If [other] has multiple values that are equivalent according to | ||
/// [equality], the first one reached during iteration takes precedence. | ||
EqualitySet.from(Equality<E> equality, Iterable<E> other) | ||
: super(new LinkedHashSet( | ||
equals: equality.equals, | ||
hashCode: equality.hash, | ||
isValidKey: equality.isValidKey)) { | ||
addAll(other); | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test("uses the given equality", () { | ||
var map = new EqualityMap(const IterableEquality()); | ||
expect(map, isEmpty); | ||
|
||
map[[1, 2, 3]] = 1; | ||
expect(map, containsPair([1, 2, 3], 1)); | ||
|
||
map[[1, 2, 3]] = 2; | ||
expect(map, containsPair([1, 2, 3], 2)); | ||
|
||
map[[2, 3, 4]] = 3; | ||
expect(map, containsPair([1, 2, 3], 2)); | ||
expect(map, containsPair([2, 3, 4], 3)); | ||
}); | ||
|
||
test("EqualityMap.from() prefers the lattermost equivalent key", () { | ||
var map = new EqualityMap.from(const IterableEquality(), { | ||
[1, 2, 3]: 1, | ||
[2, 3, 4]: 2, | ||
[1, 2, 3]: 3, | ||
[2, 3, 4]: 4, | ||
[1, 2, 3]: 5, | ||
[1, 2, 3]: 6, | ||
}); | ||
|
||
expect(map, containsPair([1, 2, 3], 6)); | ||
expect(map, containsPair([2, 3, 4], 4)); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
// Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file | ||
// for details. All rights reserved. Use of this source code is governed by a | ||
// BSD-style license that can be found in the LICENSE file. | ||
|
||
import 'package:collection/collection.dart'; | ||
import 'package:test/test.dart'; | ||
|
||
void main() { | ||
test("uses the given equality", () { | ||
var set = new EqualitySet(const IterableEquality()); | ||
expect(set, isEmpty); | ||
|
||
var list1 = [1, 2, 3]; | ||
expect(set.add(list1), isTrue); | ||
expect(set, contains([1, 2, 3])); | ||
expect(set, contains(same(list1))); | ||
|
||
var list2 = [1, 2, 3]; | ||
expect(set.add(list2), isFalse); | ||
expect(set, contains([1, 2, 3])); | ||
expect(set, contains(same(list1))); | ||
expect(set, isNot(contains(same(list2)))); | ||
|
||
var list3 = [2, 3, 4]; | ||
expect(set.add(list3), isTrue); | ||
expect(set, contains(same(list1))); | ||
expect(set, contains(same(list3))); | ||
}); | ||
|
||
test("EqualitySet.from() prefers the lattermost equivalent value", () { | ||
var list1 = [1, 2, 3]; | ||
var list2 = [2, 3, 4]; | ||
var list3 = [1, 2, 3]; | ||
var list4 = [2, 3, 4]; | ||
var list5 = [1, 2, 3]; | ||
var list6 = [1, 2, 3]; | ||
|
||
var set = new EqualitySet.from(const IterableEquality(), | ||
[list1, list2, list3, list4, list5, list6]); | ||
|
||
expect(set, contains(same(list1))); | ||
expect(set, contains(same(list2))); | ||
expect(set, isNot(contains(same(list3)))); | ||
expect(set, isNot(contains(same(list4)))); | ||
expect(set, isNot(contains(same(list5)))); | ||
expect(set, isNot(contains(same(list6)))); | ||
}); | ||
} |