2023-06-25 19:32:45 +02:00
|
|
|
#ifndef __LIBCPROJECT_HASH_MAP__
|
|
|
|
#define __LIBCPROJECT_HASH_MAP__
|
|
|
|
|
2023-08-07 00:11:07 +02:00
|
|
|
#include <errno.h>
|
2023-06-25 19:32:45 +02:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
2023-06-25 20:03:02 +02:00
|
|
|
#include <string.h>
|
2023-06-25 19:32:45 +02:00
|
|
|
|
|
|
|
#include "linked_list.h"
|
|
|
|
#include "string.h"
|
|
|
|
#include "types.h"
|
|
|
|
|
|
|
|
#define HASH_MAP_INITIAL_CAPACITY 10
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Hash map data structure.
|
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
struct hash_map {
|
|
|
|
struct linked_list **items;
|
|
|
|
|
|
|
|
size_t length;
|
|
|
|
size_t capacity;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Hash map item data structure.
|
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
struct hash_map_item {
|
|
|
|
void *data;
|
|
|
|
string_t key;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2023-06-25 20:03:02 +02:00
|
|
|
* @brief Hash function (using SipHash 1-3 algorithm).
|
2023-06-25 21:32:16 +02:00
|
|
|
* @see https://en.wikipedia.org/wiki/SipHash
|
|
|
|
* @see https://github.com/veorq/SipHash
|
2023-08-09 21:08:15 +02:00
|
|
|
*
|
|
|
|
* @param key
|
|
|
|
* @param capacity
|
|
|
|
* @return uint64_t
|
2023-06-25 19:32:45 +02:00
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
uint64_t hash(string_t key, size_t capacity);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Hash map initialization.
|
2023-08-09 21:08:15 +02:00
|
|
|
*
|
|
|
|
* @return struct hash_map*
|
2023-06-25 19:32:45 +02:00
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
struct hash_map *hash_map_initialization();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Add an item to the hash map.
|
2023-08-09 21:08:15 +02:00
|
|
|
*
|
2023-06-25 19:32:45 +02:00
|
|
|
* @param hash_map
|
|
|
|
* @param key
|
|
|
|
* @param data
|
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
void hash_map_add(struct hash_map *hash_map, string_t key, void *data);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Remove an item from the hash map.
|
|
|
|
* @param hash_map
|
|
|
|
* @param key
|
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
void hash_map_remove(struct hash_map *hash_map, string_t key);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get an item from the hash map.
|
|
|
|
* @param hash_map
|
|
|
|
* @param key
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return void*
|
2023-06-25 19:32:45 +02:00
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
void *hash_map_get(struct hash_map *hash_map, string_t key);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Check if the hash map contains a key.
|
2023-08-09 21:08:15 +02:00
|
|
|
*
|
2023-06-25 19:32:45 +02:00
|
|
|
* @param hash_map
|
|
|
|
* @param key
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return true
|
|
|
|
* @return false
|
2023-06-25 19:32:45 +02:00
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
bool hash_map_contains_key(struct hash_map *hash_map, string_t key);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Get the hash map keys.
|
|
|
|
*
|
|
|
|
* @param hash_map
|
2023-08-09 21:08:15 +02:00
|
|
|
* @return string_t*
|
2023-06-25 19:32:45 +02:00
|
|
|
* @since v2.0.0
|
|
|
|
*/
|
|
|
|
string_t *hash_map_get_keys(struct hash_map *hash_map);
|
|
|
|
|
2023-08-03 23:17:54 +02:00
|
|
|
/**
|
|
|
|
* @brief Frees the hash map.
|
2023-08-09 21:08:15 +02:00
|
|
|
*
|
|
|
|
* @param hash_map
|
2023-08-05 15:03:53 +02:00
|
|
|
* @since v3.0.0
|
2023-08-03 23:17:54 +02:00
|
|
|
*/
|
|
|
|
void hash_map_free(struct hash_map *hash_map);
|
|
|
|
|
2023-06-25 19:32:45 +02:00
|
|
|
#endif
|