Add the ft_vec_setinsert
authorLukas Jiriste <ljiriste@student.42prague.com>
Thu, 4 Jul 2024 08:24:45 +0000 (10:24 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Sun, 21 Jul 2024 18:21:19 +0000 (20:21 +0200)
This function could have been named ft_vec_insert_unique,
but that would invoke the need for insertion index which
I wanted to avoid because of the usage a set.

Makefile
ft_arr/ft_vec_setinsert.c [new file with mode: 0644]
inc/ft_arr.h
inc/ft_stat.h

index abb3971db7f7d8ebf8468f54c87d3088b0bd3a2d..7ef43fd16d4e49b387a5f3e22f041c294165548b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -131,6 +131,8 @@ SRCarr      :=      ft_vec_init.c                           \
                        ft_vec_is_subset.c                      \
                        ft_vec_is_setequal.c            \
                                                                                \
+                       ft_vec_setinsert.c                      \
+                                                                               \
                        ft_mat_init.c                           \
                        ft_mat_append.c                         \
                        ft_mat_insert_col.c                     \
diff --git a/ft_arr/ft_vec_setinsert.c b/ft_arr/ft_vec_setinsert.c
new file mode 100644 (file)
index 0000000..807a700
--- /dev/null
@@ -0,0 +1,21 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_vec_setinsert.c                                 :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   Created: 2024/07/04 09:52:19 by ljiriste          #+#    #+#             */
+/*   Updated: 2024/07/04 10:23:41 by ljiriste         ###   ########.fr       */
+/*                                                                            */
+/* ************************************************************************** */
+
+#include "ft_arr.h"
+
+t_ft_stat      ft_vec_setinsert(t_vec *vec, const void *el,
+                               int (*cmp_elements)(const void *, const void *))
+{
+       if (ft_vec_contains(vec, el, cmp_elements))
+               return (already_inside);
+       return (ft_vec_append(vec, el));
+}
index d7950f93d6f92357dfe8d4ed51b79f837c97cf10..6a103f50a14002109a0abcc6405df1d59734e45d 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <ljiriste@student.42prague.com>   +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2023/12/09 13:58:15 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/28 14:11:56 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/04 10:20:25 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -93,6 +93,9 @@ int                   ft_vec_is_subset(const t_vec *subset, const t_vec *set,
 int                    ft_vec_is_setequal(const t_vec *first, const t_vec *second,
                                int (*cmp_elements)(const void *, const void *));
 
+t_ft_stat      ft_vec_setinsert(t_vec *vec, const void *el,
+                               int (*cmp_el)(const void *, const void *));
+
 t_arr_stat     ft_mat_init(t_mat *mat, size_t el_size);
 void           ft_mat_free(t_mat *mat, void (*free_el)(void *));
 t_arr_stat     ft_mat_resize(t_mat *mat, size_t rows, size_t cols);
index c98c247197e4fe88aca839265c327089da88609d..bf78c890c784879cde2f80f77ddea6370768ac00 100644 (file)
@@ -6,7 +6,7 @@
 /*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
 /*                                                +#+#+#+#+#+   +#+           */
 /*   Created: 2024/06/20 12:40:28 by ljiriste          #+#    #+#             */
-/*   Updated: 2024/06/20 13:08:35 by ljiriste         ###   ########.fr       */
+/*   Updated: 2024/07/04 10:14:10 by ljiriste         ###   ########.fr       */
 /*                                                                            */
 /* ************************************************************************** */
 
@@ -16,6 +16,7 @@
 typedef enum e_ft_stat
 {
        success,
+       already_inside,
        alloc_fail,
        invalid_size,
        invalid_input,