Debian Codegraph @

ext/cwisstable/cwisstable/internal/test_helpers.h

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 #ifndef CWISSTABLE_TEST_HELPERS_H_
16 #define CWISSTABLE_TEST_HELPERS_H_
17
18 // Helpers for interacting with C SwissTables from C++.
19
20 #include <cstdint>
21
22 #include "cwisstable.h"
23
24 namespace cwisstable {
25 template <typename T>
26 struct DefaultHash {
27 size_t operator()(const T& val) {
28 CWISS_AbslHash_State state = CWISS_AbslHash_kInit;
29 CWISS_AbslHash_Write(&state, &val, sizeof(T));
30 return CWISS_AbslHash_Finish(state);
31 }
32 };
33
34 struct HashStdString {
35 template <typename S>
36 size_t operator()(const S& s) {
37 CWISS_AbslHash_State state = CWISS_AbslHash_kInit;
38 size_t size = s.size();
39 CWISS_AbslHash_Write(&state, &size, sizeof(size_t));
40 CWISS_AbslHash_Write(&state, s.data(), s.size());
41 return CWISS_AbslHash_Finish(state);
42 }
43 };
44
45 template <typename T>
46 struct DefaultEq {
47 bool operator()(const T& a, const T& b) { return a == b; }
48 };
49
50 template <typename T, typename Hash, typename Eq>
51 struct FlatPolicyWrapper {
52 CWISS_GCC_PUSH
53 CWISS_GCC_ALLOW("-Waddress")
54 // clang-format off
55 CWISS_DECLARE_FLAT_SET_POLICY(kPolicy, T,
56 (modifiers, static constexpr),
57 (obj_copy, [](void* dst, const void* src) {
58 new (dst) T(*static_cast<const T*>(src));
59 }),
60 (obj_dtor, [](void* val) {
61 static_cast<T*>(val)->~T();
62 }),
63 (key_hash, [](const void* val) {
64 return Hash{}(*static_cast<const T*>(val));
65 }),
66 (key_eq, [](const void* a, const void* b) {
67 return Eq{}(*static_cast<const T*>(a), *static_cast<const T*>(b));
68 }),
69 (slot_transfer, [](void* dst, void* src) {
70 T* old = static_cast<T*>(src);
71 new (dst) T(std::move(*old));
72 old->~T();
73 }));
74 // clang-format on
75 CWISS_GCC_POP
76 };
77
78 template <typename T, typename Hash = DefaultHash<T>,
79 typename Eq = DefaultEq<T>>
80 constexpr const CWISS_Policy& FlatPolicy() {
81 return FlatPolicyWrapper<T, Hash, Eq>::kPolicy;
82 }
83
84 template <typename K, typename V, typename Hash, typename Eq>
85 struct FlatMapPolicyWrapper {
86 CWISS_GCC_PUSH
87 CWISS_GCC_ALLOW("-Waddress")
88 // clang-format off
89 CWISS_DECLARE_FLAT_MAP_POLICY(kPolicy, K, V,
90 (modifiers, static constexpr),
91 (obj_copy, [](void* dst, const void* src) {
92 new (dst) kPolicy_Entry(*static_cast<const kPolicy_Entry*>(src));
93 }),
94 (obj_dtor, [](void* val) {
95 static_cast<kPolicy_Entry*>(val)->~kPolicy_Entry();
96 }),
97 (key_hash, [](const void* val) {
98 return Hash{}(static_cast<const kPolicy_Entry*>(val)->k);
99 }),
100 (key_eq, [](const void* a, const void* b) {
101 return Eq{}(static_cast<const kPolicy_Entry*>(a)->k,
102 static_cast<const kPolicy_Entry*>(b)->k);
103 }),
104 (slot_transfer, [](void* dst, void* src) {
105 kPolicy_Entry* old = static_cast<kPolicy_Entry*>(src);
106 new (dst) kPolicy_Entry(std::move(*old));
107 old->~kPolicy_Entry();
108 }));
109 // clang-format on
110 CWISS_GCC_POP
111 };
112
113 template <typename K, typename V, typename Hash = DefaultHash<K>,
114 typename Eq = DefaultEq<K>>
115 constexpr const CWISS_Policy& FlatMapPolicy() {
116 return FlatMapPolicyWrapper<K, V, Hash, Eq>::kPolicy;
117 }
118
119 // Helpers for doing some operations on tables with minimal pain.
120 //
121 // This macro expands to functions that will form an overload set with other
122 // table types.
123 #define TABLE_HELPERS(HashSet_) \
124 CWISS_INLINE_ALWAYS \
125 inline HashSet_##_Entry* Find(HashSet_& set, const HashSet_##_Key& needle) { \
126 auto it = HashSet_##_find(&set, &needle); \
127 return HashSet_##_Iter_get(&it); \
128 } \
129 CWISS_INLINE_ALWAYS \
130 inline std::pair<HashSet_##_Entry*, bool> Insert( \
131 HashSet_& set, const HashSet_##_Entry& value) { \
132 auto it = HashSet_##_insert(&set, &value); \
133 return {HashSet_##_Iter_get(&it.iter), it.inserted}; \
134 } \
135 CWISS_INLINE_ALWAYS \
136 inline std::pair<HashSet_##_Entry*, bool> LazyInsert( \
137 HashSet_& set, const HashSet_##_Key& value) { \
138 auto it = HashSet_##_deferred_insert(&set, &value); \
139 return {HashSet_##_Iter_get(&it.iter), it.inserted}; \
140 } \
141 CWISS_INLINE_ALWAYS \
142 inline std::pair<HashSet_##_Entry*, bool> MoveInsert( \
143 HashSet_& set, HashSet_##_Entry&& value) { \
144 auto [ptr, inserted] = LazyInsert(set, value); \
145 if (inserted) { \
146 new (ptr) HashSet_##_Entry(value); \
147 } \
148 return {ptr, inserted}; \
149 } \
150 CWISS_INLINE_ALWAYS \
151 inline bool Erase(HashSet_& set, const HashSet_##_Key& needle) { \
152 return HashSet_##_erase(&set, &needle); \
153 } \
154 CWISS_INLINE_ALWAYS \
155 inline std::vector<HashSet_##_Entry> Collect(const HashSet_& set) { \
156 std::vector<HashSet_##_Entry> items; \
157 items.reserve(HashSet_##_size(&set)); \
158 for (auto it = HashSet_##_citer(&set); HashSet_##_CIter_get(&it); \
159 HashSet_##_CIter_next(&it)) { \
160 items.push_back(*HashSet_##_CIter_get(&it)); \
161 } \
162 return items; \
163 }
164 } // namespace cwisstable
165
166 #endif // CWISSTABLE_TEST_HELPERS_H_