From bdefb3c7a5720247866f31c23ec7cff3f29f7ac9 Mon Sep 17 00:00:00 2001 From: Antoine Riard Date: Tue, 16 May 2017 21:41:09 +0200 Subject: [PATCH] hashtab v0 --- libft/includes/hashtab.h | 46 ++++++++++++++++++++++++++++++++ libft/srcs/htb/ft_hash_string.c | 34 +++++++++++++++++++++++ libft/srcs/htb/hashtab_destroy.c | 22 +++++++++++++++ libft/srcs/htb/hashtab_init.c | 22 +++++++++++++++ libft/srcs/htb/hashtab_insert.c | 26 ++++++++++++++++++ libft/srcs/htb/hashtab_lookup.c | 24 +++++++++++++++++ libft/srcs/htb/hashtab_print.c | 22 +++++++++++++++ libft/srcs/htb/hashtab_remove.c | 23 ++++++++++++++++ 8 files changed, 219 insertions(+) create mode 100644 libft/includes/hashtab.h create mode 100644 libft/srcs/htb/ft_hash_string.c create mode 100644 libft/srcs/htb/hashtab_destroy.c create mode 100644 libft/srcs/htb/hashtab_init.c create mode 100644 libft/srcs/htb/hashtab_insert.c create mode 100644 libft/srcs/htb/hashtab_lookup.c create mode 100644 libft/srcs/htb/hashtab_print.c create mode 100644 libft/srcs/htb/hashtab_remove.c diff --git a/libft/includes/hashtab.h b/libft/includes/hashtab.h new file mode 100644 index 00000000..0e84bf70 --- /dev/null +++ b/libft/includes/hashtab.h @@ -0,0 +1,46 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab.h :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/15 19:16:49 by ariard #+# #+# */ +/* Updated: 2017/05/16 21:03:50 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#ifndef HASHTAB_H +# define HASHTAB_H + +# include "libft.h" + +struct s_hashtab +{ + int capacity; + int size; + int (*hashfunc)(const void *key, int capacity); + struct s_list **head; +}; + +typedef struct s_hashtab t_hashtab; + +void hashtab_init(t_hashtab *htb, int capacity, + int (*hashfunc)(const void *key, int size)); + +int hashtab_insert(t_hashtab *htb, struct s_list *new, void *key, + int (*match)(const void *data_ref, const void *key)); + +struct s_list *hashtab_lookup(t_hashtab *htb, void *key, + int (*match)(const void *data_ref, const void *key)); + +struct s_list *hashtab_remove(t_hashtab *htb, void *key, + int (*match)(const void *data_ref, const void *key)); + +void hashtab_destroy(t_hashtab *htb, void (*destroy)()); + +void hashtab_print(t_hashtab *htb, int (*printer)()); + +int ft_hash_string(const void *key, int size); + +#endif diff --git a/libft/srcs/htb/ft_hash_string.c b/libft/srcs/htb/ft_hash_string.c new file mode 100644 index 00000000..55640299 --- /dev/null +++ b/libft/srcs/htb/ft_hash_string.c @@ -0,0 +1,34 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hash.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/01/02 16:35:24 by ariard #+# #+# */ +/* Updated: 2017/05/16 17:33:47 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "hashtab.h" + +int ft_hash_string(const void *key, int size) +{ + const char *ptr; + unsigned int tmp; + unsigned int val; + + val = 0; + ptr = key; + while (*ptr != '\0') + { + val = (val << 4) + (*ptr); + if ((tmp = (val & 0xf0000000))) + { + val = val ^ (tmp >> 24); + val = val ^ tmp; + } + ptr++; + } + return (val % size); +} diff --git a/libft/srcs/htb/hashtab_destroy.c b/libft/srcs/htb/hashtab_destroy.c new file mode 100644 index 00000000..0ab4bfc2 --- /dev/null +++ b/libft/srcs/htb/hashtab_destroy.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab_destroy.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/15 21:35:07 by ariard #+# #+# */ +/* Updated: 2017/05/16 20:42:05 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "hashtab.h" + +void hashtab_destroy(t_hashtab *htb, void (*destroy)()) +{ + int bucket; + + bucket = -1; + while (++bucket < htb->capacity) + ft_lstdel(&htb->head[bucket], destroy); +} diff --git a/libft/srcs/htb/hashtab_init.c b/libft/srcs/htb/hashtab_init.c new file mode 100644 index 00000000..1f6b6db7 --- /dev/null +++ b/libft/srcs/htb/hashtab_init.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab_init.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/15 19:13:06 by ariard #+# #+# */ +/* Updated: 2017/05/16 18:46:03 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "hashtab.h" + +void hashtab_init(t_hashtab *htb, int capacity, + int (*hashfunc)(const void *key, int capacity)) +{ + htb->head = (t_list **)ft_memalloc(capacity * sizeof(*htb->head)); + htb->capacity = capacity; + htb->size = 0; + htb->hashfunc = hashfunc; +} diff --git a/libft/srcs/htb/hashtab_insert.c b/libft/srcs/htb/hashtab_insert.c new file mode 100644 index 00000000..45d6e1a4 --- /dev/null +++ b/libft/srcs/htb/hashtab_insert.c @@ -0,0 +1,26 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab_insert.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/15 19:30:39 by ariard #+# #+# */ +/* Updated: 2017/05/16 21:05:19 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "hashtab.h" + +int hashtab_insert(t_hashtab *htb, t_list *new, void *key, + int (*match)(const void *data_ref, const void *key)) +{ + int bucket; + + if (hashtab_lookup(htb, key, match)) + return (-1); + bucket = htb->hashfunc(key, htb->capacity); + ft_lsteadd(&htb->head[bucket], new); + htb->size++; + return (bucket); +} diff --git a/libft/srcs/htb/hashtab_lookup.c b/libft/srcs/htb/hashtab_lookup.c new file mode 100644 index 00000000..99b6eb5f --- /dev/null +++ b/libft/srcs/htb/hashtab_lookup.c @@ -0,0 +1,24 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab_lookup.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/15 19:38:41 by ariard #+# #+# */ +/* Updated: 2017/05/16 21:34:20 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *hashtab_lookup(t_hashtab *htb, void *key, + int (*match)(const void *data_ref, const void *key)) + +{ + int bucket; + + if ((bucket = htb->hashfunc(key, htb->capacity))) + return (ft_lst_find(htb->head[bucket], key, match)); + return (NULL); +} diff --git a/libft/srcs/htb/hashtab_print.c b/libft/srcs/htb/hashtab_print.c new file mode 100644 index 00000000..c6d5498b --- /dev/null +++ b/libft/srcs/htb/hashtab_print.c @@ -0,0 +1,22 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab_print.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/16 20:38:14 by ariard #+# #+# */ +/* Updated: 2017/05/16 21:03:23 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +void hashtab_print(t_hashtab *htb, int (*printer)()) +{ + int bucket; + + bucket = -1; + while (++bucket != htb->capacity) + ft_lstiter(htb->head[bucket], printer, NULL); +} diff --git a/libft/srcs/htb/hashtab_remove.c b/libft/srcs/htb/hashtab_remove.c new file mode 100644 index 00000000..d21679b7 --- /dev/null +++ b/libft/srcs/htb/hashtab_remove.c @@ -0,0 +1,23 @@ +/* ************************************************************************** */ +/* */ +/* ::: :::::::: */ +/* hashtab_remove.c :+: :+: :+: */ +/* +:+ +:+ +:+ */ +/* By: ariard +#+ +:+ +#+ */ +/* +#+#+#+#+#+ +#+ */ +/* Created: 2017/05/15 19:46:48 by ariard #+# #+# */ +/* Updated: 2017/05/16 17:34:56 by ariard ### ########.fr */ +/* */ +/* ************************************************************************** */ + +#include "libft.h" + +t_list *hashtab_remove(t_hashtab *htb, void *key, + int (*match)(const void *data_ref, const void *key)) +{ + t_list *data; + + if ((data = hashtab_lookup(htb, key, match))) + return(ft_lst_removeif(&data, key, match)); + return (NULL); +}