| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
#include <math.h> |
| 18 |
#include <stdio.h> |
| 19 |
|
| 20 |
#if defined(CWISS_EXAMPLE_UNIFIED) |
| 21 |
#include "cwisstable.h" |
| 22 |
#elif defined(CWISS_EXAMPLE_SPLIT) |
| 23 |
#include "cwisstable/declare.h" |
| 24 |
#include "cwisstable/policy.h" |
| 25 |
#else |
| 26 |
#error "must set CWISS_EXAMPLE_UNIFIED or CWISS_EXAMPLE_SPLIT" |
| 27 |
#endif |
| 28 |
|
| 29 |
CWISS_DECLARE_NODE_HASHMAP(MyIntMap, int, float); |
| 30 |
|
| 31 |
int main(void) { |
| 32 |
MyIntMap map = MyIntMap_new(8); |
| 33 |
|
| 34 |
for (int i = 0; i < 8; ++i) { |
| 35 |
int val = i * i + 1; |
| 36 |
MyIntMap_Entry e = {val, sin(val)}; |
| 37 |
MyIntMap_dump(&map); |
| 38 |
MyIntMap_insert(&map, &e); |
| 39 |
} |
| 40 |
MyIntMap_dump(&map); |
| 41 |
printf("\n"); |
| 42 |
|
| 43 |
int k = 4; |
| 44 |
assert(!MyIntMap_contains(&map, &k)); |
| 45 |
k = 5; |
| 46 |
MyIntMap_Iter it = MyIntMap_find(&map, &k); |
| 47 |
MyIntMap_Entry* v = MyIntMap_Iter_get(&it); |
| 48 |
assert(v); |
| 49 |
printf("5: %p: %d->%f\n", v, v->key, v->val); |
| 50 |
|
| 51 |
MyIntMap_rehash(&map, 16); |
| 52 |
|
| 53 |
it = MyIntMap_find(&map, &k); |
| 54 |
v = MyIntMap_Iter_get(&it); |
| 55 |
assert(v); |
| 56 |
printf("5: %p: %d->%f\n", v, v->key, v->val); |
| 57 |
|
| 58 |
printf("entries:\n"); |
| 59 |
it = MyIntMap_iter(&map); |
| 60 |
for (MyIntMap_Entry* p = MyIntMap_Iter_get(&it); p != NULL; |
| 61 |
p = MyIntMap_Iter_next(&it)) { |
| 62 |
printf("%d->%f\n", p->key, p->val); |
| 63 |
} |
| 64 |
printf("\n"); |
| 65 |
|
| 66 |
MyIntMap_erase(&map, &k); |
| 67 |
assert(!MyIntMap_contains(&map, &k)); |
| 68 |
|
| 69 |
printf("entries:\n"); |
| 70 |
it = MyIntMap_iter(&map); |
| 71 |
for (MyIntMap_Entry* p = MyIntMap_Iter_get(&it); p != NULL; |
| 72 |
p = MyIntMap_Iter_next(&it)) { |
| 73 |
printf("%d->%f\n", p->key, p->val); |
| 74 |
} |
| 75 |
printf("\n"); |
| 76 |
|
| 77 |
MyIntMap_dump(&map); |
| 78 |
MyIntMap_destroy(&map); |
| 79 |
|
| 80 |
return 0; |
| 81 |
} |