update stuff
This commit is contained in:
parent
24b210fe86
commit
709c124028
20 changed files with 176 additions and 64 deletions
|
|
@ -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