#include "hashset.h"

class HashSet<ValueType>

This class implements an efficient abstraction for storing sets of distinct elements. This class is identical to the Set class except for the fact that it uses a hash table as its underlying representation. The advantage of the HashSet class is that it operates in constant time, as opposed to the O(log N) time for the Set class. The disadvantage of HashSet is that iterators return the values in a seemingly random order.
Constructor
HashSet()  O(1) Initializes an empty set of the specified element type.
Methods
add(value)  O(1) Adds an element to this set, if it was not already there.
clear()  O(N) Removes all elements from this set.
contains(value)  O(1) Returns true if the specified value is in this set.
equals(set)  O(N) Returns true if the two sets contain the same elements.
first()  O(1) Returns the first value in the set in the order established by a for-each loop.
isEmpty()  O(1) Returns true if this set contains no elements.
isSubsetOf(set2)  O(N) Implements the subset relation on sets.
mapAll(fn)  O(N) Iterates through the elements of the set and calls fn(value) for each one.
remove(value)  O(1) Removes an element from this set.
size()  O(1) Returns the number of elements in this set.
toString()  O(N) Converts the set to a printable string representation.
Operators
set1 == set2  O(N) Returns true if set1 and set2 contain the same elements.
set1 != set2  O(N) Returns true if set1 and set2 are different.
set1 + set2  O(N) Returns the union of sets set1 and set2, which is the set of elements that appear in at least one of the two sets.
set + value  O(N) Returns the union of set set1 and individual value value.
set1 += set2;  O(N) Adds all of the elements from set2 (or the single specified value) to set1.
set += value;  O(1) Adds the single specified value to the set.
set1 - set2  O(N) Returns the difference of sets set1 and set2, which is all of the elements that appear in set1 but not set2.
set - value  O(N) Returns the set set with value removed.
set1 -= set2;  O(N) Removes the elements from set2 (or the single specified value) from set1.
set -= value;  O(1) Removes the single specified value from the set.
set1 * set2  O(N) Returns the intersection of sets set1 and set2, which is the set of all elements that appear in both.
set1 *= set2;  O(N) Removes any elements from set1 that are not present in set2.
ostream << set O(N) Outputs the contents of the set to the given output stream.
istream >> set O(N) Reads the contents of the given input stream into the set.

Constructor detail


HashSet();
Initializes an empty set of the specified element type.

Usage:

HashSet<ValueType> set;

Method detail


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 clear();
Removes all elements from this set.

Usage:

set.clear();

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

Usage:

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

bool equals(const HashSet& grid) const;
Returns true if the two sets contain exactly the same element values. Identical in behavior to the == operator.

Usage:

if (set.equals(set2)) ...

ValueType first() const;
Returns the first value in the set in the order established by a for-each loop. If the set is empty, first generates an error.

Usage:

ValueType value = set.first();

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

Usage:

if (set.isEmpty()) ...

bool isSubsetOf(const HashSet& set2) const;
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 mapAll(void (*fn)(ValueType)) const;
void mapAll(void (*fn)(const ValueType&)) const;
void mapAll(FunctorType fn) const;
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.

Usage:

set.mapAll(fn);

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);

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

Usage:

count = set.size();

string toString();
Converts the set to a printable string representation, such as "{value1, value2, value3}". The values appear in an unpredictable order.

Usage:

string str = set.toString();

Operator detail


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

Usage:

set1 == set2

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

Usage:

set1 != set2

HashSet operator+(const HashSet& set2) const;
HashSet operator+(ValueType element) const;
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

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

Usage:

set1 * set2

HashSet operator-(const HashSet& set2) const;
HashSet operator-(ValueType element) const;
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

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

Usage:

set1 += set2;
set1 += value;

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

Usage:

set1 *= set2;

HashSet& operator-=(const HashSet& set2);
HashSet& operator-=(ValueType value);
Removes the elements from set2 (or the single specified value) from set1. As a convenience, the HashSet 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;