From a4d9777bb6409a73f0d7f372e0b437963b54e7ba Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 28 Jun 2024 12:35:06 +0200 Subject: [PATCH] Add a function to deep copy a t_vec --- Makefile | 1 + ft_arr/ft_vec_copy.c | 57 ++++++++++++++++++++++++++++++++++++++++++++ inc/ft_arr.h | 6 ++++- 3 files changed, 63 insertions(+), 1 deletion(-) create mode 100644 ft_arr/ft_vec_copy.c diff --git a/Makefile b/Makefile index 6ed432a..d017796 100644 --- a/Makefile +++ b/Makefile @@ -123,6 +123,7 @@ SRCarr := ft_vec_init.c \ ft_vec_free.c \ ft_vec_find.c \ ft_vec_find_index.c \ + ft_vec_copy.c \ \ ft_mat_init.c \ ft_mat_append.c \ diff --git a/ft_arr/ft_vec_copy.c b/ft_arr/ft_vec_copy.c new file mode 100644 index 0000000..46e5100 --- /dev/null +++ b/ft_arr/ft_vec_copy.c @@ -0,0 +1,57 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_copy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 12:02:22 by ljiriste #+# #+# */ +/* Updated: 2024/06/28 12:34:13 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" +#include + +static t_arr_stat prepare_for_copy(t_vec *dest, const t_vec *src) +{ + t_arr_stat res; + + res = ft_vec_init(dest, src->el_size); + if (res != success) + return (res); + res = ft_vec_reserve(dest, src->capacity); + return (res); +} + +// This function the exact current state of src to dest. +// copy_el function enables deep copy +// free_el function enbales cleaning after itself in a case of error +t_arr_stat ft_vec_copy(t_vec *dest, const t_vec *src, + t_ft_stat (*copy_el)(void *, const void *), void (*free_el)(void *)) +{ + size_t i; + void *tmp; + t_arr_stat res; + + tmp = malloc(dest->el_size); + if (!tmp) + return (alloc_fail); + res = prepare_for_copy(dest, src); + if (res != success) + return (res); + i = 0; + while (i < src->size && res == success) + { + res = copy_el(tmp, ft_vec_caccess(src, i)); + if (res != success) + break ; + res = ft_vec_append(dest, tmp); + if (res != success && free_el) + free_el(tmp); + } + if (res != success) + ft_vec_free(dest, free_el); + free(tmp); + return (res); +} diff --git a/inc/ft_arr.h b/inc/ft_arr.h index a5a0ca7..02e7ddd 100644 --- a/inc/ft_arr.h +++ b/inc/ft_arr.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 13:58:15 by ljiriste #+# #+# */ -/* Updated: 2024/06/21 11:43:34 by ljiriste ### ########.fr */ +/* Updated: 2024/06/28 12:30:47 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -72,6 +72,10 @@ const void *ft_vec_caccess(const t_vec *vec, size_t index); * Also implement access function that casts to desired type * eg. int *int_access(t_vec *vec, size_t ind){return ((int *)vec->vec + ind);} */ +t_arr_stat ft_vec_copy(t_vec *dest, const t_vec *src, + t_ft_stat (*copy_el)(void *, const void *), + void (*free_el)(void *)); + void *ft_vec_find(t_vec *vec, void *wanted); t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index); -- 2.30.2