-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmap.c
More file actions
226 lines (179 loc) · 5.5 KB
/
Copy pathmap.c
File metadata and controls
226 lines (179 loc) · 5.5 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/* $SchulteIT: map.c 15189 2025-10-27 05:41:45Z schulte $ */
/* $JDTAUS: map.c 9643 2026-07-29 08:15:42Z schulte $ */
/*
* Copyright (c) 2018 - 2026 Christian Schulte <cs@schulte.it>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#ifdef HAVE_HOST_H
#include "host.h"
#endif
#ifdef MULTI_THREADED
#include "thread.h"
#endif
#include "heap.h"
#include "map.h"
struct Entry {
void *restrict key;
void *restrict value;
struct Entry *restrict next;
struct Entry *restrict prev;
};
struct Map {
const struct MapOps *restrict ops;
struct Entry **restrict buckets;
#ifdef MULTI_THREADED
mtx_t mtx;
#endif
size_t capacity;
};
struct MapIterator {
size_t i;
struct Entry *restrict e;
const struct Map *restrict m;
};
inline struct Map *Map_new(const struct MapOps *restrict const ops,
const size_t capacity) {
struct Map *restrict const m = heap_malloc(sizeof(struct Map));
m->ops = ops;
m->capacity = ((capacity | !capacity) + 1) & ~1U;
m->buckets = heap_calloc(m->capacity, sizeof(struct Entry *));
#ifdef MULTI_THREADED
mutex_init(&m->mtx);
#endif
return m;
}
inline void Map_delete(struct Map *restrict const m,
void (*v_delete)(void *restrict const)) {
for (size_t i = m->capacity; i-- > 0;) {
struct Entry *restrict e = m->buckets[i];
while (e != NULL) {
if (v_delete)
v_delete(e->value);
m->ops->k_delete(e->key);
struct Entry *tmp = e;
e = e->next;
heap_free(tmp);
}
}
#ifdef MULTI_THREADED
mutex_destroy(&m->mtx);
#endif
heap_free(m->buckets);
heap_free(m);
}
inline void *Map_put(struct Map *restrict const m, void *const k,
void *const v) {
const size_t i = m->ops->k_hash(k) % m->capacity;
struct Entry *restrict e = m->buckets[i];
void *restrict value = NULL;
while (e != NULL && !m->ops->k_equals(e->key, k))
e = e->next;
if (e != NULL) {
value = e->value;
e->value = v;
} else {
e = heap_malloc(sizeof(struct Entry));
e->key = m->ops->k_copy(k);
e->value = v;
e->prev = NULL;
if ((e->next = m->buckets[i]) != NULL)
m->buckets[i]->prev = e;
m->buckets[i] = e;
}
return value;
}
inline bool Map_exists(const struct Map *restrict const m,
const void *restrict const k) {
struct Entry *restrict e = m->buckets[m->ops->k_hash(k) % m->capacity];
while (e != NULL && !m->ops->k_equals(e->key, k))
e = e->next;
return e != NULL;
}
inline void *Map_get(const struct Map *restrict const m,
const void *restrict const k) {
struct Entry *restrict e = m->buckets[m->ops->k_hash(k) % m->capacity];
while (e != NULL && !m->ops->k_equals(e->key, k))
e = e->next;
return e != NULL ? e->value : NULL;
}
inline void *Map_remove(struct Map *restrict const m, void *const k) {
const size_t i = m->ops->k_hash(k) % m->capacity;
struct Entry *restrict e = m->buckets[i];
void *restrict value = NULL;
while (e != NULL && !m->ops->k_equals(e->key, k))
e = e->next;
if (e != NULL) {
value = e->value;
m->ops->k_delete(e->key);
if (e->prev)
e->prev->next = e->next;
else
m->buckets[i] = e->next;
if (e->next)
e->next->prev = e->prev;
heap_free(e);
}
return value;
}
inline struct MapIterator *MapIterator_new(const struct Map *restrict const m) {
struct MapIterator *restrict const it =
heap_malloc(sizeof(struct MapIterator));
it->i = m->capacity;
it->e = NULL;
it->m = m;
return it;
}
inline void MapIterator_delete(struct MapIterator *restrict const it) {
heap_free(it);
}
inline bool MapIterator_next(struct MapIterator *restrict const it) {
if (it->e != NULL)
it->e = it->e->next;
while (it->e == NULL && it->i != 0)
it->e = it->m->buckets[--it->i];
return it->e != NULL;
}
inline void *MapIterator_remove(struct MapIterator *restrict const it) {
void *restrict value = NULL;
if (it->e != NULL) {
value = it->e->value;
if (it->e->prev)
it->e->prev->next = it->e->next;
else
it->m->buckets[it->i] = it->e->next;
if (it->e->next)
it->e->next->prev = it->e->prev;
it->m->ops->k_delete(it->e->key);
heap_free(it->e);
it->e = NULL;
it->i++;
}
return value;
}
inline const void *const
MapIterator_key(const struct MapIterator *restrict const it) {
return it->e != NULL ? it->e->key : NULL;
}
inline const void *const
MapIterator_value(const struct MapIterator *restrict const it) {
return it->e != NULL ? it->e->value : NULL;
}
#ifdef MULTI_THREADED
inline mtx_t *Map_mutex(struct Map *restrict const m) { return &m->mtx; }
inline void Map_lock(struct Map *restrict const m) { mutex_lock(&m->mtx); }
inline bool Map_trylock(struct Map *restrict const m) {
return mutex_trylock(&m->mtx);
}
inline void Map_unlock(struct Map *restrict const m) { mutex_unlock(&m->mtx); }
#endif