Add insertion of "empty" elements to t_vec
authorLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Thu, 31 Jul 2025 05:26:05 +0000 (07:26 +0200)
committerLukáš Jiřiště <gymnazium.jiriste@gmail.com>
Thu, 31 Jul 2025 05:26:05 +0000 (07:26 +0200)
Makefile
ft_arr/ft_vec_append_empty.c [new file with mode: 0644]
ft_arr/ft_vec_insert_empty.c [new file with mode: 0644]
inc/ft_arr.h

index aeb972a6cb51745c6889969c6ecbb2160a2497be..9e93dde3d718cda945ff1f5ec1a3ec1c0fb3f050 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -141,6 +141,8 @@ SRCarr      :=      ft_vec_init.c                                                                                   \
                        ft_vec_find.c                                                                                   \
                        ft_vec_find_index.c                                                                             \
                        ft_vec_copy.c                                                                                   \
+                       ft_vec_insert_empty.c                                                                   \
+                       ft_vec_append_empty.c                                                                   \
                                                                                                                                        \
                        ft_vec_is_equal.c                                                                               \
                                                                                                                                        \
diff --git a/ft_arr/ft_vec_append_empty.c b/ft_arr/ft_vec_append_empty.c
new file mode 100644 (file)
index 0000000..fcacec8
--- /dev/null
@@ -0,0 +1,11 @@
+#include "ft_arr.h"
+
+t_arr_stat     ft_vec_append_empty(t_vec *vec)
+{
+       return (ft_vec_append_empty_range(vec, 1));
+}
+
+t_arr_stat     ft_vec_append_empty_range(t_vec *vec, size_t count)
+{
+       return (ft_vec_insert_empty_range(vec, count, vec->size));
+}
diff --git a/ft_arr/ft_vec_insert_empty.c b/ft_arr/ft_vec_insert_empty.c
new file mode 100644 (file)
index 0000000..1cca512
--- /dev/null
@@ -0,0 +1,31 @@
+#include "ft_arr.h"
+#include "libft.h"
+
+t_arr_stat     ft_vec_insert_empty(t_vec *vec, size_t index)
+{
+       return (ft_vec_insert_empty_range(vec, 1, index));
+}
+
+t_arr_stat     ft_vec_insert_empty_range(t_vec *vec,
+                               size_t count, size_t index)
+{
+       if (count == 0)
+               return (success);
+       if (!vec || index > SIZE_MAX - count)
+               return (invalid_input);
+       while (vec->size == vec->capacity || index + count > vec->capacity)
+       {
+               if (ft_vec_enlarge(vec))
+                       return (alloc_fail);
+       }
+       if (index < vec->size)
+               ft_memmove((char *)vec->vec + vec->el_size * (index + count),
+                       (char *)vec->vec + vec->el_size * index,
+                       vec->el_size * (vec->size - index));
+       ft_bzero((char *)vec->vec + vec->el_size * index,
+               vec->el_size * count);
+       if (index > vec->size)
+               vec->size = index;
+       vec->size += count;
+       return (success);
+}
index fc88514f64dbec7dad0d3a2541a79b9db56cabad..233e303fc9a4fc8e4003294dae0bc8935c6eb2c8 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/12/09 13:58:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2025/03/26 20:34:00 by ljiriste         ###   ########.fr       */
+/*   Updated: 2025/07/31 07:21:49 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -56,6 +56,11 @@ t_arr_stat   ft_vec_forget_range(t_vec *vec, size_t index, size_t count);
 t_arr_stat     ft_vec_erase_range(t_vec *vec, size_t count, size_t index,
                                void (*free_el)(void *));
 
+t_arr_stat     ft_vec_insert_empty(t_vec *vec, size_t i);
+t_arr_stat     ft_vec_insert_empty_range(t_vec *vec, size_t count, size_t i);
+t_arr_stat     ft_vec_append_empty(t_vec *vec);
+t_arr_stat     ft_vec_append_empty_range(t_vec *vec, size_t count);
+
 // This macro should have been used for static t_vec initialization
 // as I see no other way.
 // But the Norm forbids macro functions, so just use the literal itself