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
|
|
@ -122,4 +122,13 @@ t_entry_env *hmap_env_get_entry(t_hashmap_env *hmap, t_usize hash, t_str *key, t
|
|||
/// @note The iteration can be stopped by returning 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, using the given function to duplicate the items
|
||||
/// @param self The hashmap
|
||||
/// @param func The function to call
|
||||
/// @param ctx The context to pass to the function
|
||||
/// @param out The cloned hashmap
|
||||
/// @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);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
47
output/src/hashmap/env/env_clone.c
vendored
Normal file
47
output/src/hashmap/env/env_clone.c
vendored
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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"
|
||||
|
||||
|
||||
|
||||
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_usize bucket_id;
|
||||
t_entry_env *cur;
|
||||
t_kv_env kv;
|
||||
t_hashmap_env *ret;
|
||||
|
||||
bucket_id = 0;
|
||||
ret = hmap_env_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_env_free(ret),ERROR);
|
||||
hmap_env_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