smjg.libs.util.setbase

This module provides an implementation of an abstract base class for set classes.

Since:
0.03
class Set(Element) if (isImmutable!(Element));

The Set class is the abstract base class from which set classes can be derived.

Default implementations of several methods are given. These may be overridden to provide an implementation optimised to the internal data structure used by the specific set implementation.
void add(Element elem);

Adds elem to the set.

BUGS:
As of DMD 2.058, it is possible to add a mutable object of a subclass of Element's set (issue 3731).
void remove(Element elem);

Removes elem from the set.

void removeCurrent();

Removes the element currently active in the innermost active foreach loop on this set.

Precondition:
A foreach loop must be active on this set.
Warning:
The time at which the removal takes effect may depend on the implementation: it might take effect immediately, or not until the iteration of the foreach loop exits. It is also possible that, if within one foreach loop another is initiated on the same set after a call to removeCurrent, the wrong element may be removed. There is also potential undefined behaviour if the same set is iterated through a mutable reference and, nested within this loop, through a const or immutable reference.
Future Directions:
This method may be superseded by something more robust.
const Element storedKey(Element elem);
Returns:
the set's copy of elem, or Element.init if the element is not present.
const size_t length();
Returns:
the number of elements in the set.
const Set dup();
Returns:
an exact copy of this set.
const immutable(Set) idup();
Returns:
an exact immutable copy of this set.
const Element[] data();
Returns:
an array of the set's elements.
const bool opIn_r(Element elem);
Returns:
whether elem is present in the set.
int opApply(int delegate(ref Element) dg);
const int opApply(int delegate(ref Element) dg);

Iterates over the elements of the set.

Modifying the set while foreach is in progress, except by the removeCurrent method, may lead to undefined behaviour.

const bool subsetOf(const(Set) s);
Returns:
whether this set is a subset of s.
Set remove(bool delegate(Element) dg);

Removes from this set all elements for which dg returns true.

Returns:
a set of the elements removed.
Set remove(const(Set) s);

Removes from this set all elements that are in s.

Returns:
a set of the elements removed.
string toString();
Returns:
a string of the form { element, element, ... } where the elements are formatted according to the %s format specifier.
To Do:
change to const once issue 1824 is fixed
hash_t toHash();
Returns:
the hash value of this set, calculated by XORing together the hash values of all the elements.
To Do:
change to const once issue 1824 is fixed
bool opEquals(Object other);
const bool opEquals(const(Set) other);
Returns:
whether this set is equal to other.
alias opIndex;
bool opIndexAssign(bool b, Element elem);

Adds or removes an element to or from this set.

Params:
Element elem the element to add or remove.
bool b true to add the element, or false to remove it.
Returns:
b.
const Set opAnd(const(Set) s);
Returns:
the intersection of this set and s.
const Set opOr(const(Set) s);
Returns:
the union of this set and s.
const Set opSub(const(Set) s);
Returns:
a set containing the elements of this set that are not in s.
const Set opXor(const(Set) s);
Returns:
the symmetric difference between this set and s.
const Set opAnd(bool delegate(Element) dg);
Returns:
a set containing the elements of this set that for which dg returns true.
const Set opSub(bool delegate(Element) dg);
Returns:
a set containing the elements of this set that for which dg returns false.
Set opAndAssign(const(Set) s);
Set opOrAssign(const(Set) s);
Set opSubAssign(const(Set) s);
Set opXorAssign(const(Set) s);
Set opAndAssign(bool delegate(Element) dg);
Set opSubAssign(bool delegate(Element) dg);