31 #ifndef __BASE_TRIE_HH__
32 #define __BASE_TRIE_HH__
41 template <
class Key,
class Value>
61 Node(Key _key, Key _mask, Value *_val) :
111 static const unsigned MaxBits =
sizeof(Key) * 8;
125 goesAfter(Node **parent, Node *kid, Key key, Key new_mask)
127 if (kid && kid->matches(key) && (kid->mask & new_mask) == kid->mask) {
148 return orig | (orig >> 1) | msb;
166 if (node->kids[0] && node->kids[0]->matches(key))
167 node = node->
kids[0];
168 else if (node->kids[1] && node->kids[1]->matches(key))
169 node = node->kids[1];
189 Key new_mask = ~(Key)0;
198 while (
goesAfter(&node, node->kids[0], key, new_mask) ||
199 goesAfter(&node, node->kids[1], key, new_mask))
203 Key cur_mask = node->mask;
205 if (cur_mask == new_mask) {
206 assert(!node->value);
211 for (
unsigned int i = 0;
i < 2;
i++) {
212 Node *&kid = node->kids[
i];
216 new_node =
new Node(key, new_mask, val);
217 new_node->parent = node;
227 last_mask = cur_mask;
229 done = ((key & cur_mask) != (kid->key & cur_mask)) ||
230 last_mask == new_mask;
232 cur_mask = last_mask;
235 if (cur_mask == node->mask)
239 new_node =
new Node(key, cur_mask, NULL);
240 new_node->parent = node;
241 kid->parent = new_node;
242 new_node->kids[0] = kid;
246 if (cur_mask == new_mask) {
247 new_node->value =
val;
252 new_node =
new Node(key, new_mask, val);
253 new_node->parent = kid;
254 kid->kids[1] = new_node;
258 panic(
"Reached the end of the Trie insert function!\n");
286 Value *
val = node->value;
293 panic(
"Trie: Can't remove root node.\n");
295 Node *parent = node->parent;
299 node->kids[0]->parent = parent;
301 if (parent->kids[0] == node)
302 parent->kids[0] = node->kids[0];
303 else if (parent->kids[1] == node)
304 parent->kids[1] = node->kids[0];
306 panic(
"Trie: Inconsistent parent/kid relationship.\n");
308 if (parent->kids[1] && !parent->kids[0]) {
309 parent->kids[0] = parent->kids[1];
310 parent->kids[1] = NULL;
315 if (!parent->kids[1] && !parent->value && parent->parent)
332 return remove(handle);
352 cprintf(
"**************************************************\n");
353 cprintf(
"*** Start of Trie: %s\n", title);
354 cprintf(
"*** (parent, me, key, mask, value pointer)\n");
355 cprintf(
"**************************************************\n");
Handle insert(Key key, unsigned width, Value *val)
Method which inserts a key/value pair into the 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...
static const unsigned MaxBits
Node(Key _key, Key _mask, Value *_val)
void clear()
A method which removes all key/value pairs from the trie.
Handle lookupHandle(Key key)
Method which looks up the Handle corresponding to a particular key.
Key extendMask(Key orig)
A utility method which extends a mask value one more bit towards the lsb.
Value * lookup(Key key)
Method which looks up the Value corresponding to a particular key.
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,16,32,64}_t.
#define ULL(N)
uint64_t constant
void dump(const char *title)
A debugging method which prints the contents of this trie.
if(it_gpu==gpuTypeMap.end())
void cprintf(const char *format, const Args &...args)