Base class for HashMap. More...
Classes | |
class | const_iterator |
STL compatible iterator base class. More... | |
class | Entry |
Base data entry for HashMap. More... | |
Public Member Functions | |
Entry * | GetEntry (uintptr_t uIndex) noexcept |
Return the pointer to an Entry. | |
const Entry * | GetEntry (uintptr_t uIndex) const noexcept |
Return a constant pointer to an Entry. | |
void | Clear (void) noexcept |
Purge all allocated data. | |
void | Resize (uintptr_t uNewSize) noexcept |
Sets a specific capacity to the hash. | |
void | SetCapacity (uintptr_t uNewSize) noexcept |
Sets a comfortable capacity of the hash. | |
uintptr_t | GetEntryCount (void) const noexcept |
Returns the number of valid entries in the hash. | |
uintptr_t | GetSizeMask (void) const noexcept |
Returns the mask used by the hash for rounding. | |
uint_t | IsEmpty (void) const noexcept |
Returns TRUE if the hash is empty. | |
uintptr_t | GetEntrySize (void) const noexcept |
Returns the size of each entry in bytes. | |
Static Public Attributes | |
static const uintptr_t | INVALID_HASH = UINTPTR_MAX |
Invalid hash value for marking an Entry as uninitialized. | |
static const uintptr_t | INVALID_INDEX = UINTPTR_MAX |
Error value for invalid indexes. | |
static const uintptr_t | END_OF_CHAIN = UINTPTR_MAX |
Constant to mark the end of a hash chain. | |
static const uintptr_t | EMPTY_RECORD = UINTPTR_MAX - 1 |
Constant to mark an unused hash record. | |
Protected Types | |
typedef uintptr_t(*) | HashProc(const void *pData, uintptr_t uDataSize) |
Function prototype for user supplied hash generator. | |
typedef uint_t(*) | TestProc(const void *pA, const void *pB) |
Function prototype for testing keys. | |
typedef void(*) | EntryConstructProc(Entry *pEntry) |
Function prototype for destroying entries. | |
typedef void(*) | EntryCopyProc(Entry *pEntry, const void *pT, const void *pU) |
Function prototype for destroying entries. | |
typedef void(*) | EntryInvalidateProc(Entry *pEntry) |
Function prototype for destroying entries. | |
Protected Member Functions | |
HashMapShared (uintptr_t uEntrySize, uintptr_t uFirstSize, uintptr_t uSecondOffset, TestProc pTestFunction, EntryConstructProc pEntryConstructFunction, EntryCopyProc pEntryCopyFunction, EntryInvalidateProc pEntryInvalidationFunction, HashProc pHashFunction=SDBMHashFunctor) noexcept | |
Default constructor. | |
uintptr_t | FindIndex (const void *pKey) const noexcept |
Locate an entry in the hash. | |
void | CreateBuffer (uintptr_t uCount, uintptr_t uEntrySize) noexcept |
Create a buffer to store all of the data entries. | |
void | CreateHashBuffer (uintptr_t uNewSize) noexcept |
Function to change the size of the buffer. | |
void | Erase (uintptr_t uIndex) noexcept |
Erase a specific hash entry. | |
void | Erase (const void *pKey) noexcept |
Erase a hash entry by searching for it. | |
uintptr_t | FindFirst (void) const noexcept |
Find the index for the first valid entry. | |
uintptr_t | ComputeHash (const void *pKey) const noexcept |
Calculate the hash for a key. | |
void | Copy (const HashMapShared *pInput) noexcept |
Replace the contents of this hash with a copy of another. | |
void | Add (const void *pT, const void *pU) noexcept |
Add a new key data pair into the hash. | |
const void * | GetData (const void *pT) const noexcept |
Get the pointer to the data index by a key. | |
Protected Attributes | |
void * | m_pEntries |
Pointer to the hash table Burger::Alloc(m_uEntrySize*(m_uSizeMask+1)) | |
uintptr_t | m_uEntrySize |
Size in bytes of each entry in the table. | |
uintptr_t | m_uFirstSize |
Size of the key in bytes. | |
uintptr_t | m_uSecondOffset |
Offset in bytes to the start of the data chunk. | |
uintptr_t | m_uEntryCount |
Number of valid entries in the hash. | |
uintptr_t | m_uSizeMask |
(Power of 2)-1 size mask used for masking indexes for instant table rounding | |
HashProc | m_pHashFunction |
Pointer to the hash function. | |
TestProc | m_pTestFunction |
Pointer to the equality test function. | |
EntryConstructProc | m_pEntryConstructFunction |
Pointer to function to construct Entry data. | |
EntryCopyProc | m_pEntryCopyFunction |
Pointer to function to copy construct Entry data. | |
EntryInvalidateProc | m_pEntryInvalidationFunction |
Pointer to function to destroy data in an Entry. | |
HashMap is a template class to quickly look up data chunks using a key value. To cut down on compile time and reduce code bloat, a majority of the runtime it contained in this class and the template creates a front end with only the minimum amount of code to support the class.
From a users point of view, HashMap is a 100% template based class, from a code point of view, HashMap is a dispatcher to HashMapShared.
|
protected |
Function prototype for destroying entries.
|
protected |
Function prototype for destroying entries.
|
protected |
Function prototype for destroying entries.
|
protected |
Function prototype for user supplied hash generator.
|
protected |
Function prototype for testing keys.
|
inlineprotectednoexcept |
Default constructor.
A HashMapShared needs values to properly support the derived class, notably the byte size of each data entry and a function to perform the hash calculation. This constructor is called by derived classes, it's not meant to be used by an application.
uEntrySize | Size of each Entry in bytes |
uFirstSize | Size in bytes for the key class, usually sizeof(T) |
uSecondOffset | Offset in bytes for the data value in Entry |
pTestFunction | Function that returns TRUE if the keys match |
pEntryConstructFunction | Function that invokes the default constructors for the Entry |
pEntryCopyFunction | Function that invokes the copy constructors for the Entry |
pEntryInvalidationFunction | Function that invokes the destructors for the Entry |
pHashFunction | Function that hashes the key value |
|
protectednoexcept |
Add a new key data pair into the hash.
Expand the size of the hash if needed, and then insert a new key data pair into the hash. This function should now be called if a key should be replaced if present. Use the HashMap::Set() function for that.
pT | Pointer to the key to add |
pU | Pointer to the data to add |
|
noexcept |
Purge all allocated data.
Iterate over all of the initialized entries and destroy any entry that has valid data using a function that will handle the derived class disposal.
If the invalidation function is NULL, the main buffer is discarded immediately.
|
protectednoexcept |
Calculate the hash for a key.
Given a pointer to a key and the length in bytes of the key, call the hash algorithm using the stored function pointer. If it the very rare case that a hash matches INVALID_HASH, it will be changed to INVALID_HASH -0x8000 to "validate" the hash.
pKey | Pointer to the key value |
|
protectednoexcept |
Replace the contents of this hash with a copy of another.
Clear out all the data in this hash and copy the entries from another hash into this one.
pInput | Pointer to the HashMap to copy data from |
|
protectednoexcept |
Create a buffer to store all of the data entries.
This helper function assumes that there is no data already allocated by the HashMapShared class. It will allocate a buffer and then mark each entry as empty and initialize all of the internal variables.
uCount | Number of entries to allocate (Must be a power of 2) |
uEntrySize | Size in bytes of each Entry record |
|
protectednoexcept |
Function to change the size of the buffer.
Dynamically resize the buffer and retain all data within by reentering every entry into the newly resized hash table
If uNewSize is zero, delete all data in the hash
uNewSize | Number of entries to allocate (Will be rounded up to a power of 2) |
|
protectednoexcept |
Erase a hash entry by searching for it.
Search the hash for a key and dispose of the Entry if one is found.
pKey | Pointer to the key value |
|
protectednoexcept |
Erase a specific hash entry.
Assuming a data entry is initialized, this function will remove it from the linked list and call the invalidation function.
If the entry being erased is part of a linked list chain, it will be destroyed, but the linked list will be retained. This is to allow iterators to continue to function without error.
uIndex | Index into the hash for the entry to erase. |
|
protectednoexcept |
Find the index for the first valid entry.
Iterate over the hash data and find the index to the first entry that contains valid data. This is used by HashMap::begin() and HashMap::begin() const
|
protectednoexcept |
Locate an entry in the hash.
Hash the key and use the hash to look up the data in the entry table. If found, an entry index is returned. If not, INVALID_INDEX is returned.
pKey | Pointer to the key value |
|
protectednoexcept |
Get the pointer to the data index by a key.
Given a pointer to a key, scan for it using FindIndex(const void *) and if found, return the pointer to the data pointed by the Entry pointer added with m_uSecondOffset.
pT | Pointer to the key look for |
|
inlinenoexcept |
Return a constant pointer to an Entry.
Since the Entry class is of a runtime decided size, this function manually performs the indexing into the array.
uIndex | Valid index into the array. |
|
inlinenoexcept |
Return the pointer to an Entry.
Since the Entry class is of a runtime decided size, this function manually performs the indexing into the array.
uIndex | Valid index into the array. |
|
inlinenoexcept |
Returns the number of valid entries in the hash.
|
inlinenoexcept |
Returns the size of each entry in bytes.
|
inlinenoexcept |
Returns the mask used by the hash for rounding.
When the hash buffer is created, it's set to a size that's a power of two and that value is stored as (size-1) to use as a wrap around mask. To get the hash size, take this value and add one to it.
|
inlinenoexcept |
|
noexcept |
Sets a specific capacity to the hash.
A non-destructive function to resize the hash to a specific size.
uNewSize | Number of entries for the new hash buffer |
|
noexcept |
Sets a comfortable capacity of the hash.
A non-destructive function to resize the hash to a size that has padding for new entries to be added with minimal memory allocations.
uNewSize | Minimum number of entries to size the cache |
|
static |
Constant to mark an unused hash record.
|
static |
Constant to mark the end of a hash chain.
|
static |
Invalid hash value for marking an Entry as uninitialized.
|
static |
Error value for invalid indexes.
|
protected |
Pointer to the hash table Burger::Alloc(m_uEntrySize*(m_uSizeMask+1))
|
protected |
Pointer to function to construct Entry data.
|
protected |
Pointer to function to copy construct Entry data.
|
protected |
Pointer to function to destroy data in an Entry.
|
protected |
Pointer to the hash function.
|
protected |
Pointer to the equality test function.
|
protected |
Number of valid entries in the hash.
|
protected |
Size in bytes of each entry in the table.
|
protected |
Size of the key in bytes.
|
protected |
Offset in bytes to the start of the data chunk.
|
protected |
(Power of 2)-1 size mask used for masking indexes for instant table rounding