HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2016, Regents of the University of California, |
Alexander Afanasyev | 319f2c8 | 2015-01-07 14:56:53 -0800 | [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. |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 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/>. |
Junxiao Shi | ee5a444 | 2014-07-27 17:13:43 -0700 | [diff] [blame] | 24 | */ |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 25 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 26 | #ifndef NFD_DAEMON_TABLE_NAME_TREE_HPP |
| 27 | #define NFD_DAEMON_TABLE_NAME_TREE_HPP |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 28 | |
| 29 | #include "common.hpp" |
| 30 | #include "name-tree-entry.hpp" |
| 31 | |
| 32 | namespace nfd { |
| 33 | namespace name_tree { |
| 34 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 35 | /** \brief Compute the hash value of the given name prefix's WIRE FORMAT |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 36 | */ |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 37 | size_t |
| 38 | computeHash(const Name& prefix); |
| 39 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 40 | /** \brief Incrementally compute hash values |
| 41 | * \return Return a vector of hash values, starting from the root prefix |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 42 | */ |
| 43 | std::vector<size_t> |
| 44 | computeHashSet(const Name& prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 45 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 46 | /** \brief a predicate to accept or reject an Entry in find operations |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 47 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 48 | typedef function<bool(const Entry& entry)> EntrySelector; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 49 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 50 | /** \brief a predicate to accept or reject an Entry and its children |
| 51 | * \return .first indicates whether entry should be accepted; |
| 52 | * .second indicates whether entry's children should be visited |
| 53 | */ |
| 54 | typedef function<std::pair<bool,bool>(const Entry& entry)> EntrySubTreeSelector; |
| 55 | |
| 56 | struct AnyEntry |
| 57 | { |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 58 | bool |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 59 | operator()(const Entry& entry) const |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 60 | { |
| 61 | return true; |
| 62 | } |
| 63 | }; |
| 64 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 65 | struct AnyEntrySubTree |
| 66 | { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 67 | std::pair<bool, bool> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 68 | operator()(const Entry& entry) const |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 69 | { |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 70 | return {true, true}; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 71 | } |
| 72 | }; |
| 73 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 74 | /** \brief shared name-based index for FIB, PIT, Measurements, and StrategyChoice |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 75 | */ |
| 76 | class NameTree : noncopyable |
| 77 | { |
| 78 | public: |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 79 | class const_iterator; |
| 80 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 81 | explicit |
Haowei Yuan | f52dac7 | 2014-03-24 23:35:03 -0500 | [diff] [blame] | 82 | NameTree(size_t nBuckets = 1024); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 83 | |
| 84 | ~NameTree(); |
| 85 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 86 | public: // information |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 87 | /** \brief Get the number of occupied entries in the Name Tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 88 | */ |
| 89 | size_t |
| 90 | size() const; |
| 91 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 92 | /** \brief Get the number of buckets in the Name Tree (NPHT) |
| 93 | * |
| 94 | * The number of buckets is the one that used to create the hash |
| 95 | * table, i.e., m_nBuckets. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 96 | */ |
| 97 | size_t |
| 98 | getNBuckets() const; |
| 99 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 100 | /** \return Name Tree Entry on which a table entry is attached |
| 101 | */ |
| 102 | template<typename ENTRY> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 103 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 104 | getEntry(const ENTRY& tableEntry) const; |
| 105 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 106 | /** \brief Dump all the information stored in the Name Tree for debugging. |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 107 | */ |
| 108 | void |
| 109 | dump(std::ostream& output) const; |
| 110 | |
| 111 | public: // mutation |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 112 | /** \brief Look for the Name Tree Entry that contains this name prefix. |
| 113 | * |
| 114 | * Starts from the shortest name prefix, and then increase the |
| 115 | * number of name components by one each time. All non-existing Name Tree |
| 116 | * Entries will be created. |
| 117 | * |
| 118 | * \param prefix The querying name prefix. |
| 119 | * \return The pointer to the Name Tree Entry that contains this full name prefix. |
| 120 | * \note Existing iterators are unaffected. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 121 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 122 | shared_ptr<Entry> |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 123 | lookup(const Name& prefix); |
| 124 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 125 | /** \brief get NameTree entry from FIB entry |
| 126 | * |
| 127 | * This is equivalent to .lookup(fibEntry.getPrefix()) |
| 128 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 129 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 130 | lookup(const fib::Entry& fibEntry) const; |
| 131 | |
| 132 | /** \brief get NameTree entry from PIT entry |
| 133 | * |
| 134 | * This is equivalent to .lookup(pitEntry.getName()). |
| 135 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 136 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 137 | lookup(const pit::Entry& pitEntry); |
| 138 | |
| 139 | /** \brief get NameTree entry from Measurements entry |
| 140 | * |
| 141 | * This is equivalent to .lookup(measurementsEntry.getName()) |
| 142 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 143 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 144 | lookup(const measurements::Entry& measurementsEntry) const; |
| 145 | |
| 146 | /** \brief get NameTree entry from StrategyChoice entry |
| 147 | * |
| 148 | * This is equivalent to .lookup(strategyChoiceEntry.getName()) |
| 149 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 150 | shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 151 | lookup(const strategy_choice::Entry& strategyChoiceEntry) const; |
| 152 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 153 | /** \brief Delete a Name Tree Entry if this entry is empty. |
| 154 | * \param entry The entry to be deleted if empty. |
| 155 | * \note This function must be called after a table entry is detached from Name Tree |
| 156 | * entry. The function deletes a Name Tree entry if nothing is attached to it and |
| 157 | * it has no children, then repeats the same process on its ancestors. |
| 158 | * \note Existing iterators, except those pointing to deleted entries, are unaffected. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 159 | */ |
| 160 | bool |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 161 | eraseEntryIfEmpty(shared_ptr<Entry> entry); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 162 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 163 | public: // matching |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 164 | /** \brief Exact match lookup for the given name prefix. |
| 165 | * \return nullptr if this prefix is not found; otherwise return the Name Tree Entry address |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 166 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 167 | shared_ptr<Entry> |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 168 | findExactMatch(const Name& prefix) const; |
| 169 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 170 | /** \brief Longest prefix matching for the given name |
| 171 | * |
| 172 | * Starts from the full name string, reduce the number of name component |
| 173 | * by one each time, until an Entry is found. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 174 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 175 | shared_ptr<Entry> |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 176 | findLongestPrefixMatch(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 177 | const EntrySelector& entrySelector = AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 178 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 179 | shared_ptr<Entry> |
| 180 | findLongestPrefixMatch(shared_ptr<Entry> entry, |
| 181 | const EntrySelector& entrySelector = AnyEntry()) const; |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 182 | |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 183 | /** \brief longest prefix matching for Interest name of the PIT entry |
| 184 | * |
| 185 | * This is equivalent to .findLongestPrefixMatch(pitEntry.getName(), AnyEntry()). |
| 186 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 187 | shared_ptr<Entry> |
Junxiao Shi | 4370fde | 2016-02-24 12:20:46 -0700 | [diff] [blame] | 188 | findLongestPrefixMatch(const pit::Entry& pitEntry) const; |
| 189 | |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 190 | /** \brief Enumerate all the name prefixes that satisfy the prefix and entrySelector |
| 191 | * \return an unspecified type that have .begin() and .end() methods |
| 192 | * and is usable with range-based for |
| 193 | * |
| 194 | * Example: |
| 195 | * \code{.cpp} |
| 196 | * auto&& allMatches = nt.findAllMatches(Name("/A/B/C")); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 197 | * for (const Entry& nte : allMatches) { |
Junxiao Shi | 2b73ca3 | 2014-11-17 19:16:08 -0700 | [diff] [blame] | 198 | * ... |
| 199 | * } |
| 200 | * \endcode |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 201 | * \note Iteration order is implementation-specific and is undefined |
| 202 | * \note The returned iterator may get invalidated when NameTree is modified |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 203 | */ |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 204 | boost::iterator_range<const_iterator> |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 205 | findAllMatches(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 206 | const EntrySelector& entrySelector = AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 207 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 208 | public: // enumeration |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 209 | /** \brief Enumerate all entries, optionally filtered by an EntrySelector. |
| 210 | * \return an unspecified type that have .begin() and .end() methods |
| 211 | * and is usable with range-based for |
| 212 | * |
| 213 | * Example: |
| 214 | * \code{.cpp} |
| 215 | * auto&& enumerable = nt.fullEnumerate(); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 216 | * for (const Entry& nte : enumerable) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 217 | * ... |
| 218 | * } |
| 219 | * \endcode |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 220 | * \note Iteration order is implementation-specific and is undefined |
| 221 | * \note The returned iterator may get invalidated when NameTree is modified |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 222 | */ |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 223 | boost::iterator_range<const_iterator> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 224 | fullEnumerate(const EntrySelector& entrySelector = AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 225 | |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 226 | /** \brief Enumerate all entries under a prefix, optionally filtered by an EntrySubTreeSelector. |
| 227 | * \return an unspecified type that have .begin() and .end() methods |
| 228 | * and is usable with range-based for |
| 229 | * |
| 230 | * Example: |
| 231 | * \code{.cpp} |
| 232 | * auto&& enumerable = nt.partialEnumerate(Name("/A/B/C")); |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 233 | * for (const Entry& nte : enumerable) { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 234 | * ... |
| 235 | * } |
| 236 | * \endcode |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 237 | * \note Iteration order is implementation-specific and is undefined |
| 238 | * \note The returned iterator may get invalidated when NameTree is modified |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 239 | */ |
Junxiao Shi | 5ccd0c2 | 2014-12-02 23:54:42 -0700 | [diff] [blame] | 240 | boost::iterator_range<const_iterator> |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 241 | partialEnumerate(const Name& prefix, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 242 | const EntrySubTreeSelector& entrySubTreeSelector = AnyEntrySubTree()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 243 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 244 | /** \brief Get an iterator pointing to the first NameTree entry |
| 245 | * \note Iteration order is implementation-specific and is undefined |
| 246 | * \note The returned iterator may get invalidated when NameTree is modified |
| 247 | */ |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 248 | const_iterator |
| 249 | begin() const; |
| 250 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 251 | /** \brief Get an iterator referring to the past-the-end FIB entry |
| 252 | * \note Iteration order is implementation-specific and is undefined |
| 253 | * \note The returned iterator may get invalidated when NameTree is modified |
| 254 | */ |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 255 | const_iterator |
| 256 | end() const; |
| 257 | |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 258 | enum IteratorType { |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 259 | FULL_ENUMERATE_TYPE, |
| 260 | PARTIAL_ENUMERATE_TYPE, |
| 261 | FIND_ALL_MATCHES_TYPE |
| 262 | }; |
| 263 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 264 | class const_iterator : public std::iterator<std::forward_iterator_tag, const Entry> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 265 | { |
| 266 | public: |
| 267 | friend class NameTree; |
| 268 | |
Alexander Afanasyev | 09fc3d9 | 2015-01-03 02:02:37 -0800 | [diff] [blame] | 269 | const_iterator(); |
| 270 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 271 | const_iterator(NameTree::IteratorType type, |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 272 | const NameTree& nameTree, |
| 273 | shared_ptr<Entry> entry, |
| 274 | const EntrySelector& entrySelector = AnyEntry(), |
| 275 | const EntrySubTreeSelector& entrySubTreeSelector = AnyEntrySubTree()); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 276 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 277 | const Entry& |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 278 | operator*() const; |
| 279 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 280 | shared_ptr<Entry> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 281 | operator->() const; |
| 282 | |
| 283 | const_iterator |
| 284 | operator++(); |
| 285 | |
| 286 | const_iterator |
| 287 | operator++(int); |
| 288 | |
| 289 | bool |
| 290 | operator==(const const_iterator& other) const; |
| 291 | |
| 292 | bool |
| 293 | operator!=(const const_iterator& other) const; |
| 294 | |
| 295 | private: |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 296 | const NameTree* m_nameTree; |
| 297 | shared_ptr<Entry> m_entry; |
| 298 | shared_ptr<Entry> m_subTreeRoot; |
| 299 | shared_ptr<EntrySelector> m_entrySelector; |
| 300 | shared_ptr<EntrySubTreeSelector> m_entrySubTreeSelector; |
| 301 | NameTree::IteratorType m_type; |
| 302 | bool m_shouldVisitChildren; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 303 | }; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 304 | |
| 305 | private: |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 306 | /** \brief Create a Name Tree Entry if it does not exist, or return the existing |
| 307 | * Name Tree Entry address. |
| 308 | * |
| 309 | * Called by lookup() only. |
| 310 | * |
| 311 | * \return The first item is the Name Tree Entry address, the second item is |
| 312 | * a bool value indicates whether this is an old entry (false) or a new |
| 313 | * entry (true). |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 314 | */ |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 315 | std::pair<shared_ptr<Entry>, bool> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 316 | insert(const Name& prefix); |
| 317 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 318 | /** \brief Resize the hash table size when its load factor reaches a threshold. |
| 319 | * |
| 320 | * As we are currently using a hand-written hash table implementation |
| 321 | * for the Name Tree, the hash table resize() function should be kept in the |
| 322 | * name-tree.hpp file. |
| 323 | * \param newNBuckets The number of buckets for the new hash table. |
Alexander Afanasyev | b303324 | 2014-08-04 11:09:05 -0700 | [diff] [blame] | 324 | */ |
| 325 | void |
| 326 | resize(size_t newNBuckets); |
| 327 | |
| 328 | private: |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 329 | size_t m_nItems; ///< Number of items being stored |
| 330 | size_t m_nBuckets; ///< Number of hash buckets |
| 331 | size_t m_minNBuckets; ///< Minimum number of hash buckets |
| 332 | double m_enlargeLoadFactor; |
| 333 | size_t m_enlargeThreshold; |
| 334 | int m_enlargeFactor; |
| 335 | double m_shrinkLoadFactor; |
| 336 | size_t m_shrinkThreshold; |
| 337 | double m_shrinkFactor; |
| 338 | Node** m_buckets; ///< Name Tree Buckets in the NPHT |
| 339 | shared_ptr<Entry> m_end; |
| 340 | const_iterator m_endIterator; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 341 | }; |
| 342 | |
| 343 | inline size_t |
| 344 | NameTree::size() const |
| 345 | { |
| 346 | return m_nItems; |
| 347 | } |
| 348 | |
| 349 | inline size_t |
| 350 | NameTree::getNBuckets() const |
| 351 | { |
| 352 | return m_nBuckets; |
| 353 | } |
| 354 | |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 355 | template<typename ENTRY> |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 356 | inline shared_ptr<Entry> |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 357 | NameTree::getEntry(const ENTRY& tableEntry) const |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 358 | { |
Junxiao Shi | b184e53 | 2016-05-26 18:09:57 +0000 | [diff] [blame] | 359 | return tableEntry.m_nameTreeEntry.lock(); |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame] | 360 | } |
| 361 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 362 | inline NameTree::const_iterator |
| 363 | NameTree::begin() const |
| 364 | { |
Junxiao Shi | 60607c7 | 2014-11-26 22:40:36 -0700 | [diff] [blame] | 365 | return fullEnumerate().begin(); |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | inline NameTree::const_iterator |
| 369 | NameTree::end() const |
| 370 | { |
| 371 | return m_endIterator; |
| 372 | } |
| 373 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 374 | inline const Entry& |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 375 | NameTree::const_iterator::operator*() const |
| 376 | { |
| 377 | return *m_entry; |
| 378 | } |
| 379 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 380 | inline shared_ptr<Entry> |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 381 | NameTree::const_iterator::operator->() const |
| 382 | { |
| 383 | return m_entry; |
| 384 | } |
| 385 | |
| 386 | inline NameTree::const_iterator |
| 387 | NameTree::const_iterator::operator++(int) |
| 388 | { |
| 389 | NameTree::const_iterator temp(*this); |
| 390 | ++(*this); |
| 391 | return temp; |
| 392 | } |
| 393 | |
| 394 | inline bool |
| 395 | NameTree::const_iterator::operator==(const NameTree::const_iterator& other) const |
| 396 | { |
| 397 | return m_entry == other.m_entry; |
| 398 | } |
| 399 | |
| 400 | inline bool |
| 401 | NameTree::const_iterator::operator!=(const NameTree::const_iterator& other) const |
| 402 | { |
| 403 | return m_entry != other.m_entry; |
| 404 | } |
| 405 | |
Junxiao Shi | 2570f3e | 2016-07-27 02:48:29 +0000 | [diff] [blame^] | 406 | } // namespace name_tree |
| 407 | |
| 408 | using name_tree::NameTree; |
| 409 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 410 | } // namespace nfd |
| 411 | |
Alexander Afanasyev | 613e2a9 | 2014-04-15 13:36:58 -0700 | [diff] [blame] | 412 | #endif // NFD_DAEMON_TABLE_NAME_TREE_HPP |