2 * Copyright (c) 2012 Google
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.
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.
31 #ifndef __BASE_TRIE_HH__
32 #define __BASE_TRIE_HH__
36 #include "base/cprintf.hh"
37 #include "base/misc.hh"
38 #include "base/types.hh"
40 // Key has to be an integral type.
41 template <class Key, class Value>
53 return (test & mask) == key;
61 Node(Key _key, Key _mask, Value *_val) :
62 key(_key & _mask), mask(_mask), value(_val),
87 for (int i = 1; i < level; i++) {
94 cprintf("(%p, %p, %#X, %#X, %p)\n", parent, this, key, mask, value);
96 kids[0]->dump(level + 1);
98 kids[1]->dump(level + 1);
106 typedef Node *Handle;
108 Trie() : head(0, 0, NULL)
111 static const unsigned MaxBits = sizeof(Key) * 8;
115 * A utility method which checks whether the key being looked up lies
116 * beyond the Node being examined. If so, it returns true and advances the
117 * node being examined.
118 * @param parent The node we're currently "at", which can be updated.
119 * @param kid The node we may want to move to.
120 * @param key The key we're looking for.
121 * @param new_mask The mask to use when matching against the key.
122 * @return Whether the current Node was advanced.
125 goesAfter(Node **parent, Node *kid, Key key, Key new_mask)
127 if (kid && kid->matches(key) && (kid->mask & new_mask) == kid->mask) {
136 * A utility method which extends a mask value one more bit towards the
137 * lsb. This is almost just a signed right shift, except that the shifted
138 * in bits are technically undefined. This is also slightly complicated by
140 * @param orig The original mask to extend.
141 * @return The extended mask.
146 // Just in case orig was 0.
147 const Key msb = ULL(1) << (MaxBits - 1);
148 return orig | (orig >> 1) | msb;
152 * Method which looks up the Handle corresponding to a particular key. This
153 * is useful if you want to delete the Handle corresponding to a key since
154 * the "remove" function takes a Handle as its argument.
155 * @param key The key to look up.
156 * @return The first Handle matching this key, or NULL if none was found.
159 lookupHandle(Key key)
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];
179 * Method which inserts a key/value pair into the trie.
180 * @param key The key which can later be used to look up this value.
181 * @param width How many bits of the key (from msb) should be used.
182 * @param val A pointer to the value to store in the trie.
183 * @return A Handle corresponding to this value.
186 insert(Key key, unsigned width, Value *val)
188 // Build a mask which masks off all the bits we don't care about.
189 Key new_mask = ~(Key)0;
191 new_mask <<= (MaxBits - width);
192 // Use it to tidy up the key.
195 // Walk past all the nodes this new node will be inserted after. They
196 // can be ignored for the purposes of this function.
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;
204 // If we're already where the value needs to be...
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];
215 // No kid. Add a new one.
216 new_node = new Node(key, new_mask, val);
217 new_node->parent = node;
222 // Walk down the leg until something doesn't match or we run out
227 last_mask = cur_mask;
228 cur_mask = extendMask(cur_mask);
229 done = ((key & cur_mask) != (kid->key & cur_mask)) ||
230 last_mask == new_mask;
232 cur_mask = last_mask;
234 // If this isn't the right leg to go down at all, skip it.
235 if (cur_mask == node->mask)
238 // At the point we walked to above, add a new node.
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;
245 // If we ran out of bits, the value goes right here.
246 if (cur_mask == new_mask) {
247 new_node->value = val;
251 // Still more bits to deal with, so add a new node for that path.
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");
263 * Method which looks up the Value corresponding to a particular key.
264 * @param key The key to look up.
265 * @return The first Value matching this key, or NULL if none was found.
270 Node *node = lookupHandle(key);
278 * Method to delete a value from the trie.
279 * @param node A Handle to remove.
280 * @return The Value pointer from the removed entry.
283 remove(Handle handle)
286 Value *val = node->value;
293 panic("Trie: Can't remove root node.\n");
295 Node *parent = node->parent;
297 // If there's a kid, fix up it's parent pointer.
299 node->kids[0]->parent = parent;
300 // Figure out which kid we are, and update our parent's pointers.
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");
307 // Make sure if the parent only has one kid, it's kid[0].
308 if (parent->kids[1] && !parent->kids[0]) {
309 parent->kids[0] = parent->kids[1];
310 parent->kids[1] = NULL;
313 // If the parent has less than two kids and no cargo and isn't the
314 // root, delete it too.
315 if (!parent->kids[1] && !parent->value && parent->parent)
322 * Method to lookup a value from the trie and then delete it.
323 * @param key The key to look up and then remove.
324 * @return The Value pointer from the removed entry, if any.
329 Handle handle = lookupHandle(key);
332 return remove(handle);
336 * A method which removes all key/value pairs from the trie. This is more
337 * efficient than trying to remove elements individually.
346 * A debugging method which prints the contents of this trie.
347 * @param title An identifying title to put in the dump header.
350 dump(const char *title)
352 cprintf("**************************************************\n");
353 cprintf("*** Start of Trie: %s\n", title);
354 cprintf("*** (parent, me, key, mask, value pointer)\n");
355 cprintf("**************************************************\n");