--- /dev/null
+/* ************************************************************************** */
+/* */
+/* ::: :::::::: */
+/* ft_vec_copy.c :+: :+: :+: */
+/* +:+ +:+ +:+ */
+/* By: ljiriste <marvin@42.fr> +#+ +:+ +#+ */
+/* +#+#+#+#+#+ +#+ */
+/* Created: 2024/06/28 12:02:22 by ljiriste #+# #+# */
+/* Updated: 2024/06/28 12:34:13 by ljiriste ### ########.fr */
+/* */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+#include <stdlib.h>
+
+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);
+}
/* By: ljiriste <ljiriste@student.42prague.com> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* 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 */
/* */
/* ************************************************************************** */
* 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);