diff options
Diffstat (limited to 'src/vec')
| -rw-r--r-- | src/vec/ft_vecreserve.c | 30 | ||||
| -rw-r--r-- | src/vec/ft_vecswallow.c | 35 |
2 files changed, 65 insertions, 0 deletions
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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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 <charles.cabergs@gmail.com> +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* 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); +} |
