A simple, generic hash table implementation using a dynamic struct.
More...
#include <stdbool.h>
#include <stddef.h>
Go to the source code of this file.
|
|
#define | DELETED_NODE ((void *)0xFFFFFFFFFFFFFFFUL) |
| | Special marker for deleted entries.
|
|
| typedef const char *(* | get_key_func) (const void *) |
| | Function pointer type for extracting a string key from an object.
|
A simple, generic hash table implementation using a dynamic struct.
- Author
- Alifoo
- Date
- 2025-09-03
This file provides the public interface for a hash table that can store any kind of data. The table size is dynamic and allocated at runtime.
◆ get_key_func
| typedef const char *(* get_key_func) (const void *) |
Function pointer type for extracting a string key from an object.
The function takes a pointer to an object and returns a pointer to a string (the key) associated with that object.
◆ free_hash_table()
Frees the dynamically allocated table inside the hash table.
Does not free the stored objects themselves; the user must do that separately.
- Parameters
-
| [in,out] | ht | A pointer to the hashtable instance. |
◆ hash_table_delete()
| void * hash_table_delete |
( |
hashtable * | ht, |
|
|
const char * | key ) |
Deletes an object from the hash table by its string key.
This function marks the slot as deleted to preserve the probing chain.
- Parameters
-
| [in,out] | ht | A pointer to the hashtable instance. |
| [in] | key | The key string of the object to delete. |
- Returns
- A pointer to the deleted object if found, otherwise NULL.
◆ hash_table_insert()
| bool hash_table_insert |
( |
hashtable * | ht, |
|
|
void * | item ) |
Inserts an object into the hash table using linear probing.
- Parameters
-
| [in,out] | ht | A pointer to the hashtable instance. |
| [in] | item | A pointer to the object to insert. The object is not copied. |
- Returns
- true if the insertion was successful, false if the table is full.
◆ hash_table_lookup()
| void * hash_table_lookup |
( |
const hashtable * | ht, |
|
|
const char * | key ) |
Looks up an object in the hash table by its string key.
- Parameters
-
| [in] | ht | A pointer to the hashtable instance. |
| [in] | key | The key string to search for. |
- Returns
- A pointer to the object if found, otherwise NULL.
◆ init_hash_table()
Initializes the hash table with a given size.
- Parameters
-
| [in,out] | ht | A pointer to the hashtable instance. |
| [in] | get_key | A function that extracts a string key from stored objects. |
| [in] | size | Number of buckets to allocate for the table. |
◆ print_table()
Prints the current state of the hash table to the console.
Useful for debugging and visualizing the table's contents.
- Parameters
-
| [in] | ht | A pointer to the hashtable instance. |