Nebula
|
#include <hashtable.h>
Organizes key/value pairs by a hash code.
Looks very similar to a Dictionary, but may provide better search times (up to O(1)) by computing a (ideally unique) hash code on the key and using that as an index into an array. The flipside is that the key class must provide a hash code and the memory footprint may be larger then Dictionary.
The default capacity is 128. Matching the capacity against the number of expected elements in the hash table is one key to get optimal insertion and search times, the other is to provide a good (and fast) hash code computation which produces as few collissions as possible for the key type.
The key class must implement the following method in order to work with the HashTable:
uint32_t HashCode() const;
The Util::String class implements this method as an example. Internally the hash table is implemented as a fixed array of sorted arrays. The fixed array is indexed by the hash code of the key, the sorted arrays contain all values with identical keys.
Classes | |
class | Iterator |
Public Member Functions | |
HashTable () | |
default constructor | |
HashTable (const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &rhs) | |
copy constructor | |
HashTable (HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &&rhs) | |
move constructor | |
void | operator= (const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &rhs) |
assignment operator | |
void | operator= (HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &&rhs) |
move assignment operator | |
VALUETYPE & | operator[] (const KEYTYPE &key) const |
read/write [] operator, assertion if key not found | |
SizeT | Size () const |
return current number of values in the hashtable | |
SizeT | Capacity () const |
return fixed-size capacity of the hash table | |
void | Clear () |
clear the hashtable | |
void | Reset () |
reset the hashtable arrays to 0 size, but don't run destructor | |
bool | IsEmpty () const |
return true if empty | |
void | BeginBulkAdd () |
begin bulk adding to the hashtable, which will amortize the cost of sorting the hash buckets upon end | |
const bool | IsBulkAdd () const |
returns true if currently bulk adding | |
IndexT | Add (const KeyValuePair< KEYTYPE, VALUETYPE > &kvp) |
add a key/value pair object to the hash table, returns index within hash array where item is stored | |
IndexT | Add (const KEYTYPE &key, const VALUETYPE &value) |
add a key and associated value | |
VALUETYPE & | Emplace (const KEYTYPE &key) |
adds element only if it doesn't exist, and return reference to it | |
void | EndBulkAdd () |
end bulk adding, which sorts all affected tables | |
void | Merge (const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &rhs) |
merge two dictionaries | |
void | Erase (const KEYTYPE &key) |
erase an entry | |
void | EraseIndex (const KEYTYPE &key, IndexT i) |
erase an entry with known index | |
bool | Contains (const KEYTYPE &key) const |
return true if key exists in the array | |
IndexT | FindIndex (const KEYTYPE &key) const |
find index in bucket | |
VALUETYPE & | ValueAtIndex (const KEYTYPE &key, IndexT i) const |
get value from key and bucket | |
StackArray< KeyValuePair< KEYTYPE, VALUETYPE >, STACK_SIZE > | Content () const |
return array of all key/value pairs in the table (slow) | |
StackArray< KEYTYPE, STACK_SIZE > | KeysAsArray () const |
get all keys as an Util::Array (slow) | |
StackArray< VALUETYPE, STACK_SIZE > | ValuesAsArray () const |
get all keys as an Util::Array (slow) | |
Iterator | Begin () |
get iterator to first element | |
Iterator | End () |
get iterator to last element | |
Private Member Functions | |
template<typename HASHKEY > | |
const uint32_t | GetHashCode (const typename std::enable_if< std::is_integral< HASHKEY >::value, HASHKEY >::type &key) const |
if type is integral, just use that value directly | |
template<typename HASHKEY > | |
const uint32_t | GetHashCode (const HASHKEY &key) const |
if not, call the function on HashCode on HASHKEY | |
template<typename HASHKEY > | |
const uint32_t | GetHashCode (const typename std::enable_if< std::is_pointer< HASHKEY >::value, HASHKEY >::type &key) const |
if type is pointer, convert using questionable method | |
Private Attributes | |
FixedArray< StackArray< KeyValuePair< KEYTYPE, VALUETYPE >, STACK_SIZE > > | hashArray |
int | size |
FixedArray< bool > | bulkDirty |
bool | inBulkAdd |
Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::HashTable | ( | ) |
default constructor
Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::HashTable | ( | const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > & | rhs | ) |
copy constructor
Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::HashTable | ( | HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > && | rhs | ) |
move constructor
IndexT Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Add | ( | const KEYTYPE & | key, |
const VALUETYPE & | value ) |
add a key and associated value
IndexT Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Add | ( | const KeyValuePair< KEYTYPE, VALUETYPE > & | kvp | ) |
add a key/value pair object to the hash table, returns index within hash array where item is stored
HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Iterator Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Begin | ( | ) |
get iterator to first element
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::BeginBulkAdd | ( | ) |
begin bulk adding to the hashtable, which will amortize the cost of sorting the hash buckets upon end
SizeT Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Capacity | ( | ) | const |
return fixed-size capacity of the hash table
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Clear | ( | ) |
clear the hashtable
bool Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Contains | ( | const KEYTYPE & | key | ) | const |
return true if key exists in the array
StackArray< KeyValuePair< KEYTYPE, VALUETYPE >, STACK_SIZE > Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Content | ( | ) | const |
return array of all key/value pairs in the table (slow)
VALUETYPE & Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Emplace | ( | const KEYTYPE & | key | ) |
adds element only if it doesn't exist, and return reference to it
HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Iterator Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::End | ( | ) |
get iterator to last element
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::EndBulkAdd | ( | ) |
end bulk adding, which sorts all affected tables
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Erase | ( | const KEYTYPE & | key | ) |
erase an entry
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::EraseIndex | ( | const KEYTYPE & | key, |
IndexT | i ) |
erase an entry with known index
IndexT Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::FindIndex | ( | const KEYTYPE & | key | ) | const |
find index in bucket
|
inlineprivate |
if not, call the function on HashCode on HASHKEY
|
inlineprivate |
if type is integral, just use that value directly
|
inlineprivate |
if type is pointer, convert using questionable method
|
inline |
returns true if currently bulk adding
bool Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::IsEmpty | ( | ) | const |
return true if empty
StackArray< KEYTYPE, STACK_SIZE > Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::KeysAsArray | ( | ) | const |
get all keys as an Util::Array (slow)
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Merge | ( | const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > & | rhs | ) |
merge two dictionaries
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::operator= | ( | const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > & | rhs | ) |
assignment operator
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::operator= | ( | HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > && | rhs | ) |
move assignment operator
VALUETYPE & Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::operator[] | ( | const KEYTYPE & | key | ) | const |
read/write [] operator, assertion if key not found
void Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Reset | ( | ) |
reset the hashtable arrays to 0 size, but don't run destructor
SizeT Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::Size | ( | ) | const |
return current number of values in the hashtable
VALUETYPE & Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::ValueAtIndex | ( | const KEYTYPE & | key, |
IndexT | i ) const |
get value from key and bucket
StackArray< VALUETYPE, STACK_SIZE > Util::HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE >::ValuesAsArray | ( | ) | const |
get all keys as an Util::Array (slow)
|
private |
|
private |
|
private |
|
private |