Skip to content

Commit 91e8a84

Browse files
committed
2020. 06. 01. Solved exercises of hashmap
1 parent a473c3d commit 91e8a84

File tree

7 files changed

+410
-0
lines changed

7 files changed

+410
-0
lines changed

07_hash/3_hash_map/free_map.c

+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* free_map.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/06/01 13:17:34 by mihykim #+# #+# */
9+
/* Updated: 2020/06/01 13:22:37 by mihykim ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "map.h"
14+
15+
static void free_node(t_node *curr, void (*free_data)(void *))
16+
{
17+
t_node *temp;
18+
19+
if (curr == NULL)
20+
return ;
21+
while (curr)
22+
{
23+
temp = curr;
24+
curr = curr->next;
25+
free_data(temp);
26+
free(temp);
27+
}
28+
}
29+
30+
void free_map(t_hash_map *map, void (*free_data)(void *))
31+
{
32+
unsigned int i;
33+
34+
if (map == NULL || free_data == NULL)
35+
return ;
36+
i = 0;
37+
while (i < BIG_PRIME)
38+
{
39+
free_node(map->map[i], free_data);
40+
map->size--;
41+
i++;
42+
}
43+
free(map);
44+
}

07_hash/3_hash_map/map.h

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* map.h :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/06/01 11:07:39 by mihykim #+# #+# */
9+
/* Updated: 2020/06/01 13:26:14 by mihykim ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#ifndef MAP_H
14+
# define MAP_H
15+
16+
# define BASE 109
17+
# define BIG_PRIME 1000003
18+
19+
# include <stdlib.h>
20+
# include <string.h>
21+
22+
typedef struct s_node
23+
{
24+
const char *key;
25+
void *value;
26+
struct s_node *next;
27+
} t_node;
28+
29+
typedef struct s_hash_map
30+
{
31+
unsigned int size;
32+
t_node **map;
33+
} t_hash_map;
34+
35+
t_hash_map *map_init(void);
36+
int map_insert(t_hash_map *map, const char *key, void *value);
37+
unsigned int get_hash(const char *key);
38+
void *map_get(t_hash_map *map, const char *key);
39+
void *map_delete(t_hash_map *map, const char *key);
40+
void free_map(t_hash_map *map, void (*free_data)(void *));
41+
42+
#endif

07_hash/3_hash_map/map_delete.c

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* map_delete.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/06/01 13:01:23 by mihykim #+# #+# */
9+
/* Updated: 2020/06/01 13:17:28 by mihykim ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "map.h"
14+
15+
# define SUCCESS 1
16+
# define FAILURE 0
17+
18+
void *map_delete(t_hash_map *map, const char *key)
19+
{
20+
t_node *curr;
21+
unsigned int i;
22+
23+
if (map == NULL)
24+
return (NULL);
25+
i = get_hash(key);
26+
curr = map->map[i];
27+
if (curr == NULL)
28+
return (FAILURE);
29+
if (strcmp(curr->key, key) == 0)
30+
{
31+
map->map[i] = curr->next;
32+
free(curr);
33+
map->size--;
34+
return (curr->value);
35+
}
36+
while (curr->next)
37+
{
38+
if (strcmp(curr->key, key) == 0)
39+
{
40+
curr->next = curr->next->next;
41+
free(curr);
42+
map->size--;
43+
return (curr->value);
44+
}
45+
curr = curr->next;
46+
}
47+
return (NULL);
48+
}

07_hash/3_hash_map/map_get.c

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* map_get.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/06/01 12:54:39 by mihykim #+# #+# */
9+
/* Updated: 2020/06/01 13:30:32 by mihykim ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "map.h"
14+
15+
# define NON_EXISTENT 0
16+
17+
void *map_get(t_hash_map *map, const char *key)
18+
{
19+
t_node *curr;
20+
unsigned int i;
21+
22+
if (map == NULL)
23+
return (NON_EXISTENT);
24+
i = get_hash(key);
25+
curr = map->map[i];
26+
while (curr)
27+
{
28+
if (strcmp(curr->key, key) == 0)
29+
return (curr->value);
30+
curr = curr->next;
31+
}
32+
return (NON_EXISTENT);
33+
}

07_hash/3_hash_map/map_init.c

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* map_init.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/06/01 12:30:16 by mihykim #+# #+# */
9+
/* Updated: 2020/06/01 13:01:53 by mihykim ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "map.h"
14+
15+
t_hash_map *map_init(void)
16+
{
17+
t_hash_map *map;
18+
int i;
19+
20+
map = malloc(sizeof(t_hash_map));
21+
if (map == NULL)
22+
return (NULL);
23+
map->map = malloc(sizeof(t_node *) * BIG_PRIME);
24+
if (map->map == NULL)
25+
{
26+
free(map);
27+
return (NULL);
28+
}
29+
i = 0;
30+
while (i < BIG_PRIME)
31+
{
32+
map->map[i] = NULL;
33+
i++;
34+
}
35+
map->size = 0;
36+
return (map);
37+
}

07_hash/3_hash_map/map_insert.c

+74
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
/* ************************************************************************** */
2+
/* */
3+
/* ::: :::::::: */
4+
/* map_insert.c :+: :+: :+: */
5+
/* +:+ +:+ +:+ */
6+
/* By: mihykim <[email protected]> +#+ +:+ +#+ */
7+
/* +#+#+#+#+#+ +#+ */
8+
/* Created: 2020/06/01 12:35:58 by mihykim #+# #+# */
9+
/* Updated: 2020/06/01 13:28:11 by mihykim ### ########.fr */
10+
/* */
11+
/* ************************************************************************** */
12+
13+
#include "map.h"
14+
15+
# define SUCCESS 1
16+
# define FAILURE 0
17+
18+
static t_node *create_node(const char *key, void *value)
19+
{
20+
t_node *node;
21+
22+
node = malloc(sizeof(t_node));
23+
if (node == NULL)
24+
return (NULL);
25+
node->key = key;
26+
node->value = value;
27+
node->next = NULL;
28+
return (node);
29+
}
30+
31+
unsigned int get_hash(const char *key)
32+
{
33+
unsigned int i;
34+
unsigned int hash;
35+
36+
hash = 0;
37+
i = 0;
38+
while (key[i])
39+
{
40+
hash = (hash * BASE) + key[i];
41+
hash %= BIG_PRIME;
42+
i++;
43+
}
44+
return (hash);
45+
}
46+
47+
int map_insert(t_hash_map *map, const char *key, void *value)
48+
{
49+
t_node *new;
50+
t_node *curr;
51+
unsigned int i;
52+
53+
if (map == NULL || (new = create_node(key, value)) == NULL)
54+
return (FAILURE);
55+
i = get_hash(key);
56+
if (map->map[i] == NULL)
57+
map->map[i] = new;
58+
else
59+
{
60+
curr = map->map[i];
61+
while (curr)
62+
{
63+
if (strcmp(curr->key, key) == 0)
64+
{
65+
free(new);
66+
return (FAILURE);
67+
}
68+
curr = curr->next;
69+
}
70+
curr->next = new;
71+
}
72+
map->size++;
73+
return (SUCCESS);
74+
}

0 commit comments

Comments
 (0)