| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 part of dart.collection; | |
| 6 | |
| 7 /** | |
| 8 * A [LinkedHashSet] is a hash-table based [Set] implementation. | |
| 9 * | |
| 10 * The `LinkedHashSet` also keep track of the order that elements were inserted | |
| 11 * in, and iteration happens in first-to-last insertion order. | |
| 12 * | |
| 13 * The elements of a `LinkedHashSet` must have consistent [Object.operator==] | |
| 14 * and [Object.hashCode] implementations. This means that the `==` operator | |
| 15 * must define a stable equivalence relation on the elements (reflexive, | |
| 16 * symmetric, transitive, and consistent over time), and that `hashCode` | |
| 17 * must be the same for objects that are considered equal by `==`. | |
| 18 * | |
| 19 * The set allows `null` as an element. | |
| 20 * | |
| 21 * Iteration of elements is done in element insertion order. | |
| 22 * An element that was added after another will occur later in the iteration. | |
| 23 * Adding an element that is already in the set | |
| 24 * does not change its position in the iteration order, | |
| 25 * but removing an element and adding it again, | |
| 26 * will make it the last element of an iteration. | |
| 27 * | |
| 28 * Most simple operations on `HashSet` are done in (potentially amortized) | |
| 29 * constant time: [add], [contains], [remove], and [length], provided the hash | |
| 30 * codes of objects are well distributed.. | |
| 31 */ | |
| 32 abstract class LinkedHashSet<E> implements HashSet<E> { | |
| 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 */ | |
| 55 factory LinkedHashSet({ bool equals(E e1, E e2), | |
| 56 int hashCode(E e), | |
| 57 bool isValidKey(Object potentialKey) }) { | |
| 58 if (isValidKey == null) { | |
| 59 if (hashCode == null) { | |
| 60 if (equals == null) { | |
| 61 return new _LinkedHashSet<E>(); | |
| 62 } | |
| 63 hashCode = _defaultHashCode; | |
| 64 } else { | |
| 65 if (identical(identityHashCode, hashCode) && | |
| 66 identical(identical, equals)) { | |
| 67 return new _LinkedIdentityHashSet<E>(); | |
| 68 } | |
| 69 if (equals == null) { | |
| 70 equals = _defaultEquals; | |
| 71 } | |
| 72 } | |
| 73 } else { | |
| 74 if (hashCode == null) { | |
| 75 hashCode = _defaultHashCode; | |
| 76 } | |
| 77 if (equals == null) { | |
| 78 equals = _defaultEquals; | |
| 79 } | |
| 80 } | |
| 81 return new _LinkedCustomHashSet<E>(equals, hashCode, isValidKey); | |
| 82 } | |
| 83 | |
| 84 /** | |
| 85 * Creates an insertion-ordered identity-based set. | |
| 86 * | |
| 87 * Effectively a shorthand for: | |
| 88 * | |
| 89 * new LinkedHashSet(equals: identical, hashCode: identityHashCodeOf) | |
| 90 */ | |
| 91 factory LinkedHashSet.identity() = _LinkedIdentityHashSet<E>; | |
| 92 | |
| 93 /** | |
| 94 * Create a linked hash set containing all [elements]. | |
| 95 * | |
| 96 * Creates a linked hash set as by `new LinkedHashSet<E>()` and adds each | |
| 97 * element of`elements` to this set in the order they are iterated. | |
| 98 * | |
| 99 * All the [elements] should be assignable to [E]. | |
| 100 * The `elements` iterable itself may have any element type, | |
| 101 * so this constructor can be used to down-cast a `Set`, for example as: | |
| 102 * | |
| 103 * Set<SuperType> superSet = ...; | |
| 104 * Iterable<SuperType> tmp = superSet.where((e) => e is SubType); | |
| 105 * Set<SubType> subSet = new LinkedHashSet<SubType>.from(tmp); | |
| 106 */ | |
| 107 factory LinkedHashSet.from(Iterable<E> elements) { | |
| 108 LinkedHashSet<E> result = new LinkedHashSet<E>(); | |
| 109 for (final E element in elements) { | |
| 110 result.add(element); | |
| 111 } | |
| 112 return result; | |
| 113 } | |
| 114 | |
| 115 /** | |
| 116 * Executes a function on each element of the set. | |
| 117 * | |
| 118 * The elements are iterated in insertion order. | |
| 119 */ | |
| 120 void forEach(void action(E element)); | |
| 121 | |
| 122 /** | |
| 123 * Provides an iterator that iterates over the elements in insertion order. | |
| 124 */ | |
| 125 Iterator<E> get iterator; | |
| 126 } | |
| OLD | NEW |