gem5 v24.0.0.0
Loading...
Searching...
No Matches
trie.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2012 Google
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29#ifndef __BASE_TRIE_HH__
30#define __BASE_TRIE_HH__
31
32#include <cassert>
33#include <iostream>
34#include <type_traits>
35
36#include "base/cprintf.hh"
37#include "base/logging.hh"
38#include "base/types.hh"
39
40namespace gem5
41{
42
53template <class Key, class Value>
54class Trie
55{
56 protected:
57 static_assert(std::is_integral_v<Key>, "Key has to be an integral type");
58
59 struct Node
60 {
61 Key key;
62 Key mask;
63
64 bool
66 {
67 return (test & mask) == key;
68 }
69
71
73 std::unique_ptr<Node> kids[2];
74
75 Node(Key _key, Key _mask, Value *_val) :
76 key(_key & _mask), mask(_mask), value(_val),
77 parent(NULL)
78 {
79 kids[0] = NULL;
80 kids[1] = NULL;
81 }
82
83 void
85 {
86 kids[1].reset();
87 kids[0].reset();
88 }
89
90 void
91 dump(std::ostream &os, int level)
92 {
93 for (int i = 1; i < level; i++) {
94 ccprintf(os, "|");
95 }
96 if (level == 0)
97 ccprintf(os, "Root ");
98 else
99 ccprintf(os, "+ ");
100 ccprintf(os, "(%p, %p, %#X, %#X, %p)\n",
101 parent, this, key, mask, value);
102 if (kids[0])
103 kids[0]->dump(os, level + 1);
104 if (kids[1])
105 kids[1]->dump(os, level + 1);
106 }
107 };
108
109 protected:
111
112 public:
116 typedef Node *Handle;
117
121 Trie() : head(0, 0, NULL)
122 {}
123
127 static const unsigned MaxBits = sizeof(Key) * 8;
128
129 private:
140 bool
141 goesAfter(Node **parent, Node *kid, Key key, Key new_mask)
142 {
143 if (kid && kid->matches(key) && (kid->mask & new_mask) == kid->mask) {
144 *parent = kid;
145 return true;
146 } else {
147 return false;
148 }
149 }
150
159 Key
160 extendMask(Key orig)
161 {
162 // Just in case orig was 0.
163 const Key msb = 1ULL << (MaxBits - 1);
164 return orig | (orig >> 1) | msb;
165 }
166
174 Handle
176 {
177 Node *node = &head;
178 while (node) {
179 if (node->value)
180 return node;
181
182 if (node->kids[0] && node->kids[0]->matches(key))
183 node = node->kids[0].get();
184 else if (node->kids[1] && node->kids[1]->matches(key))
185 node = node->kids[1].get();
186 else
187 node = NULL;
188 }
189
190 return NULL;
191 }
192
193 public:
203 Handle
204 insert(Key key, unsigned width, Value *val)
205 {
206 // We use NULL value pointers to mark internal nodes of the trie, so
207 // we don't allow inserting them as real values.
208 assert(val);
209
210 // Build a mask which masks off all the bits we don't care about.
211 Key new_mask = ~(Key)0;
212 if (width < MaxBits)
213 new_mask <<= (MaxBits - width);
214 // Use it to tidy up the key.
215 key &= new_mask;
216
217 // Walk past all the nodes this new node will be inserted after. They
218 // can be ignored for the purposes of this function.
219 Node *node = &head;
220 while (goesAfter(&node, node->kids[0].get(), key, new_mask) ||
221 goesAfter(&node, node->kids[1].get(), key, new_mask))
222 {}
223 assert(node);
224
225 Key cur_mask = node->mask;
226 // If we're already where the value needs to be...
227 if (cur_mask == new_mask) {
228 assert(!node->value);
229 node->value = val;
230 return node;
231 }
232
233 for (unsigned int i = 0; i < 2; i++) {
234 auto& kid = node->kids[i];
235 if (!kid) {
236 // No kid. Add a new one.
237 auto new_node = std::make_unique<Node>(key, new_mask, val);
238 new_node->parent = node;
239 kid = std::move(new_node);
240 return kid.get();
241 }
242
243 // Walk down the leg until something doesn't match or we run out
244 // of bits.
245 Key last_mask;
246 bool done;
247 do {
248 last_mask = cur_mask;
249 cur_mask = extendMask(cur_mask);
250 done = ((key & cur_mask) != (kid->key & cur_mask)) ||
251 last_mask == new_mask;
252 } while (!done);
253 cur_mask = last_mask;
254
255 // If this isn't the right leg to go down at all, skip it.
256 if (cur_mask == node->mask)
257 continue;
258
259 // At the point we walked to above, add a new node.
260 auto new_node = std::make_unique<Node>(key, cur_mask, nullptr);
261 new_node->parent = node;
262 kid->parent = new_node.get();
263 new_node->kids[0] = std::move(kid);
264 kid = std::move(new_node);
265
266 // If we ran out of bits, the value goes right here.
267 if (cur_mask == new_mask) {
268 kid->value = val;
269 return kid.get();
270 }
271
272 // Still more bits to deal with, so add a new node for that path.
273 new_node = std::make_unique<Node>(key, new_mask, val);
274 new_node->parent = kid.get();
275 kid->kids[1] = std::move(new_node);
276 return kid->kids[1].get();
277 }
278
279 panic("Reached the end of the Trie insert function!\n");
280 return NULL;
281 }
282
290 Value *
291 lookup(Key key)
292 {
293 Node *node = lookupHandle(key);
294 if (node)
295 return node->value;
296 else
297 return NULL;
298 }
299
307 Value *
309 {
310 Node *node = handle;
311 Value *val = node->value;
312 if (node->kids[1]) {
313 assert(node->value);
314 node->value = NULL;
315 return val;
316 }
317 if (!node->parent)
318 panic("Trie: Can't remove root node.\n");
319
320 Node *parent = node->parent;
321
322 // If there's a kid, fix up it's parent pointer.
323 if (node->kids[0])
324 node->kids[0]->parent = parent;
325 // Figure out which kid we are, and update our parent's pointers.
326 if (parent->kids[0].get() == node)
327 parent->kids[0] = std::move(node->kids[0]);
328 else if (parent->kids[1].get() == node)
329 parent->kids[1] = std::move(node->kids[0]);
330 else
331 panic("Trie: Inconsistent parent/kid relationship.\n");
332 // Make sure if the parent only has one kid, it's kid[0].
333 if (parent->kids[1] && !parent->kids[0]) {
334 parent->kids[0] = std::move(parent->kids[1]);
335 parent->kids[1] = nullptr;
336 }
337
338 // If the parent has less than two kids and no cargo and isn't the
339 // root, delete it too.
340 if (!parent->kids[1] && !parent->value && parent->parent)
341 remove(parent);
342 return val;
343 }
344
352 Value *
353 remove(Key key)
354 {
355 Handle handle = lookupHandle(key);
356 if (!handle)
357 return NULL;
358 return remove(handle);
359 }
360
367 void
369 {
370 head.clear();
371 }
372
377 void
378 dump(const char *title, std::ostream &os=std::cout)
379 {
380 ccprintf(os, "**************************************************\n");
381 ccprintf(os, "*** Start of Trie: %s\n", title);
382 ccprintf(os, "*** (parent, me, key, mask, value pointer)\n");
383 ccprintf(os, "**************************************************\n");
384 head.dump(os, 0);
385 }
386};
387
388} // namespace gem5
389
390#endif
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
A trie is a tree-based data structure used for data retrieval.
Definition trie.hh:55
Handle lookupHandle(Key key)
Method which looks up the Handle corresponding to a particular key.
Definition trie.hh:175
void dump(const char *title, std::ostream &os=std::cout)
A debugging method which prints the contents of this trie.
Definition trie.hh:378
bool goesAfter(Node **parent, Node *kid, Key key, Key new_mask)
A utility method which checks whether the key being looked up lies beyond the Node being examined.
Definition trie.hh:141
Key extendMask(Key orig)
A utility method which extends a mask value one more bit towards the lsb.
Definition trie.hh:160
Node head
Definition trie.hh:110
static const unsigned MaxBits
Definition trie.hh:127
Value * remove(Handle handle)
Method to delete a value from the trie.
Definition trie.hh:308
Handle insert(Key key, unsigned width, Value *val)
Method which inserts a key/value pair into the trie.
Definition trie.hh:204
Value * lookup(Key key)
Method which looks up the Value corresponding to a particular key.
Definition trie.hh:291
Node * Handle
Definition trie.hh:116
void clear()
A method which removes all key/value pairs from the trie.
Definition trie.hh:368
Value * remove(Key key)
Method to lookup a value from the trie and then delete it.
Definition trie.hh:353
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
Bitfield< 4 > width
Definition misc_types.hh:72
Bitfield< 7 > i
Definition misc_types.hh:67
Bitfield< 17 > os
Definition misc.hh:838
Bitfield< 63 > val
Definition misc.hh:804
Bitfield< 20 > level
Definition intmessage.hh:51
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
void ccprintf(cp::Print &print)
Definition cprintf.hh:130
std::unique_ptr< Node > kids[2]
Definition trie.hh:73
bool matches(Key test)
Definition trie.hh:65
Node(Key _key, Key _mask, Value *_val)
Definition trie.hh:75
void clear()
Definition trie.hh:84
void dump(std::ostream &os, int level)
Definition trie.hh:91
Node * parent
Definition trie.hh:72
Value * value
Definition trie.hh:70
Definition test.h:38

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0