updated hashmap impl to have clone function
This commit is contained in:
parent
cb11be6f61
commit
c876fa5477
6 changed files with 116 additions and 3 deletions
|
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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"
|
||||
|
||||
|
||||
|
||||
t_error hmap_C__PREFIX___clone(t_hashmap_C__PREFIX__ *self,
|
||||
t_error (*clone)(const t_kv_C__PREFIX__ *val, void *ctx, t_kv_C__PREFIX__ *out),
|
||||
void *ctx,
|
||||
t_hashmap_C__PREFIX__ **out)
|
||||
{
|
||||
t_usize bucket_id;
|
||||
t_entry_C__PREFIX__ *cur;
|
||||
t_kv_C__PREFIX__ kv;
|
||||
t_hashmap_C__PREFIX__ *ret;
|
||||
|
||||
bucket_id = 0;
|
||||
ret = hmap_C__PREFIX___new_with_buckets(self->hfunc, self->cfunc, self->drop, self->num_buckets);
|
||||
if (ret == NULL)
|
||||
return (ERROR);
|
||||
while (bucket_id < self->num_buckets)
|
||||
{
|
||||
cur = self->buckets[bucket_id];
|
||||
while (cur != NULL)
|
||||
{
|
||||
if (clone(&cur->kv, ctx, &kv))
|
||||
return (hmap_C__PREFIX___free(ret),ERROR);
|
||||
hmap_C__PREFIX___insert(ret, kv.key, kv.val);
|
||||
cur = cur->next;
|
||||
}
|
||||
bucket_id++;
|
||||
}
|
||||
*out = ret;
|
||||
return (NO_ERROR);
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue