Debian Codegraph @

ext/cwisstable/examples/hashset.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 set.
16
17 #include <stdio.h>
18
19 #if defined(CWISS_EXAMPLE_UNIFIED)
20 #include "cwisstable.h"
21 #elif defined(CWISS_EXAMPLE_SPLIT)
22 #include "cwisstable/declare.h"
23 #include "cwisstable/policy.h"
24 #else
25 #error "must set CWISS_EXAMPLE_UNIFIED or CWISS_EXAMPLE_SPLIT"
26 #endif
27
28 CWISS_DECLARE_FLAT_HASHSET(MyIntSet, int);
29
30 int main(void) {
31 MyIntSet set = MyIntSet_new(8);
32
33 for (int i = 0; i < 8; ++i) {
34 int val = i * i + 1;
35 MyIntSet_dump(&set);
36 MyIntSet_insert(&set, &val);
37 }
38 MyIntSet_dump(&set);
39 printf("\n");
40
41 int k = 4;
42 assert(!MyIntSet_contains(&set, &k));
43 k = 5;
44 MyIntSet_Iter it = MyIntSet_find(&set, &k);
45 int* v = MyIntSet_Iter_get(&it);
46 assert(v);
47 printf("5: %p: %d\n", v, *v);
48
49 MyIntSet_rehash(&set, 16);
50
51 it = MyIntSet_find(&set, &k);
52 v = MyIntSet_Iter_get(&it);
53 assert(v);
54 printf("5: %p: %d\n", v, *v);
55
56 printf("entries:\n");
57 it = MyIntSet_iter(&set);
58 for (int* p = MyIntSet_Iter_get(&it); p != NULL;
59 p = MyIntSet_Iter_next(&it)) {
60 printf("%d\n", *p);
61 }
62 printf("\n");
63
64 MyIntSet_erase(&set, &k);
65 assert(!MyIntSet_contains(&set, &k));
66
67 printf("entries:\n");
68 it = MyIntSet_iter(&set);
69 for (int* p = MyIntSet_Iter_get(&it); p != NULL;
70 p = MyIntSet_Iter_next(&it)) {
71 printf("%d\n", *p);
72 }
73 printf("\n");
74
75 MyIntSet_dump(&set);
76 MyIntSet_destroy(&set);
77
78 return 0;
79 }