#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <derive-c/core.h>
#include <derive-c/panic.h>
#include <derive-c/self.h>
#include <derive-c/structures/hashmap/utils.h>
Go to the source code of this file.
|
static SELF | new_with_capacity_for (size_t capacity) |
|
static SELF | new () |
|
static SELF | shallow_clone (SELF const *self) |
|
static void | delete (SELF *self) |
|
static V * | insert (SELF *self, K key, V value) |
|
static void | extend_capacity_for (SELF *self, size_t expected_items) |
|
static V * | try_insert (SELF *self, K key, V value) |
|
static V * | try_write (SELF *self, K key) |
|
static V * | write (SELF *self, K key) |
|
static V const * | try_read (SELF const *self, K key) |
|
static V const * | read (SELF const *self, K key) |
|
static bool | try_remove (SELF *self, K key, V *destination) |
|
static V | remove (SELF *self, K key) |
|
static void | delete_entry (SELF *self, K key) |
|
static size_t | size (SELF const *self) |
|
static KV_PAIR | next (ITER *iter) |
|
static size_t | position (ITER const *iter) |
|
static bool | empty (ITER const *iter) |
|
static ITER | get_iter (SELF *self) |
|
static KV_PAIR_CONST | next (ITER_CONST *iter) |
|
static size_t | position (ITER_CONST const *iter) |
|
static bool | empty (ITER_CONST const *iter) |
|
static ITER_CONST | get_iter_const (SELF const *self) |
|
◆ EQ
#define EQ derive_c_parameter_eq |
◆ HASH
#define HASH derive_c_parameter_hash |
#define K derive_c_parameter_key |
◆ K_DELETE
#define K_DELETE derive_c_parameter_key_delete |
◆ KEY_ENTRY
◆ KV_PAIR
◆ KV_PAIR_CONST
#define KV_PAIR_CONST NAME(SELF, kv_const) |
#define V derive_c_parameter_value |
◆ V_DELETE
#define V_DELETE derive_c_parameter_value_delete |
◆ delete()
static void delete |
( |
SELF * | self | ) |
|
|
static |
Definition at line 371 of file template.h.
371 {
374
375 for (
size_t i = 0; i < self->
capacity; i++) {
379 }
380 }
381
384}
static ITER get_iter(SELF *self)
#define DEBUG_ASSERT(expr)
◆ delete_entry()
static void delete_entry |
( |
SELF * | self, |
|
|
K | key ) |
|
static |
Definition at line 306 of file template.h.
306 {
309}
static V remove(SELF *self, INDEX index)
◆ empty() [1/2]
static bool empty |
( |
ITER const * | iter | ) |
|
|
static |
Definition at line 353 of file template.h.
353 {
355 return iter->index >= iter->map->capacity;
356}
◆ empty() [2/2]
static bool empty |
( |
ITER_CONST const * | iter | ) |
|
|
static |
Definition at line 426 of file template.h.
426 {
428 return iter->index >= iter->map->capacity;
429}
◆ extend_capacity_for()
static void extend_capacity_for |
( |
SELF * | self, |
|
|
size_t | expected_items ) |
|
static |
Definition at line 118 of file template.h.
118 {
121 if (target_capacity > self->
capacity) {
123 for (
size_t index = 0; index < self->
capacity; index++) {
127 }
128 }
131 *self = new_map;
132 }
133}
static INDEX insert(SELF *self, V value)
static SELF new_with_capacity_for(INDEX_TYPE items)
static size_t apply_capacity_policy(size_t capacity)
◆ get_iter()
static ITER get_iter |
( |
SELF * | self | ) |
|
|
static |
Definition at line 358 of file template.h.
358 {
360 size_t first_index = 0;
361 while (first_index < self->capacity && !self->
keys[first_index].
present) {
362 first_index++;
363 }
364 return (ITER){
365 .map = self,
366 .index = first_index,
367 .pos = 0,
368 };
369}
◆ get_iter_const()
static ITER_CONST get_iter_const |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 431 of file template.h.
431 {
433 size_t first_index = 0;
434 while (first_index < self->capacity && !self->keys[first_index].present) {
435 first_index++;
436 }
437 return (ITER_CONST){
438 .map = self,
439 .index = first_index,
440 .pos = 0,
441 };
442}
◆ insert()
static V * insert |
( |
SELF * | self, |
|
|
K | key, |
|
|
V | value ) |
|
static |
Definition at line 188 of file template.h.
188 {
191 return value_ptr;
192}
static V * try_insert(SELF *self, K key, V value)
◆ new()
Definition at line 89 of file template.h.
static size_t const INITIAL_CAPACITY
◆ new_with_capacity_for()
static SELF new_with_capacity_for |
( |
size_t | capacity | ) |
|
|
static |
Definition at line 71 of file template.h.
71 {
75
76
77
79 V* values = (
V*)malloc(
sizeof(
V) * real_capacity);
82 .capacity = real_capacity,
83 .items = 0,
84 .keys = keys,
85 .values = values,
86 };
87}
◆ next() [1/2]
Definition at line 331 of file template.h.
331 {
333 if (iter->index < iter->map->capacity) {
334 KV_PAIR ret_val = {.key = &iter->map->keys[iter->index].key,
335 .value = &iter->map->values[iter->index]};
336 iter->pos++;
337 iter->index++;
338 while (iter->index < iter->map->capacity && !iter->map->keys[iter->index].present) {
339 iter->index++;
340 }
341
342 return ret_val;
343 } else {
344 return (
KV_PAIR){.key = NULL, .value = NULL};
345 }
346}
◆ next() [2/2]
Definition at line 404 of file template.h.
404 {
406 if (iter->index < iter->map->capacity) {
407 KV_PAIR_CONST ret_val = {.key = &iter->map->keys[iter->index].key,
408 .value = &iter->map->values[iter->index]};
409 iter->pos++;
410 iter->index++;
411 while (iter->index < iter->map->capacity && !iter->map->keys[iter->index].present) {
412 iter->index++;
413 }
414
415 return ret_val;
416 } else {
418 }
419}
◆ position() [1/2]
static size_t position |
( |
ITER const * | iter | ) |
|
|
static |
Definition at line 348 of file template.h.
348 {
350 return iter->pos;
351}
◆ position() [2/2]
static size_t position |
( |
ITER_CONST const * | iter | ) |
|
|
static |
Definition at line 421 of file template.h.
421 {
423 return iter->pos;
424}
◆ read()
static V const * read |
( |
SELF const * | self, |
|
|
K | key ) |
|
static |
Definition at line 238 of file template.h.
238 {
241 return value;
242}
static V const * try_read(SELF const *self, INDEX index)
◆ remove()
static V remove |
( |
SELF * | self, |
|
|
K | key ) |
|
static |
Definition at line 300 of file template.h.
300 {
303 return value;
304}
static bool try_remove(SELF *self, INDEX index, V *destination)
◆ shallow_clone()
static SELF shallow_clone |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 91 of file template.h.
91 {
93
94
95
96
97
98
99
101 V* values = (
V*)malloc(
sizeof(
V) * self->capacity);
103
104 for (size_t i = 0; i < self->capacity; i++) {
105 if (self->keys[i].present) {
106 keys[i] = self->keys[i];
107 values[i] = self->values[i];
108 }
109 }
110
111 return (
SELF){.capacity = self->capacity, .items = self->items, .keys = keys, .values = values};
112}
◆ size()
static size_t size |
( |
SELF const * | self | ) |
|
|
static |
Definition at line 311 of file template.h.
311 {
313 return self->items;
314}
◆ try_insert()
static V * try_insert |
( |
SELF * | self, |
|
|
K | key, |
|
|
V | value ) |
|
static |
Definition at line 135 of file template.h.
135 {
139 }
140
141 uint16_t distance_from_desired = 0;
142 size_t hash =
HASH(&key);
144 V* placed_entry = NULL;
145 for (;;) {
148
150 if (
EQ(&entry->
key, &key)) {
151 return NULL;
152 }
153
155 K switch_key = entry->
key;
157 V switch_value = self->
values[index];
158
161 self->
values[index] = value;
162
163 key = switch_key;
164 distance_from_desired = switch_distance_from_desired;
165 value = switch_value;
166
167 if (!placed_entry) {
168 placed_entry = &self->
values[index];
169 }
170 }
171
172 distance_from_desired++;
174 } else {
178 self->
values[index] = value;
179 if (!placed_entry) {
180 placed_entry = &self->
values[index];
181 }
183 return placed_entry;
184 }
185 }
186}
static void extend_capacity_for(SELF *self, size_t expected_items)
uint16_t distance_from_desired
static size_t modulus_capacity(size_t index, size_t capacity)
◆ try_read()
static V const * try_read |
( |
SELF const * | self, |
|
|
K | key ) |
|
static |
Definition at line 219 of file template.h.
219 {
221 size_t hash =
HASH(&key);
223
224 for (;;) {
227 if (
EQ(&entry->
key, &key)) {
228 return &self->values[index];
229 } else {
231 }
232 } else {
233 return NULL;
234 }
235 }
236}
◆ try_remove()
static bool try_remove |
( |
SELF * | self, |
|
|
K | key, |
|
|
V * | destination ) |
|
static |
Definition at line 244 of file template.h.
244 {
246 size_t hash =
HASH(&key);
248
249 for (;;) {
252 if (
EQ(&entry->
key, &key)) {
254
255 *destination = self->
values[index];
257
258
259
260
261
262
263
264 size_t free_index = index;
266
269
271 free_entry->
key = check_entry->
key;
274
275 free_index = check_index;
276 free_entry = check_entry;
277
279 check_entry = &self->
keys[check_index];
280 }
281
282
283
284
285
286
287
289
290 return true;
291 } else {
293 }
294 } else {
295 return false;
296 }
297 }
298}
◆ try_write()
static V * try_write |
( |
SELF * | self, |
|
|
K | key ) |
|
static |
Definition at line 194 of file template.h.
194 {
196 size_t hash =
HASH(&key);
198
199 for (;;) {
202 if (
EQ(&entry->
key, &key)) {
203 return &self->
values[index];
204 } else {
206 }
207 } else {
208 return NULL;
209 }
210 }
211}
◆ write()
static V * write |
( |
SELF * | self, |
|
|
K | key ) |
|
static |
Definition at line 213 of file template.h.
213 {
216 return value;
217}
static V * try_write(SELF *self, INDEX index)