From: Lukas Jiriste Date: Thu, 4 Jul 2024 08:24:45 +0000 (+0200) Subject: Add the ft_vec_setinsert X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=4cb98d145783e308ab9449232686721cd23857bd;p=Libft.git Add the ft_vec_setinsert 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. --- diff --git a/Makefile b/Makefile index abb3971..7ef43fd 100644 --- 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 index 0000000..807a700 --- /dev/null +++ b/ft_arr/ft_vec_setinsert.c @@ -0,0 +1,21 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_setinsert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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)); +} diff --git a/inc/ft_arr.h b/inc/ft_arr.h index d7950f9..6a103f5 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/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); diff --git a/inc/ft_stat.h b/inc/ft_stat.h index c98c247..bf78c89 100644 --- a/inc/ft_stat.h +++ b/inc/ft_stat.h @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* 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,