Generic hash table for data objects. More...
#include <datahashtable.h>
Classes | |
class | Element |
template class for elements stored in the hash table More... | |
Public Member Functions | |
Access / modification | |
bool | has (const HashItem &h) const |
Is item h present in DataHashTable? More... | |
const Info * | get (const HashItem &h) const |
returns const pointer to Info of HashItem h or 0, if item is not found. More... | |
const Info & | operator[] (const HashItem &h) const |
references Info of HashItem h . More... | |
void | add (const HashItem &h, const Info &info) |
adds a new entry to the hash table. More... | |
void | remove (const HashItem &h) |
remove HashItem h from the DataHashTable. More... | |
void | clear () |
remove all entries from DataHashTable. More... | |
void | reMax (int newSize=-1, int newHashSize=0) |
reset size of the DataHashTable. More... | |
Debugging | |
bool | isConsistent () const |
checks whether DataHashTable is consistent More... | |
Construction / destruction | |
DataHashTable (int(*hashfun)(const HashItem *), int maxsize=265, int hashsize=0, Real factor=2.0) | |
default constructor. More... | |
DataHashTable & | operator= (const DataHashTable &base) |
assignment operator. More... | |
DataHashTable (const DataHashTable &base) | |
copy constructor. More... | |
Private Types | |
Types | |
typedef Element< HashItem, Info > | Elem |
Private Member Functions | |
Helpers | |
int | autoHashSize () const |
determine a good m_hashsize. More... | |
int | autoHashSizeold () const |
automatically computes a good m_hashsize. More... | |
int | index (const HashItem &h) const |
returns hash index of HashItem h or -1, if h is not present. More... | |
Private Attributes | |
Data | |
Array< Elem > | m_elem |
stores all elements of the hash table More... | |
int | m_hashsize |
increment added to hash index, if allready used More... | |
int | m_used |
current number of entries in the hash table More... | |
int(* | m_hashfun )(const HashItem *) |
pointer to hash function (mapping: HashItem -> int) More... | |
Real | m_memfactor |
memory is reMax()ed by this factor if a new element does't fit More... | |
int | primes [50] |
some prime numbers for fast access More... | |
int | nprimes |
number of stored prime numbers More... | |
Generic hash table for data objects.
Class DataHashTable provides a generic hash table for Data Objects, i.e., a map that maps arguments called HashItems to values called Infos. HashItem and Info types are passed as template arguments. HashItems must provide a comparison operator==(). Furthermore, both the HashItem and Info must be data objects in the sense that the assignment operator is equivalent to a memcpy()
of the structure and no destructor is required.
The construction of a DataHashTable requires a hash function that assigns an integer value to every HashItem. Provided this, pairs of a HashItem and a Info can be added to the DataHashTable. No more than one Info can be assigned to the same HashItem at a time. The Info to a HashItem can be accessed through the subscript operator[]() with the Info object as a subscript.
The maximum number of elemens a DataHashTable can hold can be specified upon construction and may be reset with reMax() later on. Further, a value hash size value is required. This value must be less then the maximum number of elements and must not have a common dominator with the maximum number of elements. If not specified explicitely, it is set automatically to a reasonable value.
The implementation relies on an array of DataHashTable::Elements, from now on referred to as elements. Upon construction, all elements are marked as FREE
. When an entry is added to the DataHashTable, the hash value is computed by calling m_hashfun for its HashItem. If this array element is unused, it is taken right away. Otherwise, the array index is incremented by the hash size (modulo the element array size()) until an unused element is found.
Removing elements is simply done by marking it as RELEASED
. Hence, when searching for an element, the search loop may not stop, when a RELEASED
element is encountered. However, such an element may be reused when adding a new element to the DataHashTable.
Further, memory management with resizing of the element array is straight forward.
Definition at line 88 of file datahashtable.h.
Definition at line 112 of file datahashtable.h.
|
explicit |
default constructor.
Allocates a DataHashTable for maxsize
entries using hashfun
as hash function. If hashsize
> 0, m_hashsize is set to the specified value, otherwise a suitable hash size is computed automatically. Parameter factor
is used for memory management: If more than maxsize
entries are added to the DataHashTable, it will automatically be reMax()ed by a factor of factor
.
hashfun | pointer to hash function. |
maxsize | maximum number of hash elements. |
hashsize | hash size. |
factor | factor for increasing data block. |
Definition at line 293 of file datahashtable.h.
References DataHashTable< HashItem, Info >::autoHashSize(), DataHashTable< HashItem, Info >::clear(), DataHashTable< HashItem, Info >::isConsistent(), DataHashTable< HashItem, Info >::m_hashsize, DataHashTable< HashItem, Info >::m_memfactor, DataHashTable< HashItem, Info >::nprimes, and DataHashTable< HashItem, Info >::primes.
DataHashTable | ( | const DataHashTable< HashItem, Info > & | base | ) |
copy constructor.
Definition at line 372 of file datahashtable.h.
References DataHashTable< HashItem, Info >::isConsistent(), DataHashTable< HashItem, Info >::m_memfactor, and DataHashTable< HashItem, Info >::primes.
void add | ( | const HashItem & | h, |
const Info & | info | ||
) |
adds a new entry to the hash table.
Adds a new entry consisting of HashItem h
and Info info
to the DataHashTable. No entry with HashItem h
must be in the DataHashTable yet. After completion, info
may be accessed via get() or operator[]() with h
as parameter. The DataHashTable is reMax()ed if it becomes neccessary.
Definition at line 177 of file datahashtable.h.
References DataHashTable< HashItem, Info >::has(), DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::m_hashfun, DataHashTable< HashItem, Info >::m_hashsize, DataHashTable< HashItem, Info >::m_memfactor, DataHashTable< HashItem, Info >::m_used, DataHashTable< HashItem, Info >::reMax(), SOPLEX_HASHTABLE_FILLFACTOR, and DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::USED.
Referenced by DataHashTable< HashItem, Info >::reMax().
|
private |
determine a good m_hashsize.
Determine next larger prime number for new m_hashsize
Definition at line 395 of file datahashtable.h.
References DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::nprimes, and DataHashTable< HashItem, Info >::primes.
Referenced by DataHashTable< HashItem, Info >::DataHashTable(), and DataHashTable< HashItem, Info >::reMax().
|
private |
automatically computes a good m_hashsize.
Computes a good m_hashsize as the product of all prime numbers not divisors of the number of elements that are <= the maximum divisor of the number of elemens.
Definition at line 432 of file datahashtable.h.
References DataHashTable< HashItem, Info >::m_elem.
void clear | ( | ) |
remove all entries from DataHashTable.
Definition at line 218 of file datahashtable.h.
References DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::FREE, DataHashTable< HashItem, Info >::m_elem, and DataHashTable< HashItem, Info >::m_used.
Referenced by DataHashTable< HashItem, Info >::DataHashTable(), and DataHashTable< HashItem, Info >::reMax().
const Info * get | ( | const HashItem & | h | ) | const |
returns const pointer to Info of HashItem h
or 0, if item is not found.
Returns a pointer to Info component of hash element h
or a zero pointer if element h
is not in the table.
Definition at line 151 of file datahashtable.h.
References DataHashTable< HashItem, Info >::index(), and DataHashTable< HashItem, Info >::m_elem.
bool has | ( | const HashItem & | h | ) | const |
Is item h
present in DataHashTable?
Definition at line 141 of file datahashtable.h.
References DataHashTable< HashItem, Info >::index().
Referenced by DataHashTable< HashItem, Info >::add(), DataHashTable< HashItem, Info >::isConsistent(), DataHashTable< HashItem, Info >::operator[](), and DataHashTable< HashItem, Info >::remove().
|
private |
returns hash index of HashItem h
or -1, if h
is not present.
Using the hash function m_hashfun, the hash value of h
is calculated. Starting with this hash index, every m_hashsize%-th element is compared with h
until h
is found or all elements have been checked.
h | HashItem, for which the hash index should be calculated |
h
or -1, if h
is not a member of the hash table Definition at line 475 of file datahashtable.h.
References DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::FREE, DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::m_hashsize, DataHashTable< HashItem, Info >::m_used, and DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::USED.
Referenced by DataHashTable< HashItem, Info >::get(), DataHashTable< HashItem, Info >::has(), DataHashTable< HashItem, Info >::operator[](), and DataHashTable< HashItem, Info >::remove().
bool isConsistent | ( | ) | const |
checks whether DataHashTable is consistent
Definition at line 251 of file datahashtable.h.
References DataHashTable< HashItem, Info >::has(), DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::m_used, SPX_MSG_INCONSISTENT, and DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::USED.
Referenced by DataHashTable< HashItem, Info >::DataHashTable(), and DataHashTable< HashItem, Info >::operator=().
DataHashTable & operator= | ( | const DataHashTable< HashItem, Info > & | base | ) |
assignment operator.
Definition at line 356 of file datahashtable.h.
References DataHashTable< HashItem, Info >::isConsistent(), DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::m_hashfun, DataHashTable< HashItem, Info >::m_hashsize, DataHashTable< HashItem, Info >::m_memfactor, DataHashTable< HashItem, Info >::m_used, DataHashTable< HashItem, Info >::nprimes, and DataHashTable< HashItem, Info >::primes.
const Info & operator[] | ( | const HashItem & | h | ) | const |
references Info of HashItem h
.
Index operator for accessing the Info associated to HashItem h
. It is required that h
belongs to the DataHashTable, otherwise it core dumps. Methods has() or get() can be used for inquiring wheater h
belongs to the DataHashTable or not.
Definition at line 164 of file datahashtable.h.
References DataHashTable< HashItem, Info >::has(), DataHashTable< HashItem, Info >::index(), and DataHashTable< HashItem, Info >::m_elem.
void reMax | ( | int | newSize = -1 , |
int | newHashSize = 0 |
||
) |
reset size of the DataHashTable.
Reset the maximum number of elements of a DataHashTable to newSize
. However, if newSize
< m_used, it is resized to m_used only. If newHashSize
< 1, a new hash size is computed automatically. Otherwise, the specified value will be taken.
Definition at line 231 of file datahashtable.h.
References DataHashTable< HashItem, Info >::add(), DataHashTable< HashItem, Info >::autoHashSize(), DataHashTable< HashItem, Info >::clear(), DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::m_hashsize, DataHashTable< HashItem, Info >::m_used, Array< T >::size(), and DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::USED.
Referenced by DataHashTable< HashItem, Info >::add().
void remove | ( | const HashItem & | h | ) |
remove HashItem h
from the DataHashTable.
Definition at line 205 of file datahashtable.h.
References DataHashTable< HashItem, Info >::has(), DataHashTable< HashItem, Info >::index(), DataHashTable< HashItem, Info >::m_elem, DataHashTable< HashItem, Info >::m_used, and DataHashTable< HashItem, Info >::Element< ElemHashItem, ElemInfo >::RELEASED.
stores all elements of the hash table
Definition at line 119 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::add(), DataHashTable< HashItem, Info >::autoHashSize(), DataHashTable< HashItem, Info >::autoHashSizeold(), DataHashTable< HashItem, Info >::clear(), DataHashTable< HashItem, Info >::get(), DataHashTable< HashItem, Info >::index(), DataHashTable< HashItem, Info >::isConsistent(), DataHashTable< HashItem, Info >::operator=(), DataHashTable< HashItem, Info >::operator[](), DataHashTable< HashItem, Info >::reMax(), and DataHashTable< HashItem, Info >::remove().
|
private |
pointer to hash function (mapping: HashItem -> int)
Definition at line 125 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::add(), and DataHashTable< HashItem, Info >::operator=().
|
private |
increment added to hash index, if allready used
Definition at line 121 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::add(), DataHashTable< HashItem, Info >::DataHashTable(), DataHashTable< HashItem, Info >::index(), DataHashTable< HashItem, Info >::operator=(), and DataHashTable< HashItem, Info >::reMax().
|
private |
memory is reMax()ed by this factor if a new element does't fit
Definition at line 127 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::add(), DataHashTable< HashItem, Info >::DataHashTable(), and DataHashTable< HashItem, Info >::operator=().
|
private |
current number of entries in the hash table
Definition at line 123 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::add(), DataHashTable< HashItem, Info >::clear(), DataHashTable< HashItem, Info >::index(), DataHashTable< HashItem, Info >::isConsistent(), DataHashTable< HashItem, Info >::operator=(), DataHashTable< HashItem, Info >::reMax(), and DataHashTable< HashItem, Info >::remove().
|
private |
number of stored prime numbers
Definition at line 131 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::autoHashSize(), DataHashTable< HashItem, Info >::DataHashTable(), and DataHashTable< HashItem, Info >::operator=().
|
private |
some prime numbers for fast access
Definition at line 129 of file datahashtable.h.
Referenced by DataHashTable< HashItem, Info >::autoHashSize(), DataHashTable< HashItem, Info >::DataHashTable(), and DataHashTable< HashItem, Info >::operator=().