Hanstable  1.0.0
Simple hash table implementation in C
hanstable.h File Reference
#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_HashtableEntryht_createHashtableEntry (void)
 
ht_Hashtableht_createHashtable (size_t initialCapacity)
 
int ht_addHashtableEntry (ht_Hashtable *hashtable, const char *key, ht_HashtableEntry *entry)
 
ht_HashtableEntryht_removeHashtableEntry (ht_Hashtable *hashtable, const char *key)
 
ht_HashtableEntryht_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)
 

Detailed Description

Everything you need to include for Hanstable!

Typedef Documentation

typedef struct ht_Hashtable ht_Hashtable

The hash table itself.

An entry in the hash table

Enumeration of all error return codes.

Enumeration Type Documentation

Enumeration of all error return codes.

Enumerator
HT_INTERNAL 

Unknown error, unable to recover.

HT_MALLOC_FAILURE 

malloc() returned NULL.

HT_INVALID_HASHTABLE 

An invalid ht_Hashtable was passed.

HT_INVALID_ENTRY_SET 

An invalid array of ht_HashtableEntrys was passed.

HT_INVALID_ENTRY 

An invalid ht_HashtableEntry was passed.

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.

Function Documentation

int ht_addHashtableEntry ( ht_Hashtable hashtable,
const char *  key,
ht_HashtableEntry entry 
)

Add an entry to the table

Parameters
[in,out]hashtableThe hashtable to add the entry to.
[in]keyThe key to associate the entry with.
[in,out]entryThe entry to add to the table. Create one on the heap or via ht_createHashtableEntry
Returns
One of the error codes in ht_HashtableError
See also
ht_createHashtableEntry
ht_HashtableError
ht_Hashtable* ht_createHashtable ( size_t  initialCapacity)

Create a hash table.

Parameters
[in]initialCapacityThe number of entries to reservate memory for. The hash table will not shrink below this number.
Returns
A fresh hash table
ht_HashtableEntry* ht_createHashtableEntry ( void  )

Create an entry on the heap. The entry must be free'd after use by ht_destroyHashtableEntry.

Returns
A new entry, or NULL on failure.
See also
ht_destroyHashtableEntry
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.

Parameters
[in,out]hashtableThe hashtable to be destroyed.
See also
ht_toIteratable
ht_destroyHashtableEntry
void ht_destroyHashtableEntry ( ht_HashtableEntry entry)

Destroy an entry.

Parameters
[in,out]entryThe entry to be destroyed.
ht_HashtableEntry* ht_getHashtableEntry ( ht_Hashtable hashtable,
const char *  key 
)

Fetch an entry from the table, without removing it.

Parameters
[in]hashtableThe hashtable to fetch the entry from.
[in]keyThe 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.
Returns
The entry or NULL on failure (no such entry).
ht_HashtableEntry* ht_removeHashtableEntry ( ht_Hashtable hashtable,
const char *  key 
)

Remove an entry from the table.

Parameters
[in,out]hashtableThe hashtable to remove the entry from.
[in]keyThe 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.
Returns
The removed entry or 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.

Parameters
[in,out]hashtable
const char* ht_strerror ( int  errorCode)

Get a text version of an error code when in soft sanitation mode.

Parameters
[in]errorCodeThe error code as returned by a failed function.
Returns
A string representing the error by text.
ht_HashtableEntry** ht_toIteratable ( ht_Hashtable hashtable)

Load all entries into an array to be iterated over.

Parameters
[in]hashtableThe hashtable to read the entries from.
Returns
An array of size hashtable->numEntries. Guaranteed not to be NULL.