forked from google/cwisstable
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstringmap.c
More file actions
124 lines (105 loc) · 3.41 KB
/
stringmap.c
File metadata and controls
124 lines (105 loc) · 3.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
// Copyright 2022 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// This file demonstrates the API of a cwisstable map using C-style strings as
// keys.
#include <math.h>
#include <stdio.h>
#include <string.h>
#if defined(CWISS_EXAMPLE_UNIFIED)
#include "cwisstable.h"
#elif defined(CWISS_EXAMPLE_SPLIT)
#include "cwisstable/declare.h"
#include "cwisstable/policy.h"
#else
#error "must set CWISS_EXAMPLE_UNIFIED or CWISS_EXAMPLE_SPLIT"
#endif
static inline void kCStrPolicy_copy(void* dst, const void* src) {
typedef struct {
char* k;
float v;
} entry;
const entry* e = (const entry*)src;
entry* d = (entry*)dst;
size_t len = strlen(e->k);
d->k = malloc(len + 1);
d->v = e->v;
memcpy(d->k, e->k, len + 1);
}
static inline void kCStrPolicy_dtor(void* val) {
char* str = *(char**)val;
free(str);
}
static inline size_t kCStrPolicy_hash(const void* val) {
const char* str = *(const char* const*)val;
size_t len = strlen(str);
CWISS_FxHash_State state = 0;
CWISS_FxHash_Write(&state, str, len);
return state;
}
static inline bool kCStrPolicy_eq(const void* a, const void* b) {
const char* ap = *(const char* const*)a;
const char* bp = *(const char* const*)b;
return strcmp(ap, bp) == 0;
}
CWISS_DECLARE_NODE_MAP_POLICY(kCStrPolicy, const char*, float,
(obj_copy, kCStrPolicy_copy),
(obj_dtor, kCStrPolicy_dtor),
(key_hash, kCStrPolicy_hash),
(key_eq, kCStrPolicy_eq));
CWISS_DECLARE_HASHMAP_WITH(MyCStrMap, const char*, float, kCStrPolicy);
static const char* kStrings[] = {
"abcd", "efgh", "ijkh", "lmno", "pqrs", "tuvw", "xyza", "bcde",
};
int main(void) {
MyCStrMap map = MyCStrMap_new(8);
for (int i = 0; i < 8; ++i) {
int val = i * i + 1;
MyCStrMap_Entry e = {kStrings[i], sin(val)};
MyCStrMap_dump(&map);
MyCStrMap_insert(&map, &e);
}
MyCStrMap_dump(&map);
printf("\n");
const char* k = "missing";
assert(!MyCStrMap_contains(&map, &k));
k = "lmno";
MyCStrMap_Iter it = MyCStrMap_find(&map, &k);
MyCStrMap_Entry* v = MyCStrMap_Iter_get(&it);
assert(v);
printf("5: %p: \"%s\"->%f\n", v, v->key, v->val);
MyCStrMap_rehash(&map, 16);
it = MyCStrMap_find(&map, &k);
v = MyCStrMap_Iter_get(&it);
assert(v);
printf("5: %p: \"%s\"->%f\n", v, v->key, v->val);
printf("entries:\n");
it = MyCStrMap_iter(&map);
for (MyCStrMap_Entry* p = MyCStrMap_Iter_get(&it); p != NULL;
p = MyCStrMap_Iter_next(&it)) {
printf("\"%s\"->%f\n", p->key, p->val);
}
printf("\n");
MyCStrMap_erase(&map, &k);
assert(!MyCStrMap_contains(&map, &k));
printf("entries:\n");
it = MyCStrMap_iter(&map);
for (MyCStrMap_Entry* p = MyCStrMap_Iter_get(&it); p != NULL;
p = MyCStrMap_Iter_next(&it)) {
printf("\"%s\"->%f\n", p->key, p->val);
}
printf("\n");
MyCStrMap_dump(&map);
MyCStrMap_destroy(&map);
return 0;
}