update stuff
This commit is contained in:
parent
24b210fe86
commit
709c124028
20 changed files with 176 additions and 64 deletions
|
|
@ -35,7 +35,7 @@ typedef struct s_kv_env
|
|||
/// @typedef A function that hashes a key
|
||||
typedef void (*t_hash_env_fn)(t_hasher *hasher, t_str *key);
|
||||
/// @typedef A function that drops a key-value pair
|
||||
typedef void (*t_drop_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 bool (*t_eq_env_fn)(t_str *lhs, t_str *rhs);
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ typedef struct s_entry_env
|
|||
/// @var hasher The hasher function
|
||||
/// @var hfunc The hash function
|
||||
/// @var cfunc The comparison function
|
||||
/// @var drop The drop function
|
||||
/// @var free The free function
|
||||
typedef struct s_hashmap_env
|
||||
{
|
||||
t_entry_env **buckets;
|
||||
|
|
@ -65,28 +65,32 @@ typedef struct s_hashmap_env
|
|||
t_hasher hasher;
|
||||
t_hash_env_fn hfunc;
|
||||
t_eq_env_fn cfunc;
|
||||
t_drop_env_fn drop;
|
||||
t_free_env_fn free;
|
||||
} t_hashmap_env;
|
||||
|
||||
/// @brief Creates a new hashmap with the given hash, comparison, and drop functions
|
||||
/// @brief Creates a new hashmap with the given hash, comparison, and free functions
|
||||
/// @param hash The hash function
|
||||
/// @param cmp The comparison function
|
||||
/// @param drop The drop function
|
||||
/// @param free The free function
|
||||
/// @return A new hashmap
|
||||
t_hashmap_env *hmap_env_new(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop);
|
||||
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 drop functions
|
||||
/// @brief Creates a new hashmap with the given hash, comparison, and free functions
|
||||
/// @param hash The hash function
|
||||
/// @param cmp The comparison function
|
||||
/// @param drop The drop function
|
||||
/// @param free The free function
|
||||
/// @param cap The number of buckets
|
||||
/// @return A new hashmap
|
||||
t_hashmap_env *hmap_env_new_with_buckets(t_hash_env_fn hash, t_eq_env_fn cmp, t_drop_env_fn drop, 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
|
||||
/// @param hmap The hashmap to free
|
||||
void hmap_env_free(t_hashmap_env *hmap);
|
||||
|
||||
/// @brief Clear the hashmap, removing all of its entries
|
||||
/// @param hmap The hashmap to clear
|
||||
void hmap_env_clear(t_hashmap_env *hmap);
|
||||
|
||||
/// @brief Inserts a key-value pair into the hashmap
|
||||
/// @param hmap The hashmap
|
||||
/// @param key The key
|
||||
|
|
|
|||
12
output/src/hashmap/env/env.c
vendored
12
output/src/hashmap/env/env.c
vendored
|
|
@ -19,15 +19,15 @@
|
|||
|
||||
t_hashmap_env *hmap_env_new(t_hash_env_fn hfunc,
|
||||
t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop)
|
||||
t_free_env_fn free)
|
||||
{
|
||||
return (
|
||||
hmap_env_new_with_buckets(hfunc, cfunc, drop, DEFAULT_BUCKETS));
|
||||
hmap_env_new_with_buckets(hfunc, cfunc, free, DEFAULT_BUCKETS));
|
||||
}
|
||||
|
||||
t_hashmap_env *hmap_env_new_with_buckets(
|
||||
t_hash_env_fn hfunc, t_eq_env_fn cfunc,
|
||||
t_drop_env_fn drop, t_usize buckets)
|
||||
t_free_env_fn free, t_usize buckets)
|
||||
{
|
||||
t_hashmap_env *hmap;
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ t_hashmap_env *hmap_env_new_with_buckets(
|
|||
hmap->hasher = hasher_sip13_new();
|
||||
hmap->hfunc = hfunc;
|
||||
hmap->cfunc = cfunc;
|
||||
hmap->drop = drop;
|
||||
hmap->free = free;
|
||||
if (hmap->buckets == NULL)
|
||||
return ((void)mem_free(hmap), NULL);
|
||||
return (hmap);
|
||||
|
|
@ -57,7 +57,7 @@ void hmap_env_free(t_hashmap_env *hmap)
|
|||
entry = hmap->buckets[index];
|
||||
while (entry != NULL)
|
||||
{
|
||||
hmap->drop(entry->kv);
|
||||
hmap->free(entry->kv);
|
||||
tmp = entry->next;
|
||||
mem_free(entry);
|
||||
entry = tmp;
|
||||
|
|
@ -117,7 +117,7 @@ bool hmap_env_insert(t_hashmap_env *hmap, t_str key,
|
|||
}
|
||||
else
|
||||
{
|
||||
hmap->drop(entry->kv);
|
||||
hmap->free(entry->kv);
|
||||
entry->kv.key = key;
|
||||
entry->kv.val = value;
|
||||
return (true);
|
||||
|
|
|
|||
40
output/src/hashmap/env/env_clear.c
vendored
Normal file
40
output/src/hashmap/env/env_clear.c
vendored
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_env.h :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: maiboyer <maiboyer@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/12/06 11:00:22 by maiboyer #+# #+# */
|
||||
/* Updated: 2023/12/11 15:24:44 by maiboyer ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "me/types.h"
|
||||
|
||||
|
||||
|
||||
#include "me/hashmap/hashmap_env.h"
|
||||
#include "me/mem/mem.h"
|
||||
|
||||
|
||||
void hmap_env_clear(t_hashmap_env *self)
|
||||
{
|
||||
t_usize bucket_id;
|
||||
t_entry_env *cur;
|
||||
t_entry_env *next;
|
||||
|
||||
bucket_id = 0;
|
||||
while (bucket_id < self->num_buckets)
|
||||
{
|
||||
cur = self->buckets[bucket_id];
|
||||
while (cur != NULL)
|
||||
{
|
||||
next = cur->next;
|
||||
self->free(cur->kv);
|
||||
mem_free(cur);
|
||||
cur = next;
|
||||
}
|
||||
bucket_id++;
|
||||
}
|
||||
}
|
||||
2
output/src/hashmap/env/env_clone.c
vendored
2
output/src/hashmap/env/env_clone.c
vendored
|
|
@ -27,7 +27,7 @@ t_error hmap_env_clone(t_hashmap_env *self,
|
|||
t_hashmap_env *ret;
|
||||
|
||||
bucket_id = 0;
|
||||
ret = hmap_env_new_with_buckets(self->hfunc, self->cfunc, self->drop, self->num_buckets);
|
||||
ret = hmap_env_new_with_buckets(self->hfunc, self->cfunc, self->free, self->num_buckets);
|
||||
if (ret == NULL)
|
||||
return (ERROR);
|
||||
while (bucket_id < self->num_buckets)
|
||||
|
|
|
|||
2
output/src/hashmap/env/env_utils.c
vendored
2
output/src/hashmap/env/env_utils.c
vendored
|
|
@ -48,7 +48,7 @@ void hmap_env_remove(t_hashmap_env *hmap, t_str *key)
|
|||
hmap->buckets[hashed_key % hmap->num_buckets] = entry->next;
|
||||
else
|
||||
prev->next = entry->next;
|
||||
hmap->drop(entry->kv);
|
||||
hmap->free(entry->kv);
|
||||
mem_free(entry);
|
||||
hmap->buckets[hashed_key % hmap->num_buckets] = NULL;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue