-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmap.c
159 lines (130 loc) · 2.3 KB
/
map.c
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
#include "map.h"
#include "pathfinding.h"
#include <stdlib.h>
#include <string.h>
#include <openssl/md5.h>
struct pair {
uint16_t key;
void *value;
};
struct map_ {
struct pair *data;
size_t sz;
size_t cap;
};
static const double LOAD_FACTOR = 0.75;
// Seems like a reasonable starting capacity?
enum { DEFAULT_MAP_CAPACITY = 8 };
map *map_create(void)
{
map *m = malloc(sizeof(*m));
if (!m) {
return NULL;
}
m->sz = 0;
m->cap = DEFAULT_MAP_CAPACITY;
m->data = calloc(m->cap, sizeof(*m->data));
if (!m->data) {
free(m);
return NULL;
}
return m;
}
bool map_put(map * m, uint16_t key, void *value)
{
if (!m) {
return false;
}
if (1.0 * m->sz / m->cap > LOAD_FACTOR) {
map *new_map = map_create();
new_map->cap = 2 * m->cap;
free(new_map->data);
new_map->data = calloc(new_map->cap, sizeof(*new_map->data));
if (!new_map->data) {
map_destroy(new_map);
return false;
}
for (size_t n = 0; n < m->cap; ++n) {
if (m->data[n].key) {
//TODO ABC
map_put(new_map, m->data[n].key,
m->data[n].value);
}
}
free(m->data);
m->data = new_map->data;
m->cap = new_map->cap;
free(new_map);
}
size_t idx = key % m->cap;
while (m->data[idx].key) {
if (m->data[idx].key == key) {
m->data[idx].value = value;
return true;
}
idx++;
idx %= m->cap;
}
m->data[idx].key = key;
if (m->data[idx].key) {
m->data[idx].value = value;
m->sz++;
return true;
}
return false;
}
void *map_get(map * m, uint16_t key)
{
if (!m || !key) {
return NULL;
}
size_t idx = key % m->cap;
while (m->data[idx].key) {
if (m->data[idx].key == key) {
return m->data[idx].value;
}
idx++;
idx %= m->cap;
}
return NULL;
}
bool map_contains(const map * m, const uint16_t key)
{
if (!m || !key) {
return false;
}
size_t idx = key % m->cap;
while (m->data[idx].key) {
if (m->data[idx].key == key) {
return true;
}
idx++;
idx %= m->cap;
}
return false;
}
void map_destroy(map * m)
{
if (!m) {
return;
}
for (size_t n = 0; n < m->cap; ++n) {
if (m->data[n].value) {
free(m->data[n].value);
}
}
free(m->data);
free(m);
}
void map_print(map * m)
{
if (!m) {
return;
}
for (size_t n = 0; n < m->cap; ++n) {
if (m->data[n].value) {
printf("%zu %u %u\n", n, m->data[n].key,
((struct step *)m->data[n].value)->data);
}
}
}