License:
see license.txt
- class
TreeSet
(V,alias ImplTemp = RBNoUpdatesTree,alias compareFunction = DefaultCompare): Set!(V);
- Implementation of the Set interface using Red-Black trees. this allows for
O(lg(n)) insertion, removal, and lookup times. It also creates a sorted
set. V must be comparable.
Adding an element does not invalidate any cursors.
Removing an element only invalidates the cursors that were pointing at
that element.
You can replace the Tree implementation with a custom implementation, the
implementation must be a struct template which can be instantiated with a
single template argument V, and must implement the following members
(non-function members can be properties unless otherwise specified):
parameters -> must be a struct with at least the following members:
compareFunction -> the compare function to use (should be a
CompareFunction!(V))
updateFunction -> the update function to use (should be an
UpdateFunction!(V))
void setup(parameters p) -> initializes the tree with the given parameters.
uint count -> count of the elements in the tree
node -> must be a struct/class with the following members:
V value -> the value which is pointed to by this position (cannot be a
property)
node next -> the next node in the tree as defined by the compare
function, or end if no other nodes exist.
node prev -> the previous node in the tree as defined by the compare
function.
bool add(V v) -> add the given value to the tree according to the order
defined by the compare function. If the element already exists in the
tree, the
node begin -> must be a node that points to the very first valid
element in the tree, or end if no elements exist.
node end -> must be a node that points to just past the very last
valid element.
node find(V v) -> returns a node that points to the element that
contains v, or end if the element doesn't exist.
node remove(node p) -> removes the given element from the tree,
returns the next valid element or end if p was last in the tree.
void clear() -> removes all elements from the tree, sets count to 0.
- alias
Impl
;
- convenience alias.
- struct
cursor
;
- Iterator for the tree set.
- V
value
();
- get the
value
in this element
- cursor
opPostInc
();
- increment this cursor, returns what the cursor was before
incrementing.
- cursor
opPostDec
();
- decrement this cursor, returns what the cursor was before
decrementing.
- cursor
opAddAssign
(int inc);
- increment the cursor by the given amount.
This is an O(inc) operation! You should only use this operator in
the form:
++i;
- cursor
opSubAssign
(int inc);
- decrement the cursor by the given amount.
This is an O(inc) operation! You should only use this operator in
the form:
--i;
- bool
opEquals
(cursor it);
- compare two cursors for equality
- int
purge
(int delegate(ref bool doPurge, ref V v) dg);
- Iterate through elements of the TreeSet, specifying which ones to
remove.
Use like this:
// remove all odd elements
foreach(ref doPurge, v; &treeSet.purge)
{
doPurge = ((v % 1) == 1);
}
- int
opApply
(int delegate(ref V v) dg);
- iterate over the collection's values
- this();
- Instantiate the tree set
- TreeSet
clear
();
- Clear the collection of all elements
- uint
length
();
- returns number of elements in the collection
- cursor
begin
();
- returns a cursor to the first element in the collection.
- cursor
end
();
- returns a cursor that points just past the last element in the
collection.
- cursor
remove
(cursor it);
-
remove
the element pointed at by the given cursor, returning an
cursor that points to the next element in the collection.
Runs in O(lg(n)) time.
- cursor
find
(V v);
-
find
the instance of a value in the collection. Returns end if the
value is not present.
Runs in O(lg(n)) time.
- bool
contains
(V v);
- Returns true if the given value exists in the collection.
Runs in O(lg(n)) time.
- TreeSet
remove
(V v);
- Removes the element that has the value v. Returns true if the value
was present and was removed.
Runs in O(lg(n)) time.
- TreeSet
remove
(V v, ref bool wasRemoved);
- Removes the element that has the value v. Returns true if the value
was present and was removed.
Runs in O(lg(n)) time.
- TreeSet
add
(V v);
- Adds a value to the collection.
Returns true.
Runs in O(lg(n)) time.
- TreeSet
add
(V v, ref bool wasAdded);
- Adds a value to the collection.
Returns true.
Runs in O(lg(n)) time.
- TreeSet
add
(Iterator!(V) it);
- Adds all the values from enumerator to the collection.
Runs in O(m lg(n)) time, where m is the number of elements in
enumerator.
- TreeSet
add
(Iterator!(V) it, ref uint numAdded);
- Adds all the values from enumerator to the collection.
Runs in O(m lg(n)) time, where m is the number of elements in
enumerator.
- TreeSet
add
(V[] array);
- Adds all the values from array to the collection.
Runs in O(m lg(n)) time, where m is the number of elements in
array.
- TreeSet
add
(V[] array, ref uint numAdded);
- Adds all the values from array to the collection.
Runs in O(m lg(n)) time, where m is the number of elements in
array.
- TreeSet
dup
();
- Return a duplicate treeset containing all the elements in this tree
set.
- TreeSet
remove
(Iterator!(V) subset);
- Remove all the elements that match in the subset
- TreeSet
remove
(Iterator!(V) subset, ref uint numRemoved);
- Remove all the elements that match in the subset. Sets numRemoved to
number of elements removed.
returns this.
- TreeSet
intersect
(Iterator!(V) subset);
- Remove all the elements that do NOT match in the subset.
returns this.
- TreeSet
intersect
(Iterator!(V) subset, ref uint numRemoved);
- Remove all the elements that do NOT match in the subset. Sets
numRemoved to number of elements removed.
returns this.
- int
opEquals
(Object o);
- Compare this set with another set. Returns true if both sets have the
same length and every element in one set exists in the other set.
If o is null or not a Set, return 0.
- V
get
();
-
get
the most convenient element in the set. This is the element that
would be iterated first. Therefore, calling remove(
get
()) is
guaranteed to be less than an O(n) operation.
- V
take
();
- Remove the most convenient element from the set, and return its value.
This is equivalent to remove(get()), except that only one lookup is
performed.
|