wlgol/include/hashtable.h

117 lines
3.1 KiB
C
Raw Normal View History

2024-06-29 23:18:00 +02:00
#ifndef HASHTABLE_H
#define HASHTABLE_H
2024-11-02 18:37:10 +01:00
#include <stdbool.h>
2024-06-29 23:18:00 +02:00
2024-11-02 18:37:10 +01:00
struct htobj;
typedef struct htobj *hashtable;
2024-06-29 23:18:00 +02:00
2024-11-02 18:37:10 +01:00
typedef struct {
struct htobj *h;
struct entry *entry;
int index;
} ht_iterator;
2024-06-29 23:18:00 +02:00
/**
* @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
*/
2024-11-02 18:37:10 +01:00
hashtable hashtable_new(int size);
2024-06-29 23:18:00 +02:00
/**
* @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
2024-11-02 18:37:10 +01:00
* @return null or pointer to the previous value of the key
2024-06-29 23:18:00 +02:00
*/
2024-11-02 18:37:10 +01:00
void *hashtable_set(hashtable h, const char *key, void *value);
2024-06-29 23:18:00 +02:00
/**
* @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
*/
2024-11-02 18:37:10 +01:00
void *hashtable_lookup(hashtable h, const char *key);
2024-06-29 23:18:00 +02:00
/**
* @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
2024-11-02 18:37:10 +01:00
* @return null or value associated with the removed key.
2024-06-29 23:18:00 +02:00
*/
2024-11-02 18:37:10 +01:00
void *hashtable_remove(hashtable h, const char *key);
/**
* @brief Destroy hashtable.
*
* Values must be destroyed by the caller earlier.
*
* @param h Reference to the hastable.
*/
void hashtable_destroy(hashtable h);
/**
* @brief Get the iterator of key-value pairs in the hashtable
*
* This function returns the iterator of key-value pairs in the hashtable.
*
* @param h Pointer to the hashtable
* @return The iterator of key-value pairs in the hashtable
*/
ht_iterator hashtable_iter(hashtable h);
/**
* @brief Get the next key-value pair from the iterator
*
* This function returns the next key-value pair from the iterator.
*
* @param iter Pointer to the iterator
* @return true if there is a next key-value pair, false otherwise
*/
bool ht_iter_next(ht_iterator *iter);
/**
* @brief Get the key from the iterator
*
* This function returns the key from the iterator.
*
* @param iter Pointer to the iterator
* @return The key from the iterator
*/
const char *ht_iter_key(ht_iterator *iter);
/**
* @brief Get the value from the iterator
*
* This function returns the value from the iterator.
*
* @param iter Pointer to the iterator
* @return The value from the iterator
*/
void *ht_iter_value(ht_iterator *iter);
2024-06-29 23:18:00 +02:00
#endif