Skip to content

Commit

Permalink
hashtable: base
Browse files Browse the repository at this point in the history
  • Loading branch information
Pixailz committed Apr 29, 2024
1 parent 5ee54e4 commit 1b6981e
Show file tree
Hide file tree
Showing 12 changed files with 437 additions and 1 deletion.
1 change: 1 addition & 0 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ jobs:
"print.c",
"random.c",
"tmpfile.c",
"hashtable.c",
]
runs-on: ubuntu-latest
steps:
Expand Down
3 changes: 2 additions & 1 deletion inc/libft.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
/* */
/* ************************************************************************** */

Expand All @@ -31,6 +31,7 @@
# include "libft_error.h"
# include "libft_network/ipv4.h"
# include "libft_unit_test.h"
# include "libft_hashtable.h"

/* ########################################################################## */

Expand Down
79 changes: 79 additions & 0 deletions inc/libft_hashtable.h
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 */
13 changes: 13 additions & 0 deletions mk/srcs.mk
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,15 @@ SRC_NET_IPV4 := network/ipv4/ft_htoi4.c \
### ERROR
SRC_UNI_TEST := unit_test/ft_assert.c

### HASHTABLE
SRC_HASHTABLE := hashtable/ft_ht_free.c \
hashtable/ft_ht_get.c \
hashtable/ft_ht_hash_key.c \
hashtable/ft_ht_item.c \
hashtable/ft_ht_len.c \
hashtable/ft_ht_new.c \
hashtable/ft_ht_set.c

## if all, add all base to SRC_C
ifeq ($(ALL),1)
SRC_C += $(SRC_INT)
Expand All @@ -203,6 +212,7 @@ SRC_C += $(SRC_RDM)
SRC_C += $(SRC_LNX)
SRC_C += $(SRC_NET_IPV4)
SRC_C += $(SRC_UNI_TEST)
SRC_C += $(SRC_HASHTABLE)

## add base part to SRC_C
else
Expand Down Expand Up @@ -245,6 +255,9 @@ endif
ifeq ($(UNIT_TEST),1)
SRC_C += $(SRC_UNI_TEST)
endif
ifeq ($(HASHTABLE),1)
SRC_C += $(SRC_HASHTABLE)
endif
endif

# OBJ
Expand Down
33 changes: 33 additions & 0 deletions src/hashtable/ft_ht_free.c
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);
}
26 changes: 26 additions & 0 deletions src/hashtable/ft_ht_get.c
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);
}
41 changes: 41 additions & 0 deletions src/hashtable/ft_ht_hash_key.c
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);
}
33 changes: 33 additions & 0 deletions src/hashtable/ft_ht_item.c
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);
}
32 changes: 32 additions & 0 deletions src/hashtable/ft_ht_len.c
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);
}
22 changes: 22 additions & 0 deletions src/hashtable/ft_ht_new.c
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;
}
24 changes: 24 additions & 0 deletions src/hashtable/ft_ht_set.c
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;
}
Loading

0 comments on commit 1b6981e

Please sign in to comment.