blob: 0f730d18c7cf18ce03beb5e4835766ee0100cced [file] [log] [blame]
HYuana9b85752014-02-26 02:32:30 -06001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev319f2c82015-01-07 14:56:53 -08003 * Copyright (c) 2014-2015, Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology,
9 * The University of Memphis.
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -070010 *
11 * This file is part of NFD (Named Data Networking Forwarding Daemon).
12 * See AUTHORS.md for complete list of NFD authors and contributors.
13 *
14 * NFD is free software: you can redistribute it and/or modify it under the terms
15 * of the GNU General Public License as published by the Free Software Foundation,
16 * either version 3 of the License, or (at your option) any later version.
17 *
18 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
19 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
20 * PURPOSE. See the GNU General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License along with
23 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
Junxiao Shi2b73ca32014-11-17 19:16:08 -070024 */
HYuana9b85752014-02-26 02:32:30 -060025
26#include "name-tree.hpp"
27#include "core/logger.hpp"
Haowei Yuanf52dac72014-03-24 23:35:03 -050028#include "core/city-hash.hpp"
Davide Pesavento52a18f92014-04-10 00:55:01 +020029
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080030#include <boost/concept/assert.hpp>
31#include <boost/concept_check.hpp>
32#include <type_traits>
33
HYuana9b85752014-02-26 02:32:30 -060034namespace nfd {
35
36NFD_LOG_INIT("NameTree");
37
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -080038// http://en.cppreference.com/w/cpp/concept/ForwardIterator
39BOOST_CONCEPT_ASSERT((boost::ForwardIterator<NameTree::const_iterator>));
40// boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html,
41// which doesn't require DefaultConstructible
42#ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE
43static_assert(std::is_default_constructible<NameTree::const_iterator>::value,
44 "NameTree::const_iterator must be default-constructible");
45#else
46BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<NameTree::const_iterator>));
47#endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE
48
HYuana9b85752014-02-26 02:32:30 -060049namespace name_tree {
50
Haowei Yuanf52dac72014-03-24 23:35:03 -050051class Hash32
HYuana9b85752014-02-26 02:32:30 -060052{
Haowei Yuanf52dac72014-03-24 23:35:03 -050053public:
54 static size_t
55 compute(const char* buffer, size_t length)
56 {
57 return static_cast<size_t>(CityHash32(buffer, length));
58 }
59};
HYuana9b85752014-02-26 02:32:30 -060060
Haowei Yuanf52dac72014-03-24 23:35:03 -050061class Hash64
62{
63public:
64 static size_t
65 compute(const char* buffer, size_t length)
66 {
67 return static_cast<size_t>(CityHash64(buffer, length));
68 }
69};
HYuana9b85752014-02-26 02:32:30 -060070
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050071/// @cond NoDocumentation
Haowei Yuanf52dac72014-03-24 23:35:03 -050072typedef boost::mpl::if_c<sizeof(size_t) >= 8, Hash64, Hash32>::type CityHash;
Alexander Afanasyevb755e9d2015-10-20 17:35:51 -050073/// @endcond
Haowei Yuanf52dac72014-03-24 23:35:03 -050074
75// Interface of different hash functions
76size_t
77computeHash(const Name& prefix)
78{
79 prefix.wireEncode(); // guarantees prefix's wire buffer is not empty
80
81 size_t hashValue = 0;
82 size_t hashUpdate = 0;
83
84 for (Name::const_iterator it = prefix.begin(); it != prefix.end(); it++)
85 {
86 const char* wireFormat = reinterpret_cast<const char*>( it->wire() );
87 hashUpdate = CityHash::compute(wireFormat, it->size());
88 hashValue ^= hashUpdate;
89 }
90
91 return hashValue;
92}
93
94std::vector<size_t>
95computeHashSet(const Name& prefix)
96{
97 prefix.wireEncode(); // guarantees prefix's wire buffer is not empty
98
99 size_t hashValue = 0;
100 size_t hashUpdate = 0;
101
102 std::vector<size_t> hashValueSet;
103 hashValueSet.push_back(hashValue);
104
105 for (Name::const_iterator it = prefix.begin(); it != prefix.end(); it++)
106 {
107 const char* wireFormat = reinterpret_cast<const char*>( it->wire() );
108 hashUpdate = CityHash::compute(wireFormat, it->size());
109 hashValue ^= hashUpdate;
110 hashValueSet.push_back(hashValue);
111 }
112
113 return hashValueSet;
HYuana9b85752014-02-26 02:32:30 -0600114}
115
116} // namespace name_tree
117
118NameTree::NameTree(size_t nBuckets)
119 : m_nItems(0)
120 , m_nBuckets(nBuckets)
Haowei Yuanf52dac72014-03-24 23:35:03 -0500121 , m_minNBuckets(nBuckets)
122 , m_enlargeLoadFactor(0.5) // more than 50% buckets loaded
123 , m_enlargeFactor(2) // double the hash table size
124 , m_shrinkLoadFactor(0.1) // less than 10% buckets loaded
125 , m_shrinkFactor(0.5) // reduce the number of buckets by half
Haowei Yuane1079fc2014-03-08 14:41:25 -0600126 , m_endIterator(FULL_ENUMERATE_TYPE, *this, m_end)
HYuana9b85752014-02-26 02:32:30 -0600127{
Haowei Yuanf52dac72014-03-24 23:35:03 -0500128 m_enlargeThreshold = static_cast<size_t>(m_enlargeLoadFactor *
129 static_cast<double>(m_nBuckets));
130
131 m_shrinkThreshold = static_cast<size_t>(m_shrinkLoadFactor *
HYuana9b85752014-02-26 02:32:30 -0600132 static_cast<double>(m_nBuckets));
133
134 // array of node pointers
135 m_buckets = new name_tree::Node*[m_nBuckets];
136 // Initialize the pointer array
137 for (size_t i = 0; i < m_nBuckets; i++)
138 m_buckets[i] = 0;
139}
140
141NameTree::~NameTree()
142{
143 for (size_t i = 0; i < m_nBuckets; i++)
144 {
Haowei Yuanf52dac72014-03-24 23:35:03 -0500145 if (m_buckets[i] != 0) {
HYuana9b85752014-02-26 02:32:30 -0600146 delete m_buckets[i];
Haowei Yuanf52dac72014-03-24 23:35:03 -0500147 }
HYuana9b85752014-02-26 02:32:30 -0600148 }
149
150 delete [] m_buckets;
151}
152
153// insert() is a private function, and called by only lookup()
154std::pair<shared_ptr<name_tree::Entry>, bool>
155NameTree::insert(const Name& prefix)
156{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700157 NFD_LOG_TRACE("insert " << prefix);
HYuana9b85752014-02-26 02:32:30 -0600158
Haowei Yuanf52dac72014-03-24 23:35:03 -0500159 size_t hashValue = name_tree::computeHash(prefix);
160 size_t loc = hashValue % m_nBuckets;
HYuana9b85752014-02-26 02:32:30 -0600161
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700162 NFD_LOG_TRACE("Name " << prefix << " hash value = " << hashValue << " location = " << loc);
HYuana9b85752014-02-26 02:32:30 -0600163
164 // Check if this Name has been stored
165 name_tree::Node* node = m_buckets[loc];
166 name_tree::Node* nodePrev = node; // initialize nodePrev to node
167
168 for (node = m_buckets[loc]; node != 0; node = node->m_next)
169 {
170 if (static_cast<bool>(node->m_entry))
171 {
172 if (prefix == node->m_entry->m_prefix)
173 {
174 return std::make_pair(node->m_entry, false); // false: old entry
175 }
176 }
177 nodePrev = node;
178 }
179
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700180 NFD_LOG_TRACE("Did not find " << prefix << ", need to insert it to the table");
HYuana9b85752014-02-26 02:32:30 -0600181
182 // If no bucket is empty occupied, we need to create a new node, and it is
183 // linked from nodePrev
184 node = new name_tree::Node();
185 node->m_prev = nodePrev;
186
187 if (nodePrev == 0)
188 {
189 m_buckets[loc] = node;
190 }
191 else
192 {
193 nodePrev->m_next = node;
194 }
195
196 // Create a new Entry
197 shared_ptr<name_tree::Entry> entry(make_shared<name_tree::Entry>(prefix));
198 entry->setHash(hashValue);
199 node->m_entry = entry; // link the Entry to its Node
200 entry->m_node = node; // link the node to Entry. Used in eraseEntryIfEmpty.
201
202 return std::make_pair(entry, true); // true: new entry
203}
204
205// Name Prefix Lookup. Create Name Tree Entry if not found
206shared_ptr<name_tree::Entry>
207NameTree::lookup(const Name& prefix)
208{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700209 NFD_LOG_TRACE("lookup " << prefix);
HYuana9b85752014-02-26 02:32:30 -0600210
211 shared_ptr<name_tree::Entry> entry;
212 shared_ptr<name_tree::Entry> parent;
213
214 for (size_t i = 0; i <= prefix.size(); i++)
215 {
216 Name temp = prefix.getPrefix(i);
217
218 // insert() will create the entry if it does not exist.
219 std::pair<shared_ptr<name_tree::Entry>, bool> ret = insert(temp);
220 entry = ret.first;
221
222 if (ret.second == true)
223 {
Haowei Yuanf52dac72014-03-24 23:35:03 -0500224 m_nItems++; // Increase the counter
HYuana9b85752014-02-26 02:32:30 -0600225 entry->m_parent = parent;
226
227 if (static_cast<bool>(parent))
228 {
229 parent->m_children.push_back(entry);
230 }
231 }
232
Haowei Yuanf52dac72014-03-24 23:35:03 -0500233 if (m_nItems > m_enlargeThreshold)
HYuana9b85752014-02-26 02:32:30 -0600234 {
Haowei Yuanf52dac72014-03-24 23:35:03 -0500235 resize(m_enlargeFactor * m_nBuckets);
HYuana9b85752014-02-26 02:32:30 -0600236 }
237
238 parent = entry;
239 }
240 return entry;
241}
242
243// Exact Match
244shared_ptr<name_tree::Entry>
245NameTree::findExactMatch(const Name& prefix) const
246{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700247 NFD_LOG_TRACE("findExactMatch " << prefix);
HYuana9b85752014-02-26 02:32:30 -0600248
Haowei Yuanf52dac72014-03-24 23:35:03 -0500249 size_t hashValue = name_tree::computeHash(prefix);
250 size_t loc = hashValue % m_nBuckets;
HYuana9b85752014-02-26 02:32:30 -0600251
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700252 NFD_LOG_TRACE("Name " << prefix << " hash value = " << hashValue <<
HYuana9b85752014-02-26 02:32:30 -0600253 " location = " << loc);
254
255 shared_ptr<name_tree::Entry> entry;
256 name_tree::Node* node = 0;
257
258 for (node = m_buckets[loc]; node != 0; node = node->m_next)
259 {
260 entry = node->m_entry;
261 if (static_cast<bool>(entry))
262 {
263 if (hashValue == entry->getHash() && prefix == entry->getPrefix())
264 {
265 return entry;
266 }
267 } // if entry
268 } // for node
269
270 // if not found, the default value of entry (null pointer) will be returned
271 entry.reset();
272 return entry;
273}
274
275// Longest Prefix Match
HYuana9b85752014-02-26 02:32:30 -0600276shared_ptr<name_tree::Entry>
Haowei Yuane1079fc2014-03-08 14:41:25 -0600277NameTree::findLongestPrefixMatch(const Name& prefix, const name_tree::EntrySelector& entrySelector) const
HYuana9b85752014-02-26 02:32:30 -0600278{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700279 NFD_LOG_TRACE("findLongestPrefixMatch " << prefix);
HYuana9b85752014-02-26 02:32:30 -0600280
281 shared_ptr<name_tree::Entry> entry;
Haowei Yuanf52dac72014-03-24 23:35:03 -0500282 std::vector<size_t> hashValueSet = name_tree::computeHashSet(prefix);
HYuana9b85752014-02-26 02:32:30 -0600283
Haowei Yuanf52dac72014-03-24 23:35:03 -0500284 size_t hashValue = 0;
285 size_t loc = 0;
286
287 for (int i = static_cast<int>(prefix.size()); i >= 0; i--)
HYuana9b85752014-02-26 02:32:30 -0600288 {
Haowei Yuanf52dac72014-03-24 23:35:03 -0500289 hashValue = hashValueSet[i];
290 loc = hashValue % m_nBuckets;
291
292 name_tree::Node* node = 0;
293 for (node = m_buckets[loc]; node != 0; node = node->m_next)
294 {
295 entry = node->m_entry;
296 if (static_cast<bool>(entry))
297 {
298 // isPrefixOf() is used to avoid making a copy of the name
299 if (hashValue == entry->getHash() &&
300 entry->getPrefix().isPrefixOf(prefix) &&
301 entrySelector(*entry))
302 {
303 return entry;
304 }
305 } // if entry
306 } // for node
HYuana9b85752014-02-26 02:32:30 -0600307 }
308
Haowei Yuanf52dac72014-03-24 23:35:03 -0500309 // if not found, the default value of entry (null pointer) will be returned
310 entry.reset();
311 return entry;
HYuana9b85752014-02-26 02:32:30 -0600312}
313
HangZhangcb4fc832014-03-11 16:57:11 +0800314shared_ptr<name_tree::Entry>
315NameTree::findLongestPrefixMatch(shared_ptr<name_tree::Entry> entry,
316 const name_tree::EntrySelector& entrySelector) const
317{
318 while (static_cast<bool>(entry))
319 {
320 if (entrySelector(*entry))
321 return entry;
322 entry = entry->getParent();
323 }
324 return shared_ptr<name_tree::Entry>();
325}
326
HYuana9b85752014-02-26 02:32:30 -0600327// return {false: this entry is not empty, true: this entry is empty and erased}
328bool
329NameTree::eraseEntryIfEmpty(shared_ptr<name_tree::Entry> entry)
330{
331 BOOST_ASSERT(static_cast<bool>(entry));
332
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700333 NFD_LOG_TRACE("eraseEntryIfEmpty " << entry->getPrefix());
HYuana9b85752014-02-26 02:32:30 -0600334
335 // first check if this Entry can be erased
Junxiao Shi40631842014-03-01 13:52:37 -0700336 if (entry->isEmpty())
HYuana9b85752014-02-26 02:32:30 -0600337 {
338 // update child-related info in the parent
339 shared_ptr<name_tree::Entry> parent = entry->getParent();
340
341 if (static_cast<bool>(parent))
342 {
343 std::vector<shared_ptr<name_tree::Entry> >& parentChildrenList =
344 parent->getChildren();
345
346 bool isFound = false;
347 size_t size = parentChildrenList.size();
348 for (size_t i = 0; i < size; i++)
349 {
350 if (parentChildrenList[i] == entry)
351 {
352 parentChildrenList[i] = parentChildrenList[size - 1];
353 parentChildrenList.pop_back();
354 isFound = true;
355 break;
356 }
357 }
358
Junxiao Shiac7b4372014-12-13 21:47:04 -0700359 BOOST_VERIFY(isFound == true);
HYuana9b85752014-02-26 02:32:30 -0600360 }
361
362 // remove this Entry and its Name Tree Node
363 name_tree::Node* node = entry->m_node;
364 name_tree::Node* nodePrev = node->m_prev;
365
366 // configure the previous node
367 if (nodePrev != 0)
368 {
369 // link the previous node to the next node
370 nodePrev->m_next = node->m_next;
371 }
372 else
373 {
374 m_buckets[entry->getHash() % m_nBuckets] = node->m_next;
375 }
376
377 // link the previous node with the next node (skip the erased one)
378 if (node->m_next != 0)
379 {
380 node->m_next->m_prev = nodePrev;
381 node->m_next = 0;
382 }
383
384 BOOST_ASSERT(node->m_next == 0);
385
386 m_nItems--;
387 delete node;
388
389 if (static_cast<bool>(parent))
390 eraseEntryIfEmpty(parent);
391
Haowei Yuanf52dac72014-03-24 23:35:03 -0500392 size_t newNBuckets = static_cast<size_t>(m_shrinkFactor *
393 static_cast<double>(m_nBuckets));
394
395 if (newNBuckets >= m_minNBuckets && m_nItems < m_shrinkThreshold)
396 {
397 resize(newNBuckets);
398 }
399
HYuana9b85752014-02-26 02:32:30 -0600400 return true;
401
402 } // if this entry is empty
403
404 return false; // if this entry is not empty
405}
406
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700407boost::iterator_range<NameTree::const_iterator>
Haowei Yuane1079fc2014-03-08 14:41:25 -0600408NameTree::fullEnumerate(const name_tree::EntrySelector& entrySelector) const
HYuana9b85752014-02-26 02:32:30 -0600409{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700410 NFD_LOG_TRACE("fullEnumerate");
HYuana9b85752014-02-26 02:32:30 -0600411
Haowei Yuane1079fc2014-03-08 14:41:25 -0600412 // find the first eligible entry
Junxiao Shi60607c72014-11-26 22:40:36 -0700413 for (size_t i = 0; i < m_nBuckets; i++) {
414 for (name_tree::Node* node = m_buckets[i]; node != 0; node = node->m_next) {
415 if (static_cast<bool>(node->m_entry) && entrySelector(*node->m_entry)) {
416 const_iterator it(FULL_ENUMERATE_TYPE, *this, node->m_entry, entrySelector);
417 return {it, end()};
418 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600419 }
Junxiao Shi60607c72014-11-26 22:40:36 -0700420 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600421
422 // If none of the entry satisfies the requirements, then return the end() iterator.
Junxiao Shi60607c72014-11-26 22:40:36 -0700423 return {end(), end()};
Haowei Yuane1079fc2014-03-08 14:41:25 -0600424}
425
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700426boost::iterator_range<NameTree::const_iterator>
Haowei Yuane1079fc2014-03-08 14:41:25 -0600427NameTree::partialEnumerate(const Name& prefix,
428 const name_tree::EntrySubTreeSelector& entrySubTreeSelector) const
429{
430 // the first step is to process the root node
431 shared_ptr<name_tree::Entry> entry = findExactMatch(prefix);
Junxiao Shi60607c72014-11-26 22:40:36 -0700432 if (!static_cast<bool>(entry)) {
433 return {end(), end()};
434 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600435
436 std::pair<bool, bool>result = entrySubTreeSelector(*entry);
437 const_iterator it(PARTIAL_ENUMERATE_TYPE,
438 *this,
439 entry,
440 name_tree::AnyEntry(),
441 entrySubTreeSelector);
442
443 it.m_shouldVisitChildren = (result.second && entry->hasChildren());
444
Junxiao Shi60607c72014-11-26 22:40:36 -0700445 if (result.first) {
446 // root node is acceptable
447 }
448 else {
449 // let the ++ operator handle it
450 ++it;
451 }
452 return {it, end()};
HYuana9b85752014-02-26 02:32:30 -0600453}
454
Junxiao Shi5ccd0c22014-12-02 23:54:42 -0700455boost::iterator_range<NameTree::const_iterator>
Haowei Yuane1079fc2014-03-08 14:41:25 -0600456NameTree::findAllMatches(const Name& prefix,
457 const name_tree::EntrySelector& entrySelector) const
HYuana9b85752014-02-26 02:32:30 -0600458{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700459 NFD_LOG_TRACE("NameTree::findAllMatches" << prefix);
HYuana9b85752014-02-26 02:32:30 -0600460
Haowei Yuane1079fc2014-03-08 14:41:25 -0600461 // As we are using Name Prefix Hash Table, and the current LPM() is
462 // implemented as starting from full name, and reduce the number of
463 // components by 1 each time, we could use it here.
464 // For trie-like design, it could be more efficient by walking down the
465 // trie from the root node.
HYuana9b85752014-02-26 02:32:30 -0600466
Haowei Yuane1079fc2014-03-08 14:41:25 -0600467 shared_ptr<name_tree::Entry> entry = findLongestPrefixMatch(prefix, entrySelector);
HYuana9b85752014-02-26 02:32:30 -0600468
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700469 if (static_cast<bool>(entry)) {
470 const_iterator begin(FIND_ALL_MATCHES_TYPE, *this, entry, entrySelector);
Junxiao Shi60607c72014-11-26 22:40:36 -0700471 return {begin, end()};
Junxiao Shi2b73ca32014-11-17 19:16:08 -0700472 }
Haowei Yuane1079fc2014-03-08 14:41:25 -0600473 // If none of the entry satisfies the requirements, then return the end() iterator.
Junxiao Shi60607c72014-11-26 22:40:36 -0700474 return {end(), end()};
HYuana9b85752014-02-26 02:32:30 -0600475}
476
477// Hash Table Resize
478void
479NameTree::resize(size_t newNBuckets)
480{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700481 NFD_LOG_TRACE("resize");
HYuana9b85752014-02-26 02:32:30 -0600482
483 name_tree::Node** newBuckets = new name_tree::Node*[newNBuckets];
Junxiao Shi40631842014-03-01 13:52:37 -0700484 size_t count = 0;
HYuana9b85752014-02-26 02:32:30 -0600485
486 // referenced ccnx hashtb.c hashtb_rehash()
487 name_tree::Node** pp = 0;
488 name_tree::Node* p = 0;
489 name_tree::Node* pre = 0;
490 name_tree::Node* q = 0; // record p->m_next
Junxiao Shi40631842014-03-01 13:52:37 -0700491 size_t i;
HYuana9b85752014-02-26 02:32:30 -0600492 uint32_t h;
493 uint32_t b;
494
495 for (i = 0; i < newNBuckets; i++)
496 {
497 newBuckets[i] = 0;
498 }
499
500 for (i = 0; i < m_nBuckets; i++)
501 {
502 for (p = m_buckets[i]; p != 0; p = q)
503 {
504 count++;
505 q = p->m_next;
506 BOOST_ASSERT(static_cast<bool>(p->m_entry));
507 h = p->m_entry->m_hash;
508 b = h % newNBuckets;
Haowei Yuan694bfb62014-04-01 14:44:11 -0500509 pre = 0;
HYuana9b85752014-02-26 02:32:30 -0600510 for (pp = &newBuckets[b]; *pp != 0; pp = &((*pp)->m_next))
511 {
512 pre = *pp;
513 continue;
514 }
515 p->m_prev = pre;
516 p->m_next = *pp; // Actually *pp always == 0 in this case
517 *pp = p;
518 }
519 }
520
521 BOOST_ASSERT(count == m_nItems);
522
523 name_tree::Node** oldBuckets = m_buckets;
524 m_buckets = newBuckets;
Haowei Yuanf52dac72014-03-24 23:35:03 -0500525 delete [] oldBuckets;
HYuana9b85752014-02-26 02:32:30 -0600526
527 m_nBuckets = newNBuckets;
Haowei Yuanf52dac72014-03-24 23:35:03 -0500528
529 m_enlargeThreshold = static_cast<size_t>(m_enlargeLoadFactor *
530 static_cast<double>(m_nBuckets));
531 m_shrinkThreshold = static_cast<size_t>(m_shrinkLoadFactor *
532 static_cast<double>(m_nBuckets));
HYuana9b85752014-02-26 02:32:30 -0600533}
534
535// For debugging
536void
Haowei Yuane1079fc2014-03-08 14:41:25 -0600537NameTree::dump(std::ostream& output) const
HYuana9b85752014-02-26 02:32:30 -0600538{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700539 NFD_LOG_TRACE("dump()");
HYuana9b85752014-02-26 02:32:30 -0600540
541 name_tree::Node* node = 0;
542 shared_ptr<name_tree::Entry> entry;
543
544 using std::endl;
545
546 for (size_t i = 0; i < m_nBuckets; i++)
547 {
548 for (node = m_buckets[i]; node != 0; node = node->m_next)
549 {
550 entry = node->m_entry;
551
552 // if the Entry exist, dump its information
553 if (static_cast<bool>(entry))
554 {
555 output << "Bucket" << i << "\t" << entry->m_prefix.toUri() << endl;
556 output << "\t\tHash " << entry->m_hash << endl;
557
558 if (static_cast<bool>(entry->m_parent))
559 {
560 output << "\t\tparent->" << entry->m_parent->m_prefix.toUri();
561 }
562 else
563 {
564 output << "\t\tROOT";
565 }
566 output << endl;
567
568 if (entry->m_children.size() != 0)
569 {
570 output << "\t\tchildren = " << entry->m_children.size() << endl;
571
572 for (size_t j = 0; j < entry->m_children.size(); j++)
573 {
574 output << "\t\t\tChild " << j << " " <<
575 entry->m_children[j]->getPrefix() << endl;
576 }
577 }
578
579 } // if (static_cast<bool>(entry))
580
581 } // for node
582 } // for int i
583
584 output << "Bucket count = " << m_nBuckets << endl;
585 output << "Stored item = " << m_nItems << endl;
586 output << "--------------------------\n";
587}
588
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800589NameTree::const_iterator::const_iterator()
590 : m_nameTree(nullptr)
591{
592}
593
Haowei Yuane1079fc2014-03-08 14:41:25 -0600594NameTree::const_iterator::const_iterator(NameTree::IteratorType type,
595 const NameTree& nameTree,
596 shared_ptr<name_tree::Entry> entry,
597 const name_tree::EntrySelector& entrySelector,
598 const name_tree::EntrySubTreeSelector& entrySubTreeSelector)
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800599 : m_nameTree(&nameTree)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600600 , m_entry(entry)
601 , m_subTreeRoot(entry)
602 , m_entrySelector(make_shared<name_tree::EntrySelector>(entrySelector))
603 , m_entrySubTreeSelector(make_shared<name_tree::EntrySubTreeSelector>(entrySubTreeSelector))
604 , m_type(type)
605 , m_shouldVisitChildren(true)
606{
607}
608
609// operator++()
610NameTree::const_iterator
611NameTree::const_iterator::operator++()
612{
Alexander Afanasyevbf9edee2014-03-31 23:05:27 -0700613 NFD_LOG_TRACE("const_iterator::operator++()");
Haowei Yuane1079fc2014-03-08 14:41:25 -0600614
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800615 BOOST_ASSERT(m_entry != m_nameTree->m_end);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600616
617 if (m_type == FULL_ENUMERATE_TYPE) // fullEnumerate
618 {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600619 // process the entries in the same bucket first
620 while (m_entry->m_node->m_next != 0)
621 {
622 m_entry = m_entry->m_node->m_next->m_entry;
623 if ((*m_entrySelector)(*m_entry))
624 {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600625 return *this;
626 }
627 }
628
629 // process other buckets
Junxiao Shiefceadc2014-03-09 18:52:57 -0700630
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800631 for (int newLocation = m_entry->m_hash % m_nameTree->m_nBuckets + 1;
632 newLocation < static_cast<int>(m_nameTree->m_nBuckets);
Junxiao Shiefceadc2014-03-09 18:52:57 -0700633 ++newLocation)
Haowei Yuane1079fc2014-03-08 14:41:25 -0600634 {
635 // process each bucket
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800636 name_tree::Node* node = m_nameTree->m_buckets[newLocation];
Haowei Yuane1079fc2014-03-08 14:41:25 -0600637 while (node != 0)
638 {
639 m_entry = node->m_entry;
640 if ((*m_entrySelector)(*m_entry))
641 {
Haowei Yuane1079fc2014-03-08 14:41:25 -0600642 return *this;
643 }
644 node = node->m_next;
645 }
646 }
hwyuan80829932015-07-11 22:59:13 -0500647
648 // Reach the end()
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800649 m_entry = m_nameTree->m_end;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600650 return *this;
651 }
652
653 if (m_type == PARTIAL_ENUMERATE_TYPE) // partialEnumerate
654 {
655 // We use pre-order traversal.
656 // if at the root, it could have already been accepted, or this
657 // iterator was just declared, and root doesn't satisfy the
658 // requirement
659 // The if() section handles this special case
660 // Essentially, we need to check root's fist child, and the rest will
661 // be the same as normal process
662 if (m_entry == m_subTreeRoot)
663 {
664 if (m_shouldVisitChildren)
665 {
666 m_entry = m_entry->getChildren()[0];
667 std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry));
668 m_shouldVisitChildren = (result.second && m_entry->hasChildren());
669 if(result.first)
670 {
671 return *this;
672 }
673 else
674 {
675 // the first child did not meet the requirement
676 // the rest of the process can just fall through the while loop
677 // as normal
678 }
679 }
680 else
681 {
682 // no children, should return end();
683 // just fall through
684 }
685 }
686
687 // The first thing to do is to visit its child, or go to find its possible
688 // siblings
689 while (m_entry != m_subTreeRoot)
690 {
691 if (m_shouldVisitChildren)
692 {
693 // If this subtree should be visited
694 m_entry = m_entry->getChildren()[0];
695 std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry));
696 m_shouldVisitChildren = (result.second && m_entry->hasChildren());
697 if (result.first) // if this node is acceptable
698 {
699 return *this;
700 }
701 else
702 {
703 // do nothing, as this node is essentially ignored
704 // send this node to the while loop.
705 }
706 }
707 else
708 {
709 // Should try to find its sibling
710 shared_ptr<name_tree::Entry> parent = m_entry->getParent();
711
712 std::vector<shared_ptr<name_tree::Entry> >& parentChildrenList = parent->getChildren();
713 bool isFound = false;
714 size_t i = 0;
715 for (i = 0; i < parentChildrenList.size(); i++)
716 {
717 if (parentChildrenList[i] == m_entry)
718 {
719 isFound = true;
720 break;
721 }
722 }
723
Junxiao Shiac7b4372014-12-13 21:47:04 -0700724 BOOST_VERIFY(isFound == true);
Haowei Yuane1079fc2014-03-08 14:41:25 -0600725 if (i < parentChildrenList.size() - 1) // m_entry not the last child
726 {
727 m_entry = parentChildrenList[i + 1];
728 std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry));
729 m_shouldVisitChildren = (result.second && m_entry->hasChildren());
730 if (result.first) // if this node is acceptable
731 {
732 return *this;
733 }
734 else
735 {
736 // do nothing, as this node is essentially ignored
737 // send this node to the while loop.
738 }
739 }
740 else
741 {
742 // m_entry is the last child, no more sibling, should try to find parent's sibling
743 m_shouldVisitChildren = false;
744 m_entry = parent;
745 }
746 }
747 }
748
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800749 m_entry = m_nameTree->m_end;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600750 return *this;
751 }
752
753 if (m_type == FIND_ALL_MATCHES_TYPE) // findAllMatches
754 {
755 // Assumption: at the beginning, m_entry was initialized with the first
756 // eligible Name Tree entry (i.e., has a PIT entry that can be satisfied
757 // by the Data packet)
758
759 while (static_cast<bool>(m_entry->getParent()))
760 {
761 m_entry = m_entry->getParent();
762 if ((*m_entrySelector)(*m_entry))
763 return *this;
764 }
765
766 // Reach to the end (Root)
Alexander Afanasyev09fc3d92015-01-03 02:02:37 -0800767 m_entry = m_nameTree->m_end;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600768 return *this;
769 }
Junxiao Shiefceadc2014-03-09 18:52:57 -0700770
771 BOOST_ASSERT(false); // unknown type
772 return *this;
Haowei Yuane1079fc2014-03-08 14:41:25 -0600773}
774
HYuana9b85752014-02-26 02:32:30 -0600775} // namespace nfd