Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 3 | * Copyright (c) 2014-2022, Regents of the University of California, |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [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. |
| 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/>. |
| 24 | */ |
| 25 | |
| 26 | #include "name-tree-iterator.hpp" |
| 27 | #include "name-tree.hpp" |
Davide Pesavento | 2cae8ca | 2019-04-18 20:48:05 -0400 | [diff] [blame] | 28 | #include "common/logger.hpp" |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 29 | |
Junxiao Shi | 9f6ca60 | 2016-08-15 04:50:29 +0000 | [diff] [blame] | 30 | #include <boost/range/concepts.hpp> |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 31 | #include <ndn-cxx/util/concepts.hpp> |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::name_tree { |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 34 | |
Davide Pesavento | 981db80 | 2018-04-10 18:05:04 -0400 | [diff] [blame] | 35 | NDN_CXX_ASSERT_FORWARD_ITERATOR(Iterator); |
Junxiao Shi | 9f6ca60 | 2016-08-15 04:50:29 +0000 | [diff] [blame] | 36 | BOOST_CONCEPT_ASSERT((boost::ForwardRangeConcept<Range>)); |
| 37 | |
Davide Pesavento | a314808 | 2018-04-12 18:21:54 -0400 | [diff] [blame] | 38 | NFD_LOG_INIT(NameTreeIterator); |
Junxiao Shi | 9f6ca60 | 2016-08-15 04:50:29 +0000 | [diff] [blame] | 39 | |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 40 | Iterator::Iterator() |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 41 | : m_entry(nullptr) |
| 42 | , m_ref(nullptr) |
| 43 | , m_state(0) |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 44 | { |
| 45 | } |
| 46 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 47 | Iterator::Iterator(shared_ptr<EnumerationImpl> impl, const Entry* ref) |
Davide Pesavento | e4b2238 | 2018-06-10 14:37:24 -0400 | [diff] [blame] | 48 | : m_impl(std::move(impl)) |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 49 | , m_entry(nullptr) |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 50 | , m_ref(ref) |
| 51 | , m_state(0) |
| 52 | { |
| 53 | m_impl->advance(*this); |
| 54 | NFD_LOG_TRACE("initialized " << *this); |
| 55 | } |
| 56 | |
| 57 | Iterator& |
| 58 | Iterator::operator++() |
| 59 | { |
| 60 | BOOST_ASSERT(m_impl != nullptr); |
| 61 | m_impl->advance(*this); |
| 62 | NFD_LOG_TRACE("advanced " << *this); |
| 63 | return *this; |
| 64 | } |
| 65 | |
| 66 | Iterator |
| 67 | Iterator::operator++(int) |
| 68 | { |
| 69 | Iterator copy = *this; |
| 70 | this->operator++(); |
| 71 | return copy; |
| 72 | } |
| 73 | |
| 74 | bool |
| 75 | Iterator::operator==(const Iterator& other) const |
| 76 | { |
| 77 | return m_entry == other.m_entry; |
| 78 | } |
| 79 | |
| 80 | std::ostream& |
| 81 | operator<<(std::ostream& os, const Iterator& i) |
| 82 | { |
| 83 | if (i.m_impl == nullptr) { |
| 84 | return os << "end"; |
| 85 | } |
| 86 | if (i.m_entry == nullptr) { |
| 87 | return os << "uninitialized"; |
| 88 | } |
| 89 | |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 90 | os << "entry=" << i.m_entry->getName(); |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 91 | if (i.m_ref == nullptr) { |
| 92 | os << " ref=null"; |
| 93 | } |
| 94 | else { |
Junxiao Shi | e3cf285 | 2016-08-09 03:50:56 +0000 | [diff] [blame] | 95 | os << " ref=" << i.m_ref->getName(); |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 96 | } |
| 97 | os << " state=" << i.m_state; |
| 98 | return os; |
| 99 | } |
| 100 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 101 | EnumerationImpl::EnumerationImpl(const NameTree& nt) |
| 102 | : nt(nt) |
| 103 | , ht(nt.m_ht) |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 104 | { |
| 105 | } |
| 106 | |
| 107 | FullEnumerationImpl::FullEnumerationImpl(const NameTree& nt, const EntrySelector& pred) |
| 108 | : EnumerationImpl(nt) |
| 109 | , m_pred(pred) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | void |
| 114 | FullEnumerationImpl::advance(Iterator& i) |
| 115 | { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 116 | // find first entry |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 117 | if (i.m_entry == nullptr) { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 118 | for (size_t bucket = 0; bucket < ht.getNBuckets(); ++bucket) { |
| 119 | const Node* node = ht.getBucket(bucket); |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 120 | if (node != nullptr) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 121 | i.m_entry = &node->entry; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 122 | break; |
| 123 | } |
| 124 | } |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 125 | if (i.m_entry == nullptr) { // empty enumerable |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 126 | i = Iterator(); |
| 127 | return; |
| 128 | } |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 129 | if (m_pred(*i.m_entry)) { // visit first entry |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 130 | return; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | // process entries in same bucket |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 135 | for (const Node* node = getNode(*i.m_entry)->next; node != nullptr; node = node->next) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 136 | if (m_pred(node->entry)) { |
| 137 | i.m_entry = &node->entry; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 138 | return; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | // process other buckets |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 143 | size_t currentBucket = ht.computeBucketIndex(getNode(*i.m_entry)->hash); |
| 144 | for (size_t bucket = currentBucket + 1; bucket < ht.getNBuckets(); ++bucket) { |
| 145 | for (const Node* node = ht.getBucket(bucket); node != nullptr; node = node->next) { |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 146 | if (m_pred(node->entry)) { |
| 147 | i.m_entry = &node->entry; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 148 | return; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // reach the end |
| 154 | i = Iterator(); |
| 155 | } |
| 156 | |
| 157 | PartialEnumerationImpl::PartialEnumerationImpl(const NameTree& nt, const EntrySubTreeSelector& pred) |
| 158 | : EnumerationImpl(nt) |
| 159 | , m_pred(pred) |
| 160 | { |
| 161 | } |
| 162 | |
| 163 | void |
| 164 | PartialEnumerationImpl::advance(Iterator& i) |
| 165 | { |
| 166 | bool wantSelf = false; |
| 167 | bool wantChildren = false; |
| 168 | |
| 169 | // initialize: start from root |
| 170 | if (i.m_entry == nullptr) { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 171 | if (i.m_ref == nullptr) { // root does not exist |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 172 | i = Iterator(); |
| 173 | return; |
| 174 | } |
| 175 | |
| 176 | i.m_entry = i.m_ref; |
| 177 | std::tie(wantSelf, wantChildren) = m_pred(*i.m_entry); |
| 178 | if (wantSelf) { // visit root |
| 179 | i.m_state = wantChildren; |
| 180 | return; |
| 181 | } |
| 182 | } |
| 183 | else { |
| 184 | wantChildren = static_cast<bool>(i.m_state); |
| 185 | } |
| 186 | BOOST_ASSERT(i.m_ref != nullptr); |
| 187 | |
| 188 | // pre-order traversal |
| 189 | while (i.m_entry != i.m_ref || (wantChildren && i.m_entry->hasChildren())) { |
| 190 | if (wantChildren && i.m_entry->hasChildren()) { // process children of m_entry |
| 191 | i.m_entry = i.m_entry->getChildren().front(); |
| 192 | std::tie(wantSelf, wantChildren) = m_pred(*i.m_entry); |
| 193 | if (wantSelf) { // visit first child |
| 194 | i.m_state = wantChildren; |
| 195 | return; |
| 196 | } |
| 197 | // first child rejected, let while loop process other children (siblings of new m_entry) |
| 198 | } |
| 199 | else { // process siblings of m_entry |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 200 | const Entry* parent = i.m_entry->getParent(); |
| 201 | const std::vector<Entry*>& siblings = parent->getChildren(); |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 202 | auto sibling = std::find(siblings.begin(), siblings.end(), i.m_entry); |
| 203 | BOOST_ASSERT(sibling != siblings.end()); |
| 204 | while (++sibling != siblings.end()) { |
| 205 | i.m_entry = *sibling; |
| 206 | std::tie(wantSelf, wantChildren) = m_pred(*i.m_entry); |
| 207 | if (wantSelf) { // visit sibling |
| 208 | i.m_state = wantChildren; |
| 209 | return; |
| 210 | } |
| 211 | if (wantChildren) { |
| 212 | break; // let outer while loop process children of m_entry |
| 213 | } |
| 214 | // process next sibling |
| 215 | } |
| 216 | if (sibling == siblings.end()) { // no more sibling |
| 217 | i.m_entry = parent; |
| 218 | wantChildren = false; |
| 219 | } |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // reach the end |
| 224 | i = Iterator(); |
| 225 | } |
| 226 | |
| 227 | PrefixMatchImpl::PrefixMatchImpl(const NameTree& nt, const EntrySelector& pred) |
| 228 | : EnumerationImpl(nt) |
| 229 | , m_pred(pred) |
| 230 | { |
| 231 | } |
| 232 | |
| 233 | void |
| 234 | PrefixMatchImpl::advance(Iterator& i) |
| 235 | { |
| 236 | if (i.m_entry == nullptr) { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 237 | if (i.m_ref == nullptr) { // empty enumerable |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 238 | i = Iterator(); |
| 239 | return; |
| 240 | } |
| 241 | |
| 242 | i.m_entry = i.m_ref; |
| 243 | if (m_pred(*i.m_entry)) { // visit starting node |
| 244 | return; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | // traverse up the tree |
| 249 | while ((i.m_entry = i.m_entry->getParent()) != nullptr) { |
| 250 | if (m_pred(*i.m_entry)) { |
| 251 | return; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | // reach the end |
| 256 | i = Iterator(); |
| 257 | } |
| 258 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 259 | } // namespace nfd::name_tree |