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-2022, 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/range/iterator_range_core.hpp>
32 
33 namespace nfd::name_tree {
34 
35 class NameTree;
36 
40 using EntrySelector = std::function<bool(const Entry&)>;
41 
45 struct AnyEntry
46 {
47  constexpr bool
48  operator()(const Entry&) const noexcept
49  {
50  return true;
51  }
52 };
53 
58 using EntrySubTreeSelector = std::function<std::pair<bool, bool>(const Entry&)>;
59 
64 {
65  constexpr std::pair<bool, bool>
66  operator()(const Entry&) const noexcept
67  {
68  return {true, true};
69  }
70 };
71 
72 class EnumerationImpl;
73 
77 class Iterator
78 {
79 public:
80  using iterator_category = std::forward_iterator_tag;
81  using value_type = const Entry;
82  using difference_type = std::ptrdiff_t;
83  using pointer = value_type*;
85 
87 
88  Iterator(shared_ptr<EnumerationImpl> impl, const Entry* ref);
89 
90  const Entry&
91  operator*() const
92  {
93  BOOST_ASSERT(m_impl != nullptr);
94  return *m_entry;
95  }
96 
97  const Entry*
98  operator->() const
99  {
100  BOOST_ASSERT(m_impl != nullptr);
101  return m_entry;
102  }
103 
104  Iterator&
105  operator++();
106 
107  Iterator
108  operator++(int);
109 
110  bool
111  operator==(const Iterator& other) const;
112 
113  bool
114  operator!=(const Iterator& other) const
115  {
116  return !this->operator==(other);
117  }
118 
119 private:
122  shared_ptr<EnumerationImpl> m_impl;
123 
126  const Entry* m_entry = nullptr;
127 
130  const Entry* m_ref = nullptr;
131 
134  int m_state = 0;
135 
136  friend std::ostream& operator<<(std::ostream&, const Iterator&);
137  friend class FullEnumerationImpl;
139  friend class PrefixMatchImpl;
140 };
141 
142 std::ostream&
143 operator<<(std::ostream& os, const Iterator& i);
144 
148 {
149 public:
150  explicit
151  EnumerationImpl(const NameTree& nt);
152 
153  virtual
154  ~EnumerationImpl() = default;
155 
156  virtual void
157  advance(Iterator& i) = 0;
158 
159 protected:
160  const NameTree& nt;
161  const Hashtable& ht;
162 };
163 
167 {
168 public:
169  FullEnumerationImpl(const NameTree& nt, const EntrySelector& pred);
170 
171  void
172  advance(Iterator& i) final;
173 
174 private:
175  EntrySelector m_pred;
176 };
177 
184 {
185 public:
187 
188  void
189  advance(Iterator& i) final;
190 
191 private:
192  EntrySubTreeSelector m_pred;
193 };
194 
199 class PrefixMatchImpl final : public EnumerationImpl
200 {
201 public:
202  PrefixMatchImpl(const NameTree& nt, const EntrySelector& pred);
203 
204 private:
205  void
206  advance(Iterator& i) final;
207 
208 private:
209  EntrySelector m_pred;
210 };
211 
217 using Range = boost::iterator_range<Iterator>;
218 
219 } // namespace nfd::name_tree
220 
221 #endif // NFD_DAEMON_TABLE_NAME_TREE_ITERATOR_HPP
An entry in the name tree.
Enumeration operation implementation.
virtual ~EnumerationImpl()=default
virtual void advance(Iterator &i)=0
Full enumeration implementation.
FullEnumerationImpl(const NameTree &nt, const EntrySelector &pred)
A hashtable for fast exact name lookup.
std::forward_iterator_tag iterator_category
const Entry & operator*() const
bool operator!=(const Iterator &other) const
friend std::ostream & operator<<(std::ostream &, const Iterator &)
const Entry * operator->() const
bool operator==(const Iterator &other) const
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