From 0fc1e473e3bae0e1df32228ff72b9ab654fd2ac3 Mon Sep 17 00:00:00 2001 From: Charles Date: Wed, 15 Jul 2020 18:31:24 +0200 Subject: Added ft_vecreserve, ft_vecswallow --- include/libft_vec.h | 4 +++- src/vec/ft_vecreserve.c | 30 ++++++++++++++++++++++++++++++ src/vec/ft_vecswallow.c | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) create mode 100644 src/vec/ft_vecreserve.c create mode 100644 src/vec/ft_vecswallow.c diff --git a/include/libft_vec.h b/include/libft_vec.h index ded2a1f..f9d4eb3 100644 --- a/include/libft_vec.h +++ b/include/libft_vec.h @@ -6,7 +6,7 @@ /* By: charles +#+ +:+ +#+ */ /* +#+#+#+#+#+ +#+ */ /* Created: 2020/04/01 18:57:16 by charles #+# #+# */ -/* Updated: 2020/06/14 16:02:32 by charles ### ########.fr */ +/* Updated: 2020/07/15 18:28:30 by charles ### ########.fr */ /* */ /* ************************************************************************** */ @@ -41,6 +41,7 @@ typedef struct s_ftvec t_ftvec *ft_vecnew(size_t capacity); void ft_vecdestroy(t_ftvec *vec, void (*del)(void *elem)); t_ftvec *ft_vecgrow(t_ftvec *vec); +t_ftvec *ft_vecreserve(t_ftvec *vec, size_t capacity); t_ftvec *ft_vecpush(t_ftvec *vec, void *pushed); t_ftvec *ft_vecpush_safe(t_ftvec *vec, void *pushed); void ft_vecpop(t_ftvec *vec, void (*del)(void *elem)); @@ -53,5 +54,6 @@ t_ftvec *ft_vecinsert_safe(t_ftvec *vec, size_t i, void *elem); void ft_vecsort(t_ftvec *vec, t_ftcompar_func cmp); void **ft_vecunwrap(t_ftvec *vec); t_ftvec *ft_vecfrom_lst(t_ftlst *lst); +t_ftvec *ft_vecswallow_at(t_ftvec *vec, size_t index, t_ftvec *swallowed); #endif diff --git a/src/vec/ft_vecreserve.c b/src/vec/ft_vecreserve.c new file mode 100644 index 0000000..c2e7bcc --- /dev/null +++ b/src/vec/ft_vecreserve.c @@ -0,0 +1,30 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecreserve.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/07/15 17:22:08 by charles #+# #+# */ +/* Updated: 2020/07/15 18:30:58 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Set vector capacity to at least a certain size +** \param vec Reserved vector +** \param capacity Minimum required capacity +** \return The Vector or NULL on error +*/ + +t_ftvec *ft_vecreserve(t_ftvec *vec, size_t capacity) +{ + while (vec->capacity < capacity) + { + if (ft_vecgrow(vec) == NULL) + return (NULL); + } + return (vec); +} diff --git a/src/vec/ft_vecswallow.c b/src/vec/ft_vecswallow.c new file mode 100644 index 0000000..614022d --- /dev/null +++ b/src/vec/ft_vecswallow.c @@ -0,0 +1,35 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* ft_vecswallow.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: charles +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2020/07/15 17:17:00 by charles #+# #+# */ +/* Updated: 2020/07/15 18:30:37 by charles ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft_vec.h" + +/* +** \brief Insert all element of a vector in an other vector +** and free the swallowed vector +** \param vec Vector where the element will be inserted +** \param index Index of the insertion +** \param swallowed Vector to swallow +** \return Destination vector or NULL on error +*/ + +t_ftvec *ft_vecswallow_at(t_ftvec *vec, size_t index, t_ftvec *swallowed) +{ + size_t i; + + if (ft_vecreserve(vec, vec->size + swallowed->size) == NULL) + return (NULL); + i = -1; + while (++i < swallowed->size) + ft_vecinsert(vec, index, swallowed->data[i]); + ft_vecdestroy(swallowed, NULL); + return (vec); +} -- cgit