copy on git
This commit is contained in:
commit
42653de246
205 changed files with 7459 additions and 0 deletions
93
libft/ft_vecint.c
Executable file
93
libft/ft_vecint.c
Executable file
|
|
@ -0,0 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_vecint.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: thrieg <thrieg@student.42mulhouse.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2024/11/18 11:14:16 by thrieg #+# #+# */
|
||||
/* Updated: 2025/02/16 19:05:45 by thrieg ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include "ft_vecint.h"
|
||||
#include "libft.h"
|
||||
|
||||
//creates a vector of initial size size
|
||||
//returns its address, or NULL if any allocations fails
|
||||
t_vecint *ft_create_vecint(size_t size)
|
||||
{
|
||||
t_vecint *ret;
|
||||
|
||||
ret = malloc(sizeof(t_vecint));
|
||||
if (!ret)
|
||||
return (NULL);
|
||||
ret->buffer = malloc(sizeof(int) * size);
|
||||
if (!ret->buffer)
|
||||
{
|
||||
free(ret);
|
||||
return (NULL);
|
||||
}
|
||||
ret->index = 0;
|
||||
ret->size = size;
|
||||
ret->finished = 0;
|
||||
return (ret);
|
||||
}
|
||||
|
||||
//append c to the vector dest
|
||||
//returns NULL if the allocation fail and the adress of dest if all goes well
|
||||
t_vecint *ft_vecint_pushback(t_vecint *dest, int c)
|
||||
{
|
||||
int *buff;
|
||||
|
||||
if (dest->index >= dest->size)
|
||||
{
|
||||
buff = malloc(sizeof(int) * ((dest->size + 1) * 2));
|
||||
if (!buff)
|
||||
return (NULL);
|
||||
ft_memcpy(buff, dest->buffer, (dest->index * sizeof(int)));
|
||||
dest->size = (dest->size + 1) * 2;
|
||||
free(dest->buffer);
|
||||
dest->buffer = buff;
|
||||
}
|
||||
dest->buffer[dest->index] = c;
|
||||
dest->index++;
|
||||
return (dest);
|
||||
}
|
||||
|
||||
//append size ints (NOT BYTES) at src to the vector dest
|
||||
//returns NULL if the allocation fail and the adress of dest if all goes well
|
||||
//returns NULL if dest or src is NULL without doing anything
|
||||
t_vecint *ft_vecint_concat(t_vecint *dest, const void *src, size_t size)
|
||||
{
|
||||
int *buff;
|
||||
|
||||
if (!dest || !src)
|
||||
return (NULL);
|
||||
if ((dest->index + size) >= dest->size)
|
||||
{
|
||||
buff = malloc(sizeof(int) * ((dest->size + size) * 2));
|
||||
if (!buff)
|
||||
return (NULL);
|
||||
ft_memcpy(buff, dest->buffer, (dest->index * sizeof(int)));
|
||||
dest->size = (dest->size + size) * 2;
|
||||
free(dest->buffer);
|
||||
dest->buffer = buff;
|
||||
}
|
||||
ft_memcpy(dest->buffer + dest->index, src, (size * sizeof(int)));
|
||||
dest->index += size;
|
||||
return (dest);
|
||||
}
|
||||
|
||||
//frees vec and all its members, sets vec to NULL and returns NULL
|
||||
void ft_free_vecint(t_vecint **vec)
|
||||
{
|
||||
if (!vec)
|
||||
return ;
|
||||
if (!(*vec))
|
||||
return ;
|
||||
if ((*vec)->buffer)
|
||||
free((*vec)->buffer);
|
||||
free(*vec);
|
||||
*vec = NULL;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue