blob: a764677277c302757eb63ce61a4415e11cdf7fe0 [file] [log] [blame]
Junxiao Shi029401f2016-08-05 12:55:14 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2014-2016, 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-entry.hpp"
30
31namespace nfd {
32namespace name_tree {
33
34/** \brief a predicate to accept or reject an Entry in find operations
35 */
36typedef function<bool(const Entry& entry)> EntrySelector;
37
38/** \brief an EntrySelector that accepts every Entry
39 */
40struct AnyEntry
41{
42 bool
43 operator()(const Entry& entry) const
44 {
45 return true;
46 }
47};
48
49/** \brief a predicate to accept or reject an Entry and its children
50 * \return .first indicates whether entry should be accepted;
51 * .second indicates whether entry's children should be visited
52 */
53typedef function<std::pair<bool,bool>(const Entry& entry)> EntrySubTreeSelector;
54
55/** \brief an EntrySubTreeSelector that accepts every Entry and its children
56 */
57struct AnyEntrySubTree
58{
59 std::pair<bool, bool>
60 operator()(const Entry& entry) const
61 {
62 return {true, true};
63 }
64};
65
66class EnumerationImpl;
67
68/** \brief NameTree iterator
69 */
70class Iterator : public std::iterator<std::forward_iterator_tag, const Entry>
71{
72public:
73 Iterator();
74
75 Iterator(shared_ptr<EnumerationImpl> impl, shared_ptr<Entry> ref);
76
77 virtual
78 ~Iterator() = default;
79
80 const Entry&
81 operator*() const
82 {
83 BOOST_ASSERT(m_impl != nullptr);
84 return *m_entry;
85 }
86
87 shared_ptr<Entry>
88 operator->() const
89 {
90 BOOST_ASSERT(m_impl != nullptr);
91 return m_entry;
92 }
93
94 Iterator&
95 operator++();
96
97 Iterator
98 operator++(int);
99
100 bool
101 operator==(const Iterator& other) const;
102
103 bool
104 operator!=(const Iterator& other) const
105 {
106 return !this->operator==(other);
107 }
108
109private:
110 /** \brief enumeration implementation; nullptr for end iterator
111 */
112 shared_ptr<EnumerationImpl> m_impl;
113
114 /** \brief current entry; nullptr for uninitialized iterator
115 */
116 shared_ptr<Entry> m_entry;
117
118 /** \brief reference entry used by enumeration implementation
119 */
120 shared_ptr<Entry> m_ref;
121
122 /** \brief state used by enumeration implementation
123 */
124 int m_state;
125
126 friend std::ostream& operator<<(std::ostream&, const Iterator&);
127 friend class FullEnumerationImpl;
128 friend class PartialEnumerationImpl;
129 friend class PrefixMatchImpl;
130};
131
132std::ostream&
133operator<<(std::ostream& os, const Iterator& i);
134
135/** \brief enumeration operation implementation
136 */
137class EnumerationImpl
138{
139public:
140 explicit
141 EnumerationImpl(const NameTree& nt);
142
143 virtual void
144 advance(Iterator& i) = 0;
145
146protected:
147 const NameTree& nt;
148};
149
150/** \brief full enumeration implementation
151 */
152class FullEnumerationImpl : public EnumerationImpl
153{
154public:
155 FullEnumerationImpl(const NameTree& nt, const EntrySelector& pred);
156
157 virtual void
158 advance(Iterator& i) override;
159
160private:
161 EntrySelector m_pred;
162};
163
164/** \brief partial enumeration implementation
165 *
166 * Iterator::m_ref should be initialized to subtree root.
167 * Iterator::m_state LSB indicates whether to visit children of m_entry.
168 */
169class PartialEnumerationImpl : public EnumerationImpl
170{
171public:
172 PartialEnumerationImpl(const NameTree& nt, const EntrySubTreeSelector& pred);
173
174 virtual void
175 advance(Iterator& i) override;
176
177private:
178 EntrySubTreeSelector m_pred;
179};
180
181/** \brief partial enumeration implementation
182 *
183 * Iterator::m_ref should be initialized to longest prefix matched entry.
184 */
185class PrefixMatchImpl : public EnumerationImpl
186{
187public:
188 PrefixMatchImpl(const NameTree& nt, const EntrySelector& pred);
189
190private:
191 virtual void
192 advance(Iterator& i) override;
193
194private:
195 EntrySelector m_pred;
196};
197
198} // namespace name_tree
199} // namespace nfd
200
201#endif // NFD_DAEMON_TABLE_NAME_TREE_ITERATOR_HPP