Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions inc/hashmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,19 @@
/* */
/* ************************************************************************** */

#ifndef LIBHASHMAP_H
# define LIBHASHMAP_H
#ifndef HASHMAP_H
# define HASHMAP_H

#include <stddef.h>
#include "pair.h"
# include <stddef.h>
# include "pair.h"

void *hm_new(size_t _size);
void hm_destroy(void *_hm, void (*f)(void *));
void *hm_set(void *_hm, const char *_key, void *_value);
size_t hm_size(const void *_hm);
void *hm_get(const void *_hm, const char *_key);
t_pair hm_get_seq(const void *_hm);
size_t hm_get_collision_count(void *_hm);
void hm_remove(void *_hm, const char *_key, void (*_ft_delete)(void*));
void *hm_new(size_t _size);
void hm_destroy(void *_hm, void (*f)(void *));
void *hm_set(void *_hm, const char *_key, void *_value);
size_t hm_size(const void *_hm);
void *hm_get(const void *_hm, const char *_key);
t_pair hm_get_seq(const void *_hm);
size_t hm_get_collision_count(void *_hm);
void hm_remove(void *_hm, const char *_key, void (*_ft_delete)(void*));

#endif
48 changes: 31 additions & 17 deletions inc/hashmap_internal.h
Original file line number Diff line number Diff line change
@@ -1,26 +1,40 @@
#ifndef LIBHASHMAP_INTERNAL_H
# define LIBHASHMAP_INTERNAL_H
#ifndef HASHMAP_INTERNAL_H
# define HASHMAP_INTERNAL_H

# include <stdlib.h>
# include <stdint.h>
# include "libft.h"
# include "hashmap.h"

typedef struct s_hm_node {
char *key;
void *value;
struct s_hm_node *next;
struct s_hm_node *prev;
} t_hm_node;
typedef struct s_hm_node {
struct s_hm_node *next;
struct s_hm_node *prev;
char *key;
void *value;
} t_hm_node;

typedef struct s_hash_map {
t_hm_node **nodes;
t_hm_node *first_node;
t_hm_node *last_node;
t_hm_node *history;
size_t collisions;
size_t cap;
size_t size;
} t_hash_map;
typedef struct s_hash_map {
t_hm_node **nodes;
t_hm_node *first_node;
t_hm_node *last_node;
t_hm_node *history;
size_t collisions;
size_t cap;
size_t size;
} t_hash_map;

uint64_t hash(const size_t cap, const char *key);
t_hm_node *new_node(const char *_key, void *_value);
void add_node_to_history(t_hm_node **root, t_hm_node *node);
void history_destroy(t_hm_node *root, void (*f)(void *));
t_hm_node *find_node_by_key(t_hm_node *_node, const char *_key);
void node_remove_from_list(t_hash_map *_hm, t_hm_node *_node);
void *hm_add_new_key(t_hash_map *hm, t_hm_node **node,
const char *_key, void *_value);
void node_destroy(t_hm_node *_to_destroy, void (*_ft_delete)(void*));
void *hm_find_existing_node_and_replace_value(t_hash_map *hm,
t_hm_node *node_at_index, const char *_key, void *_value);
void *hm_insert_node(t_hash_map *hm, t_hm_node *node, t_hm_node *new);
void *hm_handle_collision(t_hash_map *hm,
t_hm_node *node, const char *_key, void *_value);
#endif
2 changes: 1 addition & 1 deletion lib/pair
Submodule pair updated 4 files
+2 −2 CMakeLists.txt
+12 −12 inc/pair.h
+1 −1 src/CMakeLists.txt
+4 −4 src/pair.c
2 changes: 1 addition & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ file(GLOB SRC_SUBROOT_LIST CONFIGURE_DEPENDS "${hashmap_SOURCE_DIR}/src/*/*.c")
add_library(hashmap ${SRC_LIST} ${SRC_SUBROOT_LIST} ${HEADER_LIST})

# Set the compilation standards
set_property(TARGET hashmap PROPERTY C_STANDARD 99)
set_property(TARGET hashmap PROPERTY C_STANDARD 11)

# We need this directory, and users of our library will need it too
target_include_directories(hashmap PUBLIC ../inc)
Expand Down
30 changes: 30 additions & 0 deletions src/add_node_to_history.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: haachtch <haachtch@student.codam.n> +#+ */
/* +#+ */
/* Created: 2020/07/13 09:34:21 by haachtch #+# #+# */
/* Updated: 2020/09/03 14:12:42 by haachtch ######## odam.nl */
/* */
/* ************************************************************************** */

#include "hashmap_internal.h"

void add_node_to_history(t_hm_node **root, t_hm_node *node)
{
t_hm_node *lst;

if (!*root)
{
(*root) = node;
}
else
{
lst = *root;
while (lst->next)
lst = lst->next;
lst->next = node;
}
}
26 changes: 26 additions & 0 deletions src/find_node_by_key.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: haachtch <haachtch@student.codam.n> +#+ */
/* +#+ */
/* Created: 2020/07/13 09:34:21 by haachtch #+# #+# */
/* Updated: 2020/09/03 14:12:42 by haachtch ######## odam.nl */
/* */
/* ************************************************************************** */

#include "hashmap_internal.h"

t_hm_node *find_node_by_key(t_hm_node *_node, const char *_key)
{
while (_node)
{
if (ft_strcmp(_key, _node->key) == 0)
break ;
_node = _node->next;
}
if (!_node || ft_strcmp(_key, _node->key) != 0)
return (NULL);
return (_node);
}
31 changes: 31 additions & 0 deletions src/hash.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/* ************************************************************************** */
/* */
/* :::::::: */
/* main.c :+: :+: */
/* +:+ */
/* By: haachtch <haachtch@student.codam.n> +#+ */
/* +#+ */
/* Created: 2020/07/13 09:34:21 by haachtch #+# #+# */
/* Updated: 2020/09/03 14:12:42 by haachtch ######## odam.nl */
/* */
/* ************************************************************************** */

#include "hashmap_internal.h"

uint64_t hash(const size_t cap, const char *key)
{
uint64_t value;
size_t i;
size_t length;

value = 0;
i = 0;
length = ft_strlen(key);
while (i < length)
{
value = value * 37 + key[i];
i++;
}
value = value % cap;
return (value);
}
Loading