Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, 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 | #ifndef NFD_DAEMON_TABLE_NAME_TREE_ITERATOR_HPP |
| 27 | #define NFD_DAEMON_TABLE_NAME_TREE_ITERATOR_HPP |
| 28 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 29 | #include "name-tree-hashtable.hpp" |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 31 | #include <boost/operators.hpp> |
Davide Pesavento | a9b09b6 | 2022-06-04 14:07:25 -0400 | [diff] [blame] | 32 | #include <boost/range/iterator_range_core.hpp> |
| 33 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 34 | #include <functional> |
| 35 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 36 | namespace nfd::name_tree { |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 37 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 38 | class NameTree; |
| 39 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 40 | /** |
| 41 | * \brief A predicate to accept or reject an Entry in find operations. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 42 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 43 | using EntrySelector = std::function<bool(const Entry&)>; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 44 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 45 | /** |
| 46 | * \brief An #EntrySelector that accepts every Entry. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 47 | */ |
| 48 | struct AnyEntry |
| 49 | { |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 50 | constexpr bool |
| 51 | operator()(const Entry&) const noexcept |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 52 | { |
| 53 | return true; |
| 54 | } |
| 55 | }; |
| 56 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 57 | /** \brief A predicate to accept or reject an Entry and its children. |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 58 | * \return `.first` indicates whether entry should be accepted; |
| 59 | * `.second` indicates whether entry's children should be visited |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 60 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 61 | using EntrySubTreeSelector = std::function<std::pair<bool, bool>(const Entry&)>; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 62 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 63 | /** |
| 64 | * \brief An #EntrySubTreeSelector that accepts every Entry and its children. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 65 | */ |
| 66 | struct AnyEntrySubTree |
| 67 | { |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 68 | constexpr std::pair<bool, bool> |
| 69 | operator()(const Entry&) const noexcept |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 70 | { |
| 71 | return {true, true}; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | class EnumerationImpl; |
| 76 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 77 | /** |
| 78 | * \brief NameTree iterator. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 79 | */ |
Davide Pesavento | 8f0b8b6 | 2023-08-29 21:04:08 -0400 | [diff] [blame] | 80 | class Iterator : public boost::forward_iterator_helper<Iterator, const Entry> |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 81 | { |
| 82 | public: |
| 83 | Iterator(); |
| 84 | |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 85 | Iterator(shared_ptr<EnumerationImpl> impl, const Entry* ref); |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 86 | |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 87 | const Entry& |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 88 | operator*() const noexcept |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 89 | { |
| 90 | BOOST_ASSERT(m_impl != nullptr); |
| 91 | return *m_entry; |
| 92 | } |
| 93 | |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 94 | Iterator& |
| 95 | operator++(); |
| 96 | |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 97 | friend bool |
| 98 | operator==(const Iterator& lhs, const Iterator& rhs) noexcept |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 99 | { |
Davide Pesavento | 191a7a2 | 2023-05-17 22:40:43 -0400 | [diff] [blame] | 100 | return lhs.m_entry == rhs.m_entry; |
| 101 | } |
| 102 | |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 103 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 104 | /** \brief Enumeration implementation; nullptr for end iterator. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 105 | */ |
| 106 | shared_ptr<EnumerationImpl> m_impl; |
| 107 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 108 | /** \brief Current entry; nullptr for uninitialized iterator. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 109 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 110 | const Entry* m_entry = nullptr; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 111 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 112 | /** \brief Reference entry used by enumeration implementation. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 113 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 114 | const Entry* m_ref = nullptr; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 115 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 116 | /** \brief State used by enumeration implementation. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 117 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 118 | int m_state = 0; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 119 | |
| 120 | friend std::ostream& operator<<(std::ostream&, const Iterator&); |
| 121 | friend class FullEnumerationImpl; |
| 122 | friend class PartialEnumerationImpl; |
| 123 | friend class PrefixMatchImpl; |
| 124 | }; |
| 125 | |
| 126 | std::ostream& |
| 127 | operator<<(std::ostream& os, const Iterator& i); |
| 128 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 129 | /** |
| 130 | * \brief Enumeration operation implementation. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 131 | */ |
| 132 | class EnumerationImpl |
| 133 | { |
| 134 | public: |
| 135 | explicit |
| 136 | EnumerationImpl(const NameTree& nt); |
| 137 | |
| 138 | virtual void |
| 139 | advance(Iterator& i) = 0; |
| 140 | |
| 141 | protected: |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 142 | ~EnumerationImpl() = default; |
| 143 | |
| 144 | protected: |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 145 | const NameTree& nt; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 146 | const Hashtable& ht; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 147 | }; |
| 148 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 149 | /** |
| 150 | * \brief Full enumeration implementation. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 151 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 152 | class FullEnumerationImpl final : public EnumerationImpl |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 153 | { |
| 154 | public: |
| 155 | FullEnumerationImpl(const NameTree& nt, const EntrySelector& pred); |
| 156 | |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 157 | void |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 158 | advance(Iterator& i) final; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 159 | |
| 160 | private: |
| 161 | EntrySelector m_pred; |
| 162 | }; |
| 163 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 164 | /** |
| 165 | * \brief Partial enumeration implementation. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 166 | * |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 167 | * Iterator::m_ref should be initialized to subtree root. |
| 168 | * Iterator::m_state LSB indicates whether to visit children of m_entry. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 169 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 170 | class PartialEnumerationImpl final : public EnumerationImpl |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 171 | { |
| 172 | public: |
| 173 | PartialEnumerationImpl(const NameTree& nt, const EntrySubTreeSelector& pred); |
| 174 | |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 175 | void |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 176 | advance(Iterator& i) final; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 177 | |
| 178 | private: |
| 179 | EntrySubTreeSelector m_pred; |
| 180 | }; |
| 181 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 182 | /** |
| 183 | * \brief Partial enumeration implementation. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 184 | * |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 185 | * Iterator::m_ref should be initialized to longest prefix matched entry. |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 186 | */ |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 187 | class PrefixMatchImpl final : public EnumerationImpl |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 188 | { |
| 189 | public: |
| 190 | PrefixMatchImpl(const NameTree& nt, const EntrySelector& pred); |
| 191 | |
| 192 | private: |
Alexander Afanasyev | 847de40 | 2017-09-21 18:57:30 -0400 | [diff] [blame] | 193 | void |
Davide Pesavento | 3db9807 | 2021-03-09 23:03:27 -0500 | [diff] [blame] | 194 | advance(Iterator& i) final; |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 195 | |
| 196 | private: |
| 197 | EntrySelector m_pred; |
| 198 | }; |
| 199 | |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 200 | /** |
| 201 | * \brief A forward range of name tree entries. |
Junxiao Shi | 9f6ca60 | 2016-08-15 04:50:29 +0000 | [diff] [blame] | 202 | * |
Davide Pesavento | 0a05f7a | 2023-10-16 20:28:06 -0400 | [diff] [blame] | 203 | * This type has `.begin()` and `.end()` methods which return Iterator. |
| 204 | * This type is usable with range-based for loops. |
Junxiao Shi | 9f6ca60 | 2016-08-15 04:50:29 +0000 | [diff] [blame] | 205 | */ |
Davide Pesavento | 87fc0f8 | 2018-04-11 23:43:51 -0400 | [diff] [blame] | 206 | using Range = boost::iterator_range<Iterator>; |
Junxiao Shi | 9f6ca60 | 2016-08-15 04:50:29 +0000 | [diff] [blame] | 207 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 208 | } // namespace nfd::name_tree |
Junxiao Shi | 029401f | 2016-08-05 12:55:14 +0000 | [diff] [blame] | 209 | |
| 210 | #endif // NFD_DAEMON_TABLE_NAME_TREE_ITERATOR_HPP |