@@ -2,18 +2,15 @@ import 'dart:collection';
22
33import 'package:ordered_set/comparing_ordered_set.dart' ;
44import 'package:ordered_set/mapping_ordered_set.dart' ;
5- import 'package:ordered_set/queryable_ordered_set .dart' ;
5+ import 'package:ordered_set/read_only_ordered_set .dart' ;
66
77/// A simple interface of an ordered set for Dart.
88///
99/// It accepts some way of comparing items for their priority. Unlike
1010/// [SplayTreeSet] , it allows for several different elements with the same
1111/// priority to be added. It also implements [Iterable] , so you can iterate it
1212/// in O(n).
13- abstract class OrderedSet <E > extends IterableMixin <E > {
14- /// The tree's elements in reversed order; should be cached when possible.
15- Iterable <E > reversed ();
16-
13+ abstract class OrderedSet <E > extends ReadOnlyOrderedSet <E > {
1714 /// Adds the element [e] to this, and returns whether the element was
1815 /// added or not. If the element already exists in the collection, it isn't
1916 /// added.
@@ -67,12 +64,22 @@ abstract class OrderedSet<E> extends IterableMixin<E> {
6764 /// Remove all elements that match the [test] condition; returns the removed
6865 /// elements.
6966 Iterable <E > removeWhere (bool Function (E element) test) {
70- return where (test).toList (growable: false )..forEach (remove);
67+ final elements = where (test).toList (growable: false );
68+ for (final element in elements) {
69+ remove (element);
70+ }
71+ return elements;
7172 }
7273
7374 /// Remove all [elements] and returns the removed elements.
7475 Iterable <E > removeAll (Iterable <E > elements) {
75- return elements.where (remove).toList (growable: false );
76+ final removed = < E > [];
77+ for (final element in elements) {
78+ if (remove (element)) {
79+ removed.add (element);
80+ }
81+ }
82+ return removed;
7683 }
7784
7885 /// Removes the element at [index] .
@@ -86,43 +93,40 @@ abstract class OrderedSet<E> extends IterableMixin<E> {
8693 ///
8794 /// This implementation will not store component priorities, so it is
8895 /// susceptible to race conditions if priorities are changed while iterating.
89- static ComparingOrderedSet <E > comparing <E >([
96+ static ComparingOrderedSet <E > comparing <E >({
9097 int Function (E a, E b)? compare,
91- ]) {
92- return ComparingOrderedSet <E >(compare);
98+ bool strictMode = true ,
99+ }) {
100+ return ComparingOrderedSet <E >(compare: compare, strictMode: strictMode);
93101 }
94102
95103 /// Creates an instance of [OrderedSet] using the [MappingOrderedSet]
96104 /// implementation and the provided [mappingFunction] .
97105 static MappingOrderedSet <K , E > mapping <K extends Comparable <K >, E >(
98- K Function (E a) mappingFunction,
99- ) {
100- return MappingOrderedSet (mappingFunction);
106+ K Function (E a) mappingFunction, {
107+ bool strictMode = true ,
108+ }) {
109+ return MappingOrderedSet (mappingFunction, strictMode: strictMode);
101110 }
102111
103112 /// Creates an instance of [OrderedSet] for items that are already
104113 /// [Comparable] using the [MappingOrderedSet] implementation.
105114 /// Use this for classes that implement [Comparable] of a different class.
106115 /// Equivalent to `mapping<K, E>((a) => a)` .
107116 static MappingOrderedSet <K , E >
108- comparable <K extends Comparable <K >, E extends K >() {
109- return mapping <K , E >((a) => a);
117+ comparable <K extends Comparable <K >, E extends K >({
118+ bool strictMode = true ,
119+ }) {
120+ return mapping <K , E >((a) => a, strictMode: strictMode);
110121 }
111122
112123 /// Creates an instance of [OrderedSet] for items that are already
113124 /// [Comparable] of themselves, using the [MappingOrderedSet] implementation.
114125 /// Use this for classes that implement [Comparable] of themselves.
115126 /// Equivalent to `mapping<K, K>((a) => a)` .
116- static MappingOrderedSet <E , E > simple <E extends Comparable <E >>() {
117- return comparable <E , E >();
118- }
119-
120- /// Creates an instance of [OrderedSet] using the [QueryableOrderedSet]
121- /// by wrapping the provided [backingSet] .
122- static QueryableOrderedSet <E > queryable <E >(
123- OrderedSet <E > backingSet, {
127+ static MappingOrderedSet <E , E > simple <E extends Comparable <E >>({
124128 bool strictMode = true ,
125129 }) {
126- return QueryableOrderedSet < E >(backingSet, strictMode: strictMode);
130+ return comparable < E , E >( strictMode: strictMode);
127131 }
128132}
0 commit comments