Added hashtable

This commit is contained in:
2024-11-02 18:37:10 +01:00
parent ac136d4ffb
commit 2a436f1ea5
4 changed files with 303 additions and 110 deletions

View File

@ -1,23 +1,16 @@
#ifndef HASHTABLE_H
#define HASHTABLE_H
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
// entry in the hashtable
typedef struct entry
{
const char *key;
void *value;
struct entry *next;
} entry;
struct htobj;
typedef struct htobj *hashtable;
// hashtable
typedef struct
{
int size;
entry **table;
} hashtable;
typedef struct {
struct htobj *h;
struct entry *entry;
int index;
} ht_iterator;
/**
* @brief Create a new hashtable
@ -28,7 +21,7 @@ typedef struct
* @param size The size of the hashtable
* @return A pointer to the newly created hashtable
*/
hashtable *hashtable_new(int size);
hashtable hashtable_new(int size);
/**
* @brief Insert a new key-value pair into the hashtable
@ -40,8 +33,9 @@ hashtable *hashtable_new(int size);
* @param h Pointer to the hashtable
* @param key Pointer to the key
* @param value Pointer to the value
* @return null or pointer to the previous value of the key
*/
void hashtable_insert(hashtable *h, const char *key, void *value);
void *hashtable_set(hashtable h, const char *key, void *value);
/**
* @brief Get the value associated with a key from the hashtable
@ -54,7 +48,7 @@ void hashtable_insert(hashtable *h, const char *key, void *value);
* @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);
void *hashtable_lookup(hashtable h, const char *key);
/**
* @brief Remove a key-value pair from the hashtable
@ -66,6 +60,57 @@ void *hashtable_lookup(hashtable *h, const char *key);
*
* @param h Pointer to the hashtable
* @param key Pointer to the key to remove
* @return null or value associated with the removed key.
*/
void hashtable_remove(hashtable *h, const char *key);
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);
#endif