| 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 on some instances of E, the | |
| 40 * [isValidKey] predicate can be used to restrict the keys that the functions | |
| 41 * are applied to. | |
| 42 * Any key for which [isValidKey] returns false is automatically assumed | |
| 43 * to not be in the set when asking `contains`. | |
| 44 * | |
| 45 * If [equals] or [hashCode] are omitted, the set uses | |
| 46 * the elements' intrinsic [Object.operator==] and [Object.hashCode], | |
| 47 * and [isValidKey] is ignored since these operations are assumed | |
| 48 * to work on all objects. | |
| 49 * | |
| 50 * If you supply one of [equals] and [hashCode], | |
| 51 * you should generally also to supply the other. | |
| 52 * | |
| 53 * If the supplied `equals` or `hashCode` functions won't work on all [E] | |
| 54 * objects, and the map will be used in a setting where a non-`E` object | |
| 55 * is passed to, e.g., `contains`, then the [isValidKey] function should | |
| 56 * also be supplied. | |
| 57 * | |
| 58 * If [isValidKey] is omitted, it defaults to testing if the object is an | |
| 59 * [E] instance. That means that: | |
| 60 * | |
| 61 * new LinkedHashSet<int>(equals: (int e1, int e2) => (e1 - e2) % 5 == 0, | |
| 62 * hashCode: (int e) => e % 5) | |
| 63 * | |
| 64 * does not need an `isValidKey` argument, because it defaults to only | |
| 65 * accepting `int` values which are accepted by both `equals` and `hashCode`. | |
| 66 * | |
| 67 * If neither `equals`, `hashCode`, nor `isValidKey` is provided, | |
| 68 * the default `isValidKey` instead accepts all values. | |
| 69 * The default equality and hashcode operations are assumed to work on all | |
| 70 * objects. | |
| 71 * | |
| 72 * Likewise, if `equals` is [identical], `hashCode` is [identityHashCode] | |
| 73 * and `isValidKey` is omitted, the resulting set is identity based, | |
| 74 * and the `isValidKey` defaults to accepting all keys. | |
| 75 * Such a map can be created directly using [LinkedHashSet.identity]. | |
| 76 */ | |
| 77 external factory LinkedHashSet({bool equals(E e1, E e2), | |
| 78 int hashCode(E e), | |
| 79 bool isValidKey(Object potentialKey)}); | |
| 80 | |
| 81 /** | |
| 82 * Creates an insertion-ordered identity-based set. | |
| 83 * | |
| 84 * Effectively a shorthand for: | |
| 85 * | |
| 86 * new LinkedHashSet(equals: identical, | |
| 87 * hashCode: identityHashCode) | |
| 88 */ | |
| 89 external factory LinkedHashSet.identity(); | |
| 90 | |
| 91 /** | |
| 92 * Create a linked hash set containing all [elements]. | |
| 93 * | |
| 94 * Creates a linked hash set as by `new LinkedHashSet<E>()` and adds each | |
| 95 * element of`elements` to this set in the order they are iterated. | |
| 96 * | |
| 97 * All the [elements] should be assignable to [E]. | |
| 98 * The `elements` iterable itself may have any element type, | |
| 99 * so this constructor can be used to down-cast a `Set`, for example as: | |
| 100 * | |
| 101 * Set<SuperType> superSet = ...; | |
| 102 * Iterable<SuperType> tmp = superSet.where((e) => e is SubType); | |
| 103 * Set<SubType> subSet = new LinkedHashSet<SubType>.from(tmp); | |
| 104 */ | |
| 105 factory LinkedHashSet.from(Iterable elements) { | |
| 106 LinkedHashSet<E> result = new LinkedHashSet<E>(); | |
| 107 for (final element in elements) { | |
| 108 E e = element as Object/*=E*/; | |
| 109 result.add(e); | |
| 110 } | |
| 111 return result; | |
| 112 } | |
| 113 | |
| 114 /** | |
| 115 * Executes a function on each element of the set. | |
| 116 * | |
| 117 * The elements are iterated in insertion order. | |
| 118 */ | |
| 119 void forEach(void action(E element)); | |
| 120 | |
| 121 /** | |
| 122 * Provides an iterator that iterates over the elements in insertion order. | |
| 123 */ | |
| 124 Iterator<E> get iterator; | |
| 125 } | |
| OLD | NEW |