removing the libft of rparodi

This commit is contained in:
Raphael 2024-11-08 19:37:30 +01:00
parent 0391323626
commit be6038dcc8
523 changed files with 724 additions and 3336 deletions

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arg_custom_checker.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 14:13:26 by bgoulard #+# #+# */
/* Updated: 2024/06/01 14:10:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include <stddef.h>
static t_data_is singleton_custom_checker(t_data_is custom_checker)
{
static t_data_is custom_checker_ptr = NULL;
if (custom_checker)
custom_checker_ptr = custom_checker;
return (custom_checker_ptr);
}
void ft_arg_set_custom_checker(t_data_is custom_checker)
{
singleton_custom_checker(custom_checker);
}
t_data_is ft_arg_get_custom_checker(void)
{
return (singleton_custom_checker(NULL));
}

View file

@ -0,0 +1,68 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 01:10:29 by bgoulard #+# #+# */
/* Updated: 2024/08/21 10:28:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include "ft_args_types.h"
#include "ft_string.h"
#include "internal/args_helper.h"
#include <stdlib.h>
#include <sys/types.h>
#include <stdbool.h>
int run_opt_func(const t_opt opt, void *usr_control_struct, const char **arg, \
int *i)
{
const char *arg_ptr;
void (*func_arg)(void *, const char *);
void (*func_no_arg)(void *);
func_arg = opt.func;
func_no_arg = opt.func;
if (opt.type & OPT_ARG && opt.type & OPT_EQSIGN)
arg_ptr = ft_strchr(arg[*i], '=') + 1;
else if (opt.type & OPT_ARG)
arg_ptr = arg[*i + 1];
else
arg_ptr = NULL;
if (checker_arg(opt.type, arg_ptr) != 0)
return (arg_type_err(opt, arg_ptr));
if (opt.type & OPT_ARG)
func_arg(usr_control_struct, arg_ptr);
else
func_no_arg(usr_control_struct);
*i += 1 + ((opt.type != 0) && (opt.type & OPT_EQSIGN) == 0);
return (EXIT_SUCCESS);
}
int ft_parse_args(const char **argv, void *usr_control_struct)
{
const t_opt *opt;
ssize_t opt_index;
int i;
opt = ft_get_opt_list();
i = 1;
while (argv[i] && argv[i][0] == '-')
{
if (argv[i][1] == '-' && argv[i][2] == '\0')
return (i + 1);
else if (argv[i][1] == '-')
opt_index = parse_long_opt(argv[i] + 2, opt);
else
opt_index = parse_short_opt(argv[i] + 1, opt);
if (opt_index == -1)
return (arg_opt_err(argv[i]));
if (run_opt_func(opt[opt_index], usr_control_struct, argv, &i) != 0)
return (-1);
}
return (i);
}

View file

@ -0,0 +1,87 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_err.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:23:49 by bgoulard #+# #+# */
/* Updated: 2024/05/31 18:22:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args_types.h"
#include "ft_args.h"
#include "ft_char.h"
#include "ft_string.h"
#include <unistd.h>
#include <stdlib.h>
// 0 success, !0 failure
int checker_arg(t_opt_type type, const char *arg)
{
if (!arg || arg == (char *)0x1)
return (type);
if ((type & ARG_MASK_ATYPE) == OPT_INT)
return (!ft_str_isint(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_STRING)
return (EXIT_SUCCESS);
else if ((type & ARG_MASK_ATYPE) == OPT_BOOL)
return (!ft_str_isbool(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_FLOAT)
return (!ft_str_isfloat(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_LONG)
return (!ft_str_islong(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_DOUBLE)
return (!ft_str_isdouble(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_ALPHANUM)
return (!ft_str_isvalid(arg, ft_isalnum));
else if ((type & ARG_MASK_ATYPE) == OPT_HEX)
return (!ft_str_ishex(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_OCT)
return (!ft_str_isoct(arg));
else if ((type & ARG_MASK_ATYPE) == OPT_OTHER
&& ft_arg_get_custom_checker())
return (!ft_arg_get_custom_checker()(arg));
return (EXIT_SUCCESS);
}
int arg_type_err(t_opt opt, const char *arg)
{
const char *progname;
progname = ft_progname();
if (progname)
{
ft_putstr_fd(progname, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
}
ft_putstr_fd("Error: invalid argument for option: ", STDERR_FILENO);
if (opt.long_name)
ft_putstr_fd(opt.long_name, STDERR_FILENO);
else
ft_putchar_fd(opt.short_name, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
if (!arg || arg == (char *)1)
ft_putstr_fd("NULL", STDERR_FILENO);
else
ft_putstr_fd(arg, STDERR_FILENO);
ft_putstr_fd("\n", STDERR_FILENO);
return (EXIT_FAILURE);
}
int arg_opt_err(const char *arg)
{
const char *progname;
progname = ft_progname();
if (progname)
{
ft_putstr_fd(progname, STDERR_FILENO);
ft_putstr_fd(": ", STDERR_FILENO);
}
ft_putstr_fd("Error: invalid option: ", STDERR_FILENO);
ft_putstr_fd(arg, STDERR_FILENO);
ft_putstr_fd("\n", STDERR_FILENO);
return (EXIT_FAILURE);
}

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_parse_opt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 14:06:00 by bgoulard #+# #+# */
/* Updated: 2024/05/31 17:57:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args_types.h"
#include "ft_math.h"
#include "ft_string.h"
#include <sys/types.h>
ssize_t parse_short_opt(char *str_op, const t_opt *opt_list)
{
ssize_t i;
i = 0;
while (opt_list[i].func)
{
if (opt_list[i].short_name && \
opt_list[i].short_name == str_op[0])
return (i);
i++;
}
return (-1);
}
ssize_t parse_long_opt(char *str_op, const t_opt *opt_list)
{
ssize_t i;
ssize_t offset;
i = 0;
offset = 0;
if (ft_strchr(str_op, '='))
offset = ft_strchr(str_op, '=') - str_op;
while (opt_list[i].func)
{
if (opt_list[i].long_name && ft_strncmp(opt_list[i].long_name, str_op, \
ft_max(offset, ft_strlen(opt_list[i].long_name))) == 0)
return (i);
i++;
}
return (-1);
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_progname.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 00:00:49 by bgoulard #+# #+# */
/* Updated: 2024/04/21 14:44:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include <stddef.h>
static const char *singleton_progname(const char *progname)
{
static const char *singleton_progname = NULL;
if (progname)
singleton_progname = progname;
return (singleton_progname);
}
void ft_set_progname(const char *progname)
{
if (progname)
singleton_progname(progname);
else
singleton_progname("a.out");
}
const char *ft_progname(void)
{
return (singleton_progname(NULL));
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_set_opt_args.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/14 00:01:33 by bgoulard #+# #+# */
/* Updated: 2024/04/21 14:44:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include "ft_args_types.h"
#include <stddef.h>
static const t_opt *singleton_opt_list(const t_opt *opt_list)
{
static const t_opt *singleton_opt_list = NULL;
if (opt_list)
singleton_opt_list = opt_list;
return (singleton_opt_list);
}
void ft_set_opt_list(const t_opt *opt_list)
{
if (opt_list)
singleton_opt_list(opt_list);
}
const t_opt *ft_get_opt_list(void)
{
return (singleton_opt_list(NULL));
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_setup_prog.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 11:31:28 by bgoulard #+# #+# */
/* Updated: 2024/05/30 00:47:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
void ft_setup_prog(const char *const *av)
{
ft_set_progname(av[0]);
ft_set_version(VERSION);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_version.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/13 23:57:19 by bgoulard #+# #+# */
/* Updated: 2024/04/21 14:45:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_args.h"
#include <stddef.h>
static const char *singleton_version(const char *version)
{
static const char *singleton_version = NULL;
if (version)
singleton_version = version;
return (singleton_version);
}
void ft_set_version(const char *version)
{
if (version)
singleton_version(version);
else
singleton_version(VERSION);
return ;
}
const char *ft_progversion(void)
{
return (singleton_version(NULL));
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:17:48 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void ft_dl_add_back(t_dlist **head, t_dlist *const added)
{
t_dlist *last;
if (!head || !added)
return ;
if (!*head)
return (ft_dl_add_front(head, added));
last = ft_dl_end(*head);
last->next = added;
added->prev = last;
}
void ft_dl_add_front(t_dlist **head, t_dlist *const added)
{
if (!head || !added)
return ;
if (!*head)
return ((void)(*head = added));
added->next = *head;
(*head)->prev = added;
*head = added;
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_apply.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 18:59:42 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:59:43 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_dl_apply(const t_dlist *start, t_data_apply applied)
{
return (ft_dl_apply_range(start, NULL, applied));
}
size_t ft_dl_apply_range(const t_dlist *start, const t_dlist *end,
t_data_apply applied)
{
size_t i;
t_dlist *it;
it = (t_dlist *)start;
i = 0;
while (it != end)
{
applied(it->data);
it = it->next;
i++;
}
return (i);
}
size_t ft_dl_apply_range_node(const t_dlist *start, const t_dlist *end,
t_dnode_apply applied)
{
size_t i;
t_dlist *it;
it = (t_dlist *)start;
i = 0;
while (it != end)
{
applied(it);
it = it->next;
i++;
}
return (i);
}

View file

@ -0,0 +1,40 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:33:59 by iron #+# #+# */
/* Updated: 2024/06/23 18:59:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_dl_clear(t_dlist **head, t_data_apply del)
{
size_t ret;
ret = ft_dl_delete_range(*head, NULL, del);
*head = NULL;
return (ret);
}
size_t ft_dl_clear_range(t_dlist *start, t_dlist *end, t_data_apply del)
{
size_t i;
i = 0;
if (!start)
return (0);
while (start != end)
{
if (del)
del(start->data);
start->data = NULL;
start = start->next;
i++;
}
return (i);
}

View file

@ -0,0 +1,65 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:05 by iron #+# #+# */
/* Updated: 2024/06/23 18:59:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_create(const void *data)
{
t_dlist *ret;
ret = ft_dl_new();
if (!ret)
return (ret);
ret->data = (void *)data;
return (ret);
}
t_dlist *ft_dl_copy_node(const t_dlist *const other)
{
t_dlist *ret;
ret = ft_dl_new();
if (!ret)
return (ret);
ret->data = (void *)other->data;
ret->next = (t_dlist *)other->next;
ret->prev = (t_dlist *)other->prev;
return (ret);
}
t_dlist *ft_dl_copy_list(const t_dlist *const other)
{
t_dlist *node;
t_dlist *head;
t_dlist *prev;
t_dlist *it;
it = (t_dlist *)other;
node = NULL;
head = NULL;
prev = NULL;
while (it)
{
node = ft_dl_copy_node(it);
if (!node)
return (ft_dl_delete(&head, NULL), head);
node->prev = prev;
if (prev)
prev->next = node;
else
head = node;
if (it->next)
prev = node;
it = it->next;
}
return (head);
}

View file

@ -0,0 +1,102 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_delete.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:10 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:01 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
int ft_dl_delete_self(t_dlist *node, t_data_apply del)
{
if (!node)
return (FTLIST_FAILURE);
if (del)
del(node->data);
if (node->prev)
node->prev->next = node->next;
if (node->next)
node->next->prev = node->prev;
free(node);
return (FTLIST_SUCCESS);
}
size_t ft_dl_delete_range(t_dlist *start, const t_dlist *target,
t_data_apply del)
{
t_dlist *next;
t_dlist *prev;
size_t i;
i = 0;
next = NULL;
prev = NULL;
if (start && start->prev)
prev = start->prev;
while (start != target)
{
next = start->next;
if (del)
del(start->data);
free(start);
start = next;
i++;
}
if (prev)
prev->next = next;
if (next)
next->prev = prev;
return (i);
}
size_t ft_dl_delete(t_dlist **head, t_data_apply del)
{
size_t i;
t_dlist *firs_elem;
if (!head)
return (0);
firs_elem = ft_dl_begin(*head);
i = ft_dl_delete_range(firs_elem, NULL, del);
*head = NULL;
return (i);
}
/*
size_t ft_dl_delete_dup(t_dlist **src)
{
t_dlist **node_ptrs;
size_t nb_del;
size_t length;
node_ptrs = NULL;
nb_del = 0;
length = 0;
if (!*src)
return (0);
node_ptrs = ft_dl_get_nodes(*src);
if (!node_ptrs)
return (0);
length = ft_dl_count(*src);
for (size_t j = 0; j + 1 != length; j++) {
for (size_t k = j + 1; k != length; k++) {
if (!node_ptrs[j] || !node_ptrs[k])
continue ;
if (node_ptrs[j]->data == node_ptrs[k]->data) {
ft_dl_delete_self(node_ptrs[k]);
node_ptrs[k] = NULL;
nb_del++;
j++;
}
}
}
free(node_ptrs);
return (nb_del);
}
*/

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_find.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 13:05:32 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_find(const t_dlist *head, const void *data,
int (*cmp)(const void *, const void *))
{
t_dlist *it;
it = (t_dlist *)head;
while (it)
{
if (data == it->data || (cmp && !cmp(it->data, data)))
return (it);
it = it->next;
}
return (NULL);
}

View file

@ -0,0 +1,58 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_getters.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:21 by iron #+# #+# */
/* Updated: 2024/07/19 17:36:04 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
#include <stdlib.h>
void **ft_dl_get_datas(const t_dlist *src)
{
size_t size;
void **ret;
if (!src)
return (NULL);
size = ft_dl_size(src);
ret = ft_calloc(sizeof(void *), (size + 1));
if (!ret)
return (NULL);
ret[size] = NULL;
size = 0;
while (src)
{
ret[size++] = (void *)src->data;
src = src->next;
}
return (ret);
}
t_dlist **ft_dl_get_nodes(const t_dlist *src)
{
size_t size;
t_dlist **ret;
size = ft_dl_size(src);
ret = NULL;
if (!src)
return (NULL);
ret = ft_calloc(sizeof(t_dlist *), (size + 1));
if (!ret)
return (ret);
ret[size] = NULL;
size = 0;
while (src)
{
ret[size++] = (t_dlist *)src;
src = src->next;
}
return (ret);
}

View file

@ -0,0 +1,48 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_iterator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:27 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_at(const t_dlist *head, size_t index)
{
size_t i;
t_dlist *it;
it = (t_dlist *)head;
i = 0;
while (i != index)
{
i++;
it = it->next;
}
return (it);
}
t_dlist *ft_dl_end(const t_dlist *head)
{
t_dlist *it;
it = (t_dlist *)head;
while (it->next)
it = it->next;
return (it);
}
t_dlist *ft_dl_begin(const t_dlist *head)
{
t_dlist *it;
it = (t_dlist *)head;
while (it->prev)
it = it->prev;
return (it);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:21:43 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_map(const t_dlist *lst, t_data_tr f, t_data_apply del)
{
t_dlist *ret;
t_dlist *tmp;
if (!lst || !f)
return (NULL);
ret = NULL;
while (lst)
{
tmp = ft_dl_create(f(lst->data));
if (!tmp)
return (ft_dl_clear(&ret, del), NULL);
ft_dl_add_back(&ret, tmp);
lst = lst->next;
}
return (ret);
}

View file

@ -0,0 +1,27 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:42:00 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
t_dlist *ft_dl_new(void)
{
t_dlist *ret;
ret = ft_calloc(1, sizeof(t_dlist));
if (!ret)
return (ret);
ret->data = NULL;
ret->prev = NULL;
ret->next = NULL;
return (ret);
}

View file

@ -0,0 +1,80 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_pushpop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:38 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
t_dlist *ft_dl_push(t_dlist **node, const void *data)
{
t_dlist *added;
added = ft_dl_create(data);
added->next = *node;
if (*node)
(*node)->prev = added;
*node = added;
return (added);
}
t_dlist *ft_dl_push_back(t_dlist **node, const void *data)
{
t_dlist *added;
t_dlist *it;
it = *node;
if (!*node)
return (ft_dl_push(node, data));
added = ft_dl_create(data);
while (it->next)
it = it->next;
it->next = added;
added->prev = it;
return (*node);
}
void *ft_dl_pop(t_dlist **node)
{
void *data;
t_dlist *it;
data = NULL;
if (!node || !*node)
return (NULL);
it = *node;
*node = (*node)->next;
data = it->data;
free(it);
if (*node)
(*node)->prev = NULL;
return (data);
}
void *ft_dl_pop_back(t_dlist **node)
{
void *data;
t_dlist *it;
t_dlist *prev;
data = NULL;
prev = NULL;
if (!node || !*node)
return (NULL);
it = ft_dl_end(*node);
data = it->data;
prev = it->prev;
if (prev)
prev->next = NULL;
else
*node = NULL;
free(it);
return (data);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_rev.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iron <iron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:20:27 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_rev(t_dlist **head)
{
t_dlist *it;
t_dlist *tmp;
if (!head || !*head)
return (NULL);
it = *head;
while (it)
{
tmp = it->next;
it->next = it->prev;
it->prev = tmp;
if (!tmp)
*head = it;
it = tmp;
}
return (*head);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:50 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:35 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_dl_size(const t_dlist *head)
{
t_dlist *it;
size_t i;
i = 0;
it = (t_dlist *)head;
while (it)
{
it = it->next;
i++;
}
return (i);
}
size_t ft_dl_size_of_data(const t_dlist *head, t_data_is function)
{
t_dlist *it;
size_t i;
it = (t_dlist *)head;
i = 0;
while (it)
{
if (function(it->data))
i++;
it = it->next;
}
return (i);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_dl_sub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/27 21:34:45 by iron #+# #+# */
/* Updated: 2024/06/23 19:00:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_dlist *ft_dl_subrange(const t_dlist *src, const t_dlist *to)
{
t_dlist *ret;
t_dlist *prev;
t_dlist *new_node;
t_dlist *it;
ret = NULL;
if (!src)
return (ret);
ret = ft_dl_create(src->data);
if (src == to)
return (ret);
prev = ret;
it = src->next;
while (it != to)
{
new_node = ft_dl_create(it->data);
if (!new_node)
return (ft_dl_delete(&prev, NULL), NULL);
new_node->prev = prev;
new_node->next = NULL;
prev->next = new_node;
prev = new_node;
it = it->next;
}
return (ret);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_add.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:48:35 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:50 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void ft_ll_add_front(t_list **lst, t_list *new)
{
if (!lst)
return ;
new->next = *lst;
*lst = new;
}
void ft_ll_add_back(t_list **lst, t_list *new)
{
if (!*lst)
*lst = new;
else
ft_ll_end(*lst)->next = new;
}

View file

@ -0,0 +1,60 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_apply.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:01:53 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:54 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void ft_ll_apply(const t_list *lst, t_data_apply f)
{
t_list *it;
if (!f)
return ;
it = (t_list *)lst;
while (it)
{
f(it->data);
it = it->next;
}
}
void ft_ll_apply_range(const t_list *lst, const t_list *end, t_data_apply f)
{
t_list *it;
if (!f)
return ;
it = (t_list *)lst;
while (it && it != end)
{
f(it->data);
it = it->next;
}
}
// nxt = lst->next;
// f may modify lst->next, we dont know
void ft_ll_apply_range_node(const t_list *lst, const t_list *end,
t_lnode_apply f)
{
t_list *nxt;
t_list *it;
if (!f)
return ;
it = (t_list *)lst;
while (it && it != end)
{
nxt = it->next;
f(it);
it = nxt;
}
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 03:12:14 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:00:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
void ft_ll_clear(t_list **lst, t_data_apply del)
{
t_list *runner;
t_list *next;
if (!lst)
return ;
runner = *lst;
while (runner)
{
next = runner->next;
if (del)
del(runner->data);
runner->data = NULL;
free(runner);
runner = next;
}
*lst = NULL;
}

View file

@ -0,0 +1,61 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 11:40:49 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_create(const void *const data)
{
t_list *ret;
ret = ft_ll_new();
if (!ret)
return (ret);
ret->data = (void *)data;
return (ret);
}
t_list *ft_ll_copy_node(const t_list *const other)
{
t_list *ret;
ret = ft_ll_new();
if (!ret)
return (ret);
ret->data = (void *)other->data;
ret->next = (t_list *)other->next;
return (ret);
}
t_list *ft_ll_copy_list(const t_list *const other)
{
t_list *node;
t_list *head;
t_list *prev;
t_list *it;
it = (t_list *)other;
head = ft_ll_copy_node(it);
if (!head)
return (head);
prev = head;
it = (t_list *)it->next;
while (it)
{
node = ft_ll_copy_node(it);
if (!node)
return (ft_ll_clear(&head, NULL), NULL);
prev->next = node;
prev = node;
it = (t_list *)it->next;
}
return (head);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_delete.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/08 03:09:13 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include <stdlib.h>
void ft_ll_delone(t_list *lst, t_data_apply del)
{
if (!lst)
return ;
if (del)
del(lst->data);
free(lst);
}
size_t ft_ll_delete_range(t_list *lst, const t_list *end, t_data_apply del)
{
t_list *tmp;
size_t i;
i = 0;
if (!lst || !del)
return (i);
while (lst && lst != end)
{
i++;
tmp = lst->next;
del(lst->data);
free(lst);
lst = tmp;
}
return (i);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_find.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/08 11:12:59 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
void *ft_ll_find(const t_list *list, const void *data,
int (*cmp)(const void *, const void *))
{
t_list *it;
it = (t_list *)list;
while (it)
{
if (data == it->data || (cmp && cmp(it->data, data) == 0))
return (it->data);
it = it->next;
}
return (NULL);
}

View file

@ -0,0 +1,54 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_getters.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 11:54:40 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
void **ft_ll_get_datas(const t_list *src)
{
void **datas;
size_t i;
if (!src)
return (NULL);
datas = ft_calloc(sizeof(void *), ft_ll_size(src) + 1);
if (!datas)
return (NULL);
i = 0;
while (src)
{
datas[i] = src->data;
src = src->next;
i++;
}
return (datas);
}
t_list **ft_ll_get_nodes(const t_list *src)
{
t_list **nodes;
size_t i;
if (!src)
return (NULL);
nodes = ft_calloc(sizeof(t_list *), ft_ll_size(src) + 1);
if (!nodes)
return (NULL);
i = 0;
while (src)
{
nodes[i] = (t_list *)src;
src = src->next;
i++;
}
return (nodes);
}

View file

@ -0,0 +1,37 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_iterator.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:43:27 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:12 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_end(const t_list *lst)
{
if (!lst)
return (NULL);
while (lst->next)
lst = lst->next;
return ((t_list *)lst);
}
t_list *ft_ll_at(const t_list *lst, size_t index)
{
size_t i;
i = 0;
while (lst && i < index)
{
lst = lst->next;
i++;
}
if (i != index)
return (NULL);
return ((t_list *)lst);
}

View file

@ -0,0 +1,32 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_map.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:04:38 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_map(const t_list *lst, t_data_tr f, t_data_apply del)
{
t_list *ret;
t_list *node;
ret = NULL;
if (!f || !del || !lst)
return (NULL);
while (lst)
{
node = ft_ll_create(f(lst->data));
if (!node)
return (ft_ll_clear(&ret, del), NULL);
ft_ll_add_back(&ret, node);
lst = lst->next;
}
return (ret);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:30:32 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
t_list *ft_ll_new(void)
{
t_list *elem;
elem = ft_calloc(sizeof(t_list), 1);
return (elem);
}

View file

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_pushpop.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:02:47 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_string.h"
t_list *ft_ll_push(t_list **lst, const void *data)
{
t_list *new;
if (!lst)
return (NULL);
new = ft_ll_create(data);
if (!new)
return (NULL);
new->next = *lst;
*lst = new;
return (*lst);
}
t_list *ft_ll_push_back(t_list **lst, const void *data)
{
t_list *added;
if (!lst)
return (NULL);
added = ft_ll_create(data);
if (!added)
return (NULL);
if (!*lst)
*lst = added;
else
ft_ll_end(*lst)->next = added;
return (*lst);
}
void *ft_ll_pop(t_list **lst)
{
t_list *tmp;
void *data;
if (!lst || !*lst)
return (NULL);
tmp = *lst;
data = (*lst)->data;
*lst = (*lst)->next;
ft_free((void **)&tmp);
return (data);
}
void *ft_ll_pop_back(t_list **lst)
{
t_list *pre_last;
void *data;
if (!lst || !*lst)
return (NULL);
if (!(*lst)->next)
return (ft_ll_pop(lst));
pre_last = *lst;
while (pre_last->next->next)
pre_last = pre_last->next;
data = pre_last->next->data;
free(pre_last->next);
pre_last->next = NULL;
return (data);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_rev.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: iron <iron@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/21 15:22:54 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_rev(t_list **head)
{
t_list *next;
t_list *it;
t_list *prev;
if (!head || !*head)
return (NULL);
it = *head;
prev = NULL;
next = it->next;
while (next)
{
next = it->next;
it->next = prev;
prev = it;
it = next;
}
*head = prev;
return (*head);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_size.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:38:32 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:28 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
size_t ft_ll_size(const t_list *lst)
{
t_list *it;
size_t i;
i = 0;
it = (t_list *)lst;
while (it)
{
it = it->next;
i++;
}
return (i);
}
size_t ft_ll_size_match(const t_list *lst, t_data_is function)
{
size_t i;
t_list *it;
i = 0;
it = (t_list *)lst;
while (it)
{
if (function(it->data))
i++;
it = it->next;
}
return (i);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ll_sub.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/10 12:10:57 by bgoulard #+# #+# */
/* Updated: 2024/06/23 19:01:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
t_list *ft_ll_subrange(const t_list *lst, const t_list *end)
{
t_list *sub;
sub = NULL;
if (!lst)
return (sub);
if (lst == end)
return (ft_ll_create(lst->data));
while (lst && lst != end)
{
ft_ll_push_back(&sub, lst->data);
lst = lst->next;
}
return (sub);
}

View file

@ -0,0 +1,34 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_clear.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:29:23 by bgoulard #+# #+# */
/* Updated: 2024/05/31 23:30:14 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_vector.h"
void ft_map_clear(t_map *map)
{
size_t i;
t_list *cur;
i = 0;
while (i < map->capacity)
{
while (map->nodes[i])
{
cur = map->nodes[i];
map->nodes[i] = map->nodes[i]->next;
ft_vec_add(&map->reserved_nodes, cur);
}
map->weights[i] = 0;
i++;
}
map->w_total = 0;
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_create.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 16:11:41 by bgoulard #+# #+# */
/* Updated: 2024/05/31 23:12:34 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_string.h"
#include "ft_vector.h"
t_map *ft_map_create(size_t size)
{
t_map *map;
map = ft_calloc(1, sizeof(t_map));
if (!map)
return (0);
map->capacity = size;
map->w_total = 0;
map->weights = ft_calloc(size, sizeof(size_t));
if (!map->weights)
return (free(map), NULL);
map->nodes = ft_calloc(size, sizeof(t_list *));
if (!map->nodes)
return (free(map->weights), free(map), NULL);
map->hash = &ft_hash_djb2;
map->cmp = (int (*)(const void *, const void *)) & ft_strcmp;
map->reserved_nodes = ft_vec_new();
if (!map->reserved_nodes)
return (free(map->weights), free(map->nodes), free(map), NULL);
return (map);
}

View file

@ -0,0 +1,72 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 17:32:38 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:26:38 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include "ft_list.h"
#include "ft_map.h"
#include "ft_vector.h"
#include <stdlib.h>
static t_data_apply singleton_custom_destroy(const t_data_apply custom_destroy,
bool set)
{
static t_data_apply f_ptr = NULL;
if (set == true)
f_ptr = custom_destroy;
return (f_ptr);
}
static void wrapper_destroy(void *restrict data)
{
t_data_apply destroy;
t_map_node *map_node;
destroy = singleton_custom_destroy(NULL, false);
map_node = (t_map_node *)data;
if (destroy)
(*destroy)(map_node->data);
free(map_node);
}
void ft_map_destroy(t_map *map)
{
ft_map_destroy_free(map, NULL);
}
static void rsv_del(void *data)
{
t_list *node;
node = (t_list *)data;
if (node && node->data)
wrapper_destroy(node->data);
free(node);
}
void ft_map_destroy_free(t_map *map, t_data_apply free_fun)
{
size_t i;
singleton_custom_destroy(free_fun, true);
i = 0;
while (i < map->capacity)
{
ft_ll_clear(&map->nodes[i++], wrapper_destroy);
}
free(map->nodes);
free(map->weights);
ft_vec_apply(map->reserved_nodes, rsv_del);
ft_vec_destroy(&map->reserved_nodes);
free(map);
singleton_custom_destroy(NULL, true);
}

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:05:52 by bgoulard #+# #+# */
/* Updated: 2024/06/01 11:54:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include <stdio.h>
t_map_node *ft_map_get_node(t_map *map, const void *key, size_t size)
{
t_list *bucket;
t_map_node *map_node;
bucket = map->nodes[map->hash(key, size) % map->capacity];
map_node = NULL;
while (bucket)
{
map_node = bucket->data;
if (map->cmp(map_node->key, key) == 0)
break ;
bucket = bucket->next;
}
if (map_node && map->cmp(map_node->key, key) == 0)
return (map_node);
return (NULL);
}
void *ft_map_get(t_map *map, const void *key, size_t size)
{
t_map_node *map_node;
map_node = ft_map_get_node(map, key, size);
if (!map_node)
return (NULL);
return (map_node->data);
}
size_t ft_map_size(const t_map *map)
{
return (map->w_total);
}
size_t ft_map_capacity(const t_map *map)
{
return (map->capacity);
}

View file

@ -0,0 +1,93 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_hash.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:07:04 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:18:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
// Default hash function provided by map
// Overflows are possible
// Default hash function for ft_map
// Very fast, but not very good
// see http://www.cse.yorku.ca/~oz/hash.html
// See ft_hash_sdbm or ft_hash_fnv1a for less collisions prone hash functions
size_t ft_hash_djb2(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
hash = 5381;
str = (unsigned char *)key;
while (size--)
hash = (hash << 5) + hash + str[size];
return (hash);
}
// Better hash function
// Slower, but better
// see http://www.cse.yorku.ca/~oz/hash.html
// overal better than djb2 but default hash function stays djb2 because it's
// faster and easier to understand, plus it's not that bad... right ?
size_t ft_hash_sdbm(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
hash = 0;
str = (unsigned char *)key;
while (size--)
hash = *str++ + (hash << 6) + (hash << 16) - hash;
return (hash);
}
// Another hash function
// Slower, but better
// see
// https://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
//
// 64 bits version check is just that the size of size_t impact the starting
// hash value to lower the number of collisions otherwise we use the 32 bits
// version.
// lets hope that 128 bits computers will not be a thing...
//
// note: djb2 and sdbm are 32 bits hash functions and are not impacted by
// the size of size_t
size_t ft_hash_fnv1a(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
if (sizeof(size_t) == 8)
hash = 0xcbf29ce484222325;
else
hash = 0x811c9dc5;
str = (unsigned char *)key;
while (size--)
hash = (*str++ ^ hash) * 0x01000193;
return (hash);
}
// dumbest hash function ever
// just for testing purposes when you don't care about collisions
// or when you want to test your map with a hash function that doesn't
// overflow at all ever
//
size_t ft_hash_dummy(const void *key, size_t size)
{
unsigned char *str;
size_t hash;
str = (unsigned char *)key;
hash = 0;
while (size--)
hash += *str++;
return (hash);
}

View file

@ -0,0 +1,46 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_remove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 18:08:04 by bgoulard #+# #+# */
/* Updated: 2024/06/01 11:56:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_vector.h"
#include "ft_list_types.h"
#include <stdio.h>
void *ft_map_remove(t_map *map, const void *key, size_t size)
{
size_t hash;
t_list *prev;
t_list *cur;
hash = map->hash(key, size) % map->capacity;
prev = NULL;
cur = map->nodes[hash];
while (cur)
{
if (map->cmp(((t_map_node *)cur->data)->key, key) == 0)
break ;
prev = cur;
cur = cur->next;
}
if (!cur)
return (NULL);
if (!prev)
map->nodes[hash] = cur->next;
else
prev->next = cur->next;
cur->next = NULL;
ft_vec_add(&map->reserved_nodes, cur);
map->weights[hash]--;
map->w_total--;
return (((t_map_node *)cur->data)->data);
}

View file

@ -0,0 +1,67 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_map_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/11 17:36:14 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:16:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdio.h>
#include <stdlib.h>
#include "ft_list.h"
#include "ft_map.h"
#include "ft_map_types.h"
#include "ft_string.h"
#include "ft_vector.h"
static void setup_map_node(t_map_node *map_node, const void *key,
const void *value)
{
map_node->data = (void *)value;
map_node->key = key;
map_node->hash = 0;
}
// works but dog shit doesnt check all alloc faillure cases
//
bool ft_map_set(t_map *map, const void *key, const void *value, size_t size)
{
t_map_node *map_node;
t_list *reuse_node;
map_node = ft_map_get_node(map, key, size);
reuse_node = NULL;
if (map_node)
return (map_node->data = (void *)value, true);
if (map->reserved_nodes->count)
reuse_node = ft_vec_pop(map->reserved_nodes);
else
map_node = ft_malloc(sizeof(t_map_node));
if (!map_node && !reuse_node)
return (false);
if (reuse_node)
map_node = reuse_node->data;
else
reuse_node = ft_ll_create(map_node);
setup_map_node(map_node, key, value);
map_node->hash = map->hash(key, size);
ft_ll_add_front(&map->nodes[map_node->hash % map->capacity], reuse_node);
map->weights[map_node->hash % map->capacity]++;
map->w_total++;
return (true);
}
void ft_map_set_cmp(t_map *map, t_data_cmp cmp)
{
map->cmp = cmp;
}
void ft_map_set_hash(t_map *map, t_memhash hash)
{
map->hash = hash;
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_abs.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:10:52 by bgoulard #+# #+# */
/* Updated: 2024/05/26 10:21:20 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_abs(int x)
{
if (x < 0)
return (-x);
return (x);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_align.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/26 19:51:21 by bgoulard #+# #+# */
/* Updated: 2024/06/26 20:46:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_align_2(size_t size, size_t alignment)
{
return ((size + alignment - 1) & ~(alignment - 1));
}
size_t ft_align(size_t size, size_t alignment)
{
if (alignment < 2)
return (size);
if (size % alignment == 0)
return (size);
size += alignment - size % alignment;
return (size);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_torange.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/16 14:51:08 by bgoulard #+# #+# */
/* Updated: 2024/05/16 15:04:23 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_clamp(int value, int min, int max)
{
if (value < min)
return (min);
else if (value > max)
return (max);
return (value);
}
float ft_clamp_f(float value, float min, float max)
{
if (value < min)
return (min);
else if (value > max)
return (max);
return (value);
}
double ft_clamp_d(double value, double min, double max)
{
if (value < min)
return (min);
else if (value > max)
return (max);
return (value);
}

View file

@ -0,0 +1,35 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_complex.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/01 08:11:12 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:39:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_math.h"
double ft_complex_abs(t_complex nb)
{
return (ft_sqrt(nb.real * nb.real + nb.imaginary * nb.imaginary));
}
t_complex ft_complex_addl(t_complex nb, long factor)
{
return ((t_complex){.real = nb.real + (double)factor,
.imaginary = nb.imaginary + (double)factor});
}
t_complex ft_complex_mull(t_complex nb, long factor)
{
return (ft_complex_muld(nb, (double)factor));
}
t_complex ft_complex_muld(t_complex nb, double factor)
{
return ((t_complex){.real = nb.real * factor,
.imaginary = nb.imaginary * factor});
}

View file

@ -0,0 +1,49 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_intrange.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/29 16:22:01 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:41:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_math.h"
int ft_range(int x, int min, int max, int new_max)
{
int res;
if (max == min || new_max == 0 || min >= max)
return (0);
if (x <= min)
return (0);
if (x >= max)
return (new_max);
res = x - min;
max -= min;
return ((int)(((double)res / max) * new_max));
}
float ft_range_f(float x, float min, float max, float new_max)
{
return ((float)ft_range_d((double)x, (double)min, (double)max, \
(double)new_max));
}
double ft_range_d(double x, double min, double max, double new_max)
{
double res;
if (max == min || new_max == 0 || min >= max)
return (0);
if (x <= min)
return (0);
if (x >= max)
return (new_max);
res = x - min;
max -= min;
return ((res / max) * new_max);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_log.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 10:09:57 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:27:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_ullogof(unsigned long long nbr, int base)
{
const unsigned long long bul = (unsigned long long)base;
int pow;
pow = -1;
while (nbr)
{
nbr /= bul;
pow++;
}
return (pow);
}
int ft_llogof(long long nbr, int base)
{
if (nbr <= 0)
return (-1);
return (ft_ullogof((unsigned long long)nbr, base));
}
int ft_logof(int nbr, int base)
{
if (nbr <= 0)
return (-1);
return (ft_llogof((long long)nbr, base));
}
int ft_log(int nbr)
{
return (ft_logof(nbr, 10));
}

View file

@ -0,0 +1,25 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_minmax.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/23 10:23:07 by bgoulard #+# #+# */
/* Updated: 2023/11/23 10:35:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_min(int a, int b)
{
if (a < b)
return (a);
return (b);
}
int ft_max(int a, int b)
{
if (a > b)
return (a);
return (b);
}

View file

@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pow.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:04:24 by bgoulard #+# #+# */
/* Updated: 2024/05/24 01:01:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_pow(size_t x, size_t y)
{
size_t res;
res = 1;
if (y == 0)
return (1);
if (x == 0)
return (0);
while (y > 0)
{
res *= x;
y--;
}
return (res);
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_round.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:10:18 by bgoulard #+# #+# */
/* Updated: 2024/05/23 23:10:36 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
double ft_round(double x)
{
double dec;
double round;
dec = x - (int)x;
if (dec >= 0.5)
round = (int)x + 1;
else
round = (int)x;
return (round);
}

View file

@ -0,0 +1,29 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_sqrt.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/23 23:00:06 by bgoulard #+# #+# */
/* Updated: 2024/05/24 09:33:30 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
// using newton's method
double ft_sqrt(double nb)
{
double x;
double y;
x = nb;
if (nb <= 0)
return (-1);
y = 1;
while (x - y > 0.0000001)
{
x = (x + y) / 2;
y = nb / x;
}
return (x);
}

View file

@ -0,0 +1,44 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_chain.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:26 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:04:29 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
#include <stdbool.h>
bool ft_optional_chain(t_optional *opt, const t_data_tr_i *f)
{
size_t i;
if (!f)
return (false);
i = 0;
while (f[i] && opt->pres != OPT_NONE)
{
opt->val = f[i](opt->val);
if (!opt->val)
opt->pres = OPT_NONE;
i++;
}
if (opt->pres == OPT_NONE)
return (false);
return (true);
}
t_optional ft_optional_map(t_optional *opt, void *(**f)(void *))
{
t_optional ret;
ft_optional_copy(&ret, opt);
if (ret.pres == OPT_NONE || !f)
return (ret);
ft_optional_chain(&ret, f);
return (ret);
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_copy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:40 by bgoulard #+# #+# */
/* Updated: 2024/05/18 15:39:27 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
void ft_optional_copy(t_optional *dest, t_optional *src)
{
dest->pres = src->pres;
dest->val = src->val;
}

View file

@ -0,0 +1,24 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:49 by bgoulard #+# #+# */
/* Updated: 2024/05/18 15:39:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
#include <stdbool.h>
#include <stdlib.h>
bool ft_optional_destroy(t_optional *opt)
{
if (!opt)
return (false);
if (opt->pres == OPT_NONE)
return (free(opt), true);
return (false);
}

View file

@ -0,0 +1,53 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:06:55 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:15:03 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
#include "ft_string.h"
#include <stdlib.h>
t_optional *ft_optional_new(void)
{
t_optional *elem;
elem = ft_malloc(sizeof(*elem));
if (!elem)
return (NULL);
elem->pres = OPT_NONE;
elem->val = NULL;
return (elem);
}
t_optional *ft_optional_from_val(void *ptr)
{
t_optional *elem;
elem = ft_optional_new();
if (!elem)
return (elem);
elem->pres = OPT_SOME;
elem->val = ptr;
return (elem);
}
// As this function can fail it should never be called,
// the type t_optional is supposed to be ultra reliable.
// Carefull use is preconized.
t_optional *ft_optional_dup(t_optional *org)
{
t_optional *ret;
ret = ft_optional_new();
if (!ret)
return (NULL);
ft_optional_copy(ret, org);
return (ret);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_optional_unwrap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/01/02 18:07:00 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:28:55 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_optional.h"
void *ft_optional_unwrap(t_optional opt)
{
if (opt.pres != OPT_NONE)
return (opt.val);
return (NULL);
}

View file

@ -0,0 +1,52 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_cmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:58:24 by bgoulard #+# #+# */
/* Updated: 2024/07/06 16:52:21 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pair.h"
int ft_pair_cmp(t_pair *pair1, t_pair *pair2, t_data_cmp cmp)
{
if (!pair1 && !pair2)
return (0);
if (!pair1)
return (-(pair2 != NULL));
if (!pair2)
return (pair1 != NULL);
if (cmp)
return (cmp(pair1, pair2));
return (pair1 - pair2);
}
int ft_pair_cmp_first(t_pair *pair1, t_pair *pair2, t_data_cmp cmp)
{
if (!pair1 && !pair2)
return (0);
if (!pair1)
return (-(pair2->first != NULL));
if (!pair2)
return (pair1->first != NULL);
if (cmp)
return (cmp(pair1->first, pair2->first));
return (pair1->first - pair2->first);
}
int ft_pair_cmp_second(t_pair *pair1, t_pair *pair2, t_data_cmp cmp)
{
if (!pair1 && !pair2)
return (0);
if (!pair1)
return (-(pair2->second != NULL));
if (!pair2)
return (pair1->second != NULL);
if (cmp)
return (cmp(pair1->second, pair2->second));
return (pair1->second - pair2->second);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_destroy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 23:04:56 by bgoulard #+# #+# */
/* Updated: 2024/07/06 17:13:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include "ft_pair_types.h"
#include "ft_string.h"
void ft_pair_destroy(t_pair **pair, t_data_apply del_f, t_data_apply del_s)
{
if (!pair)
return ;
if (del_f)
del_f((*pair)->first);
if (del_s)
del_s((*pair)->second);
ft_free((void **)pair);
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_get.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:57:05 by bgoulard #+# #+# */
/* Updated: 2024/07/06 16:30:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pair_types.h"
#include <stddef.h>
void *ft_pair_first(t_pair *pair)
{
if (!pair)
return (NULL);
return (pair->first);
}
void *ft_pair_second(t_pair *pair)
{
if (!pair)
return (NULL);
return (pair->second);
}

View file

@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_new.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:28:49 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:15:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pair.h"
#include "ft_pair_types.h"
#include "ft_string.h"
#include <stdlib.h>
t_pair *ft_pair_new(void *a, void *b)
{
t_pair *ret;
ret = ft_malloc(sizeof(*ret));
if (!ret)
return (NULL);
return (ft_pair_set(ret, a, b), ret);
}

View file

@ -0,0 +1,22 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_pair_set.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/23 22:55:07 by bgoulard #+# #+# */
/* Updated: 2024/06/24 00:10:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_pair_types.h"
#include "ft_pair.h"
void ft_pair_set(t_pair *pair, void *first, void *second)
{
if (!pair)
return ;
pair->first = first;
pair->second = second;
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalnum.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:19:44 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:57:10 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_isalnum(int c)
{
return (ft_isalpha(c) || ft_isdigit(c));
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isalpha.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:13:03 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:57:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_isalpha(int c)
{
return (ft_islower(c) || ft_isupper(c));
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isascii.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:23:34 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:57:19 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isascii(int c)
{
return (c >= 0 && c <= 127);
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:27:37 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:56:59 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isdigit(int c)
{
return (c >= '0' && c <= '9');
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_ishexdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:48:46 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:56:56 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_ishexdigit(int c)
{
return (ft_isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F'));
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_islower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:14:15 by bgoulard #+# #+# */
/* Updated: 2023/11/10 15:56:13 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_islower(int c)
{
return (c >= 'a' && c <= 'z');
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isoctdigit.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/21 15:52:06 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:56:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isoctdigit(int c)
{
return (c >= '0' && c <= '7');
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isprint.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:31:52 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:58:09 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isprint(int c)
{
return (c >= ' ' && c <= '~');
}

View file

@ -0,0 +1,17 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isspace.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 11:53:46 by bgoulard #+# #+# */
/* Updated: 2023/11/10 14:58:15 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isspace(int c)
{
return (c == ' ' || c == '\f' || c == '\n' \
|| c == '\r' || c == '\t' || c == '\v');
}

View file

@ -0,0 +1,16 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_isupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:42:20 by bgoulard #+# #+# */
/* Updated: 2023/11/10 15:34:21 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
int ft_isupper(int c)
{
return (c >= 'A' && c <= 'Z');
}

View file

@ -0,0 +1,19 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_putchar_fd.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 23:14:51 by bgoulard #+# #+# */
/* Updated: 2023/11/09 10:03:06 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <unistd.h>
void ft_putchar_fd(char c, int fd)
{
write(fd, &c, 1);
return ;
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_tolower.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:42:46 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:17:51 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_tolower(int c)
{
if (ft_isupper(c))
c += 32;
return (c);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_toupper.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 11:43:00 by bgoulard #+# #+# */
/* Updated: 2024/05/21 18:18:05 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_char.h"
int ft_toupper(int c)
{
if (ft_islower(c))
c -= 32;
return (c);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_apply_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/11 09:47:17 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:08:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include <stddef.h>
void ft_apply_2d(void **array, t_data_apply f)
{
size_t i;
i = 0;
while (array[i])
f(array[i++]);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_arena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 17:10:05 by bgoulard #+# #+# */
/* Updated: 2024/06/25 17:40:54 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void *ft_arena_alloc(size_t size)
{
return (ft_narena_alloc(size, 0));
}
void *ft_arena_realloc(void *ptr, size_t size)
{
return (ft_narena_realloc(ptr, size, 0));
}
void *ft_arena_calloc(size_t count, size_t size)
{
return (ft_narena_calloc(count, size, 0));
}
void ft_arena_free(void *ptr)
{
ft_narena_free(ptr, 0);
}

View file

@ -0,0 +1,50 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_bzero.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:05:47 by bgoulard #+# #+# */
/* Updated: 2024/06/26 19:48:45 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
static void byte_bzero(char *s, size_t n)
{
while (n--)
{
*s = 0;
s++;
}
}
static void word_bzero(size_t *s, size_t *n)
{
size_t word_count;
size_t i;
word_count = *n / sizeof(size_t);
i = 0;
while (i < word_count)
{
s[i] = 0;
i++;
}
*n %= sizeof(size_t);
}
void ft_bzero(void *s, size_t n)
{
if (!n || !s)
return ;
if (n < sizeof(size_t))
return (byte_bzero(s, n));
byte_bzero(s, (size_t)s % sizeof(size_t));
n -= (size_t)s % sizeof(size_t);
s += (size_t)s % sizeof(size_t);
word_bzero(s, &n);
byte_bzero(s, n);
}

View file

@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_calloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:28:30 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:20:53 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdint.h>
#include <stdlib.h>
#include "ft_string.h"
void *ft_calloc(size_t nmemb, size_t weight)
{
void *ret;
size_t tot;
if (nmemb == 0 || weight == 0)
return (ft_malloc(0));
if (nmemb > SIZE_MAX / weight)
return (NULL);
tot = nmemb * weight;
ret = ft_malloc(tot);
if (!ret)
return (NULL);
return (ft_memset(ret, 0, tot));
}

View file

@ -0,0 +1,45 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_fd_to_buff.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/18 19:52:02 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:46:24 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <fcntl.h>
#include <unistd.h>
#include "ft_string.h"
#include "ft_string_types.h"
char *ft_fd_to_buff(int fd)
{
char buff[BUFFER_SIZE + 1];
char *file;
char *prev;
ssize_t ret;
if (fd == -1)
return (NULL);
file = NULL;
ret = read(fd, buff, BUFFER_SIZE);
if (ret == -1)
return (NULL);
buff[ret] = '\0';
file = ft_strdup(buff);
while (ret == BUFFER_SIZE)
{
ret = read(fd, buff, BUFFER_SIZE);
if (ret == -1)
return (ft_free((void **)&file), NULL);
buff[ret] = '\0';
prev = file;
file = ft_strjoin(file, buff);
ft_free((void **)&prev);
}
return (file);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/05 10:18:13 by bgoulard #+# #+# */
/* Updated: 2024/05/27 09:26:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
void ft_free(void **ptr)
{
if (!ptr || !*ptr)
return ;
free(*ptr);
*ptr = NULL;
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_free_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:32:28 by bgoulard #+# #+# */
/* Updated: 2024/05/28 23:47:22 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
void ft_free_2d(void **arr)
{
ft_apply_2d(arr, free);
free(arr);
}

View file

@ -0,0 +1,23 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_len_2d.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/09 09:41:42 by bgoulard #+# #+# */
/* Updated: 2024/06/23 18:22:57 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
size_t ft_len_2d(const void *const *array)
{
size_t len;
len = 0;
while (array[len])
len++;
return (len);
}

View file

@ -0,0 +1,21 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_malloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/07/19 18:20:32 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:20:49 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stdlib.h>
#include "ft_string.h"
void *ft_malloc(size_t size)
{
return (malloc(size));
}

View file

@ -0,0 +1,120 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memchr.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:15:12 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:19:54 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
static void *sword_memchr(const void *s, int c, size_t n)
{
char *casted;
size_t i;
const char target = (char)c;
casted = (char *)s;
i = 0;
while (n != i)
{
if (casted[i++] == target)
return (casted + i - 1);
}
return (NULL);
}
/*
** we check if the size of size_t is 8 bytes by checkning the platform
** we are on
** if yes we use the 1st function else we are on the 32 bit platform
** so use the second version
*/
#ifdef __x86_64__
static void constr_bword(size_t *target, size_t *rp_one, size_t *rp_eight,
char c)
{
*target = (size_t)c;
*target |= *target << 8;
*target |= *target << 16;
*target |= *target << 32;
*rp_one = 0x0101010101010101;
*rp_eight = 0x8080808080808080;
}
#else
static void constr_bword(size_t *target, size_t *rp_one, size_t *rp_eight,
char c)
{
*target = (size_t)c;
*target |= *target << 8;
*target |= *target << 16;
*rp_one = 0x0101010101010101;
*rp_eight = 0x8080808080808080;
}
#endif
/*
** *casted ^ target : XOR between the target and the casted value to leave a
** segment of bits where if the target is present, the bits will be 0.
** ((*casted ^ target) - 0x0101010101010101) : XOR result - 0x0101010101010101
** remove 1 to the said bits. if target is there the result will be FF.
** ~((casted ^ target) : XOR result inverted aka 0x00 -> 0xFF and 0xFF -> 0x00
** Remember if target is present on xor segment the res is 0 so ~ will be 0xFF
** we effectively have a mask where the target is present.
** ((*casted ^ target) - repeated_1) & ~((*casted ^ target) :
** XOR result - repeated_1 & ~XOR result
** xor result gets us to 0 where the target is
** -repeated_1 will get us to FF where the target is
** ~XOR result will get us to FF where the target is
** & between the two will get us to FF where the target is
** to filter the match we only keep the 8th bit of the result
** if the 8th bit is different from 0 we have a match in the section.
** when match send to sword_memchr to find the exact position of the target.
**
** For more infos check ft_strlen.c it is the same principle but simpler
** as we are looking directly for a 0x00.
*/
static void *bword_memchr(const void *s, int c, size_t n)
{
size_t *casted;
size_t target;
size_t rp_one;
size_t rp_eight;
casted = (size_t *)s;
constr_bword(&target, &rp_one, &rp_eight, c);
while (n > sizeof(size_t))
{
if ((((*casted ^ target) - rp_one) & ~((*casted ^ target) & rp_eight)))
return (sword_memchr(casted, c, sizeof(size_t)));
casted++;
n -= sizeof(size_t);
}
if (n)
return (sword_memchr(casted, c, n));
return (NULL);
}
void *ft_memchr(const void *s, int c, size_t n)
{
char *res;
if (n < sizeof(n))
return (sword_memchr(s, c, n));
res = sword_memchr(s, c, (size_t)s % sizeof(size_t));
if (res)
return (res);
res = (char *)s + ((size_t)s % sizeof(size_t));
n -= (size_t)s % sizeof(size_t);
return (bword_memchr(res, c, n));
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcmp.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:11:32 by bgoulard #+# #+# */
/* Updated: 2023/11/10 15:59:31 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
int ft_memcmp(const void *s1, const void *s2, size_t n)
{
size_t index;
index = 0;
if (s1 == s2 || n == 0)
return (0);
while (index != n && ((const unsigned char *)s1)[index] == \
((const unsigned char *)s2)[index])
index++;
if (index == n)
index--;
return (((unsigned char *)s1)[index] - ((unsigned char *)s2)[index]);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memcpy.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 13:48:06 by bgoulard #+# #+# */
/* Updated: 2024/06/25 22:57:46 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void *ft_memcpy(void *dest, const void *src, size_t n)
{
return (ft_memmove(dest, src, n));
}

View file

@ -0,0 +1,36 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/27 18:14:50 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:15:58 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_defs.h"
#include "ft_string.h"
#include <stddef.h>
#include <stdlib.h>
void **ft_memmap(void **src, size_t nb_e, t_data_tr f)
{
void **dst;
size_t i;
if (!src || !f || !nb_e)
return (NULL);
dst = ft_malloc((nb_e + 1) * sizeof(void *));
if (!dst)
return (NULL);
dst[nb_e] = NULL;
i = 0;
while (i < nb_e)
{
dst[i] = f(src[i]);
i++;
}
return (dst);
}

View file

@ -0,0 +1,116 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memmove.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 14:14:49 by bgoulard #+# #+# */
/* Updated: 2024/07/11 11:59:25 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
#include <stdio.h>
static void *byte_memcpy(void *dest, const void *src, size_t n)
{
size_t i;
i = 0;
if (dest > src)
while (n--)
((unsigned char *)dest)[n] = ((unsigned char *)src)[n];
else
{
while (i < n)
{
((unsigned char *)dest)[i] = ((unsigned char *)src)[i];
i++;
}
}
return (dest);
}
static void word_memcpy(void *dest, const void *src, size_t *n)
{
size_t *casted_dest;
size_t *casted_src;
size_t word_count;
size_t i;
if (*n < sizeof(size_t))
return ;
casted_dest = (size_t *)dest;
casted_src = (size_t *)src;
word_count = *n / sizeof(size_t);
i = 0;
if (dest < src)
{
while (i++ < word_count)
casted_dest[i - 1] = casted_src[i - 1];
}
else
{
i = word_count;
while (i--)
casted_dest[i] = casted_src[i];
}
*n %= sizeof(size_t);
}
/*
* If src is not aligned then we copy byte by byte otherwise
* the compiler will need to align each time we pull from src
*/
static void *ft_memmove_forward(void *dest, const void *src, size_t n)
{
const void *ret = dest;
size_t align_offset;
size_t prev_n;
if ((size_t)src % sizeof(size_t))
return (byte_memcpy(dest, src, n));
align_offset = (size_t)dest % sizeof(size_t);
byte_memcpy(dest, src, align_offset);
n -= align_offset;
src += align_offset;
dest += align_offset;
prev_n = n;
word_memcpy(dest, src, &n);
dest += prev_n - n;
src += prev_n - n;
if (n)
byte_memcpy(dest, src, n);
return ((void *)ret);
}
static void *ft_memmove_backward(void *dest, const void *src, size_t n)
{
const void *ret = dest;
size_t align_offset;
if ((size_t)src % sizeof(size_t))
return (byte_memcpy(dest, src, n));
align_offset = (size_t)(dest + n) % sizeof(size_t);
n -= align_offset;
byte_memcpy(dest + n, src + n, align_offset);
word_memcpy(dest, src, &n);
if (n)
byte_memcpy(dest, src, n);
return ((void *)ret);
}
void *ft_memmove(void *dest, const void *src, size_t n)
{
const void *ret = dest;
if (dest == src || !n || !dest || !src)
return ((void *)ret);
if (n < sizeof(size_t))
return (byte_memcpy(dest, src, n));
if (dest < src)
return (ft_memmove_forward(dest, src, n));
return (ft_memmove_backward(dest, src, n));
}

View file

@ -0,0 +1,28 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_memset.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/06 12:07:20 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:48:32 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include <stddef.h>
void *ft_memset(void *s, int c, size_t n)
{
char *casted;
casted = (char *)s;
if (!n)
return (s);
while (n--)
{
*casted = (char)c;
casted++;
}
return (s);
}

View file

@ -0,0 +1,76 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_narena.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/06/25 17:41:16 by bgoulard #+# #+# */
/* Updated: 2024/06/30 18:06:29 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_list.h"
#include "ft_list_types.h"
#include "ft_math.h"
#include <stddef.h>
#include <stdlib.h>
#define FT_NARENAS_MAX 16
#define FT_AREA_BLOCK_SIZE 4096
typedef struct s_arena
{
void *block_origin;
void *block_current;
void *block_end;
} t_arena;
t_list *ft_arena_get(int n)
{
static t_list arena_array[FT_NARENAS_MAX] = {0};
if (n < 0 || n >= FT_NARENAS_MAX)
return (NULL);
return (&arena_array[n]);
}
bool ft_arena_create_handler(t_list *ar_ptr, size_t size)
{
t_arena *arena;
size = ft_align_2(size + sizeof(t_arena), FT_AREA_BLOCK_SIZE);
arena = malloc(size);
if (!arena)
return (false);
arena->block_origin = arena + 1;
arena->block_current = arena->block_origin;
arena->block_end = (void *)arena + size;
ar_ptr->data = arena;
return (true);
}
void *ft_arena_alloc(size_t size, int ar_nb)
{
t_list *ar_list;
t_arena *arena;
void *ret;
size = ft_align_2(size, FT_AREA_BLOCK_SIZE);
ar_list = ft_arena_get(ar_nb);
if (!ar_list)
return (NULL);
arena = ft_ll_end(ar_list)->data;
if (!arena && ft_arena_create_handler(ar_list, size) == false)
return (NULL);
// create new block handler - if fail ret null
if (arena->block_origin == NULL || \
arena->block_current + size > arena->block_end)
{
// allocate a new block
;
}
ret = arena->block_current;
arena->block_current += size;
return (ret);
}

View file

@ -0,0 +1,42 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_qsort.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 11:31:15 by bgoulard #+# #+# */
/* Updated: 2023/12/30 12:37:00 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
void ft_qsort(void *array, size_t nmb, size_t size, \
int (*cmp)(const void *, const void *))
{
size_t i;
size_t j;
size_t pivot;
if (nmb < 2)
return ;
i = 0;
j = nmb - 1;
pivot = nmb / 2;
while (i < j)
{
while (cmp(array + (i * size), array + (pivot * size)) < 0)
i++;
while (cmp(array + (j * size), array + (pivot * size)) > 0)
j--;
if (i < j)
{
ft_swap(array + (i * size), array + (j * size));
if (i == pivot || j == pivot)
pivot = (i == pivot) * i + (j == pivot) * j;
}
}
ft_qsort(array, pivot, size, cmp);
ft_qsort(array + ((pivot + 1) * size), nmb - pivot - 1, size, cmp);
}

View file

@ -0,0 +1,33 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_realloc.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:27:54 by bgoulard #+# #+# */
/* Updated: 2024/07/19 18:16:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include <stdlib.h>
// direct call to ft_malloc as ft_bzero is not needed
// >> we are copying the content of ptr to new
void *ft_realloc(void *ptr, size_t sizeNew, size_t sizeOld)
{
void *new;
if (sizeNew == sizeOld)
return (ptr);
new = ft_malloc(sizeNew);
if (!new)
return (NULL);
if (sizeNew < sizeOld)
ft_memcpy(new, ptr, sizeNew);
else
ft_memcpy(new, ptr, sizeOld);
free(ptr);
return (new);
}

View file

@ -0,0 +1,20 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_swap.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/12 11:34:29 by bgoulard #+# #+# */
/* Updated: 2023/12/12 11:39:33 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
void ft_swap(void **a, void **b)
{
void *tmp;
tmp = *a;
*a = *b;
*b = tmp;
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atof.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/04 11:09:02 by bgoulard #+# #+# */
/* Updated: 2024/05/26 16:37:16 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_char.h"
static int loc_forward(const char *str, int *i)
{
bool neg;
neg = false;
*i = 0;
while (ft_isspace(str[*i]) || str[*i] == '-' || str[*i] == '+' || \
ft_isdigit(str[*i]))
{
if (str[*i] == '-')
neg = true;
(*i)++;
}
return (neg);
}
double ft_atof(const char *str)
{
int whole;
bool neg;
double decimal;
int i;
int j;
j = 1;
decimal = 0;
whole = ft_atoi(str);
neg = loc_forward(str, &i);
if (str[i] == '.')
{
i++;
while (ft_isdigit(str[i]))
{
decimal = decimal * 10 + str[i] - '0';
i++;
j *= 10;
}
}
if (neg == true)
decimal *= -1;
return ((double)whole + (double)decimal / j);
}

View file

@ -0,0 +1,38 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/07 20:05:24 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:36:45 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_char.h"
int ft_atoi(const char *str)
{
int ret;
int sign;
size_t offset;
ret = 0;
offset = 0;
sign = 0;
while (ft_isspace(str[offset]))
offset++;
while (str[offset] == '+' || str[offset] == '-')
{
if (str[offset] == '-')
sign++;
offset++;
}
while (ft_isdigit(str[offset]))
ret = ret * 10 + str[offset++] - '0';
if (sign % 2 == 1)
return (-ret);
return (ret);
}

View file

@ -0,0 +1,57 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_atoi_base.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/12/09 17:52:58 by bgoulard #+# #+# */
/* Updated: 2024/06/13 16:54:39 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
#include "ft_char.h"
static int ft_is_valid(const char *base)
{
int i;
int j;
i = 0;
if (ft_strlen(base) < 2)
return (0);
while (base[i])
{
if (base[i] == '+' || base[i] == '-' || ft_isspace(base[i]))
return (0);
j = i + 1;
while (base[j])
if (base[i] == base[j++])
return (0);
i++;
}
return (1);
}
int ft_atoi_base(const char *str, const char *base)
{
const size_t base_len = ft_strlen(base);
int i;
int nb;
int sign;
i = 0;
nb = 0;
sign = 1;
if (!ft_is_valid(base))
return (0);
while (ft_isspace(str[i]))
i++;
while (str[i] == '+' || str[i] == '-')
if (str[i++] == '-')
sign *= -1;
while (str[i] && ft_strchr(base, str[i]))
nb = nb * base_len + ft_strchr(base, str[i++]) - base;
return (nb * sign);
}

View file

@ -0,0 +1,18 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_itoa.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: bgoulard <bgoulard@student.42.fr> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2023/11/09 09:20:28 by bgoulard #+# #+# */
/* Updated: 2024/05/21 17:37:11 by bgoulard ### ########.fr */
/* */
/* ************************************************************************** */
#include "ft_string.h"
char *ft_itoa(int nbr)
{
return (ft_itoa_base(nbr, "0123456789"));
}

Some files were not shown because too many files have changed in this diff Show more