41template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE = 128,
int STACK_SIZE = 1>
class HashTable
73 IndexT Add(
const KEYTYPE& key,
const VALUETYPE& value);
112 friend class HashTable<KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE>;
130 template <
typename HASHKEY>
const uint32_t
GetHashCode(
const typename std::enable_if<std::is_integral<HASHKEY>::value, HASHKEY>::type& key)
const {
using UNSIGNED =
typename std::make_unsigned<HASHKEY>::type;
return uint32_t(UNSIGNED(key) % UNSIGNED(this->hashArray.
Size())); };
132 template <
typename HASHKEY>
const uint32_t
GetHashCode(
const HASHKEY& key)
const {
return key.HashCode() % this->hashArray.
Size(); };
134 template <
typename HASHKEY>
const uint32_t
GetHashCode(
const typename std::enable_if<std::is_pointer<HASHKEY>::value, HASHKEY>::type& key)
const {
return key->HashCode() % this->hashArray.
Size(); }
140template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
145 for (
int i = 0; i < this->
hashArray.Size(); i++)
147 for (
int j = 0; j < this->
hashArray[i].Size();j++)
158template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
163 for (
int i = 0; i < this->
hashArray.Size(); i++)
165 for (
int j = 0; j < this->
hashArray[i].Size(); j++)
176template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
187 for (i = 0; i < this->
hashArray.Size(); i++)
204template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
220template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
233template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
246template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
259template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
274template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
280 this->
hashArray = std::move(rhs.hashArray);
283 this->
size = rhs.size;
284 rhs.inBulkAdd =
false;
292template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
300 int numHashElements = hashElements.
Size();
301 #if NEBULA_BOUNDSCHECKS
305 if (numHashElements > 0)
309 hashElementIndex = hashElements.template BinarySearchIndex<KEYTYPE>(key);
311 #if NEBULA_BOUNDSCHECKS
314 return hashElements[hashElementIndex].Value();
320template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
330template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
340template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
346 for (i = 0; i < num; i++)
356template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
362 for (i = 0; i < num; i++)
372template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
376 return (0 == this->
size);
382template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
394template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
404template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
408#if NEBULA_BOUNDSCHECKS
420 ret = this->
hashArray[hashIndex].InsertSorted(kvp);
429template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
434 return this->
Add(kvp);
440template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
448 if (hashElements.
Size() == 0)
450 elementIndex = this->
Add(key, VALUETYPE());
458 elementIndex = hashElements.template BinarySearchIndex<KEYTYPE>(key);
462 elementIndex = this->
Add(key, VALUETYPE());
465 return hashElements[elementIndex].Value();
471template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
477 for (i = 0; i < this->
bulkDirty.Size(); i++)
491template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
497 for (i = 0; i < rhs.
hashArray.Size(); i++)
500 for (j = 0; j < rhs.
hashArray[i].Size(); j++)
508template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
512 #if NEBULA_BOUNDSCHECKS
518 IndexT hashElementIndex = hashElements.template BinarySearchIndex<KEYTYPE>(key);
519 #if NEBULA_BOUNDSCHECKS
529template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
533#if NEBULA_BOUNDSCHECKS
538#if NEBULA_BOUNDSCHECKS
548template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
556 if (hashElements.
Size() == 0)
return false;
563 hashElementIndex = hashElements.template BinarySearchIndex<KEYTYPE>(key);
576template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
586 hashElementIndex = hashElements.template BinarySearchIndex<KEYTYPE>(key);
587 return hashElementIndex;
593template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
599 return hashElements[i].Value();
605template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
612 for (i = 0; i < num; i++)
625template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
674template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
684template<
class KEYTYPE,
class VALUETYPE,
int TABLE_SIZE,
int STACK_SIZE>
688 return !(*
this == rhs);
void Append(const TYPE &first, const ELEM_TYPE &... elements)
Append multiple elements to the end of the array.
Definition array.h:1392
void AppendArray(const Array< TYPE, SMALL_VECTOR_SIZE > &rhs)
append the contents of an array to this array
Definition array.h:801
void EraseIndex(IndexT index)
erase element at index, keep sorting intact
Definition array.h:1069
const SizeT Size() const
get number of elements in array
Definition array.h:913
Implements a fixed size one-dimensional array.
Definition fixedarray.h:20
const SizeT Size() const
get number of elements
Definition fixedarray.h:538
Definition hashtable.h:98
IndexT bucketIndex
Definition hashtable.h:115
Iterator & operator++(int)
progress to next item in the hash table
Definition hashtable.h:627
const bool operator!=(const Iterator &rhs) const
check if iterator is identical
Definition hashtable.h:686
const bool operator==(const Iterator &rhs) const
check if iterator is identical
Definition hashtable.h:676
KEYTYPE const * key
Definition hashtable.h:110
uint32_t hashIndex
Definition hashtable.h:114
FixedArray< StackArray< KeyValuePair< KEYTYPE, VALUETYPE >, STACK_SIZE > > * arr
Definition hashtable.h:113
VALUETYPE * val
the current value
Definition hashtable.h:109
StackArray< KeyValuePair< KEYTYPE, VALUETYPE >, STACK_SIZE > Content() const
return array of all key/value pairs in the table (slow)
Definition hashtable.h:607
void EndBulkAdd()
end bulk adding, which sorts all affected tables
Definition hashtable.h:473
IndexT FindIndex(const KEYTYPE &key) const
find index in bucket
Definition hashtable.h:578
VALUETYPE & Emplace(const KEYTYPE &key)
adds element only if it doesn't exist, and return reference to it
Definition hashtable.h:442
void Reset()
reset the hashtable arrays to 0 size, but don't run destructor
Definition hashtable.h:358
void operator=(const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &rhs)
assignment operator
Definition hashtable.h:261
FixedArray< bool > bulkDirty
Definition hashtable.h:126
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
Definition hashtable.h:130
VALUETYPE & ValueAtIndex(const KEYTYPE &key, IndexT i) const
get value from key and bucket
Definition hashtable.h:595
VALUETYPE & operator[](const KEYTYPE &key) const
read/write [] operator, assertion if key not found
Definition hashtable.h:294
StackArray< KEYTYPE, STACK_SIZE > KeysAsArray() const
get all keys as an Util::Array (slow)
Definition hashtable.h:142
int size
Definition hashtable.h:124
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
Definition hashtable.h:406
HashTable()
default constructor
Definition hashtable.h:221
SizeT Size() const
return current number of values in the hashtable
Definition hashtable.h:322
const uint32_t GetHashCode(const HASHKEY &key) const
if not, call the function on HashCode on HASHKEY
Definition hashtable.h:132
bool IsEmpty() const
return true if empty
Definition hashtable.h:374
void Clear()
clear the hashtable
Definition hashtable.h:342
void Erase(const KEYTYPE &key)
erase an entry
Definition hashtable.h:510
SizeT Capacity() const
return fixed-size capacity of the hash table
Definition hashtable.h:332
void BeginBulkAdd()
begin bulk adding to the hashtable, which will amortize the cost of sorting the hash buckets upon end
Definition hashtable.h:384
void Merge(const HashTable< KEYTYPE, VALUETYPE, TABLE_SIZE, STACK_SIZE > &rhs)
merge two dictionaries
Definition hashtable.h:493
bool Contains(const KEYTYPE &key) const
return true if key exists in the array
Definition hashtable.h:550
StackArray< VALUETYPE, STACK_SIZE > ValuesAsArray() const
get all keys as an Util::Array (slow)
Definition hashtable.h:160
bool inBulkAdd
Definition hashtable.h:127
void EraseIndex(const KEYTYPE &key, IndexT i)
erase an entry with known index
Definition hashtable.h:531
Iterator Begin()
get iterator to first element
Definition hashtable.h:178
const bool IsBulkAdd() const
returns true if currently bulk adding
Definition hashtable.h:396
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
Definition hashtable.h:134
Iterator End()
get iterator to last element
Definition hashtable.h:206
FixedArray< StackArray< KeyValuePair< KEYTYPE, VALUETYPE >, STACK_SIZE > > hashArray
Definition hashtable.h:123
Key/Value pair objects are used by most assiociative container classes, like Dictionary or HashTable.
Definition keyvaluepair.h:19
const KEYTYPE & Key() const
read access to key
Definition keyvaluepair.h:282
#define n_assert(exp)
Definition debug.h:50
A quad tree designed to return regions of free 2D space.
Definition String.cs:6
Array< TYPE, STACK_SIZE > StackArray
Definition array.h:2001
Nebula's scalar datatype.
static const int InvalidIndex
Definition types.h:47
int SizeT
Definition types.h:42
int IndexT
Definition types.h:41