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