style: norming hashmap_env

This commit is contained in:
Raphael 2024-09-19 12:11:22 +02:00
parent 36edf768bf
commit d2bcfa4522

View file

@ -1,44 +1,41 @@
/* ************************************************************************** */ /* ************************************************************************** */
/* */ /* */
/* ::: :::::::: */ /* ::: :::::::: */
/* hashmap_env.h :+: :+: :+: */ /* hashmap_env.h :+: :+: :+: */
/* +:+ +:+ +:+ */ /* +:+ +:+ +:+ */
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */ /* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */ /* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */ /* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */ /* Updated: 2024/09/19 12:11:03 by rparodi ### ########.fr */
/* */ /* */
/* ************************************************************************** */ /* ************************************************************************** */
#ifndef HASHMAP_ENV_H #ifndef HASHMAP_ENV_H
#define HASHMAP_ENV_H # define HASHMAP_ENV_H
#define DEFAULT_BUCKETS 750 # define DEFAULT_BUCKETS 750
# include "me/hash/hasher.h"
# include "me/types.h"
#include "me/hash/hasher.h" # include <stddef.h>
#include "me/types.h" # include <stdint.h>
#include <stddef.h> # include <stdio.h>
#include <stdint.h> # include <stdlib.h>
#include <stdio.h> # include <string.h>
#include <stdlib.h>
#include <string.h>
/// @struct A key-value pair for the hashmap /// @struct A key-value pair for the hashmap
typedef struct s_kv_env typedef struct s_kv_env
{ {
t_str key; t_str key;
t_str val; t_str val;
} t_kv_env; } t_kv_env;
/// @typedef A function that hashes a key /// @typedef A function that hashes a key
typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key); typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key);
/// @typedef A function that drops a key-value pair /// @typedef A function that drops a key-value pair
typedef void (*t_free_env_fn)(t_kv_env val); typedef void (*t_free_env_fn)(t_kv_env val);
/// @typedef A function that compares two keys and returns true if they are equal /// @typedef A function that compares two keys & returns true if they are equal
typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs); typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs);
/// @struct A single entry in the hashmap /// @struct A single entry in the hashmap
/// @var hash_id The hash of the key /// @var hash_id The hash of the key
@ -46,10 +43,10 @@ typedef bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs);
/// @var next The next entry in the bucket /// @var next The next entry in the bucket
typedef struct s_entry_env typedef struct s_entry_env
{ {
t_usize hash_id; t_usize hash_id;
t_kv_env kv; t_kv_env kv;
struct s_entry_env *next; struct s_entry_env *next;
} t_entry_env; } t_entry_env;
/// @struct A hashmap of keys t_str and values t_str /// @struct A hashmap of keys t_str and values t_str
/// @var buckets The array of buckets /// @var buckets The array of buckets
@ -60,54 +57,59 @@ typedef struct s_entry_env
/// @var free The free function /// @var free The free function
typedef struct s_hashmap_env typedef struct s_hashmap_env
{ {
t_entry_env **buckets; t_entry_env **buckets;
t_usize num_buckets; t_usize num_buckets;
t_hasher hasher; t_hasher hasher;
t_hash_env_fn hfunc; t_hash_env_fn hfunc;
t_eq_env_fn cfunc; t_eq_env_fn cfunc;
t_free_env_fn free; t_free_env_fn free;
} t_hashmap_env; } t_hashmap_env;
/// @brief Creates a new hashmap with the given hash, comparison, and free functions /// @brief Creates a new hashmap with the given hash, \
/// comparison, & free functions
/// @param hash The hash function /// @param hash The hash function
/// @param cmp The comparison function /// @param cmp The comparison function
/// @param free The free function /// @param free The free function
/// @return A new hashmap /// @return A new hashmap
t_hashmap_env *hmap_env_new(t_hash_env_fn hash, t_eq_env_fn cmp, t_free_env_fn free); t_hashmap_env *hmap_env_new(\
t_hash_env_fn hash, t_eq_env_fn cmp, t_free_env_fn free);
/// @brief Creates a new hashmap with the given hash, comparison, and free functions /// @brief Creates a new hashmap with the given hash, \
/// comparison, and free functions
/// @param hash The hash function /// @param hash The hash function
/// @param cmp The comparison function /// @param cmp The comparison function
/// @param free The free function /// @param free The free function
/// @param cap The number of buckets /// @param cap The number of buckets
/// @return A new hashmap /// @return A new hashmap
t_hashmap_env *hmap_env_new_with_buckets(t_hash_env_fn hash, t_eq_env_fn cmp, t_free_env_fn free, size_t cap); t_hashmap_env *hmap_env_new_with_buckets(\
t_hash_env_fn hash, t_eq_env_fn cmp, t_free_env_fn free, size_t cap);
/// @brief Free the hashmap and all of its entries /// @brief Free the hashmap and all of its entries
/// @param hmap The hashmap to free /// @param hmap The hashmap to free
void hmap_env_free(t_hashmap_env *hmap); void hmap_env_free(t_hashmap_env *hmap);
/// @brief Clear the hashmap, removing all of its entries /// @brief Clear the hashmap, removing all of its entries
/// @param hmap The hashmap to clear /// @param hmap The hashmap to clear
void hmap_env_clear(t_hashmap_env *hmap); void hmap_env_clear(t_hashmap_env *hmap);
/// @brief Inserts a key-value pair into the hashmap /// @brief Inserts a key-value pair into the hashmap
/// @param hmap The hashmap /// @param hmap The hashmap
/// @param key The key /// @param key The key
/// @param value The value /// @param value The value
/// @return true if the key already existed before, false otherwise /// @return true if the key already existed before, false otherwise
bool hmap_env_insert(t_hashmap_env *hmap, t_str key, t_str value); bool hmap_env_insert(t_hashmap_env *hmap, t_str key, t_str value);
/// @brief Gets the value associated with the key /// @brief Gets the value associated with the key
/// @param hmap The hashmap /// @param hmap The hashmap
/// @param key The key /// @param key The key
/// @return The value associated with the key, or NULL if the key is not in the hashmap /// @return The value associated with the key, \
t_str *hmap_env_get(t_hashmap_env *hmap, t_str *key); /// or NULL if the key is not in the hashmap
t_str *hmap_env_get(t_hashmap_env *hmap, t_str *key);
/// @brief Removes the key-value pair from the hashmap /// @brief Removes the key-value pair from the hashmap
/// @param hmap The hashmap /// @param hmap The hashmap
/// @param key The key /// @param key The key
void hmap_env_remove(t_hashmap_env *hmap, t_str *key); void hmap_env_remove(t_hashmap_env *hmap, t_str *key);
/// @brief Get an entry from the hashmap /// @brief Get an entry from the hashmap
/// @param hmap The hashmap /// @param hmap The hashmap
@ -116,23 +118,31 @@ void hmap_env_remove(t_hashmap_env *hmap, t_str *key);
/// @param prev The previous entry in the bucket /// @param prev The previous entry in the bucket
/// @return The entry, or NULL if the key is not in the hashmap /// @return The entry, or NULL if the key is not in the hashmap
/// @note this is an internal function /// @note this is an internal function
t_entry_env *hmap_env_get_entry(t_hashmap_env *hmap, t_usize hash, t_str *key, t_entry_env **prev); t_entry_env *hmap_env_get_entry(\
t_hashmap_env *hmap, t_usize hash, t_str *key, t_entry_env **prev);
/// @brief Iterates over the hashmap and calls the given function for each key-value pair /// @brief Iterates over the hashmap & \
/// calls the given function for each key-value pair
/// @param self The hashmap /// @param self The hashmap
/// @param func The function to call /// @param func The function to call
/// @param ctx The context to pass to the function /// @param ctx The context to pass to the function
/// @return An error code /// @return An error code
/// @note The iteration can be stopped by returning an error code from the function /// @note The iteration can be stopped by returning \
t_error hmap_env_iter(t_hashmap_env *self, t_error (*func)(t_usize idx, const t_str *key, t_str *val, void *ctx), void *ctx); /// an error code from the function
t_error hmap_env_iter(\
t_hashmap_env *self, t_error (*func)(\
t_usize idx, const t_str *key, t_str *val, void *ctx), void *ctx);
/// @brief Clone an entire hashmap, \
/// @brief Clone an entire hashmap, using the given function to duplicate the items /// using the given function to duplicate the items
/// @param self The hashmap /// @param self The hashmap
/// @param func The function to call /// @param func The function to call
/// @param ctx The context to pass to the function /// @param ctx The context to pass to the function
/// @param out The cloned hashmap /// @param out The cloned hashmap
/// @return An error code /// @return An error code
t_error hmap_env_clone(t_hashmap_env *self, t_error (*clone)(const t_kv_env *val, void *ctx, t_kv_env *out), void *ctx, t_hashmap_env **out); t_error hmap_env_clone(\
t_hashmap_env *self, t_error (*clone)(\
const t_kv_env *val, void *ctx, t_kv_env *out), \
void *ctx, t_hashmap_env **out);
#endif #endif