-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
437 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,6 +84,7 @@ jobs: | |
"print.c", | ||
"random.c", | ||
"tmpfile.c", | ||
"hashtable.c", | ||
] | ||
runs-on: ubuntu-latest | ||
steps: | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/02/22 13:16:35 by brda-sil #+# #+# */ | ||
/* Updated: 2024/02/24 00:52:13 by brda-sil ### ########.fr */ | ||
/* Updated: 2024/04/29 06:01:14 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
|
@@ -31,6 +31,7 @@ | |
# include "libft_error.h" | ||
# include "libft_network/ipv4.h" | ||
# include "libft_unit_test.h" | ||
# include "libft_hashtable.h" | ||
|
||
/* ########################################################################## */ | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* libft_hashtable.h :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2022/02/22 13:16:35 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:00:10 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#ifndef LIBFT_HASHTABLE_H | ||
# define LIBFT_HASHTABLE_H | ||
|
||
/* ########################################################################## */ | ||
/* INCLUDE */ | ||
/* ####### */ | ||
|
||
# include "libft_memory.h" | ||
|
||
/* ########################################################################## */ | ||
|
||
/* ########################################################################## */ | ||
/* CONFIG */ | ||
/* ###### */ | ||
|
||
#define HT_SIZE ((1UL<<8) - 1) | ||
|
||
/* ########################################################################## */ | ||
|
||
/* ########################################################################## */ | ||
/* STRUCT */ | ||
/* ###### */ | ||
|
||
typedef struct _ht_item | ||
{ | ||
void *value; | ||
char *key; | ||
t_bool is_deleted; | ||
struct _ht_item *next; | ||
} ht_item; | ||
|
||
typedef struct _ht | ||
{ | ||
ht_item **item; | ||
} ht; | ||
|
||
/* ########################################################################## */ | ||
|
||
/* ########################################################################## */ | ||
/* FILES */ | ||
/* ##### */ | ||
|
||
// hashtable/ft_ht_free.c | ||
void ft_ht_free(ht *table); | ||
|
||
// hashtable/ft_ht_get.c | ||
void *ft_ht_get(ht *table, char *key); | ||
|
||
// hashtable/ft_ht_hash_key.c | ||
t_uint32 ft_ht_hash_key(const char *k); | ||
|
||
// hashtable/ft_ht_item.c | ||
ht_item *ft_ht_new_item(char *key, void *value); | ||
ht_item *ft_ht_add_item(ht_item **item, char *key, void *value); | ||
|
||
// hashtable/ft_ht_len.c | ||
t_size ft_ht_len(ht table); | ||
|
||
// hashtable/ft_ht_new.c | ||
ht *ft_ht_new(); | ||
|
||
// hashtable/ft_ht_set.c | ||
t_bool ft_ht_set(ht *table, char *key, void *value); | ||
|
||
/* ########################################################################## */ | ||
|
||
#endif /* LIBFT_HASHTABLE_H */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_free.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:00:45 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:10:40 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
void ft_ht_free(ht *table) | ||
{ | ||
ht_item *ptr; | ||
ht_item *tmp; | ||
|
||
for (unsigned long int i = 0; i < HT_SIZE; i++) | ||
{ | ||
ptr = table->item[i]; | ||
while (ptr) | ||
{ | ||
tmp = ptr->next; | ||
free(ptr->key); | ||
free(ptr); | ||
ptr = tmp; | ||
} | ||
} | ||
free(table->item); | ||
free(table); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_get.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:00:45 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:02:55 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
void *ft_ht_get(ht *table, char *key) | ||
{ | ||
t_uint32 key_hash = ft_ht_hash_key(key); | ||
ht_item *ptr = table->item[key_hash]; | ||
t_size len_str = ft_strlen(key); | ||
|
||
while (ptr && ft_strncmp(ptr->key, key, len_str)) | ||
ptr = ptr->next; | ||
if (ptr) | ||
return ptr->value; | ||
return (FT_NULL); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_hash_key.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:00:45 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:01:17 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
t_uint32 ft_ht_hash_key(const char *k) | ||
{ | ||
t_size k_len = ft_strlen(k); | ||
t_uint32 hash = 1; | ||
t_size index = 0; | ||
|
||
while (TRUE) | ||
{ | ||
if (index + 4 >= k_len) | ||
break ; | ||
|
||
hash = (hash * (*((t_uint32 *)(k + index)))); | ||
index += 4; | ||
} | ||
|
||
if (index != k_len) | ||
{ | ||
unsigned char buff[4] = {0}; | ||
|
||
for (int i = 0; i + index < k_len; i++) | ||
buff[i] = k[index + i]; | ||
|
||
hash = (hash * (*((t_uint32 *)(buff)))); | ||
} | ||
|
||
return (hash % HT_SIZE); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_item.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:01:50 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:02:01 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
ht_item *ft_ht_new_item(char *key, void *value) | ||
{ | ||
ht_item *ptr; | ||
|
||
ptr = (ht_item *)ft_calloc(1, sizeof(ht_item)); | ||
ptr->value = value; | ||
ptr->key = ft_strdup(key); | ||
return (ptr); | ||
} | ||
|
||
ht_item *ft_ht_add_item(ht_item **item, char *key, void *value) | ||
{ | ||
ht_item **ptr = item; | ||
|
||
while (*ptr) | ||
ptr = &((*ptr)->next); | ||
*ptr = ft_ht_new_item(key, value); | ||
return (*item); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_len.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:00:45 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:11:05 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
t_size ft_ht_len(ht table) | ||
{ | ||
t_size len = 0; | ||
ht_item *ptr; | ||
|
||
for (unsigned long int i = 0; i < HT_SIZE; i++) | ||
{ | ||
ptr = table.item[i]; | ||
|
||
while (ptr) | ||
{ | ||
if (ptr->value) | ||
len++; | ||
ptr = ptr->next; | ||
} | ||
} | ||
return (len); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_new.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:00:45 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:03:04 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
ht *ft_ht_new() | ||
{ | ||
ht *table; | ||
|
||
table = (ht *)ft_calloc(1, sizeof(ht)); | ||
table->item = (ht_item **)ft_calloc(HT_SIZE, sizeof(ht_item *)); | ||
return table; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/* ************************************************************************** */ | ||
/* */ | ||
/* ::: :::::::: */ | ||
/* ft_ht_set.c :+: :+: :+: */ | ||
/* +:+ +:+ +:+ */ | ||
/* By: brda-sil <[email protected] +#+ +:+ +#+ */ | ||
/* +#+#+#+#+#+ +#+ */ | ||
/* Created: 2024/04/29 06:00:45 by brda-sil #+# #+# */ | ||
/* Updated: 2024/04/29 06:03:46 by brda-sil ### ########.fr */ | ||
/* */ | ||
/* ************************************************************************** */ | ||
|
||
#include "libft_hashtable.h" | ||
|
||
t_bool ft_ht_set(ht *table, char *key, void *value) | ||
{ | ||
t_uint32 key_hash = ft_ht_hash_key(key); | ||
|
||
if (!table->item[key_hash]) | ||
table->item[key_hash] = ft_ht_new_item(key, value); | ||
else | ||
table->item[key_hash] = ft_ht_add_item(&table->item[key_hash], key, value); | ||
return TRUE; | ||
} |
Oops, something went wrong.