Debian Codegraph @

ext/cwisstable/examples/hashmap.c

raw symbols SCIP
1 // Copyright 2022 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 // This file demonstrates the API of a basic cwisstable map.
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 }