|
Hanstable
1.0.0
Simple hash table implementation in C
|
#include <stdlib.h>#include <stdint.h>#include <limits.h>Go to the source code of this file.
Data Structures | |
| struct | ht_Hashtable |
| struct | ht_HashtableEntry |
Typedefs | |
| typedef enum ht_HashtableError | ht_HashtableError |
| typedef struct ht_Hashtable | ht_Hashtable |
| typedef struct ht_HashtableEntry | ht_HashtableEntry |
Enumerations | |
| enum | ht_HashtableError { HT_INTERNAL = INT_MIN, HT_MALLOC_FAILURE, HT_INVALID_HASHTABLE, HT_INVALID_ENTRY_SET, HT_INVALID_ENTRY, HT_INVALID_KEY, HT_ENTRIES_IN_LIMBO, HT_OK = 0 } |
Functions | |
| const char * | ht_strerror (int errorCode) |
| ht_HashtableEntry * | ht_createHashtableEntry (void) |
| ht_Hashtable * | ht_createHashtable (size_t initialCapacity) |
| int | ht_addHashtableEntry (ht_Hashtable *hashtable, const char *key, ht_HashtableEntry *entry) |
| ht_HashtableEntry * | ht_removeHashtableEntry (ht_Hashtable *hashtable, const char *key) |
| ht_HashtableEntry * | ht_getHashtableEntry (ht_Hashtable *hashtable, const char *key) |
| void | ht_destroyHashtable (ht_Hashtable *hashtable) |
| void | ht_destroyHashtableEntry (ht_HashtableEntry *entry) |
| int | ht_resizeHashtable (ht_Hashtable *hashtable) |
| ht_HashtableEntry ** | ht_toIteratable (ht_Hashtable *hashtable) |
Everything you need to include for Hanstable!
| typedef struct ht_Hashtable ht_Hashtable |
The hash table itself.
| typedef struct ht_HashtableEntry ht_HashtableEntry |
An entry in the hash table
| typedef enum ht_HashtableError ht_HashtableError |
Enumeration of all error return codes.
| enum ht_HashtableError |
Enumeration of all error return codes.
| Enumerator | |
|---|---|
| HT_INTERNAL |
Unknown error, unable to recover. |
| HT_MALLOC_FAILURE |
|
| HT_INVALID_HASHTABLE |
An invalid |
| HT_INVALID_ENTRY_SET |
An invalid array of |
| HT_INVALID_ENTRY |
An invalid |
| HT_INVALID_KEY |
An invalid key was passed. |
| HT_ENTRIES_IN_LIMBO |
The reported number of entries is not equal to the actual amount of entries in memory. |
| HT_OK |
Not an error, move along. |
| int ht_addHashtableEntry | ( | ht_Hashtable * | hashtable, |
| const char * | key, | ||
| ht_HashtableEntry * | entry | ||
| ) |
Add an entry to the table
| [in,out] | hashtable | The hashtable to add the entry to. |
| [in] | key | The key to associate the entry with. |
| [in,out] | entry | The entry to add to the table. Create one on the heap or via ht_createHashtableEntry |
ht_HashtableError | ht_Hashtable* ht_createHashtable | ( | size_t | initialCapacity | ) |
Create a hash table.
| [in] | initialCapacity | The number of entries to reservate memory for. The hash table will not shrink below this number. |
| ht_HashtableEntry* ht_createHashtableEntry | ( | void | ) |
Create an entry on the heap. The entry must be free'd after use by ht_destroyHashtableEntry.
NULL on failure. | void ht_destroyHashtable | ( | ht_Hashtable * | hashtable | ) |
Destroy a hash table. This will not destroy the entries! To destroy entries that have been allocated on the heap, use ht_toIteratable to get all entries and then call ht_destroyHashtableEntry on each entry.
| [in,out] | hashtable | The hashtable to be destroyed. |
| void ht_destroyHashtableEntry | ( | ht_HashtableEntry * | entry | ) |
Destroy an entry.
| [in,out] | entry | The entry to be destroyed. |
| ht_HashtableEntry* ht_getHashtableEntry | ( | ht_Hashtable * | hashtable, |
| const char * | key | ||
| ) |
Fetch an entry from the table, without removing it.
| [in] | hashtable | The hashtable to fetch the entry from. |
| [in] | key | The key that belongs to the entry. If there are multiple entries under the same key, it may not be determinable which one will be returned. |
NULL on failure (no such entry). | ht_HashtableEntry* ht_removeHashtableEntry | ( | ht_Hashtable * | hashtable, |
| const char * | key | ||
| ) |
Remove an entry from the table.
| [in,out] | hashtable | The hashtable to remove the entry from. |
| [in] | key | The key that belongs to the entry. If there are multiple entries under the same key, it may not be determinable which one will be removed. |
NULL on failure (no such entry). | int ht_resizeHashtable | ( | ht_Hashtable * | hashtable | ) |
Force a resize. ht_removeHashtableEntry does not call this automatically, so use this after removing a substantial amount of entries.
| [in,out] | hashtable |
| const char* ht_strerror | ( | int | errorCode | ) |
Get a text version of an error code when in soft sanitation mode.
| [in] | errorCode | The error code as returned by a failed function. |
| ht_HashtableEntry** ht_toIteratable | ( | ht_Hashtable * | hashtable | ) |
Load all entries into an array to be iterated over.
| [in] | hashtable | The hashtable to read the entries from. |
NULL.