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_C__PREFIX__
|
|||
/// @typedef A function that hashes a key
|
||||
typedef void (*t_hash_C__PREFIX___fn)(t_hasher *hasher, C__KEYTYPE__ *key);
|
||||
/// @typedef A function that drops a key-value pair
|
||||
typedef void (*t_drop_C__PREFIX___fn)(t_kv_C__PREFIX__ val);
|
||||
typedef void (*t_free_C__PREFIX___fn)(t_kv_C__PREFIX__ val);
|
||||
/// @typedef A function that compares two keys and returns true if they are equal
|
||||
typedef bool (*t_eq_C__PREFIX___fn)(C__KEYTYPE__ *lhs, C__KEYTYPE__ *rhs);
|
||||
|
||||
|
|
@ -57,7 +57,7 @@ typedef struct s_entry_C__PREFIX__
|
|||
/// @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_C__PREFIX__
|
||||
{
|
||||
t_entry_C__PREFIX__ **buckets;
|
||||
|
|
@ -65,28 +65,32 @@ typedef struct s_hashmap_C__PREFIX__
|
|||
t_hasher hasher;
|
||||
t_hash_C__PREFIX___fn hfunc;
|
||||
t_eq_C__PREFIX___fn cfunc;
|
||||
t_drop_C__PREFIX___fn drop;
|
||||
t_free_C__PREFIX___fn free;
|
||||
} t_hashmap_C__PREFIX__;
|
||||
|
||||
/// @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_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop);
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_free_C__PREFIX___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_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_drop_C__PREFIX___fn drop, size_t cap);
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(t_hash_C__PREFIX___fn hash, t_eq_C__PREFIX___fn cmp, t_free_C__PREFIX___fn free, size_t cap);
|
||||
|
||||
/// @brief Free the hashmap and all of its entries
|
||||
/// @param hmap The hashmap to free
|
||||
void hmap_C__PREFIX___free(t_hashmap_C__PREFIX__ *hmap);
|
||||
|
||||
/// @brief Clear the hashmap, removing all of its entries
|
||||
/// @param hmap The hashmap to clear
|
||||
void hmap_C__PREFIX___clear(t_hashmap_C__PREFIX__ *hmap);
|
||||
|
||||
/// @brief Inserts a key-value pair into the hashmap
|
||||
/// @param hmap The hashmap
|
||||
/// @param key The key
|
||||
|
|
|
|||
|
|
@ -19,15 +19,15 @@
|
|||
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new(t_hash_C__PREFIX___fn hfunc,
|
||||
t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop)
|
||||
t_free_C__PREFIX___fn free)
|
||||
{
|
||||
return (
|
||||
hmap_C__PREFIX___new_with_buckets(hfunc, cfunc, drop, DEFAULT_BUCKETS));
|
||||
hmap_C__PREFIX___new_with_buckets(hfunc, cfunc, free, DEFAULT_BUCKETS));
|
||||
}
|
||||
|
||||
t_hashmap_C__PREFIX__ *hmap_C__PREFIX___new_with_buckets(
|
||||
t_hash_C__PREFIX___fn hfunc, t_eq_C__PREFIX___fn cfunc,
|
||||
t_drop_C__PREFIX___fn drop, t_usize buckets)
|
||||
t_free_C__PREFIX___fn free, t_usize buckets)
|
||||
{
|
||||
t_hashmap_C__PREFIX__ *hmap;
|
||||
|
||||
|
|
@ -39,7 +39,7 @@ t_hashmap_C__PREFIX__ *hmap_C__PREFIX___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_C__PREFIX___free(t_hashmap_C__PREFIX__ *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_C__PREFIX___insert(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ key,
|
|||
}
|
||||
else
|
||||
{
|
||||
hmap->drop(entry->kv);
|
||||
hmap->free(entry->kv);
|
||||
entry->kv.key = key;
|
||||
entry->kv.val = value;
|
||||
return (true);
|
||||
|
|
|
|||
|
|
@ -0,0 +1,40 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* hashmap_C__PREFIX__.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_C__PREFIX__.h"
|
||||
#include "me/mem/mem.h"
|
||||
|
||||
|
||||
void hmap_C__PREFIX___clear(t_hashmap_C__PREFIX__ *self)
|
||||
{
|
||||
t_usize bucket_id;
|
||||
t_entry_C__PREFIX__ *cur;
|
||||
t_entry_C__PREFIX__ *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++;
|
||||
}
|
||||
}
|
||||
|
|
@ -27,7 +27,7 @@ t_error hmap_C__PREFIX___clone(t_hashmap_C__PREFIX__ *self,
|
|||
t_hashmap_C__PREFIX__ *ret;
|
||||
|
||||
bucket_id = 0;
|
||||
ret = hmap_C__PREFIX___new_with_buckets(self->hfunc, self->cfunc, self->drop, self->num_buckets);
|
||||
ret = hmap_C__PREFIX___new_with_buckets(self->hfunc, self->cfunc, self->free, self->num_buckets);
|
||||
if (ret == NULL)
|
||||
return (ERROR);
|
||||
while (bucket_id < self->num_buckets)
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ void hmap_C__PREFIX___remove(t_hashmap_C__PREFIX__ *hmap, C__KEYTYPE__ *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