name-tree-iterator.hpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2014-2024, Regents of the University of California,
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 
29 #include "name-tree-hashtable.hpp"
30 
31 #include <boost/operators.hpp>
32 #include <boost/range/iterator_range_core.hpp>
33 
34 #include <functional>
35 
36 namespace nfd::name_tree {
37 
38 class NameTree;
39 
43 using EntrySelector = std::function<bool(const Entry&)>;
44 
48 struct AnyEntry
49 {
50  constexpr bool
51  operator()(const Entry&) const noexcept
52  {
53  return true;
54  }
55 };
56 
61 using EntrySubTreeSelector = std::function<std::pair<bool, bool>(const Entry&)>;
62 
67 {
68  constexpr std::pair<bool, bool>
69  operator()(const Entry&) const noexcept
70  {
71  return {true, true};
72  }
73 };
74 
75 class EnumerationImpl;
76 
80 class Iterator : public boost::forward_iterator_helper<Iterator, const Entry>
81 {
82 public:
84 
85  Iterator(shared_ptr<EnumerationImpl> impl, const Entry* ref);
86 
87  const Entry&
88  operator*() const noexcept
89  {
90  BOOST_ASSERT(m_impl != nullptr);
91  return *m_entry;
92  }
93 
94  Iterator&
95  operator++();
96 
97  friend bool
98  operator==(const Iterator& lhs, const Iterator& rhs) noexcept
99  {
100  return lhs.m_entry == rhs.m_entry;
101  }
102 
103 private:
106  shared_ptr<EnumerationImpl> m_impl;
107 
110  const Entry* m_entry = nullptr;
111 
114  const Entry* m_ref = nullptr;
115 
118  int m_state = 0;
119 
120  friend std::ostream& operator<<(std::ostream&, const Iterator&);
121  friend class FullEnumerationImpl;
123  friend class PrefixMatchImpl;
124 };
125 
126 std::ostream&
127 operator<<(std::ostream& os, const Iterator& i);
128 
133 {
134 public:
135  explicit
136  EnumerationImpl(const NameTree& nt);
137 
138  virtual void
139  advance(Iterator& i) = 0;
140 
141 protected:
142  ~EnumerationImpl() = default;
143 
144 protected:
145  const NameTree& nt;
146  const Hashtable& ht;
147 };
148 
153 {
154 public:
155  FullEnumerationImpl(const NameTree& nt, const EntrySelector& pred);
156 
157  void
158  advance(Iterator& i) final;
159 
160 private:
161  EntrySelector m_pred;
162 };
163 
171 {
172 public:
174 
175  void
176  advance(Iterator& i) final;
177 
178 private:
179  EntrySubTreeSelector m_pred;
180 };
181 
187 class PrefixMatchImpl final : public EnumerationImpl
188 {
189 public:
190  PrefixMatchImpl(const NameTree& nt, const EntrySelector& pred);
191 
192 private:
193  void
194  advance(Iterator& i) final;
195 
196 private:
197  EntrySelector m_pred;
198 };
199 
206 using Range = boost::iterator_range<Iterator>;
207 
208 } // namespace nfd::name_tree
209 
210 #endif // NFD_DAEMON_TABLE_NAME_TREE_ITERATOR_HPP
An entry in the name tree.
Enumeration operation implementation.
virtual void advance(Iterator &i)=0
Full enumeration implementation.
FullEnumerationImpl(const NameTree &nt, const EntrySelector &pred)
A hashtable for fast exact name lookup.
const Entry & operator*() const noexcept
friend bool operator==(const Iterator &lhs, const Iterator &rhs) noexcept
friend std::ostream & operator<<(std::ostream &, const Iterator &)
A common index structure for FIB, PIT, StrategyChoice, and Measurements.
Definition: name-tree.hpp:37
Partial enumeration implementation.
PartialEnumerationImpl(const NameTree &nt, const EntrySubTreeSelector &pred)
Partial enumeration implementation.
PrefixMatchImpl(const NameTree &nt, const EntrySelector &pred)
std::ostream & operator<<(std::ostream &os, const Iterator &i)
boost::iterator_range< Iterator > Range
A forward range of name tree entries.
std::function< bool(const Entry &)> EntrySelector
A predicate to accept or reject an Entry in find operations.
std::function< std::pair< bool, bool >(const Entry &)> EntrySubTreeSelector
A predicate to accept or reject an Entry and its children.
An EntrySelector that accepts every Entry.
constexpr bool operator()(const Entry &) const noexcept
An EntrySubTreeSelector that accepts every Entry and its children.
constexpr std::pair< bool, bool > operator()(const Entry &) const noexcept