From b390fa8b737f2fd3f109f09580d5cde0e420e17b Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 28 Jun 2024 14:13:22 +0200 Subject: [PATCH] Add function for equating two t_vecs --- Makefile | 2 ++ ft_arr/ft_vec_is_equal.c | 33 +++++++++++++++++++++++++++++++++ inc/ft_arr.h | 5 ++++- 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 ft_arr/ft_vec_is_equal.c diff --git a/Makefile b/Makefile index 3f7e42a..abb3971 100644 --- a/Makefile +++ b/Makefile @@ -125,6 +125,8 @@ SRCarr := ft_vec_init.c \ ft_vec_find_index.c \ ft_vec_copy.c \ \ + ft_vec_is_equal.c \ + \ ft_vec_contains.c \ ft_vec_is_subset.c \ ft_vec_is_setequal.c \ diff --git a/ft_arr/ft_vec_is_equal.c b/ft_arr/ft_vec_is_equal.c new file mode 100644 index 0000000..693a9c4 --- /dev/null +++ b/ft_arr/ft_vec_is_equal.c @@ -0,0 +1,33 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vec_is_equal.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ljiriste +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2024/06/28 14:07:57 by ljiriste #+# #+# */ +/* Updated: 2024/06/28 14:12:53 by ljiriste ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "ft_arr.h" + +int ft_vec_is_equal(const t_vec *vec1, const t_vec *vec2, + int (*cmp_elements)(const void *, const void *)) +{ + size_t i; + const void *el1; + const void *el2; + + if (vec1->size != vec2->size) + return (0); + i = 0; + while (i < vec1->size) + { + el1 = ft_vec_caccess(vec1, i); + el2 = ft_vec_caccess(vec2, i); + if (cmp_elements(el1, el2) != 0) + return (0); + } + return (1); +} diff --git a/inc/ft_arr.h b/inc/ft_arr.h index a1c3b33..d7950f9 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 13:47:36 by ljiriste ### ########.fr */ +/* Updated: 2024/06/28 14:11:56 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -79,6 +79,9 @@ t_arr_stat ft_vec_copy(t_vec *dest, const t_vec *src, void *ft_vec_find(t_vec *vec, void *wanted); t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index); +int ft_vec_is_equal(const t_vec *vec1, const t_vec *vec2, + int (*cmp_elements)(const void *, const void *)); + // The following are functions that would operate on sets. // I have not implemented set structure as it would be just a wrapper // on t_vec with worsened access to the elements. -- 2.30.2