Grafalgo
Library of useful data structures and algorithms
|
Maintains a set, where an element is a 64 bit integer. More...
#include <HashSet.h>
Public Member Functions | |
HashSet (int) | |
Constructor for HashSet, allocates space and initializes set. | |
~HashSet () | |
Destructor for HashSet. | |
void | clear () |
Clear the set contents. | |
void | resize (int) |
Resize a HashSet object, discarding old contents. | |
void | expand (int) |
Expand the space available for this HashSet. | |
void | copyFrom (const HashSet &) |
Copy into this from source. | |
int | size () const |
Determine the number of elements in the set. | |
bool | member (int64_t) const |
Determine if an element is in the set. | |
index | first () const |
Get the index of the first element in the set. | |
index | next (index) const |
Determine the index of the next element in the set. | |
bool | isValid (index) const |
Determine if an index corresponds to a set element. | |
uint64_t | val (index) const |
Get the value of an element in the set. | |
index | getIndex (int64_t) const |
Perform a lookup in the set. | |
index | insert (int64_t) |
Insert a value into the set. | |
void | remove (int64_t) |
Remove a value from the set. | |
string & | toString (string &) const |
Create a string representation of the set. | |
Private Types | |
typedef uint32_t | bkt_t [BKT_SIZ] |
bucket type | |
Private Member Functions | |
uint32_t | hashit (int64_t, int) const |
Compute a bucket index for a given val. | |
void | makeSpace (int) |
Allocate and initialize space for list. | |
void | freeSpace () |
Free dynamic storage used by list. | |
Private Attributes | |
int | nb |
# of buckets in each half | |
uint32_t | bktMsk |
mask used by hash function | |
bkt_t * | bkt |
buckets for hash table | |
SetPair * | ex |
indexes of in-set elements and out | |
Static Private Attributes | |
static const int | BKT_SIZ = 8 |
# of elements per bucket |
Maintains a set, where an element is a 64 bit integer.
Main methods member - tests an element for membership in the set insert - adds an element to the set remove - removes an element from the set
Each element is assigned an index that can be used for iterating through the set.
The implementation uses a 2-left hash table with eight items in each bucket. The table is dimensioned for a load factor of no more than 50%.
grafalgo::HashSet::HashSet | ( | int | n1 | ) |
Constructor for HashSet, allocates space and initializes set.
N1 is the target number of elements in the table; the table is dimensioned for a load of no more than 0.5.
Definition at line 17 of file HashSet.cpp.
grafalgo::HashSet::~HashSet | ( | ) |
Destructor for HashSet.
Definition at line 20 of file HashSet.cpp.
void grafalgo::HashSet::clear | ( | ) | [virtual] |
Clear the set contents.
Implements grafalgo::Adt.
Definition at line 69 of file HashSet.cpp.
void grafalgo::HashSet::copyFrom | ( | const HashSet & | source | ) |
Copy into this from source.
Definition at line 74 of file HashSet.cpp.
void grafalgo::HashSet::expand | ( | int | size | ) | [virtual] |
Expand the space available for this HashSet.
Rebuilds old value in new space. This operation changes the index values assigned to each element.
size | is the size of the resized object. |
Implements grafalgo::Adt.
Definition at line 62 of file HashSet.cpp.
index grafalgo::HashSet::first | ( | ) | const [inline] |
void grafalgo::HashSet::freeSpace | ( | ) | [private] |
Free dynamic storage used by list.
Definition at line 44 of file HashSet.cpp.
index grafalgo::HashSet::getIndex | ( | int64_t | val | ) | const |
Perform a lookup in the set.
val | is the value to be looked up in the table |
Definition at line 109 of file HashSet.cpp.
uint32_t grafalgo::HashSet::hashit | ( | int64_t | val, |
int | hf | ||
) | const [private] |
Compute a bucket index for a given val.
Hashit uses multiplicative hashing with one of two different multipliers, after first converting the 64 bit integer into a 32 bit integer.
val | is the value to be hashed |
hf | is either 0 or 1 and selects one of two hash functions |
Definition at line 92 of file HashSet.cpp.
index grafalgo::HashSet::insert | ( | int64_t | val | ) |
Insert a value into the set.
If the value is already in the set, no change is made.
val | is the value to insert |
Definition at line 133 of file HashSet.cpp.
bool grafalgo::HashSet::isValid | ( | index | x | ) | const [inline] |
void grafalgo::HashSet::makeSpace | ( | int | size | ) | [private] |
Allocate and initialize space for list.
size | is number of index values to provide space for |
Definition at line 25 of file HashSet.cpp.
bool grafalgo::HashSet::member | ( | int64_t | e | ) | const [inline] |
index grafalgo::HashSet::next | ( | index | x | ) | const [inline] |
void grafalgo::HashSet::remove | ( | int64_t | val | ) |
Remove a value from the set.
val | is the value of the pair to be removed |
Definition at line 170 of file HashSet.cpp.
void grafalgo::HashSet::resize | ( | int | size | ) | [virtual] |
Resize a HashSet object, discarding old contents.
size | is the size of the resized object. |
Implements grafalgo::Adt.
Definition at line 49 of file HashSet.cpp.
int grafalgo::HashSet::size | ( | ) | const [inline] |
string & grafalgo::HashSet::toString | ( | string & | s | ) | const [virtual] |
Create a string representation of the set.
s | is a reference to a string in whcih result is returned |
Implements grafalgo::Adt.
Definition at line 193 of file HashSet.cpp.
uint64_t grafalgo::HashSet::val | ( | index | x | ) | const [inline] |