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