Debian Codegraph @

ext/cwisstable/examples/stringmap.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 cwisstable map using C-style strings as
16 // keys.
17
18 #include <math.h>
19 #include <stdio.h>
20 #include <string.h>
21
22 #if defined(CWISS_EXAMPLE_UNIFIED)
23 #include "cwisstable.h"
24 #elif defined(CWISS_EXAMPLE_SPLIT)
25 #include "cwisstable/declare.h"
26 #include "cwisstable/policy.h"
27 #else
28 #error "must set CWISS_EXAMPLE_UNIFIED or CWISS_EXAMPLE_SPLIT"
29 #endif
30
31 static inline void kCStrPolicy_copy(void* dst, const void* src) {
32 typedef struct {
33 char* k;
34 float v;
35 } entry;
36 const entry* e = (const entry*)src;
37 entry* d = (entry*)dst;
38
39 size_t len = strlen(e->k);
40 d->k = malloc(len + 1);
41 d->v = e->v;
42 memcpy(d->k, e->k, len + 1);
43 }
44 static inline void kCStrPolicy_dtor(void* val) {
45 char* str = *(char**)val;
46 free(str);
47 }
48
49 static inline size_t kCStrPolicy_hash(const void* val) {
50 const char* str = *(const char* const*)val;
51 size_t len = strlen(str);
52 CWISS_FxHash_State state = 0;
53 CWISS_FxHash_Write(&state, str, len);
54 return state;
55 }
56 static inline bool kCStrPolicy_eq(const void* a, const void* b) {
57 const char* ap = *(const char* const*)a;
58 const char* bp = *(const char* const*)b;
59 return strcmp(ap, bp) == 0;
60 }
61
62 CWISS_DECLARE_NODE_MAP_POLICY(kCStrPolicy, const char*, float,
63 (obj_copy, kCStrPolicy_copy),
64 (obj_dtor, kCStrPolicy_dtor),
65 (key_hash, kCStrPolicy_hash),
66 (key_eq, kCStrPolicy_eq));
67
68 CWISS_DECLARE_HASHMAP_WITH(MyCStrMap, const char*, float, kCStrPolicy);
69
70 static const char* kStrings[] = {
71 "abcd", "efgh", "ijkh", "lmno", "pqrs", "tuvw", "xyza", "bcde",
72 };
73
74 int main(void) {
75 MyCStrMap map = MyCStrMap_new(8);
76
77 for (int i = 0; i < 8; ++i) {
78 int val = i * i + 1;
79 MyCStrMap_Entry e = {kStrings[i], sin(val)};
80 MyCStrMap_dump(&map);
81 MyCStrMap_insert(&map, &e);
82 }
83 MyCStrMap_dump(&map);
84 printf("\n");
85
86 const char* k = "missing";
87 assert(!MyCStrMap_contains(&map, &k));
88 k = "lmno";
89 MyCStrMap_Iter it = MyCStrMap_find(&map, &k);
90 MyCStrMap_Entry* v = MyCStrMap_Iter_get(&it);
91 assert(v);
92 printf("5: %p: \"%s\"->%f\n", v, v->key, v->val);
93
94 MyCStrMap_rehash(&map, 16);
95
96 it = MyCStrMap_find(&map, &k);
97 v = MyCStrMap_Iter_get(&it);
98 assert(v);
99 printf("5: %p: \"%s\"->%f\n", v, v->key, v->val);
100
101 printf("entries:\n");
102 it = MyCStrMap_iter(&map);
103 for (MyCStrMap_Entry* p = MyCStrMap_Iter_get(&it); p != NULL;
104 p = MyCStrMap_Iter_next(&it)) {
105 printf("\"%s\"->%f\n", p->key, p->val);
106 }
107 printf("\n");
108
109 MyCStrMap_erase(&map, &k);
110 assert(!MyCStrMap_contains(&map, &k));
111
112 printf("entries:\n");
113 it = MyCStrMap_iter(&map);
114 for (MyCStrMap_Entry* p = MyCStrMap_Iter_get(&it); p != NULL;
115 p = MyCStrMap_Iter_next(&it)) {
116 printf("\"%s\"->%f\n", p->key, p->val);
117 }
118 printf("\n");
119
120 MyCStrMap_dump(&map);
121 MyCStrMap_destroy(&map);
122
123 return 0;
124 }