Add a function to deep copy a t_vec
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 28 Jun 2024 10:35:06 +0000 (12:35 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 28 Jun 2024 15:10:18 +0000 (17:10 +0200)
Makefile
ft_arr/ft_vec_copy.c [new file with mode: 0644]
inc/ft_arr.h

index 6ed432a59929ebbc6251cb96b4a80d861489ed35..d0177965aba26e1b623b256820c072331c0c8dec 100644 (file)
--- 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 (file)
index 0000000..46e5100
--- /dev/null
@@ -0,0 +1,57 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   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);
+}
index a5a0ca730d3319f01c531582d0cc05b0e4453ad0..02e7dddbec2990a9e542380c4ac7bdbbc280febe 100644 (file)
@@ -6,7 +6,7 @@
 /*   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       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -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);