|
UFO: Alien Invasion
|
Basic hash table for fast lookups. More...
#include "hashtable.h"#include <assert.h>#include <stdlib.h>#include <stddef.h>#include <string.h>#include "common.h"#include "mem.h"
Go to the source code of this file.
Data Structures | |
| struct | hashItem_s |
| The linked list item. More... | |
| struct | hashBucket_s |
| The hash bucket structure, contains a linked list of items. More... | |
| struct | hashTable_s |
| The hash table structure, contains an array of buckets being indexed by the hash function. More... | |
Macros | |
| #define | HASH_TABLE_SIZE 256 |
Functions | |
| static void * | _hash_alloc (memPool_t *pool, int n, size_t s) |
| static void | _hash_free (void *p) |
| static HASH_INDEX | default_hash (const void *key, int len) |
| Default hash function to map keys into a 0..255 index. | |
| static int | default_compare (const void *key1, int len1, const void *key2, int len2) |
| Default compare function for comparing key values. | |
| static hashItem_s * | _find_bucket_entry (hashBucket_s *b, hashTable_compare c, const void *key, int nkey, hashItem_s **prev) |
| Internal function to scan the list of entries in the bucket for an exact match. | |
| static void | _release_entry (hashItem_s **i, bool ownsKeys, bool ownsValues) |
| Internal function, release an entry including key and value if owned. | |
| static void | _release_bucket (hashBucket_s **b, bool ownsKeys, bool ownsValues) |
| Internal function, releases bucket including entries. | |
| static void | _release_table (hashTable_s **t, bool full) |
| Internal function, releases entire table. | |
| static hashItem_s * | _iterator_first (hashTable_s *t) |
| start iterating with the first item. | |
| static hashItem_s * | _iterator_next (hashTable_s *t, hashItem_s *i) |
| iterate to next item | |
| hashTable_s * | HASH_NewTable (bool ownsKeys, bool ownsValues, bool duplicateOverwrite) |
| Creates a new hash table and sets it initial capacity. | |
| hashTable_s * | HASH_NewTable (bool ownsKeys, bool ownsValues, bool duplicateOverwrite, hashTable_hash h, hashTable_compare c) |
| Creates a new hash table and sets it initial capacity. | |
| hashTable_s * | HASH_NewTable (bool ownsKeys, bool ownsValues, bool duplicateOverwrite, hashTable_hash h, hashTable_compare c, memPool_t *keys, memPool_t *values, memPool_t *table) |
| Creates a new hash table and sets it initial capacity. | |
| hashTable_s * | HASH_CloneTable (hashTable_s *source) |
| void | HASH_DeleteTable (hashTable_s **t) |
| Deletes a hash table and sets the pointer to NULL. | |
| bool | HASH_Insert (hashTable_s *t, const void *key, int nkey, const void *value, int nvalue) |
| Inserts a new value with given key into the hash table. | |
| void * | HASH_Remove (hashTable_s *t, const void *key, int nkey) |
| Removes an existing value with given key from the hash table. | |
| void | HASH_Clear (hashTable_s *t) |
| Clears the hash table. | |
| void * | HASH_Get (hashTable_s *t, const void *key, int nkey) |
| Returns the value for a given key. | |
| int | HASH_Count (hashTable_s *t) |
| Returns the number of entries in the hash table. | |
Basic hash table for fast lookups.
Definition in file hashtable.cpp.
| #define HASH_TABLE_SIZE 256 |
Definition at line 60 of file hashtable.cpp.
Referenced by _iterator_first(), _iterator_next(), _release_table(), default_hash(), and HASH_Count().
|
static |
Internal function to scan the list of entries in the bucket for an exact match.
| [in] | b | The bucket to search in. |
| [in] | c | The compare function to use. |
| [in] | key | The key value to search for. |
| [in] | nkey | The lenght of the key value. |
| [out] | prev | A reference to a hashItem_s pointer for the entry preceding the found item. Set to NULL if the preceding entry is not needed. |
Definition at line 181 of file hashtable.cpp.
References i, key, and hashBucket_s::root.
Referenced by HASH_Get(), HASH_Insert(), and HASH_Remove().
Definition at line 122 of file hashtable.cpp.
References Mem_Alloc, and Mem_PoolAlloc.
Referenced by HASH_Insert(), and HASH_NewTable().
Definition at line 130 of file hashtable.cpp.
References Mem_Free.
Referenced by _release_bucket(), _release_entry(), and _release_table().
|
static |
start iterating with the first item.
Definition at line 244 of file hashtable.cpp.
References HASH_TABLE_SIZE, hashBucket_s::root, and hashTable_s::table.
Referenced by HASH_CloneTable().
|
static |
iterate to next item
Definition at line 257 of file hashtable.cpp.
References hashTable_s::hash, HASH_TABLE_SIZE, i, hashBucket_s::root, and hashTable_s::table.
Referenced by HASH_CloneTable().
|
static |
Internal function, releases bucket including entries.
Definition at line 210 of file hashtable.cpp.
References _hash_free(), _release_entry(), and i.
Referenced by _release_table().
|
static |
Internal function, release an entry including key and value if owned.
Definition at line 200 of file hashtable.cpp.
References _hash_free(), and i.
Referenced by _release_bucket(), and HASH_Remove().
|
static |
Internal function, releases entire table.
| [out] | t | table pointer to release |
| [in] | full | If true, the tabel node is also freed. |
Definition at line 229 of file hashtable.cpp.
References _hash_free(), _release_bucket(), HASH_TABLE_SIZE, and i.
Referenced by HASH_Clear(), and HASH_DeleteTable().
Default compare function for comparing key values.
| [in] | key1 | The first key value to compare. |
| [in] | len1 | The length in bytes of the first key value. |
| [in] | key2 | The second key value to compare. |
| [in] | len2 | The length in bytes of the second key value. |
Definition at line 165 of file hashtable.cpp.
Referenced by HASH_NewTable().
|
static |
Default hash function to map keys into a 0..255 index.
Definition at line 143 of file hashtable.cpp.
References HASH_TABLE_SIZE, i, key, and len.
Referenced by HASH_NewTable().
| void HASH_Clear | ( | hashTable_s * | t | ) |
| hashTable_s * HASH_CloneTable | ( | hashTable_s * | source | ) |
Definition at line 336 of file hashtable.cpp.
References _iterator_first(), _iterator_next(), hashTable_s::compare, hashTable_s::duplicateOverwrite, hashTable_s::hash, HASH_Insert(), HASH_NewTable(), hashItem_s::key, hashItem_s::nkey, hashItem_s::nvalue, hashTable_s::ownsKeys, hashTable_s::ownsValues, and hashItem_s::value.
Referenced by UI_CloneNode().
| int HASH_Count | ( | hashTable_s * | t | ) |
Returns the number of entries in the hash table.
Definition at line 481 of file hashtable.cpp.
References hashBucket_s::count, HASH_TABLE_SIZE, i, and hashTable_s::table.
| void HASH_DeleteTable | ( | hashTable_s ** | t | ) |
Deletes a hash table and sets the pointer to NULL.
| [in,out] | t | A reference to a hashTable_s pointer. |
Definition at line 351 of file hashtable.cpp.
References _release_table().
Referenced by CL_ShutdownLua(), and uiNode::deleteNode().
| void * HASH_Get | ( | hashTable_s * | t, |
| const void * | key, | ||
| int | nkey ) |
Returns the value for a given key.
Definition at line 467 of file hashtable.cpp.
References _find_bucket_entry(), hashTable_s::compare, hashTable_s::hash, i, key, and hashTable_s::table.
Referenced by CL_ExecuteCallback(), UI_GetBehaviourMethod(), and UI_GetNodeMethod().
| bool HASH_Insert | ( | hashTable_s * | t, |
| const void * | key, | ||
| int | nkey, | ||
| const void * | value, | ||
| int | nvalue ) |
Inserts a new value with given key into the hash table.
| [in] | t | A pointer to a hashTable_s structure. |
| [in] | key | A pointer to a key. |
| [in] | nkey | The length of the key value in bytes. |
| [in] | value | A pointer to a value. |
| [in] | nvalue | The length of the value in bytes. |
Definition at line 369 of file hashtable.cpp.
References _find_bucket_entry(), _hash_alloc(), hashTable_s::compare, hashBucket_s::count, hashTable_s::duplicateOverwrite, hashTable_s::hash, i, hashTable_s::internalPool, key, hashTable_s::keyPool, hashTable_s::ownsKeys, hashTable_s::ownsValues, hashBucket_s::root, hashTable_s::table, and hashTable_s::valuePool.
Referenced by CL_RegisterCallback(), HASH_CloneTable(), UI_AddBehaviourMethod(), and UI_AddNodeMethod().
| hashTable_s * HASH_NewTable | ( | bool | ownsKeys, |
| bool | ownsValues, | ||
| bool | duplicateOverwrite ) |
Creates a new hash table and sets it initial capacity.
| [in] | ownsKeys | If true, the hash table stores a copy of the key. |
| [in] | ownsValues | If true, the hash table stores a copy of the value. |
| [in] | duplicateOverwrite | If true, inserting a value twice will overwrite the old value. If false, the operation asserts. |
Definition at line 287 of file hashtable.cpp.
References default_compare(), default_hash(), and HASH_NewTable().
Referenced by CL_InitLua(), HASH_CloneTable(), HASH_NewTable(), HASH_NewTable(), UI_AddBehaviourMethod(), and UI_AddNodeMethod().
| hashTable_s * HASH_NewTable | ( | bool | ownsKeys, |
| bool | ownsValues, | ||
| bool | duplicateOverwrite, | ||
| hashTable_hash | h, | ||
| hashTable_compare | c ) |
Creates a new hash table and sets it initial capacity.
| [in] | ownsKeys | If true, the hash table stores a copy of the key. |
| [in] | ownsValues | If true, the hash table stores a copy of the value. |
| [in] | duplicateOverwrite | If true, inserting a value twice will overwrite the old value. If false, the operation asserts. |
| [in] | h | The hash function to be used when indexing. |
| [in] | c | The compare function to be used when comparing. |
Definition at line 303 of file hashtable.cpp.
References HASH_NewTable().
| hashTable_s * HASH_NewTable | ( | bool | ownsKeys, |
| bool | ownsValues, | ||
| bool | duplicateOverwrite, | ||
| hashTable_hash | h, | ||
| hashTable_compare | c, | ||
| memPool_t * | keys, | ||
| memPool_t * | values, | ||
| memPool_t * | table ) |
Creates a new hash table and sets it initial capacity.
| [in] | ownsKeys | If true, the hash table stores a copy of the key. |
| [in] | ownsValues | If true, the hash table stores a copy of the value. |
| [in] | duplicateOverwrite | If true, inserting a value twice will overwrite the old value. If false, the operation asserts. |
| [in] | h | The hash function to be used when indexing. |
| [in] | c | The compare function to be used when comparing. |
| [in] | keys | If not null, should point to a memory pool used to allocate key values. |
| [in] | values | If not null, should point to a memory pool used to allocated copies of inserted values. |
| [in] | table | If not null, should point to a memory pool used to allocate the table internal structures, including the hashTable_s structure returned. |
Definition at line 322 of file hashtable.cpp.
References _hash_alloc(), hashTable_s::compare, hashTable_s::duplicateOverwrite, hashTable_s::hash, hashTable_s::internalPool, hashTable_s::keyPool, hashTable_s::ownsKeys, hashTable_s::ownsValues, and hashTable_s::valuePool.
| void * HASH_Remove | ( | hashTable_s * | t, |
| const void * | key, | ||
| int | nkey ) |
Removes an existing value with given key from the hash table.
Definition at line 429 of file hashtable.cpp.
References _find_bucket_entry(), _release_entry(), hashTable_s::compare, hashBucket_s::count, hashTable_s::hash, i, key, hashItem_s::next, hashTable_s::ownsKeys, hashTable_s::ownsValues, hashBucket_s::root, hashTable_s::table, and v.