Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2014, 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 /** | |
| 6 * Base implementations of [Set]. | |
| 7 */ | |
| 8 part of dart.collection; | |
| 9 | |
| 10 /** | |
| 11 * Mixin implementation of [Set]. | |
| 12 * | |
| 13 * This class provides a base implementation of a `Set` that depends only | |
| 14 * on the abstract members: [add], [contains], [lookup], [remove], | |
| 15 * [iterator] and [cloneEmpty]. | |
| 16 * | |
| 17 * Implementations of `Set` using this mixin should consider also implementing | |
| 18 * `clear` in constant time. The default implementation works by removing every | |
| 19 * element. | |
| 20 * | |
| 21 * This class does not implement most of the methods that Set inherit | |
| 22 * from [Iterable]. | |
| 23 * When using the mixin, it should be applied to an iterable, | |
| 24 * or be combined with [IterableMixin]. | |
|
floitsch
2014/05/19 13:13:43
This is not how ListMixin is implemented. I prefer
| |
| 25 */ | |
| 26 abstract class SetMixin<E> implements Set<E> { | |
| 27 | |
| 28 bool add(E element); | |
| 29 | |
| 30 bool contains(Object element); | |
| 31 | |
| 32 E lookup(E element); | |
| 33 | |
| 34 bool remove(Object element); | |
| 35 | |
| 36 Iterable<E> get iterator; | |
| 37 | |
| 38 Set<E> cloneEmpty(); | |
| 39 | |
| 40 void clear() { | |
| 41 removeAll(toList()); | |
| 42 } | |
| 43 | |
| 44 void addAll(Iterable<E> elements) { | |
| 45 for (E element in elements) add(element); | |
| 46 } | |
| 47 | |
| 48 void removeAll(Iterable<Object> elements) { | |
| 49 for (Object element in elements) remove(element); | |
| 50 } | |
| 51 | |
| 52 void retainAll(Iterable<Object> elements) { | |
| 53 // Create a copy of the set, remove all of elements from the copy, | |
| 54 // then remove all remaining elements in copy from this. | |
| 55 Set<E> toRemove = toSet(); | |
| 56 for (Object o in elements) { | |
| 57 toRemove.remove(o); | |
| 58 } | |
| 59 removeAll(toRemove); | |
| 60 } | |
| 61 | |
| 62 void removeWhere(bool test(E element)) { | |
| 63 List toRemove = []; | |
| 64 for (E element in this) { | |
| 65 if (test(element)) toRemove.add(element); | |
| 66 } | |
| 67 removeAll(toRemove); | |
| 68 } | |
| 69 | |
| 70 void retainWhere(bool test(E element)) { | |
| 71 List toRemove = []; | |
| 72 for (E element in this) { | |
| 73 if (!test(element)) toRemove.add(element); | |
| 74 } | |
| 75 removeAll(toRemove); | |
| 76 } | |
| 77 | |
| 78 bool containsAll(Iterable<Object> other) { | |
| 79 for (Object o in other) { | |
| 80 if (!contains(o)) return false; | |
| 81 } | |
| 82 return true; | |
| 83 } | |
| 84 | |
| 85 Set<E> intersection(Set<Object> other) { | |
| 86 Set<E> result = cloneEmpty(); | |
| 87 for (E element in this) { | |
| 88 if (other.contains(element)) result.add(element); | |
| 89 } | |
| 90 return result; | |
| 91 } | |
| 92 | |
| 93 Set<E> union(Set<E> other) { | |
| 94 return toSet()..addAll(other); | |
| 95 } | |
| 96 | |
| 97 Set<E> difference(Set<E> other) { | |
| 98 Set<E> result = cloneEmpty(); | |
| 99 for (E element in this) { | |
| 100 if (!other.contains(element)) result.add(element); | |
| 101 } | |
| 102 return result; | |
| 103 } | |
| 104 | |
| 105 List<E> toList({bool growable: true}) { | |
| 106 List<E> result = growable ? (new List<E>()..length = length) | |
| 107 : new List<E>(length); | |
| 108 int i = 0; | |
| 109 for (E element in this) result[i++] = element; | |
| 110 return result; | |
| 111 } | |
| 112 | |
| 113 Set<E> toSet() { | |
| 114 return cloneEmpty()..addAll(this); | |
| 115 } | |
| 116 | |
| 117 String toString() => IterableMixinWorkaround.toStringIterable(this, '{', '}'); | |
| 118 } | |
| 119 | |
| 120 /** | |
| 121 * Base implementation of [Set]. | |
| 122 * | |
| 123 * This class provides a base implementation of a `Set` that depends only | |
| 124 * on the abstract members: [add], [contains], [lookup], [remove], | |
| 125 * [iterator] and [cloneEmpty]. | |
| 126 * It includes [Iterable] methods from [IterableBase]. | |
| 127 * | |
| 128 * Implementations of `Set` using this base should consider also implementing | |
| 129 * `clear` in constant time. The default implementation works by removing every | |
| 130 * element. | |
| 131 */ | |
| 132 abstract class SetBase<E> = IterableBase<E> with SetMixin<E>; | |
| 133 | |
| OLD | NEW |