HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [diff] [blame] | 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 Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 10 | * |
| 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 Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
| 26 | #include "name-tree.hpp" |
| 27 | #include "core/logger.hpp" |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 28 | #include "core/city-hash.hpp" |
Davide Pesavento | 52a18f9 | 2014-04-10 00:55:01 +0200 | [diff] [blame] | 29 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 30 | #include <boost/concept/assert.hpp> |
| 31 | #include <boost/concept_check.hpp> |
| 32 | #include <type_traits> |
| 33 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 34 | namespace nfd { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 35 | namespace name_tree { |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 36 | |
| 37 | NFD_LOG_INIT("NameTree"); |
| 38 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 39 | // http://en.cppreference.com/w/cpp/concept/ForwardIterator |
| 40 | BOOST_CONCEPT_ASSERT((boost::ForwardIterator<NameTree::const_iterator>)); |
| 41 | // boost::ForwardIterator follows SGI standard http://www.sgi.com/tech/stl/ForwardIterator.html, |
| 42 | // which doesn't require DefaultConstructible |
| 43 | #ifdef HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 44 | static_assert(std::is_default_constructible<NameTree::const_iterator>::value, |
| 45 | "NameTree::const_iterator must be default-constructible"); |
| 46 | #else |
| 47 | BOOST_CONCEPT_ASSERT((boost::DefaultConstructible<NameTree::const_iterator>)); |
| 48 | #endif // HAVE_IS_DEFAULT_CONSTRUCTIBLE |
| 49 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 50 | class Hash32 |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 51 | { |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 52 | public: |
| 53 | static size_t |
| 54 | compute(const char* buffer, size_t length) |
| 55 | { |
| 56 | return static_cast<size_t>(CityHash32(buffer, length)); |
| 57 | } |
| 58 | }; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 59 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 60 | class Hash64 |
| 61 | { |
| 62 | public: |
| 63 | static size_t |
| 64 | compute(const char* buffer, size_t length) |
| 65 | { |
| 66 | return static_cast<size_t>(CityHash64(buffer, length)); |
| 67 | } |
| 68 | }; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 69 | |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 70 | /// @cond NoDocumentation |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 71 | typedef boost::mpl::if_c<sizeof(size_t) >= 8, Hash64, Hash32>::type CityHash; |
Alexander Afanasyev | b755e9d | 2015-10-20 17:35:51 -0500 | [diff] [blame] | 72 | /// @endcond |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 73 | |
| 74 | // Interface of different hash functions |
| 75 | size_t |
| 76 | computeHash(const Name& prefix) |
| 77 | { |
| 78 | prefix.wireEncode(); // guarantees prefix's wire buffer is not empty |
| 79 | |
| 80 | size_t hashValue = 0; |
| 81 | size_t hashUpdate = 0; |
| 82 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 83 | for (Name::const_iterator it = prefix.begin(); it != prefix.end(); ++it) { |
| 84 | const char* wireFormat = reinterpret_cast<const char*>( it->wire() ); |
| 85 | hashUpdate = CityHash::compute(wireFormat, it->size()); |
| 86 | hashValue ^= hashUpdate; |
| 87 | } |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 88 | |
| 89 | return hashValue; |
| 90 | } |
| 91 | |
| 92 | std::vector<size_t> |
| 93 | computeHashSet(const Name& prefix) |
| 94 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 95 | prefix.wireEncode(); // guarantees prefix's wire buffer is not empty |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 96 | |
| 97 | size_t hashValue = 0; |
| 98 | size_t hashUpdate = 0; |
| 99 | |
| 100 | std::vector<size_t> hashValueSet; |
| 101 | hashValueSet.push_back(hashValue); |
| 102 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 103 | for (Name::const_iterator it = prefix.begin(); it != prefix.end(); ++it) { |
| 104 | const char* wireFormat = reinterpret_cast<const char*>( it->wire() ); |
| 105 | hashUpdate = CityHash::compute(wireFormat, it->size()); |
| 106 | hashValue ^= hashUpdate; |
| 107 | hashValueSet.push_back(hashValue); |
| 108 | } |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 109 | |
| 110 | return hashValueSet; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 111 | } |
| 112 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 113 | NameTree::NameTree(size_t nBuckets) |
| 114 | : m_nItems(0) |
| 115 | , m_nBuckets(nBuckets) |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 116 | , m_minNBuckets(nBuckets) |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 117 | , m_enlargeLoadFactor(0.5) // more than 50% buckets loaded |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 118 | , m_enlargeFactor(2) // double the hash table size |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 119 | , m_shrinkLoadFactor(0.1) // less than 10% buckets loaded |
| 120 | , m_shrinkFactor(0.5) // reduce the number of buckets by half |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 121 | , m_endIterator(FULL_ENUMERATE_TYPE, *this, m_end) |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 122 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 123 | m_enlargeThreshold = static_cast<size_t>(m_enlargeLoadFactor * static_cast<double>(m_nBuckets)); |
| 124 | m_shrinkThreshold = static_cast<size_t>(m_shrinkLoadFactor * static_cast<double>(m_nBuckets)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 125 | |
| 126 | // array of node pointers |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 127 | m_buckets = new Node*[m_nBuckets]; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 128 | // Initialize the pointer array |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 129 | for (size_t i = 0; i < m_nBuckets; ++i) { |
| 130 | m_buckets[i] = nullptr; |
| 131 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | NameTree::~NameTree() |
| 135 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 136 | for (size_t i = 0; i < m_nBuckets; ++i) { |
| 137 | if (m_buckets[i] != nullptr) { |
| 138 | delete m_buckets[i]; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 139 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 140 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 141 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 142 | delete[] m_buckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 143 | } |
| 144 | |
| 145 | // insert() is a private function, and called by only lookup() |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 146 | std::pair<shared_ptr<Entry>, bool> |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 147 | NameTree::insert(const Name& prefix) |
| 148 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 149 | NFD_LOG_TRACE("insert " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 150 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 151 | size_t hashValue = computeHash(prefix); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 152 | size_t loc = hashValue % m_nBuckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 153 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 154 | NFD_LOG_TRACE("Name " << prefix << " hash value = " << hashValue << " location = " << loc); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 155 | |
| 156 | // Check if this Name has been stored |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 157 | Node* node = m_buckets[loc]; |
| 158 | Node* nodePrev = node; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 159 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 160 | for (node = m_buckets[loc]; node != nullptr; node = node->m_next) { |
| 161 | if (node->m_entry != nullptr) { |
| 162 | if (prefix == node->m_entry->m_prefix) { |
| 163 | return {node->m_entry, false}; // false: old entry |
| 164 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 165 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 166 | nodePrev = node; |
| 167 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 168 | |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 169 | NFD_LOG_TRACE("Did not find " << prefix << ", need to insert it to the table"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 170 | |
| 171 | // If no bucket is empty occupied, we need to create a new node, and it is |
| 172 | // linked from nodePrev |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 173 | node = new Node(); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 174 | node->m_prev = nodePrev; |
| 175 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 176 | if (nodePrev == nullptr) { |
| 177 | m_buckets[loc] = node; |
| 178 | } |
| 179 | else{ |
| 180 | nodePrev->m_next = node; |
| 181 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 182 | |
| 183 | // Create a new Entry |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 184 | auto entry = make_shared<Entry>(prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 185 | entry->setHash(hashValue); |
| 186 | node->m_entry = entry; // link the Entry to its Node |
| 187 | entry->m_node = node; // link the node to Entry. Used in eraseEntryIfEmpty. |
| 188 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 189 | return {entry, true}; // true: new entry |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | // Name Prefix Lookup. Create Name Tree Entry if not found |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 193 | shared_ptr<Entry> |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 194 | NameTree::lookup(const Name& prefix) |
| 195 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 196 | NFD_LOG_TRACE("lookup " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 197 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 198 | shared_ptr<Entry> entry; |
| 199 | shared_ptr<Entry> parent; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 200 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 201 | for (size_t i = 0; i <= prefix.size(); ++i) { |
| 202 | Name temp = prefix.getPrefix(i); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 203 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 204 | // insert() will create the entry if it does not exist. |
| 205 | bool isNew = false; |
| 206 | std::tie(entry, isNew) = insert(temp); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 207 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 208 | if (isNew) { |
| 209 | ++m_nItems; // Increase the counter |
| 210 | entry->m_parent = parent; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 211 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 212 | if (parent != nullptr) { |
| 213 | parent->m_children.push_back(entry); |
| 214 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 215 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 216 | |
| 217 | if (m_nItems > m_enlargeThreshold) { |
| 218 | resize(m_enlargeFactor * m_nBuckets); |
| 219 | } |
| 220 | |
| 221 | parent = entry; |
| 222 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 223 | return entry; |
| 224 | } |
| 225 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 226 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 227 | NameTree::lookup(const fib::Entry& fibEntry) const |
| 228 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 229 | shared_ptr<Entry> nte = this->getEntry(fibEntry); |
Junxiao Shi | a6de429 | 2016-07-12 02:08:10 +0000 | [diff] [blame] | 230 | BOOST_ASSERT(nte == nullptr || nte->getFibEntry() == &fibEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 231 | return nte; |
| 232 | } |
| 233 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 234 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 235 | NameTree::lookup(const pit::Entry& pitEntry) |
| 236 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 237 | shared_ptr<Entry> nte = this->getEntry(pitEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 238 | if (nte == nullptr) { |
| 239 | return nullptr; |
| 240 | } |
| 241 | |
| 242 | if (nte->getPrefix().size() == pitEntry.getName().size()) { |
| 243 | return nte; |
| 244 | } |
| 245 | |
| 246 | BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest()); |
| 247 | BOOST_ASSERT(nte->getPrefix() == pitEntry.getName().getPrefix(-1)); |
| 248 | return this->lookup(pitEntry.getName()); |
| 249 | } |
| 250 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 251 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 252 | NameTree::lookup(const measurements::Entry& measurementsEntry) const |
| 253 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 254 | shared_ptr<Entry> nte = this->getEntry(measurementsEntry); |
Junxiao Shi | 80f9fcd | 2016-07-23 02:48:36 +0000 | [diff] [blame] | 255 | BOOST_ASSERT(nte == nullptr || nte->getMeasurementsEntry() == &measurementsEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 256 | return nte; |
| 257 | } |
| 258 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 259 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 260 | NameTree::lookup(const strategy_choice::Entry& strategyChoiceEntry) const |
| 261 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 262 | shared_ptr<Entry> nte = this->getEntry(strategyChoiceEntry); |
Junxiao Shi | ff10da6 | 2016-07-13 17:57:43 +0000 | [diff] [blame] | 263 | BOOST_ASSERT(nte == nullptr || nte->getStrategyChoiceEntry() == &strategyChoiceEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 264 | return nte; |
| 265 | } |
| 266 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 267 | // return {false: this entry is not empty, true: this entry is empty and erased} |
| 268 | bool |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 269 | NameTree::eraseEntryIfEmpty(shared_ptr<Entry> entry) |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 270 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 271 | BOOST_ASSERT(entry != nullptr); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 272 | |
| 273 | NFD_LOG_TRACE("eraseEntryIfEmpty " << entry->getPrefix()); |
| 274 | |
| 275 | // first check if this Entry can be erased |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 276 | if (entry->isEmpty()) { |
| 277 | // update child-related info in the parent |
| 278 | shared_ptr<Entry> parent = entry->getParent(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 279 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 280 | if (parent != nullptr) { |
| 281 | std::vector<shared_ptr<Entry>>& parentChildrenList = parent->getChildren(); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 282 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 283 | bool isFound = false; |
| 284 | size_t size = parentChildrenList.size(); |
| 285 | for (size_t i = 0; i < size; ++i) { |
| 286 | if (parentChildrenList[i] == entry) { |
| 287 | parentChildrenList[i] = parentChildrenList[size - 1]; |
| 288 | parentChildrenList.pop_back(); |
| 289 | isFound = true; |
| 290 | break; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 291 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 292 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 293 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 294 | BOOST_VERIFY(isFound == true); |
| 295 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 296 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 297 | // remove this Entry and its Name Tree Node |
| 298 | Node* node = entry->m_node; |
| 299 | Node* nodePrev = node->m_prev; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 300 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 301 | // configure the previous node |
| 302 | if (nodePrev != nullptr) { |
| 303 | // link the previous node to the next node |
| 304 | nodePrev->m_next = node->m_next; |
| 305 | } |
| 306 | else { |
| 307 | m_buckets[entry->getHash() % m_nBuckets] = node->m_next; |
| 308 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 309 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 310 | // link the previous node with the next node (skip the erased one) |
| 311 | if (node->m_next != nullptr) { |
| 312 | node->m_next->m_prev = nodePrev; |
| 313 | node->m_next = 0; |
| 314 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 315 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 316 | BOOST_ASSERT(node->m_next == nullptr); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 317 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 318 | --m_nItems; |
| 319 | delete node; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 320 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 321 | if (parent != nullptr) { |
| 322 | eraseEntryIfEmpty(parent); |
| 323 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 324 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 325 | size_t newNBuckets = static_cast<size_t>(m_shrinkFactor * static_cast<double>(m_nBuckets)); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 326 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 327 | if (newNBuckets >= m_minNBuckets && m_nItems < m_shrinkThreshold) { |
| 328 | resize(newNBuckets); |
| 329 | } |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 330 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 331 | return true; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 332 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 333 | } |
| 334 | |
| 335 | return false; |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 336 | } |
| 337 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 338 | // Exact Match |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 339 | shared_ptr<Entry> |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 340 | NameTree::findExactMatch(const Name& prefix) const |
| 341 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 342 | NFD_LOG_TRACE("findExactMatch " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 343 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 344 | size_t hashValue = computeHash(prefix); |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 345 | size_t loc = hashValue % m_nBuckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 346 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 347 | NFD_LOG_TRACE("Name " << prefix << " hash value = " << hashValue << " location = " << loc); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 348 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 349 | shared_ptr<Entry> entry; |
| 350 | Node* node = nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 351 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 352 | for (node = m_buckets[loc]; node != nullptr; node = node->m_next) { |
| 353 | entry = node->m_entry; |
| 354 | if (entry != nullptr) { |
| 355 | if (hashValue == entry->getHash() && prefix == entry->getPrefix()) { |
| 356 | return entry; |
| 357 | } |
| 358 | } |
| 359 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 360 | |
| 361 | // if not found, the default value of entry (null pointer) will be returned |
| 362 | entry.reset(); |
| 363 | return entry; |
| 364 | } |
| 365 | |
| 366 | // Longest Prefix Match |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 367 | shared_ptr<Entry> |
| 368 | NameTree::findLongestPrefixMatch(const Name& prefix, const EntrySelector& entrySelector) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 369 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 370 | NFD_LOG_TRACE("findLongestPrefixMatch " << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 371 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 372 | shared_ptr<Entry> entry; |
| 373 | std::vector<size_t> hashValueSet = computeHashSet(prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 374 | |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 375 | size_t hashValue = 0; |
| 376 | size_t loc = 0; |
| 377 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 378 | for (int i = static_cast<int>(prefix.size()); i >= 0; --i) { |
| 379 | hashValue = hashValueSet[i]; |
| 380 | loc = hashValue % m_nBuckets; |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 381 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 382 | Node* node = nullptr; |
| 383 | for (node = m_buckets[loc]; node != nullptr; node = node->m_next) { |
| 384 | entry = node->m_entry; |
| 385 | if (entry != nullptr) { |
| 386 | // isPrefixOf() is used to avoid making a copy of the name |
| 387 | if (hashValue == entry->getHash() && |
| 388 | entry->getPrefix().isPrefixOf(prefix) && |
| 389 | entrySelector(*entry)) { |
| 390 | return entry; |
| 391 | } |
| 392 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 393 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 394 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 395 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 396 | return nullptr; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 397 | } |
| 398 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 399 | shared_ptr<Entry> |
| 400 | NameTree::findLongestPrefixMatch(shared_ptr<Entry> entry, |
| 401 | const EntrySelector& entrySelector) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 402 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 403 | while (entry != nullptr) { |
| 404 | if (entrySelector(*entry)) { |
| 405 | return entry; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 406 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 407 | entry = entry->getParent(); |
| 408 | } |
| 409 | return nullptr; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 410 | } |
| 411 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 412 | shared_ptr<Entry> |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 413 | NameTree::findLongestPrefixMatch(const pit::Entry& pitEntry) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 414 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 415 | shared_ptr<Entry> nte = this->getEntry(pitEntry); |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 416 | BOOST_ASSERT(nte != nullptr); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 417 | if (nte->getPrefix().size() == pitEntry.getName().size()) { |
| 418 | return nte; |
| 419 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 420 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 421 | BOOST_ASSERT(pitEntry.getName().at(-1).isImplicitSha256Digest()); |
| 422 | BOOST_ASSERT(nte->getPrefix() == pitEntry.getName().getPrefix(-1)); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 423 | shared_ptr<Entry> exact = this->findExactMatch(pitEntry.getName()); |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 424 | return exact == nullptr ? nte : exact; |
| 425 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 426 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 427 | boost::iterator_range<NameTree::const_iterator> |
| 428 | NameTree::findAllMatches(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 429 | const EntrySelector& entrySelector) const |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 430 | { |
| 431 | NFD_LOG_TRACE("NameTree::findAllMatches" << prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 432 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 433 | // As we are using Name Prefix Hash Table, and the current LPM() is |
| 434 | // implemented as starting from full name, and reduce the number of |
| 435 | // components by 1 each time, we could use it here. |
| 436 | // For trie-like design, it could be more efficient by walking down the |
| 437 | // trie from the root node. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 438 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 439 | shared_ptr<Entry> entry = findLongestPrefixMatch(prefix, entrySelector); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 440 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 441 | if (entry != nullptr) { |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 442 | const_iterator begin(FIND_ALL_MATCHES_TYPE, *this, entry, entrySelector); |
| 443 | return {begin, end()}; |
| 444 | } |
| 445 | // If none of the entry satisfies the requirements, then return the end() iterator. |
| 446 | return {end(), end()}; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 447 | } |
| 448 | |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 449 | boost::iterator_range<NameTree::const_iterator> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 450 | NameTree::fullEnumerate(const EntrySelector& entrySelector) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 451 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 452 | NFD_LOG_TRACE("fullEnumerate"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 453 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 454 | // find the first eligible entry |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 455 | for (size_t i = 0; i < m_nBuckets; ++i) { |
| 456 | for (Node* node = m_buckets[i]; node != nullptr; node = node->m_next) { |
| 457 | if (node->m_entry != nullptr && entrySelector(*node->m_entry)) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 458 | const_iterator it(FULL_ENUMERATE_TYPE, *this, node->m_entry, entrySelector); |
| 459 | return {it, end()}; |
| 460 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 461 | } |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 462 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 463 | |
| 464 | // If none of the entry satisfies the requirements, then return the end() iterator. |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 465 | return {end(), end()}; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 466 | } |
| 467 | |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 468 | boost::iterator_range<NameTree::const_iterator> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 469 | NameTree::partialEnumerate(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 470 | const EntrySubTreeSelector& entrySubTreeSelector) const |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 471 | { |
| 472 | // the first step is to process the root node |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 473 | shared_ptr<Entry> entry = findExactMatch(prefix); |
| 474 | if (entry == nullptr) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 475 | return {end(), end()}; |
| 476 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 477 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 478 | std::pair<bool, bool> result = entrySubTreeSelector(*entry); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 479 | const_iterator it(PARTIAL_ENUMERATE_TYPE, |
| 480 | *this, |
| 481 | entry, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 482 | AnyEntry(), |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 483 | entrySubTreeSelector); |
| 484 | |
| 485 | it.m_shouldVisitChildren = (result.second && entry->hasChildren()); |
| 486 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 487 | if (result.first) { |
| 488 | // root node is acceptable |
| 489 | } |
| 490 | else { |
| 491 | // let the ++ operator handle it |
| 492 | ++it; |
| 493 | } |
| 494 | return {it, end()}; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 495 | } |
| 496 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 497 | // Hash Table Resize |
| 498 | void |
| 499 | NameTree::resize(size_t newNBuckets) |
| 500 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 501 | NFD_LOG_TRACE("resize"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 502 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 503 | Node** newBuckets = new Node*[newNBuckets]; |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 504 | size_t count = 0; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 505 | |
| 506 | // referenced ccnx hashtb.c hashtb_rehash() |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 507 | Node** pp = nullptr; |
| 508 | Node* p = nullptr; |
| 509 | Node* pre = nullptr; |
| 510 | Node* q = nullptr; // record p->m_next |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 511 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 512 | for (size_t i = 0; i < newNBuckets; ++i) { |
| 513 | newBuckets[i] = nullptr; |
| 514 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 515 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 516 | for (size_t i = 0; i < m_nBuckets; ++i) { |
| 517 | for (p = m_buckets[i]; p != nullptr; p = q) { |
| 518 | ++count; |
| 519 | q = p->m_next; |
| 520 | BOOST_ASSERT(p->m_entry != nullptr); |
| 521 | uint32_t h = p->m_entry->m_hash; |
| 522 | uint32_t b = h % newNBuckets; |
| 523 | pre = nullptr; |
| 524 | for (pp = &newBuckets[b]; *pp != nullptr; pp = &((*pp)->m_next)) { |
| 525 | pre = *pp; |
| 526 | } |
| 527 | p->m_prev = pre; |
| 528 | p->m_next = *pp; // Actually *pp always == nullptr in this case |
| 529 | *pp = p; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 530 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 531 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 532 | |
| 533 | BOOST_ASSERT(count == m_nItems); |
| 534 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 535 | Node** oldBuckets = m_buckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 536 | m_buckets = newBuckets; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 537 | delete[] oldBuckets; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 538 | |
| 539 | m_nBuckets = newNBuckets; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 540 | m_enlargeThreshold = static_cast<size_t>(m_enlargeLoadFactor * static_cast<double>(m_nBuckets)); |
| 541 | m_shrinkThreshold = static_cast<size_t>(m_shrinkLoadFactor * static_cast<double>(m_nBuckets)); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 542 | } |
| 543 | |
| 544 | // For debugging |
| 545 | void |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 546 | NameTree::dump(std::ostream& output) const |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 547 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 548 | NFD_LOG_TRACE("dump()"); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 549 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 550 | Node* node = nullptr; |
| 551 | shared_ptr<Entry> entry; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 552 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 553 | for (size_t i = 0; i < m_nBuckets; ++i) { |
| 554 | for (node = m_buckets[i]; node != nullptr; node = node->m_next) { |
| 555 | entry = node->m_entry; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 556 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 557 | // if the Entry exist, dump its information |
| 558 | if (entry != nullptr) { |
| 559 | output << "Bucket" << i << '\t' << entry->m_prefix.toUri() << '\n'; |
| 560 | output << "\t\tHash " << entry->m_hash << '\n'; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 561 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 562 | if (entry->m_parent != nullptr) { |
| 563 | output << "\t\tparent->" << entry->m_parent->m_prefix.toUri(); |
| 564 | } |
| 565 | else { |
| 566 | output << "\t\tROOT"; |
| 567 | } |
| 568 | output << '\n'; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 569 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 570 | if (!entry->m_children.empty()) { |
| 571 | output << "\t\tchildren = " << entry->m_children.size() << '\n'; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 572 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 573 | for (size_t j = 0; j < entry->m_children.size(); ++j) { |
| 574 | output << "\t\t\tChild " << j << " " << entry->m_children[j]->getPrefix() << '\n'; |
| 575 | } |
| 576 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 577 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 578 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 579 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 580 | } |
| 581 | } |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 582 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 583 | output << "Bucket count = " << m_nBuckets << '\n'; |
| 584 | output << "Stored item = " << m_nItems << '\n'; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 585 | output << "--------------------------\n"; |
| 586 | } |
| 587 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 588 | NameTree::const_iterator::const_iterator() |
| 589 | : m_nameTree(nullptr) |
| 590 | { |
| 591 | } |
| 592 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 593 | NameTree::const_iterator::const_iterator(NameTree::IteratorType type, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 594 | const NameTree& nameTree, |
| 595 | shared_ptr<Entry> entry, |
| 596 | const EntrySelector& entrySelector, |
| 597 | const EntrySubTreeSelector& entrySubTreeSelector) |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 598 | : m_nameTree(&nameTree) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 599 | , m_entry(entry) |
| 600 | , m_subTreeRoot(entry) |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 601 | , m_entrySelector(make_shared<EntrySelector>(entrySelector)) |
| 602 | , m_entrySubTreeSelector(make_shared<EntrySubTreeSelector>(entrySubTreeSelector)) |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 603 | , m_type(type) |
| 604 | , m_shouldVisitChildren(true) |
| 605 | { |
| 606 | } |
| 607 | |
| 608 | // operator++() |
| 609 | NameTree::const_iterator |
| 610 | NameTree::const_iterator::operator++() |
| 611 | { |
Alexander Afanasyev | bf9edee | 2014-03-31 23:05:27 -0700 | [diff] [blame] | 612 | NFD_LOG_TRACE("const_iterator::operator++()"); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 613 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 614 | BOOST_ASSERT(m_entry != m_nameTree->m_end); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 615 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 616 | if (m_type == FULL_ENUMERATE_TYPE) { |
| 617 | // process the entries in the same bucket first |
| 618 | while (m_entry->m_node->m_next != nullptr) { |
| 619 | m_entry = m_entry->m_node->m_next->m_entry; |
| 620 | if ((*m_entrySelector)(*m_entry)) { |
| 621 | return *this; |
| 622 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 623 | } |
| 624 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 625 | // process other buckets |
| 626 | |
| 627 | for (int newLocation = m_entry->m_hash % m_nameTree->m_nBuckets + 1; |
| 628 | newLocation < static_cast<int>(m_nameTree->m_nBuckets); |
| 629 | ++newLocation) { |
| 630 | // process each bucket |
| 631 | Node* node = m_nameTree->m_buckets[newLocation]; |
| 632 | while (node != nullptr) { |
| 633 | m_entry = node->m_entry; |
| 634 | if ((*m_entrySelector)(*m_entry)) { |
| 635 | return *this; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 636 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 637 | node = node->m_next; |
| 638 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 639 | } |
| 640 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 641 | // Reach the end() |
| 642 | m_entry = m_nameTree->m_end; |
| 643 | return *this; |
| 644 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 645 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 646 | if (m_type == PARTIAL_ENUMERATE_TYPE) { |
| 647 | // We use pre-order traversal. |
| 648 | // if at the root, it could have already been accepted, or this |
| 649 | // iterator was just declared, and root doesn't satisfy the |
| 650 | // requirement |
| 651 | // The if() section handles this special case |
| 652 | // Essentially, we need to check root's fist child, and the rest will |
| 653 | // be the same as normal process |
| 654 | if (m_entry == m_subTreeRoot) { |
| 655 | if (m_shouldVisitChildren) { |
| 656 | m_entry = m_entry->getChildren()[0]; |
| 657 | std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry)); |
| 658 | m_shouldVisitChildren = (result.second && m_entry->hasChildren()); |
| 659 | if (result.first) { |
| 660 | return *this; |
| 661 | } |
| 662 | else { |
| 663 | // the first child did not meet the requirement |
| 664 | // the rest of the process can just fall through the while loop as normal |
| 665 | } |
| 666 | } |
| 667 | else { |
| 668 | // no children, should return end(); |
| 669 | // just fall through |
| 670 | } |
| 671 | } |
| 672 | |
| 673 | // The first thing to do is to visit its child, or go to find its possible siblings |
| 674 | while (m_entry != m_subTreeRoot) { |
| 675 | if (m_shouldVisitChildren) { |
| 676 | // If this subtree should be visited |
| 677 | m_entry = m_entry->getChildren()[0]; |
| 678 | std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry)); |
| 679 | m_shouldVisitChildren = (result.second && m_entry->hasChildren()); |
| 680 | if (result.first) { // if this node is acceptable |
| 681 | return *this; |
| 682 | } |
| 683 | else { |
| 684 | // do nothing, as this node is essentially ignored |
| 685 | // send this node to the while loop. |
| 686 | } |
| 687 | } |
| 688 | else { |
| 689 | // Should try to find its sibling |
| 690 | shared_ptr<Entry> parent = m_entry->getParent(); |
| 691 | |
| 692 | std::vector<shared_ptr<Entry>>& parentChildrenList = parent->getChildren(); |
| 693 | bool isFound = false; |
| 694 | size_t i = 0; |
| 695 | for (i = 0; i < parentChildrenList.size(); ++i) { |
| 696 | if (parentChildrenList[i] == m_entry) { |
| 697 | isFound = true; |
| 698 | break; |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | BOOST_VERIFY(isFound == true); |
| 703 | if (i < parentChildrenList.size() - 1) { // m_entry not the last child |
| 704 | m_entry = parentChildrenList[i + 1]; |
| 705 | std::pair<bool, bool> result = ((*m_entrySubTreeSelector)(*m_entry)); |
| 706 | m_shouldVisitChildren = (result.second && m_entry->hasChildren()); |
| 707 | if (result.first) { // if this node is acceptable |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 708 | return *this; |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 709 | } |
| 710 | else { |
| 711 | // do nothing, as this node is essentially ignored |
| 712 | // send this node to the while loop. |
| 713 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 714 | } |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 715 | else { |
| 716 | // m_entry is the last child, no more sibling, should try to find parent's sibling |
| 717 | m_shouldVisitChildren = false; |
| 718 | m_entry = parent; |
| 719 | } |
| 720 | } |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 721 | } |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 722 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 723 | m_entry = m_nameTree->m_end; |
| 724 | return *this; |
| 725 | } |
| 726 | |
| 727 | if (m_type == FIND_ALL_MATCHES_TYPE) { |
| 728 | // Assumption: at the beginning, m_entry was initialized with the first |
| 729 | // eligible Name Tree entry (i.e., has a PIT entry that can be satisfied |
| 730 | // by the Data packet) |
| 731 | |
| 732 | while (m_entry->getParent() != nullptr) { |
| 733 | m_entry = m_entry->getParent(); |
| 734 | if ((*m_entrySelector)(*m_entry)) { |
| 735 | return *this; |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | // Reach to the end (Root) |
| 740 | m_entry = m_nameTree->m_end; |
| 741 | return *this; |
| 742 | } |
| 743 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 744 | BOOST_ASSERT(false); // unknown type |
| 745 | return *this; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 746 | } |
| 747 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 748 | } // namespace name_tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 749 | } // namespace nfd |