From db5fd5b345729c0c1bd71f6678375c090c99ee5c Mon Sep 17 00:00:00 2001 From: Lukas Jiriste Date: Fri, 21 Jun 2024 11:48:16 +0200 Subject: [PATCH] Add const version of ft_vec_access --- ft_arr/ft_vec_access.c | 11 ++++++++++- inc/ft_arr.h | 3 ++- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/ft_arr/ft_vec_access.c b/ft_arr/ft_vec_access.c index 8606b25..c60cce9 100644 --- a/ft_arr/ft_vec_access.c +++ b/ft_arr/ft_vec_access.c @@ -6,7 +6,7 @@ /* By: ljiriste +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2023/12/09 17:14:49 by ljiriste #+# #+# */ -/* Updated: 2023/12/11 20:00:39 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 11:43:36 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -21,3 +21,12 @@ void *ft_vec_access(t_vec *vec, size_t index) return (NULL); return ((char *)vec->vec + vec->el_size * index); } + +const void *ft_vec_caccess(const t_vec *vec, size_t index) +{ + if (!vec) + return (NULL); + if (index >= vec->size) + return (NULL); + return ((const char *)vec->vec + vec->el_size * index); +} diff --git a/inc/ft_arr.h b/inc/ft_arr.h index 7347e1a..a5a0ca7 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/20 13:37:03 by ljiriste ### ########.fr */ +/* Updated: 2024/06/21 11:43:34 by ljiriste ### ########.fr */ /* */ /* ************************************************************************** */ @@ -63,6 +63,7 @@ t_arr_stat ft_vec_erase_range(t_vec *vec, size_t count, size_t index, t_arr_stat ft_vec_init(t_vec *vec, size_t el_size); void ft_vec_free(t_vec *vec, void (*free_el)(void *)); void *ft_vec_access(t_vec *vec, size_t index); +const void *ft_vec_caccess(const t_vec *vec, size_t index); /* It is probably better to use (type *)vec->vec + index for pointer * or ((type *)vec->vec)[index] for value -- 2.30.2