summaryrefslogtreecommitdiff
path: root/graphs/piscine/hash_map/hash_map.c
blob: 4690e8b3f31924cf963d6c0693a9ad6b9d9886ad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#include "hash_map.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

struct hash_map *hash_map_init(size_t size)
{
    struct hash_map *new;
    if ((new = malloc(sizeof(struct hash_map))) == NULL)
    {
        return NULL;
    }
    if ((new->data = malloc(size * sizeof(struct pair_list *))) == NULL)
    {
        return NULL;
    }
    new->size = size;

    for (size_t i = 0; i < size; i++)
    {
        new->data[i] = NULL;
    }

    return new;
}

bool hash_map_insert(struct hash_map *hash_map, const char *key, char *value,
                     bool *updated)
{
    if (hash_map == NULL || updated == NULL || hash_map->size == 0)
    {
        return false;
    }
    *updated = false;

    size_t h = hash(key);
    if (h >= hash_map->size)
    {
        h %= hash_map->size;
    }

    struct pair_list *new = malloc(sizeof(struct pair_list));
    if (new == NULL)
    {
        return false;
    }

    for (struct pair_list *p = hash_map->data[h]; p; p = p->next)
    {
        if (strcmp(p->key, key) == 0)
        {
            p->value = value;
            *updated = true;
            free(new);
            return true;
        }
    }

    new->next = hash_map->data[h];
    new->key = key;
    new->value = value;
    hash_map->data[h] = new;
    return true;
}

void hash_map_free(struct hash_map *hash_map)
{
    if (hash_map == NULL)
    {
        return;
    }

    if (hash_map->data == NULL)
    {
        free(hash_map);
        return;
    }

    for (size_t i = 0; i < hash_map->size; i++)
    {
        while (hash_map->data[i])
        {
            struct pair_list *tmp = hash_map->data[i]->next;
            free(hash_map->data[i]);
            hash_map->data[i] = tmp;
        }
    }

    free(hash_map->data);
    free(hash_map);
}

void hash_map_dump(struct hash_map *hash_map)
{
    if (hash_map == NULL || hash_map->data == NULL)
    {
        return;
    }

    for (size_t i = 0; i < hash_map->size; i++)
    {
        if (hash_map->data[i] != NULL)
        {
            printf("%s: %s", hash_map->data[i]->key, hash_map->data[i]->value);
            for (struct pair_list *p = hash_map->data[i]->next; p; p = p->next)
            {
                printf(", %s: %s", p->key, p->value);
            }
            printf("\n");
        }
    }
}

const char *hash_map_get(const struct hash_map *hash_map, const char *key)
{
    if (hash_map == NULL || hash_map->data == NULL || hash_map->size == 0)
    {
        return NULL;
    }

    size_t h = hash(key);
    if (h >= hash_map->size)
    {
        h %= hash_map->size;
    }
    struct pair_list *p;
    for (p = hash_map->data[h]; p && strcmp(p->key, key) != 0; p = p->next)
    {
        continue;
    }

    if (p)
    {
        return p->value;
    }
    return NULL;
}

bool hash_map_remove(struct hash_map *hash_map, const char *key)
{
    if (hash_map == NULL || hash_map->data == NULL || hash_map->size == 0)
    {
        return false;
    }

    size_t h = hash(key);
    if (h >= hash_map->size)
    {
        h %= hash_map->size;
    }
    if (hash_map->data[h] == NULL)
    {
        return false;
    }

    if (strcmp(hash_map->data[h]->key, key) == 0)
    {
        struct pair_list *tmp = hash_map->data[h]->next;
        free(hash_map->data[h]);
        hash_map->data[h] = tmp;
        return true;
    }

    struct pair_list *p;
    for (p = hash_map->data[h]; p->next; p = p->next)
    {
        if (strcmp(p->next->key, key) == 0)
        {
            struct pair_list *tmp = p->next->next;
            free(p->next);
            p->next = tmp;
            return true;
        }
    }
    return false;
}