Add function for equating two t_vecs
authorLukas Jiriste <ljiriste@student.42prague.com>
Fri, 28 Jun 2024 12:13:22 +0000 (14:13 +0200)
committerLukas Jiriste <ljiriste@student.42prague.com>
Fri, 28 Jun 2024 15:10:18 +0000 (17:10 +0200)
Makefile
ft_arr/ft_vec_is_equal.c [new file with mode: 0644]
inc/ft_arr.h

index 3f7e42a1aba73a56c52a6b5890040c346c2d7cb6..abb3971db7f7d8ebf8468f54c87d3088b0bd3a2d 100644 (file)
--- 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 (file)
index 0000000..693a9c4
--- /dev/null
@@ -0,0 +1,33 @@
+/* ************************************************************************** */
+/*                                                                            */
+/*                                                        :::      ::::::::   */
+/*   ft_vec_is_equal.c                                  :+:      :+:    :+:   */
+/*                                                    +:+ +:+         +:+     */
+/*   By: ljiriste <marvin@42.fr>                    +#+  +:+       +#+        */
+/*                                                +#+#+#+#+#+   +#+           */
+/*   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);
+}
index a1c3b33e33b43d8e1a68f4a1f1a6b9c08e192d6e..d7950f93d6f92357dfe8d4ed51b79f837c97cf10 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 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.