removing the libft of rparodi
This commit is contained in:
parent
0391323626
commit
be6038dcc8
523 changed files with 724 additions and 3336 deletions
34
libft/src/ft_map/ft_map_clear.c
Normal file
34
libft/src/ft_map/ft_map_clear.c
Normal 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;
|
||||
}
|
||||
38
libft/src/ft_map/ft_map_create.c
Normal file
38
libft/src/ft_map/ft_map_create.c
Normal 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);
|
||||
}
|
||||
72
libft/src/ft_map/ft_map_destroy.c
Normal file
72
libft/src/ft_map/ft_map_destroy.c
Normal 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);
|
||||
}
|
||||
53
libft/src/ft_map/ft_map_get.c
Normal file
53
libft/src/ft_map/ft_map_get.c
Normal 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);
|
||||
}
|
||||
93
libft/src/ft_map/ft_map_hash.c
Normal file
93
libft/src/ft_map/ft_map_hash.c
Normal 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);
|
||||
}
|
||||
46
libft/src/ft_map/ft_map_remove.c
Normal file
46
libft/src/ft_map/ft_map_remove.c
Normal 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);
|
||||
}
|
||||
67
libft/src/ft_map/ft_map_set.c
Normal file
67
libft/src/ft_map/ft_map_set.c
Normal 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;
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue