diff options
| -rw-r--r-- | .libftignore | 1 | ||||
| -rw-r--r-- | Makefile | 6 | ||||
| -rw-r--r-- | include/libft_ht.h | 10 | ||||
| -rw-r--r-- | include/libft_lst.h | 2 | ||||
| -rw-r--r-- | src/ht/ft_htdelone.c | 4 | ||||
| -rw-r--r-- | src/ht/ft_htdelone_key.c | 3 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy.c | 3 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_all.c | 4 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_key.c | 3 | ||||
| -rw-r--r-- | src/ht/ft_htdestroy_value.c | 25 | ||||
| -rw-r--r-- | src/ht/ft_htget.c | 4 | ||||
| -rw-r--r-- | src/ht/ft_hthash.c | 6 | ||||
| -rw-r--r-- | src/ht/ft_htnew.c | 1 | ||||
| -rw-r--r-- | src/ht/ft_htset.c | 5 |
14 files changed, 33 insertions, 44 deletions
diff --git a/.libftignore b/.libftignore index 7ec6e32..68d827a 100644 --- a/.libftignore +++ b/.libftignore @@ -1,2 +1 @@ *ft_printf* -*ft_ht* @@ -32,9 +32,9 @@ OBJ = $(SRC:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o) HEADER = $(shell find $(INCLUDE_DIR) -name "*.h") -all: make_build_dirs $(NAME) +all: prebuild $(NAME) -make_build_dirs: +prebuild: @for dir in $$(find $(SRC_DIR)/* $(FIND_ARGS) -type d | \ sed 's_$(SRC_DIR)/_$(OBJ_DIR)/_g'); \ do \ @@ -48,7 +48,7 @@ $(NAME): $(OBJ) $(HEADER) $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c @echo "Compiling: $@" - $(CC) $(CCFLAGS) -c -o $@ $< + @$(CC) $(CCFLAGS) -c -o $@ $< clean: @echo "Removing objects" diff --git a/include/libft_ht.h b/include/libft_ht.h index b890316..8dff87a 100644 --- a/include/libft_ht.h +++ b/include/libft_ht.h @@ -1,8 +1,9 @@ -#ifndef FT_HT -# define FT_HT +#ifndef LIBFT_HT +# define LIBFT_HT # include "libft.h" +# include "libft_lst.h" typedef struct { @@ -10,7 +11,7 @@ typedef struct void *value; } t_ftht_content; -typedef t_list* t_ftht_entry; +typedef t_ftlst* t_ftht_entry; typedef struct { @@ -21,11 +22,12 @@ typedef struct typedef t_ftuint t_ftht_digest; +t_ftht_digest ft_hthash(t_ftht *ht, char *key); + t_ftht *ft_htnew(t_ftsize size); void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)); void ft_htdestroy_all(t_ftht *ht); void ft_htdestroy_key(t_ftht *ht); -void ft_htdestroy_value(t_ftht *ht); t_ftht_content *ft_htget(t_ftht *ht, char *key); t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value); void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)); diff --git a/include/libft_lst.h b/include/libft_lst.h index 8f77233..b9475f9 100644 --- a/include/libft_lst.h +++ b/include/libft_lst.h @@ -26,5 +26,7 @@ void ft_lstreverse(t_ftlst **lst); void ft_lstremove_if(t_ftlst **lst, t_ftbool (*equal)(void *ref, void *content), void *ref, void (*del)(void *content)); +t_ftlst *ft_lstbsearch(t_ftlst *lst, + t_ftbool (*equal)(void *ref, void *content), void *ref); #endif diff --git a/src/ht/ft_htdelone.c b/src/ht/ft_htdelone.c index 8d350ae..b376d8b 100644 --- a/src/ht/ft_htdelone.c +++ b/src/ht/ft_htdelone.c @@ -15,5 +15,7 @@ void ft_htdelone(t_ftht *ht, char *key, void (*del)(t_ftht_content*)) { - ft_lstremove_if(ht->entries + ft_hthash(key), ft_iter_htkey_equal, key, del); + ft_lstremove_if(ht->entries + ft_hthash(ht, key), + (t_ftbool (*)(void*, void*))ft_inter_htkey_equal, key, + (void (*)(void*))del); } diff --git a/src/ht/ft_htdelone_key.c b/src/ht/ft_htdelone_key.c index 5dc0c16..96d55ec 100644 --- a/src/ht/ft_htdelone_key.c +++ b/src/ht/ft_htdelone_key.c @@ -11,8 +11,9 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_ht.h" -void ft_htdelone_key(t_ftht *ht, char *key) +void ft_htdelone_key(t_ftht *ht, char *key) { ft_htdelone(ht, key, ft_inter_htdelcontent_key); } diff --git a/src/ht/ft_htdestroy.c b/src/ht/ft_htdestroy.c index 6e04386..e0442c6 100644 --- a/src/ht/ft_htdestroy.c +++ b/src/ht/ft_htdestroy.c @@ -11,13 +11,14 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_ht.h" void ft_htdestroy(t_ftht *ht, void (*del)(t_ftht_content*)) { if (ht == NULL) return ; while (ht->size-- > 0) - ft_lstclear(ht->entries + ht->size, del); + ft_lstclear(ht->entries + ht->size, (void (*)(void*))del); free(ht->entries); free(ht); } diff --git a/src/ht/ft_htdestroy_all.c b/src/ht/ft_htdestroy_all.c index 761f577..ec54044 100644 --- a/src/ht/ft_htdestroy_all.c +++ b/src/ht/ft_htdestroy_all.c @@ -10,7 +10,7 @@ /* */ /* ************************************************************************** */ -#include "libft.h" +#include "libft_ht.h" static void st_htdelcontent_all(t_ftht_content *content) { @@ -22,5 +22,5 @@ static void st_htdelcontent_all(t_ftht_content *content) void ft_htdestroy_all(t_ftht *ht) { - ft_htdestroy(ht, *st_dtdelcontent_all); + ft_htdestroy(ht, *st_htdelcontent_all); } diff --git a/src/ht/ft_htdestroy_key.c b/src/ht/ft_htdestroy_key.c index e3d562f..1cae2fd 100644 --- a/src/ht/ft_htdestroy_key.c +++ b/src/ht/ft_htdestroy_key.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_ht.h" void ft_inter_htdelcontent_key(t_ftht_content *content) { @@ -21,5 +22,5 @@ void ft_inter_htdelcontent_key(t_ftht_content *content) void ft_htdestroy_key(t_ftht *ht) { - ft_htdestroy(ht, *st_dtdelcontent_key); + ft_htdestroy(ht, *ft_inter_htdelcontent_key); } diff --git a/src/ht/ft_htdestroy_value.c b/src/ht/ft_htdestroy_value.c deleted file mode 100644 index b71b960..0000000 --- a/src/ht/ft_htdestroy_value.c +++ /dev/null @@ -1,25 +0,0 @@ -/* ************************************************************************** */ -/* */ -/* ::: :::::::: */ -/* ft_htdestroy_value.c :+: :+: :+: */ -/* +:+ +:+ +:+ */ -/* By: cacharle <marvin@42.fr> +#+ +:+ +#+ */ -/* +#+#+#+#+#+ +#+ */ -/* Created: 2020/01/30 08:31:29 by cacharle #+# #+# */ -/* Updated: 2020/01/30 08:31:49 by cacharle ### ########.fr */ -/* */ -/* ************************************************************************** */ - -#include "libft.h" - -static void st_htdelcontent_value(t_ftht_content *content) -{ - if (content == NULL) - return ; - free(content->value); -} - -void ft_htdestroy_value(t_ftht *ht) -{ - ft_htdestroy(ht, *st_dtdelcontent_value); -} diff --git a/src/ht/ft_htget.c b/src/ht/ft_htget.c index 983fd74..b4715a3 100644 --- a/src/ht/ft_htget.c +++ b/src/ht/ft_htget.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_ht.h" t_ftht_content *ft_htget(t_ftht *ht, char *key) { @@ -20,5 +21,6 @@ t_ftht_content *ft_htget(t_ftht *ht, char *key) return (NULL); digest = ft_hthash(ht, key); return (ft_lstbsearch(ht->entries[digest], - &ft_inter_htkey_equal, key)->content); + (t_ftbool (*)(void*, void*))ft_inter_htkey_equal, + key)->content); } diff --git a/src/ht/ft_hthash.c b/src/ht/ft_hthash.c index 66f8efb..e7e696c 100644 --- a/src/ht/ft_hthash.c +++ b/src/ht/ft_hthash.c @@ -10,6 +10,8 @@ /* */ /* ************************************************************************** */ +#include "libft_ht.h" + t_ftht_digest ft_hthash(t_ftht *ht, char *key) { t_ftht_digest digest; @@ -19,8 +21,8 @@ t_ftht_digest ft_hthash(t_ftht *ht, char *key) digest = *key++ << 7; while (*key != '\0') { - digest = ((1000003 * digest) ^ *key) & (1<<32); + digest = ((1000003 * digest) ^ *key) & (1 << 16); key++; } - return (digest); + return (digest % ht->size); } diff --git a/src/ht/ft_htnew.c b/src/ht/ft_htnew.c index c3b7cc7..bcf81d1 100644 --- a/src/ht/ft_htnew.c +++ b/src/ht/ft_htnew.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_ht.h" t_ftht *ft_htnew(t_ftsize size) { diff --git a/src/ht/ft_htset.c b/src/ht/ft_htset.c index 32aa448..86e9690 100644 --- a/src/ht/ft_htset.c +++ b/src/ht/ft_htset.c @@ -11,6 +11,7 @@ /* ************************************************************************** */ #include "libft.h" +#include "libft_ht.h" t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value) { @@ -19,10 +20,10 @@ t_ftht_content *ft_htset(t_ftht *ht, char *key, void *value) t_ftht_entry entry; if (ht == NULL || key == NULL) - return (NULL) + return (NULL); if ((content = ft_htcontent_new(key, value)) == NULL) return (NULL); - if ((entry = ft_lstnew(content)) = NULL) + if ((entry = ft_lstnew(content)) == NULL) { free(content); return (NULL); |
