hashtab v0
This commit is contained in:
parent
df11d70ab4
commit
bdefb3c7a5
8 changed files with 219 additions and 0 deletions
46
libft/includes/hashtab.h
Normal file
46
libft/includes/hashtab.h
Normal file
|
|
@ -0,0 +1,46 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab.h :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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
|
||||||
34
libft/srcs/htb/ft_hash_string.c
Normal file
34
libft/srcs/htb/ft_hash_string.c
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hash.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <marvin@42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
22
libft/srcs/htb/hashtab_destroy.c
Normal file
22
libft/srcs/htb/hashtab_destroy.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab_destroy.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
22
libft/srcs/htb/hashtab_init.c
Normal file
22
libft/srcs/htb/hashtab_init.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab_init.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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;
|
||||||
|
}
|
||||||
26
libft/srcs/htb/hashtab_insert.c
Normal file
26
libft/srcs/htb/hashtab_insert.c
Normal file
|
|
@ -0,0 +1,26 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab_insert.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
24
libft/srcs/htb/hashtab_lookup.c
Normal file
24
libft/srcs/htb/hashtab_lookup.c
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab_lookup.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
22
libft/srcs/htb/hashtab_print.c
Normal file
22
libft/srcs/htb/hashtab_print.c
Normal file
|
|
@ -0,0 +1,22 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab_print.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
23
libft/srcs/htb/hashtab_remove.c
Normal file
23
libft/srcs/htb/hashtab_remove.c
Normal file
|
|
@ -0,0 +1,23 @@
|
||||||
|
/* ************************************************************************** */
|
||||||
|
/* */
|
||||||
|
/* ::: :::::::: */
|
||||||
|
/* hashtab_remove.c :+: :+: :+: */
|
||||||
|
/* +:+ +:+ +:+ */
|
||||||
|
/* By: ariard <ariard@student.42.fr> +#+ +:+ +#+ */
|
||||||
|
/* +#+#+#+#+#+ +#+ */
|
||||||
|
/* 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);
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue