Debian Codegraph @

ext/cwisstable/examples/arraymap.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 heterogenous lookup API.
16
17 #include <math.h>
18 #include <stdio.h>
19 #include <stdlib.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 typedef struct {
32 int* ptr;
33 size_t len, cap;
34 } IntArray;
35
36 typedef struct {
37 int* ptr;
38 size_t len;
39 } IntView;
40
41 static IntArray IntArray_new(void) { return (IntArray){0}; }
42
43 static IntArray IntArray_from_view(IntView view) {
44 // This is still private API even if it's used in examples. :)
45 uint32_t width = CWISS_BitWidth(view.len);
46 uint32_t cap = (((size_t)1) << width) - 1;
47
48 IntArray array = {
49 malloc(cap * sizeof(int)),
50 view.len,
51 cap,
52 };
53 memcpy(array.ptr, view.ptr, view.len * sizeof(int));
54 return array;
55 }
56
57 static IntArray IntArray_dup(const IntArray* self) {
58 IntArray array = {
59 malloc(self->cap * sizeof(int)),
60 self->len,
61 self->cap,
62 };
63 memcpy(array.ptr, self->ptr, self->len * sizeof(int));
64 return array;
65 }
66
67 static void IntArray_destroy(IntArray* self) {
68 free(self->ptr);
69 self->ptr = (int*)0x5a5a5a5a00;
70 }
71
72 static void IntArray_push(IntArray* self, int val) {
73 if (self->len == self->cap) {
74 if (self->cap == 0) {
75 self->cap = 8;
76 self->ptr = malloc(self->cap * sizeof(int));
77 } else {
78 self->cap *= 2;
79 self->ptr = realloc(self->ptr, self->cap * sizeof(int));
80 }
81 }
82 self->ptr[self->len] = val;
83 ++self->len;
84 }
85
86 static void IntArray_dump(const IntArray* self) {
87 printf("%p[%zu:%zu] {", self->ptr, self->len, self->cap);
88 for (size_t i = 0; i < self->len; ++i) {
89 if (i != 0) {
90 printf(", %d", self->ptr[i]);
91 } else {
92 printf("%d", self->ptr[i]);
93 }
94 }
95 printf("}");
96 }
97
98 static inline void kIntArrayPolicy_copy(void* dst, const void* src) {
99 typedef struct {
100 IntArray k;
101 float v;
102 } entry;
103 const entry* e = (const entry*)src;
104 entry* d = (entry*)dst;
105
106 d->k = IntArray_dup(&e->k);
107 d->v = e->v;
108 }
109 static inline void kIntArrayPolicy_dtor(void* val) {
110 IntArray_destroy((IntArray*)val);
111 }
112
113 static inline size_t kIntArrayPolicy_hash(const void* val) {
114 const IntArray* arr = (const IntArray*)val;
115 CWISS_FxHash_State state = 0;
116 CWISS_FxHash_Write(&state, &arr->len, sizeof(arr->len));
117 CWISS_FxHash_Write(&state, arr->ptr, arr->len * sizeof(int));
118 return state;
119 }
120 static inline bool kIntArrayPolicy_eq(const void* a, const void* b) {
121 const IntArray* ap = (const IntArray*)a;
122 const IntArray* bp = (const IntArray*)b;
123 return ap->len == bp->len &&
124 memcmp(ap->ptr, bp->ptr, ap->len * sizeof(int)) == 0;
125 }
126
127 CWISS_DECLARE_NODE_MAP_POLICY(kIntArrayPolicy, IntArray, float,
128 (obj_copy, kIntArrayPolicy_copy),
129 (obj_dtor, kIntArrayPolicy_dtor),
130 (key_hash, kIntArrayPolicy_hash),
131 (key_eq, kIntArrayPolicy_eq));
132
133 CWISS_DECLARE_HASHMAP_WITH(MyArrayMap, IntArray, float, kIntArrayPolicy);
134
135 static inline size_t MyArrayMap_IntView_hash(const IntView* self) {
136 CWISS_FxHash_State state = 0;
137 CWISS_FxHash_Write(&state, &self->len, sizeof(self->len));
138 CWISS_FxHash_Write(&state, self->ptr, self->len * sizeof(int));
139 return state;
140 }
141
142 static inline bool MyArrayMap_IntView_eq(const IntView* self,
143 const MyArrayMap_Entry* that) {
144 IntArray_dump(&that->key);
145 return self->len == that->key.len &&
146 memcmp(self->ptr, that->key.ptr, self->len * sizeof(int)) == 0;
147 }
148
149 CWISS_DECLARE_LOOKUP(MyArrayMap, IntView);
150
151 int main(void) {
152 MyArrayMap map = MyArrayMap_new(8);
153 IntArray arr = IntArray_new();
154
155 for (int i = 0; i < 8; ++i) {
156 IntArray_push(&arr, i);
157 IntArray_dump(&arr);
158 printf("\n");
159
160 int val = i * i + 1;
161 MyArrayMap_Entry e = {arr, sin(val)};
162 // MyArrayMap_dump(&map);
163 MyArrayMap_insert(&map, &e);
164 }
165 MyArrayMap_dump(&map);
166 printf("\n");
167
168 int buf[5] = {2, 3, 4};
169 IntView k = {buf, 3};
170 assert(!MyArrayMap_contains_by_IntView(&map, &k));
171 k.ptr = arr.ptr;
172 MyArrayMap_Iter it = MyArrayMap_find_by_IntView(&map, &k);
173 MyArrayMap_Entry* v = MyArrayMap_Iter_get(&it);
174 assert(v);
175 printf("5: %p: ", v);
176 IntArray_dump(&v->key);
177 printf("->%f\n", v->val);
178
179 MyArrayMap_rehash(&map, 16);
180
181 it = MyArrayMap_find_by_IntView(&map, &k);
182 printf("5: %p: ", v);
183 IntArray_dump(&v->key);
184 printf("->%f\n", v->val);
185
186 printf("entries:\n");
187 it = MyArrayMap_iter(&map);
188 for (MyArrayMap_Entry* p = MyArrayMap_Iter_get(&it); p != NULL;
189 p = MyArrayMap_Iter_next(&it)) {
190 IntArray_dump(&p->key);
191 printf("->%f\n", p->val);
192 }
193 printf("\n");
194
195 MyArrayMap_erase_by_IntView(&map, &k);
196 assert(!MyArrayMap_contains_by_IntView(&map, &k));
197
198 MyArrayMap_Insert in = MyArrayMap_deferred_insert_by_IntView(&map, &k);
199 assert(in.inserted);
200 v = MyArrayMap_Iter_get(&in.iter);
201 v->key = IntArray_from_view(k);
202 v->val = 42;
203
204 printf("entries:\n");
205 it = MyArrayMap_iter(&map);
206 for (MyArrayMap_Entry* p = MyArrayMap_Iter_get(&it); p != NULL;
207 p = MyArrayMap_Iter_next(&it)) {
208 IntArray_dump(&p->key);
209 printf("->%f\n", p->val);
210 }
211 printf("\n");
212
213 MyArrayMap_dump(&map);
214 MyArrayMap_destroy(&map);
215
216 return 0;
217 }