diff --git a/exec/src/run_ast.c b/exec/src/run_ast.c index 9c3d1a33..b6072077 100644 --- a/exec/src/run_ast.c +++ b/exec/src/run_ast.c @@ -6,7 +6,7 @@ /* By: maiboyer +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/07/11 17:22:29 by maiboyer #+# #+# */ -/* Updated: 2024/07/26 14:43:54 by maiboyer ### ########.fr */ +/* Updated: 2024/07/28 14:47:25 by maiboyer ### ########.fr */ /* */ /* ************************************************************************** */ @@ -582,8 +582,8 @@ t_error run_expansion(t_ast_expansion *self, t_state *state, t_expansion_result t_error run_command(t_ast_command *command, t_state *state, t_command_result *out) { - t_vec_str args; - t_usize i; + t_vec_str args; + t_usize i; if (command == NULL || state == NULL || out == NULL) return (ERROR); diff --git a/input.toml b/input.toml index da6d34b9..92139bf6 100644 --- a/input.toml +++ b/input.toml @@ -17,6 +17,7 @@ sources = [ "stdme/generic_sources/src/hashmap/C__PREFIX__.c__TEMPLATE__", "stdme/generic_sources/src/hashmap/C__PREFIX___utils.c__TEMPLATE__", "stdme/generic_sources/src/hashmap/C__PREFIX___iter.c__TEMPLATE__", + "stdme/generic_sources/src/hashmap/C__PREFIX___clone.c__TEMPLATE__", ] replace.C__VALTYPE__ = "type" replace.C__KEYTYPE__ = "type" diff --git a/output/include/me/hashmap/hashmap_env.h b/output/include/me/hashmap/hashmap_env.h index e664a3d9..0be349c7 100644 --- a/output/include/me/hashmap/hashmap_env.h +++ b/output/include/me/hashmap/hashmap_env.h @@ -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 diff --git a/output/src/hashmap/env/env_clone.c b/output/src/hashmap/env/env_clone.c new file mode 100644 index 00000000..e41dfb55 --- /dev/null +++ b/output/src/hashmap/env/env_clone.c @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_env.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} diff --git a/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ b/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ index c4514806..9c4a932b 100644 --- a/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ +++ b/stdme/generic_sources/header/hashmap_C__PREFIX__.h__TEMPLATE__ @@ -122,4 +122,13 @@ t_entry_C__PREFIX__ *hmap_C__PREFIX___get_entry(t_hashmap_C__PREFIX__ *hmap, t_u /// @note The iteration can be stopped by returning an error code from the function t_error hmap_C__PREFIX___iter(t_hashmap_C__PREFIX__ *self, t_error (*func)(t_usize idx, const C__KEYTYPE__ *key, C__VALTYPE__ *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_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); + #endif diff --git a/stdme/generic_sources/src/hashmap/C__PREFIX___clone.c__TEMPLATE__ b/stdme/generic_sources/src/hashmap/C__PREFIX___clone.c__TEMPLATE__ new file mode 100644 index 00000000..72a54285 --- /dev/null +++ b/stdme/generic_sources/src/hashmap/C__PREFIX___clone.c__TEMPLATE__ @@ -0,0 +1,47 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashmap_C__PREFIX__.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: maiboyer +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +}