wlgol/include/hashtable.h
2024-06-29 23:18:00 +02:00

72 lines
1.9 KiB
C

#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <stdlib.h>
#include <string.h>
// entry in the hashtable
typedef struct entry
{
const char *key;
void *value;
struct entry *next;
} entry;
// hashtable
typedef struct
{
int size;
entry **table;
} hashtable;
/**
* @brief Create a new hashtable
*
* This function creates a new hashtable with the specified size.
* It initializes the table with NULL entries.
*
* @param size The size of the hashtable
* @return A pointer to the newly created hashtable
*/
hashtable *hashtable_new(int size);
/**
* @brief Insert a new key-value pair into the hashtable
*
* This function inserts a new key-value pair into the hashtable.
* It calculates the index using the hash function and inserts the
* key-value pair at the beginning of the linked list at that index.
*
* @param h Pointer to the hashtable
* @param key Pointer to the key
* @param value Pointer to the value
*/
void hashtable_insert(hashtable *h, const char *key, void *value);
/**
* @brief Get the value associated with a key from the hashtable
*
* This function takes a hashtable and a key as input and returns the
* value associated with the key. If the key is not found in the
* hashtable, it returns NULL.
*
* @param h Pointer to the hashtable
* @param key Pointer to the key
* @return Pointer to the value associated with the key, or NULL if not found
*/
void *hashtable_lookup(hashtable *h, const char *key);
/**
* @brief Remove a key-value pair from the hashtable
*
* This function removes a key-value pair from the hashtable.
* It calculates the index using the hash function and traverses
* the linked list at that index. If the key is found, it removes
* the corresponding entry from the linked list and frees the memory.
*
* @param h Pointer to the hashtable
* @param key Pointer to the key to remove
*/
void hashtable_remove(hashtable *h, const char *key);
#endif