29#ifndef __BASE_TRIE_HH__
30#define __BASE_TRIE_HH__
53template <
class Key,
class Value>
57 static_assert(std::is_integral_v<Key>,
"Key has to be an integral type");
135 static const unsigned MaxBits =
sizeof(Key) * 8;
171 const Key msb = 1ULL << (
MaxBits - 1);
172 return orig | (orig >> 1) | msb;
191 node = node->
kids[0];
193 node = node->
kids[1];
219 Key new_mask = ~(Key)0;
233 Key cur_mask = node->
mask;
235 if (cur_mask == new_mask) {
236 assert(!node->
value);
241 for (
unsigned int i = 0;
i < 2;
i++) {
246 new_node =
new Node(key, new_mask,
val);
257 last_mask = cur_mask;
259 done = ((key & cur_mask) != (kid->
key & cur_mask)) ||
260 last_mask == new_mask;
262 cur_mask = last_mask;
265 if (cur_mask == node->
mask)
269 new_node =
new Node(key, cur_mask, NULL);
272 new_node->
kids[0] = kid;
276 if (cur_mask == new_mask) {
282 new_node =
new Node(key, new_mask,
val);
284 kid->
kids[1] = new_node;
288 panic(
"Reached the end of the Trie insert function!\n");
327 panic(
"Trie: Can't remove root node.\n");
335 if (parent->
kids[0] == node)
337 else if (parent->
kids[1] == node)
340 panic(
"Trie: Inconsistent parent/kid relationship.\n");
342 if (parent->
kids[1] && !parent->
kids[0]) {
344 parent->
kids[1] = NULL;
388 dump(
const char *title, std::ostream &
os=std::cout)
390 ccprintf(
os,
"**************************************************\n");
391 ccprintf(
os,
"*** Start of Trie: %s\n", title);
392 ccprintf(
os,
"*** (parent, me, key, mask, value pointer)\n");
393 ccprintf(
os,
"**************************************************\n");
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
A trie is a tree-based data structure used for data retrieval.
Handle lookupHandle(Key key)
Method which looks up the Handle corresponding to a particular key.
void dump(const char *title, std::ostream &os=std::cout)
A debugging method which prints the contents of this trie.
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.
Key extendMask(Key orig)
A utility method which extends a mask value one more bit towards the lsb.
static const unsigned MaxBits
Value * remove(Handle handle)
Method to delete a value from the trie.
Handle insert(Key key, unsigned width, Value *val)
Method which inserts a key/value pair into the trie.
Value * lookup(Key key)
Method which looks up the Value corresponding to a particular key.
void clear()
A method which removes all key/value pairs from the trie.
Value * remove(Key key)
Method to lookup a value from the trie and then delete it.
#define panic(...)
This implements a cprintf based panic() function.
Reference material can be found at the JEDEC website: UFS standard http://www.jedec....
void ccprintf(cp::Print &print)
Node(Key _key, Key _mask, Value *_val)
void dump(std::ostream &os, int level)