SpiecsEngine
 
Loading...
Searching...
No Matches
RadixTrie.h
Go to the documentation of this file.
1/**
2* @file RadixTrie.h.
3* @brief The RadixTrie Class Definitions and Implementation.
4* @author tcmalloc.
5*/
6
7#pragma once
8#include "Core/Memory/MemoryPool.h"
9#include "Core/Memory/ObjectPool.h"
10
11namespace scl {
12
13 /**
14 * @brief Declare of radix_trie.
15 * @tparam BITS Bits number.
16 * @tparam LAYER page index layer.
17 */
18 template<size_t BITS, size_t LAYER>
19 class radix_trie;
20
21 /**
22 * @brief Implementation of radix_trie with 1 LAYER.
23 * @tparam BITS Bits number.
24 */
25 template<size_t BITS>
26 class radix_trie<BITS, 1>
27 {
28 private:
29
30 /**
31 * @brief array length.
32 */
33 static constexpr size_t LENGTH = 1 << BITS;
34
35 /**
36 * @brief Leaf Defines.
37 */
38 struct Leaf
39 {
40 std::array<void*, LENGTH> values { nullptr };
41 };
42
43 /**
44 * @brief Root Node.
45 */
47
48 public:
49
50 /**
51 * @brief Constructor Function.
52 */
53 explicit radix_trie()
54 {
55 m_Root = new Leaf;
56 }
57
58 /**
59 * @brief Deconstruct Function.
60 */
61 virtual ~radix_trie()
62 {
63 if (m_Root)
64 {
65 delete m_Root;
66 }
67 }
68
69 /**
70 * @brief Get item by key.
71 * @param[in] k key.
72 * @return Returns value;
73 */
74 void* get(size_t k) const
75 {
76 if ((k >> BITS) > 0)
77 {
78 return nullptr;
79 }
80
81 return m_Root->values[k];
82 }
83
84 /**
85 * @brief Set pair of key - value.
86 * @param[in] k key.
87 * @param[in] v value.
88 */
89 void set(size_t k, void* v)
90 {
91 assert(k >> BITS == 0);
92
93 m_Root->values[k] = v;
94 }
95 };
96
97 /**
98 * @brief Implementation of radix_trie with 2 LAYER.
99 * @tparam BITS Bits number.
100 */
101 template<size_t BITS>
102 class radix_trie<BITS, 2>
103 {
104 private:
105
106 /**
107 * @brief root bits.
108 */
109 static constexpr size_t ROOT_BITS = 5;
110
111 /**
112 * @brief root array length.
113 */
114 static constexpr size_t ROOT_LENGTH = 1 << ROOT_BITS;
115
116 /**
117 * @brief leaf bits.
118 */
119 static constexpr size_t LEAF_BITS = BITS - ROOT_BITS;
120
121 /**
122 * @brief leaf array length.
123 */
124 static constexpr size_t LEAF_LENGTH = 1 << LEAF_BITS;
125
126 /**
127 * @brief Node Defines.
128 */
129 struct Node
130 {
131 std::array<Node*, ROOT_LENGTH> ptrs { nullptr };
132 };
133
134 /**
135 * @brief Leaf Defines.
136 */
137 struct Leaf
138 {
139 std::array<void*, LEAF_LENGTH> values { nullptr };
140 };
141
142 /**
143 * @brief Root Node.
144 */
146
147 /**
148 * @brief ObjectPool of Leaf.
149 */
151
152 public:
153
154 /**
155 * @brief Constructor Function.
156 */
157 explicit radix_trie()
159 {
160 m_Root = new Node;
161 }
162
163 /**
164 * @brief Deconstruct Function.
165 */
166 virtual ~radix_trie()
167 {
168 if (m_Root)
169 {
170 delete m_Root;
171 }
172 }
173
174 /**
175 * @brief Get item by key.
176 * @param[in] k key.
177 * @return Returns value;
178 */
179 void* get(size_t k) const
180 {
181 const size_t i1 = k >> LEAF_BITS;
182 const size_t i2 = k & (LEAF_LENGTH - 1 );
183
184 if ((k >> BITS) > 0 || m_Root->ptrs[i1] == nullptr)
185 {
186 return nullptr;
187 }
188
189 return reinterpret_cast<Leaf*>(m_Root->ptrs[i1])->values[i2];
190 }
191
192 /**
193 * @brief Set pair of key - value.
194 * @param[in] k key.
195 * @param[in] v value.
196 */
197 void set(size_t k, void* v)
198 {
199 assert(k >> BITS == 0);
200
201 const size_t i1 = k >> LEAF_BITS;
202 const size_t i2 = k & (LEAF_LENGTH - 1);
203
204 assert(i1 < ROOT_LENGTH);
205 assert(i2 < LEAF_LENGTH);
206
207 if (!m_Root->ptrs[i1])
208 {
209 m_Root->ptrs[i1] = reinterpret_cast<Node*>(m_LeafPool.New());
210 }
211
212 reinterpret_cast<Leaf*>(m_Root->ptrs[i1])->values[i2] = v;
213 }
214 };
215
216 /**
217 * @brief Implementation of radix_trie with 3 LAYER.
218 * @tparam BITS Bits number.
219 */
220 template<size_t BITS>
221 class radix_trie<BITS, 3>
222 {
223 private:
224
225 /**
226 * @brief interior bits.
227 */
228 static constexpr size_t INTERIOR_BITS = (BITS + 2) / 3;
229
230 /**
231 * @brief interior array length.
232 */
233 static constexpr size_t INTERIOR_LENGTH = 1 << INTERIOR_BITS;
234
235 /**
236 * @brief leaf bits.
237 */
238 static constexpr size_t LEAF_BITS = BITS - 2 * INTERIOR_BITS;
239
240 /**
241 * @brief leaf array length.
242 */
243 static constexpr size_t LEAF_LENGTH = 1 << LEAF_BITS;
244
245 /**
246 * @brief Node Defines.
247 */
248 struct Node
249 {
251 };
252
253 /**
254 * @brief Leaf Defines.
255 */
256 struct Leaf
257 {
258 std::array<void*, LEAF_LENGTH> values { nullptr };
259 };
260
261 /**
262 * @brief Root Node.
263 */
265
266 /**
267 * @brief ObjectPool of Node.
268 */
270
271 /**
272 * @brief ObjectPool of Leaf.
273 */
275
276 public:
277
278 /**
279 * @brief Constructor Function.
280 */
281 explicit radix_trie()
284 {
285 m_Root = m_NodePool.New();
286 }
287
288 /**
289 * @brief Deconstruct Function.
290 */
291 virtual ~radix_trie() = default;
292
293 /**
294 * @brief Get item by key.
295 * @param[in] k key.
296 * @return Returns value;
297 */
298 void* get(size_t k) const
299 {
300 const size_t i1 = k >> (LEAF_BITS + INTERIOR_BITS);
301 const size_t i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
302 const size_t i3 = k & (LEAF_LENGTH - 1);
303
304 if((k >> BITS) || m_Root->ptrs[i1] == nullptr || m_Root->ptrs[i1]->ptrs[i2] == nullptr)
305 {
306 return nullptr;
307 }
308
309 return reinterpret_cast<Leaf*>(m_Root->ptrs[i1]->ptrs[i2])->values[i3];
310 }
311
312 /**
313 * @brief Set pair of key - value.
314 * @param[in] k key.
315 * @param[in] v value.
316 */
317 void set(size_t k, void* v)
318 {
319 assert(k >> BITS == 0);
320
321 const size_t i1 = k >> (LEAF_BITS + INTERIOR_BITS);
322 const size_t i2 = (k >> LEAF_BITS) & (INTERIOR_LENGTH - 1);
323 const size_t i3 = k & (LEAF_LENGTH - 1);
324
325 assert(i1 < INTERIOR_LENGTH);
326 assert(i2 < INTERIOR_LENGTH);
327 assert(i3 < LEAF_LENGTH);
328
329 if (!m_Root->ptrs[i1])
330 {
331 m_Root->ptrs[i1] = m_NodePool.New();
332 }
333
334 if (!m_Root->ptrs[i1]->ptrs[i2])
335 {
336 m_Root->ptrs[i1]->ptrs[i2] = reinterpret_cast<Node*>(m_LeafPool.New());
337 }
338
339 reinterpret_cast<Leaf*>(m_Root->ptrs[i1]->ptrs[i2])->values[i3] = v;
340 }
341 };
342}
CentralCache()=default
Constructor Function.
static scl::span * GetOneSpan(scl::span_list &list, size_t size)
Get a not empty span.
size_t FetchRange(void *&start, void *&end, size_t batchNum, size_t size)
Fetch range memory to tc.
static CentralCache m_CentralCache
Single instance of this.
CentralCache & operator=(const CentralCache &)=delete
Copy Assignment Operation.
virtual ~CentralCache()=default
Destructor Function.
void ReleaseListToSpans(void *start, size_t size)
Release memory to pc.
std::array< scl::span_list, MemoryPool::FREE_LIST_NUM > m_SpanLists
FreeList Array.
CentralCache(const CentralCache &)=delete
Copy Constructor Function.
static CentralCache * Get()
Get this single Instance.
Central memory cache. Second level of memory allocator.
static void *& PointerSpace(void *obj)
Get object first 4/8 bytes as a pointer.
MemoryPool Class.
Definition MemoryPool.h:22
ObjectPool Class. Specific situation(Fixed size of block) of MemoryPool.
Definition ObjectPool.h:31
scl::radix_trie< 64 - MemoryPool::PAGE_SHIFT, 3 > m_IdSpanMap
radix trie for [pageId - span]
Definition PageCache.h:106
virtual ~PageCache()=default
Destructor Function.
PageCache & operator=(const PageCache &)=delete
Copy Assignment Operation.
static PageCache m_PageCache
this single instance.
Definition PageCache.h:86
PageCache()=default
Constructor Function.
scl::span * MapObjectToSpan(void *obj) const
Find span by memory pointer.
Definition PageCache.cpp:21
scl::span * InternalNewSpan(size_t k)
Fetch pages span(internal call).
static PageCache * Get()
Get this single instance.
Definition PageCache.h:50
void ReleaseSpanToPageCache(scl::span *s)
Release span from cc to pc,.
Definition PageCache.cpp:43
std::mutex m_Mutex
mutex for pc.
Definition PageCache.h:101
scl::span * NewSpan(size_t k)
Fetch pages span.
Definition PageCache.cpp:14
ObjectPool< scl::span > m_SpanPool
ObjectPool for span.
Definition PageCache.h:96
PageCache(const PageCache &)=delete
Copy Constructor Function.
std::array< scl::span_list, MemoryPool::PAGE_NUM > m_SpanLists
FreeList Array.
Definition PageCache.h:91
Page memory cache. Third level of memory allocator.
Definition PageCache.h:21
Leaf * m_Root
Root Node.
Definition RadixTrie.h:46
radix_trie()
Constructor Function.
Definition RadixTrie.h:53
void * get(size_t k) const
Get item by key.
Definition RadixTrie.h:74
static constexpr size_t LENGTH
array length.
Definition RadixTrie.h:33
void set(size_t k, void *v)
Set pair of key - value.
Definition RadixTrie.h:89
virtual ~radix_trie()
Deconstruct Function.
Definition RadixTrie.h:61
virtual ~radix_trie()
Deconstruct Function.
Definition RadixTrie.h:166
void * get(size_t k) const
Get item by key.
Definition RadixTrie.h:179
Node * m_Root
Root Node.
Definition RadixTrie.h:145
static constexpr size_t ROOT_BITS
root bits.
Definition RadixTrie.h:109
static constexpr size_t LEAF_BITS
leaf bits.
Definition RadixTrie.h:119
radix_trie()
Constructor Function.
Definition RadixTrie.h:157
void set(size_t k, void *v)
Set pair of key - value.
Definition RadixTrie.h:197
static constexpr size_t LEAF_LENGTH
leaf array length.
Definition RadixTrie.h:124
Spices::ObjectPool< Leaf > m_LeafPool
ObjectPool of Leaf.
Definition RadixTrie.h:150
static constexpr size_t ROOT_LENGTH
root array length.
Definition RadixTrie.h:114
static constexpr size_t LEAF_LENGTH
leaf array length.
Definition RadixTrie.h:243
radix_trie()
Constructor Function.
Definition RadixTrie.h:281
void * get(size_t k) const
Get item by key.
Definition RadixTrie.h:298
void set(size_t k, void *v)
Set pair of key - value.
Definition RadixTrie.h:317
Node * m_Root
Root Node.
Definition RadixTrie.h:264
virtual ~radix_trie()=default
Deconstruct Function.
static constexpr size_t INTERIOR_LENGTH
interior array length.
Definition RadixTrie.h:233
Spices::ObjectPool< Leaf > m_LeafPool
ObjectPool of Leaf.
Definition RadixTrie.h:274
static constexpr size_t INTERIOR_BITS
interior bits.
Definition RadixTrie.h:228
Spices::ObjectPool< Node > m_NodePool
ObjectPool of Node.
Definition RadixTrie.h:269
static constexpr size_t LEAF_BITS
leaf bits.
Definition RadixTrie.h:238
void PushFront(span *s)
Push a span to this list.
Definition SpanList.cpp:40
span * Begin() const
Get begin pointer.
Definition SpanList.cpp:59
span * End() const
Get end pointer.
Definition SpanList.cpp:64
Bidirectional cyclic linked list for span.
Definition SpanList.h:73
span * m_Next
next span.
Definition SpanList.h:41
void * m_FreeList
current pointer.
Definition SpanList.h:51
bool m_IsUse
True if in use.
Definition SpanList.h:61
Used for manage multiple page memory.
Definition SpanList.h:15
ObjectPoolSizeMode
enum of ObjectPool expand mode
Definition ObjectPool.h:19
std::array< void *, LENGTH > values
Definition RadixTrie.h:40
std::array< void *, LEAF_LENGTH > values
Definition RadixTrie.h:139
std::array< Node *, ROOT_LENGTH > ptrs
Definition RadixTrie.h:131
std::array< void *, LEAF_LENGTH > values
Definition RadixTrie.h:258
std::array< Node *, INTERIOR_LENGTH > ptrs
Definition RadixTrie.h:250