From: Lukas Jiriste Date: Tue, 17 Dec 2024 19:36:22 +0000 (+0100) Subject: Improve ft_vec_find and ft_vec_find_index X-Git-Url: https://git.ljiriste.work/?a=commitdiff_plain;h=d0985dc4a6e2234daa57f638980ef3769c899767;p=Libft.git Improve ft_vec_find and ft_vec_find_index I have no idea, why I have not allowed the users to provide their own comparison function for finding an element... --- diff --git a/ft_arr/ft_vec_find.c b/ft_arr/ft_vec_find.c index 93564ab..98bf348 100644 --- a/ft_arr/ft_vec_find.c +++ b/ft_arr/ft_vec_find.c @@ -6,26 +6,20 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 11:04:05 by ljiriste #+# #+# */ -/* Updated: 2024/01/11 12:27:10 by ljiriste ### ########.fr */ +/* Updated: 2024/12/17 20:35:19 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -void *ft_vec_find(t_vec *vec, void *wanted) +void *ft_vec_find(t_vec *vec, void *wanted, + int (*cmp_element)(const void *, const void *)) { - size_t i; + size_t index; if (!vec || !wanted) return (NULL); - i = 0; - while (i < vec->size) - { - if (!ft_memcmp(ft_vec_access(vec, i), wanted, vec->el_size)) - { - return (ft_vec_access(vec, i)); - } - ++i; - } + if (ft_vec_find_index(vec, wanted, &index, cmp_element) == success) + return (ft_vec_access(vec, index)); return (NULL); } diff --git a/ft_arr/ft_vec_find_index.c b/ft_arr/ft_vec_find_index.c index 988acfc..5ec5cb8 100644 --- a/ft_arr/ft_vec_find_index.c +++ b/ft_arr/ft_vec_find_index.c @@ -6,19 +6,28 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2024/01/11 11:49:41 by ljiriste #+# #+# */ -/* Updated: 2024/01/12 17:10:23 by ljiriste ### ########.fr */ +/* Updated: 2024/12/17 20:34:45 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ #include "libft.h" -t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index) +t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index, + int (*cmp_elements)(const void *, const void *)) { + size_t i; + if (!vec || !wanted || !index) return (invalid_input); - wanted = ft_vec_find(vec, wanted); - if (wanted == NULL) - return (non_specific_failure); - *index = (size_t)(((char *)wanted - (char *)vec->vec) / vec->el_size); - return (success); + i = 0; + while (i < vec->size) + { + if (cmp_elements(wanted, ft_vec_caccess(vec, i)) == 0) + { + *index = i; + return (success); + } + ++i; + } + return (non_specific_failure); } diff --git a/inc/ft_arr.h b/inc/ft_arr.h index 6a103f5..ce46e15 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/07/04 10:20:25 by ljiriste ### ########.fr */ +/* Updated: 2024/12/17 20:34:11 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -76,8 +76,10 @@ t_arr_stat ft_vec_copy(t_vec *dest, const t_vec *src, t_ft_stat (*copy_el)(void *, const void *), void (*free_el)(void *)); -void *ft_vec_find(t_vec *vec, void *wanted); -t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index); +void *ft_vec_find(t_vec *vec, void *wanted, + int (*cmp_elements)(const void *, const void *)); +t_arr_stat ft_vec_find_index(t_vec *vec, void *wanted, size_t *index, + int (*cmp_elements)(const void *, const void *)); int ft_vec_is_equal(const t_vec *vec1, const t_vec *vec2, int (*cmp_elements)(const void *, const void *));