psh/src/hashtable.h

77 lines
1.7 KiB
C
Raw Normal View History

2023-08-04 21:52:07 +02:00
/**
* @file: src/hashtable.h
* @author: Jozef Nagy <schkwve@gmail.com>
* @copyright: MIT (See LICENSE.md)
* @brief: This file contains the routines for
* hashtable lookup for built-in commands.
*/
#ifndef __HASHTABLE_H_
#define __HASHTABLE_H_
#include <stddef.h>
#include "builtin.h"
typedef struct {
2023-08-04 21:52:07 +02:00
char *key;
builtin_func func_ptr;
2023-08-04 21:52:07 +02:00
} hashtable_entry_t;
typedef struct {
size_t size;
size_t count;
2023-08-04 21:52:07 +02:00
hashtable_entry_t **entry;
} hashtable_t;
extern hashtable_t *g_builtin_hashtable;
2023-08-04 21:52:07 +02:00
/**
* @brief This routine will allocate enough memory to store
* the hashtable and initialize it with NULL values.
*/
hashtable_t *hashtable_create();
2023-08-04 21:52:07 +02:00
/**
* @brief This routine inserts an entry into a hashtable.
2023-08-04 21:52:07 +02:00
*/
2023-08-05 23:35:20 +02:00
void hashtable_insert(hashtable_t *hashtable, const char *key,
builtin_func func_ptr);
2023-08-04 21:52:07 +02:00
/**
* @brief This routine creates an hashtable entry
*/
hashtable_entry_t *hashtable_new_entry(const char *key, builtin_func func_ptr);
/**
* @brief This routine searches for a key in a hashtable.
*/
builtin_func hashtable_search(hashtable_t *hashtable, const char *key);
/**
* @brief This routine calculates the hash of a key
* with basic collision mitigation
*/
2023-08-05 23:35:20 +02:00
int hashtable_get_hash(const char *key, const size_t hashmap_size,
const int att);
/**
2023-08-05 12:41:36 +02:00
* @brief This routine removes an entry from a hashtable.
2023-08-04 21:52:07 +02:00
*/
2023-08-05 12:41:36 +02:00
void hashtable_remove_entry(hashtable_t *hashtable, const char *key);
2023-08-04 21:52:07 +02:00
/**
* @brief This routine removes all entries from a hashtable and
* free()'s it.
2023-08-04 21:52:07 +02:00
*/
void hashtable_destroy(hashtable_t *hashtable);
2023-08-04 21:52:07 +02:00
/**
* @brief This routine hashes a key.
2023-08-04 21:52:07 +02:00
*
* @return Hash
*/
int _hashtable_hash(const char *key, const int a, const size_t size);
2023-08-04 21:52:07 +02:00
#endif // __HASHTABLE_H_