#include "set.h"

class Set<ValueType>

This template class stores a collection of distinct elements.
Constructor
Set()
Set(cmpFn) 
Initializes a set of the specified element type, which is either empty or initialized to match the elements of the C++ array passed as the initializers parameter.
Methods
size() Returns the number of elements in this set.
isEmpty() Returns true if this set contains no elements.
add(value) Adds an element to this set, if it was not already there.
remove(value) Removes an element from this set.
contains(value) Returns true if the specified value is in this set.
isSubsetOf(set2) Implements the subset relation on sets.
clear() Removes all elements from this set.
first() Returns the first value in the set in the order established by the foreach macro.
mapAll(fn)
mapAll(fn, data) 
Iterates through the elements of the set and calls fn(value) for each one.
Operators
set1 == set2 Returns true if set1 and set2 contain the same elements.
set1 != set2 Returns true if set1 and set2 are different.
set1 + set2 Returns the union of sets set1 and set2, which is the set of elements that appear in at least one of the two sets.
set1 * set2 Returns the intersection of sets set1 and set2, which is the set of all elements that appear in both.
set1 - set2 Returns the difference of sets set1 and set2, which is all of the elements that appear in set1 but not set2.
set1 += set2; Adds all of the elements from set2 (or the single specified value) to set1.
set1 *= set2; Removes any elements from set1 that are not present in set2.
set1 -= set2; Removes the elements from set2 (or the single specified value) from set1.
Macro
foreach(ValueType value in set) Iterates over the elements of the set.

Constructor detail


Set(int (*cmpFn)(ValueType, ValueType) = operatorCmp);
Initializes a set of the specified element type, which is either empty or initialized to match the elements of the C++ array passed as the initializers parameter. The optional cmpFn argument specifies a comparison function, which is called to compare data values. This argument is typically omitted, in which case the implementation uses the operatorCmp function from cmpfn.h, which applies the built-in operators < and == to determine the ordering.

Usage:

Set<ValueType> set;
Set<ValueType> set(cmpFn);

Method detail


int size();
Returns the number of elements in this set.

Usage:

count = set.size();

bool isEmpty();
Returns true if this set contains no elements.

Usage:

if (set.isEmpty()) . . .

void add(ValueType value);
void insert(ValueType value);
Adds an element to this set, if it was not already there. For compatibility with the STL set class, this method is also exported as insert.

Usage:

set.add(value);

void remove(ValueType value);
Removes an element from this set. If the value was not contained in the set, no error is generated and the set remains unchanged.

Usage:

set.remove(value);

bool contains(ValueType value);
Returns true if the specified value is in this set.

Usage:

if (set.contains(value)) . . .

bool isSubsetOf(Set set2);
Implements the subset relation on sets. It returns true if every element of this set is contained in set2.

Usage:

if (set.isSubsetOf(set2)) . . .

void clear();
Removes all elements from this set.

Usage:

set.clear();

ValueType first();
Returns the first value in the set in the order established by the foreach macro. If the set is empty, first generates an error.

Usage:

ValueType value = set.first();

void mapAll(void (*fn)(ValueType value));
void mapAll(void (*fn)(ValueType, ClientDataType &), ClientDataType & data);
Iterates through the elements of the set and calls fn(value) for each one. The values are processed in ascending order, as defined by the comparison function. The second form of the call allows the client to pass a data value of any type to the callback function.

Usage:

set.mapAll(fn);
set.mapAll(fn, data);

Operator detail


bool operator==(Set set2);
Returns true if set1 and set2 contain the same elements.

Usage:

set1 == set2

bool operator!=(Set set2);
Returns true if set1 and set2 are different.

Usage:

set1 != set2

Set operator+(Set set2);
Set operator+(ValueType element);
Returns the union of sets set1 and set2, which is the set of elements that appear in at least one of the two sets. The right hand set can be replaced by an element of the value type, in which case the operator returns a new set formed by adding that element.

Usage:

set1 + set2
set1 + element

Set operator*(Set set2);
Returns the intersection of sets set1 and set2, which is the set of all elements that appear in both.

Usage:

set1 * set2

Set operator-(Set set2);
Set operator-(ValueType element);
Returns the difference of sets set1 and set2, which is all of the elements that appear in set1 but not set2. The right hand set can be replaced by an element of the value type, in which case the operator returns a new set formed by removing that element.

Usage:

set1 - set2
set1 - element

Set & operator+=(Set set2);
Set & operator+=(ValueType value);
Adds all of the elements from set2 (or the single specified value) to set1. As a convenience, the Set package also overloads the comma operator so that it is possible to initialize a set like this:
   Set digits;
   digits += 0, 1, 2, 3, 4, 5, 6, 7, 8, 9;

Usage:

set1 += set2;
set1 += value;

Set & operator*=(Set set2);
Removes any elements from set1 that are not present in set2.

Usage:

set1 *= set2;

Set & operator-=(Set set2);
Set & operator-=(ValueType value);
Removes the elements from set2 (or the single specified value) from set1. As a convenience, the Set package also overloads the comma operator so that it is possible to remove multiple elements from a set like this:
   digits -= 0, 2, 4, 6, 8;
which removes the values 0, 2, 4, 6, and 8 from the set digits.

Usage:

set1 -= set2;
set1 -= value;

Macro detail


foreach (ValueType value in set) . . .
Iterates over the elements of the set. The values are returned in ascending order, as defined by the comparison function.