6.20.5.1 Initializing a Set

The Set_T datastructure itself is defined in the templated module collections.set(T). However, the Set_T structure is abstract, with many of its methods defaulting to null. To get a working implementation, you need either a HashSet_T or a BTreeSet_T:

HashSet_T()
HashSet_T(T nullT,
          bool equiv(T a, T b) = operator ==,
          bool isNullT(T) = new bool(T t) { return equiv(t, nullT); }
         )

These two functions, which create a hash set, must be accessed from the collections.hashset(T) templated module. The type T must have a hash method, see Hash functions. If isNullT is not defined, or if !isNullT(null), then passing null to most methods will throw an error.

BTreeSet_T()
BTreeSet_T(T nullT, bool isNullT(T) = new bool(T t) { return t == nullT; })

These two functions must be accessed from the collections.btree(T) module. They create a b-tree set, which stores its elements in order based on the < operator. The < operator must define a strict weak ordering on all items of type T, except those satisfying isNullT. Two items are considered equivalent if neither is less than the other.

Accessing the collections.btree(T) module requires that operator < is defined for the type T; if not one can use the constructors defined in collections.btreegeneral(T) module, as discussed next.

BTreeSet_T(bool lessThan(T,T))
BTreeSet_T(bool lessThan(T,T),
           T nullT,
           bool isNullT(T) = new bool(T t) { return t == nullT; })

These two functions must be accessed from the collections.btreegeneral(T) module. They create a BTreeSet that stores its elements in sorted order based on the lessThan function. The lessThan function must define a strict weak ordering on all items of type T, except those satisfying isNullT. Two items are considered equivalent if neither is less than the other.

Both HashSet_T and BTreeSet_T can be implicitly cast to Set_T and have all of the methods of Set_T. Similarly, the nullT and isNullT optional parameters behave the same way for both types. See The Set API.

Additionally, BTreeSet_T can be implicitly cast to SortedSet_T and has all its methods, which provide additional functionality specific to sorted sets (such as retrieving the least element or the next element greater than a given element). See The SortedSet API.