| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 part of dart.collection; | 5 part of dart.collection; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * A [LinkedHashSet] is a hash-table based [Set] implementation. | 8 * A [LinkedHashSet] is a hash-table based [Set] implementation. |
| 9 * | 9 * |
| 10 * The `LinkedHashSet` also keep track of the order that elements were inserted | 10 * The `LinkedHashSet` also keep track of the order that elements were inserted |
| (...skipping 12 matching lines...) Expand all Loading... |
| 23 * Adding an element that is already in the set | 23 * Adding an element that is already in the set |
| 24 * does not change its position in the iteration order, | 24 * does not change its position in the iteration order, |
| 25 * but removing an element and adding it again, | 25 * but removing an element and adding it again, |
| 26 * will make it the last element of an iteration. | 26 * will make it the last element of an iteration. |
| 27 * | 27 * |
| 28 * Most simple operations on `HashSet` are done in (potentially amortized) | 28 * Most simple operations on `HashSet` are done in (potentially amortized) |
| 29 * constant time: [add], [contains], [remove], and [length], provided the hash | 29 * constant time: [add], [contains], [remove], and [length], provided the hash |
| 30 * codes of objects are well distributed.. | 30 * codes of objects are well distributed.. |
| 31 */ | 31 */ |
| 32 abstract class LinkedHashSet<E> implements HashSet<E> { | 32 abstract class LinkedHashSet<E> implements HashSet<E> { |
| 33 @patch | 33 /** |
| 34 * Create an insertion-ordered hash set using the provided |
| 35 * [equals] and [hashCode]. |
| 36 * |
| 37 * The provided [equals] must define a stable equivalence relation, and |
| 38 * [hashCode] must be consistent with [equals]. If the [equals] or [hashCode] |
| 39 * methods won't work on all objects, but only to instances of E, the |
| 40 * [isValidKey] predicate can be used to restrict the keys that they are |
| 41 * applied to. Any key for which [isValidKey] returns false is automatically |
| 42 * assumed to not be in the set. |
| 43 * |
| 44 * If [equals] or [hashCode] are omitted, the set uses |
| 45 * the objects' intrinsic [Object.operator==] and [Object.hashCode], |
| 46 * |
| 47 * If [isValidKey] is omitted, it defaults to testing if the object is an |
| 48 * [E] instance. |
| 49 * |
| 50 * If you supply one of [equals] and [hashCode], |
| 51 * you should generally also to supply the other. |
| 52 * An example would be using [identical] and [identityHashCode], |
| 53 * which is equivalent to using the shorthand [LinkedSet.identity]). |
| 54 */ |
| 34 factory LinkedHashSet({ bool equals(E e1, E e2), | 55 factory LinkedHashSet({ bool equals(E e1, E e2), |
| 35 int hashCode(E e), | 56 int hashCode(E e), |
| 36 bool isValidKey(potentialKey) }) { | 57 bool isValidKey(potentialKey) }) { |
| 37 if (isValidKey == null) { | 58 if (isValidKey == null) { |
| 38 if (hashCode == null) { | 59 if (hashCode == null) { |
| 39 if (equals == null) { | 60 if (equals == null) { |
| 40 return new _LinkedHashSet<E>(); | 61 return new _LinkedHashSet<E>(); |
| 41 } | 62 } |
| 42 hashCode = _defaultHashCode; | 63 hashCode = _defaultHashCode; |
| 43 } else { | 64 } else { |
| 44 if (identical(identityHashCode, hashCode) && | 65 if (identical(identityHashCode, hashCode) && |
| 45 identical(identical, equals)) { | 66 identical(identical, equals)) { |
| 46 return new _LinkedIdentityHashSet<E>(); | 67 return new _LinkedIdentityHashSet<E>(); |
| 47 } | 68 } |
| 48 if (equals == null) { | 69 if (equals == null) { |
| 49 equals = _defaultEquals; | 70 equals = _defaultEquals; |
| 50 } | 71 } |
| 51 } | 72 } |
| 52 } else { | 73 } else { |
| 53 if (hashCode == null) { | 74 if (hashCode == null) { |
| 54 hashCode = _defaultHashCode; | 75 hashCode = _defaultHashCode; |
| 55 } | 76 } |
| 56 if (equals == null) { | 77 if (equals == null) { |
| 57 equals = _defaultEquals; | 78 equals = _defaultEquals; |
| 58 } | 79 } |
| 59 } | 80 } |
| 60 return new _LinkedCustomHashSet<E>(equals, hashCode, isValidKey); | 81 return new _LinkedCustomHashSet<E>(equals, hashCode, isValidKey); |
| 61 } | 82 } |
| 62 | 83 |
| 63 @patch | 84 /** |
| 85 * Creates an insertion-ordered identity-based set. |
| 86 * |
| 87 * Effectively a shorthand for: |
| 88 * |
| 89 * new LinkedHashSet(equals: identical, hashCode: identityHashCodeOf) |
| 90 */ |
| 64 factory LinkedHashSet.identity() = _LinkedIdentityHashSet<E>; | 91 factory LinkedHashSet.identity() = _LinkedIdentityHashSet<E>; |
| 65 | 92 |
| 66 /** | 93 /** |
| 67 * Create a linked hash set containing all [elements]. | 94 * Create a linked hash set containing all [elements]. |
| 68 * | 95 * |
| 69 * Creates a linked hash set as by `new LinkedHashSet<E>()` and adds each | 96 * Creates a linked hash set as by `new LinkedHashSet<E>()` and adds each |
| 70 * element of`elements` to this set in the order they are iterated. | 97 * element of`elements` to this set in the order they are iterated. |
| 71 * | 98 * |
| 72 * All the [elements] should be assignable to [E]. | 99 * All the [elements] should be assignable to [E]. |
| 73 * The `elements` iterable itself may have any element type, | 100 * The `elements` iterable itself may have any element type, |
| (...skipping 16 matching lines...) Expand all Loading... |
| 90 * | 117 * |
| 91 * The elements are iterated in insertion order. | 118 * The elements are iterated in insertion order. |
| 92 */ | 119 */ |
| 93 void forEach(void action(E element)); | 120 void forEach(void action(E element)); |
| 94 | 121 |
| 95 /** | 122 /** |
| 96 * Provides an iterator that iterates over the elements in insertion order. | 123 * Provides an iterator that iterates over the elements in insertion order. |
| 97 */ | 124 */ |
| 98 Iterator<E> get iterator; | 125 Iterator<E> get iterator; |
| 99 } | 126 } |
| OLD | NEW |