HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | // Name Tree (Name Prefix Hash Table) |
| 8 | |
| 9 | #ifndef NFD_TABLE_NAME_TREE_HPP |
| 10 | #define NFD_TABLE_NAME_TREE_HPP |
| 11 | |
| 12 | #include "common.hpp" |
| 13 | #include "name-tree-entry.hpp" |
| 14 | |
| 15 | namespace nfd { |
| 16 | namespace name_tree { |
| 17 | |
| 18 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 19 | * \brief Compute the hash value of the given name prefix. |
| 20 | * \todo 1) have another parameter that specifies the number of components |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 21 | * 2) optimize the hash function to return a list of have values for all |
| 22 | * the (or a certain number of, like a batch operation) prefixes. 3) move |
| 23 | * the hash-related code to a separate file in /core or ndn-cpp-dev lib. |
| 24 | */ |
| 25 | uint32_t |
| 26 | hashName(const Name& prefix); |
| 27 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 28 | /// a predicate to accept or reject an Entry in find operations |
| 29 | typedef function<bool (const Entry& entry)> EntrySelector; |
| 30 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 31 | /** |
| 32 | * \brief a predicate to accept or reject an Entry and its children |
| 33 | * \return .first indicates whether entry should be accepted; |
| 34 | * .second indicates whether entry's children should be visited |
| 35 | */ |
| 36 | typedef function<std::pair<bool,bool> (const Entry& entry)> EntrySubTreeSelector; |
| 37 | |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 38 | struct AnyEntry { |
| 39 | bool |
| 40 | operator()(const Entry& entry) |
| 41 | { |
| 42 | return true; |
| 43 | } |
| 44 | }; |
| 45 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 46 | struct AnyEntrySubTree { |
| 47 | std::pair<bool, bool> |
| 48 | operator()(const Entry& entry) |
| 49 | { |
| 50 | return std::make_pair(true, true); |
| 51 | } |
| 52 | }; |
| 53 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 54 | } // namespace name_tree |
| 55 | |
| 56 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 57 | * \brief Class Name Tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 58 | */ |
| 59 | class NameTree : noncopyable |
| 60 | { |
| 61 | public: |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 62 | class const_iterator; |
| 63 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 64 | explicit |
| 65 | NameTree(size_t nBuckets); |
| 66 | |
| 67 | ~NameTree(); |
| 68 | |
| 69 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 70 | * \brief Get the number of occupied entries in the Name Tree |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 71 | */ |
| 72 | size_t |
| 73 | size() const; |
| 74 | |
| 75 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 76 | * \brief Get the number of buckets in the Name Tree (NPHT) |
| 77 | * \details The number of buckets is the one that used to create the hash |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 78 | * table, i.e., m_nBuckets. |
| 79 | */ |
| 80 | size_t |
| 81 | getNBuckets() const; |
| 82 | |
| 83 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 84 | * \brief Look for the Name Tree Entry that contains this name prefix. |
| 85 | * \details Starts from the shortest name prefix, and then increase the |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 86 | * number of name components by one each time. All non-existing Name Tree |
| 87 | * Entries will be created. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 88 | * \param prefix The querying name prefix. |
| 89 | * \return The pointer to the Name Tree Entry that contains this full name |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 90 | * prefix. |
| 91 | */ |
| 92 | shared_ptr<name_tree::Entry> |
| 93 | lookup(const Name& prefix); |
| 94 | |
| 95 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 96 | * \brief Exact match lookup for the given name prefix. |
| 97 | * \return a null shared_ptr if this prefix is not found; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 98 | * otherwise return the Name Tree Entry address |
| 99 | */ |
| 100 | shared_ptr<name_tree::Entry> |
| 101 | findExactMatch(const Name& prefix) const; |
| 102 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 103 | shared_ptr<name_tree::Entry> |
| 104 | findExactMatch(const fib::Entry& fibEntry) const; |
| 105 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 106 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 107 | * \brief Erase a Name Tree Entry if this entry is empty. |
| 108 | * \details If a Name Tree Entry contains no Children, no FIB, no PIT, and |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 109 | * no Measurements entries, then it can be erased. In addition, its parent entry |
| 110 | * will also be examined by following the parent pointer until all empty entries |
| 111 | * are erased. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 112 | * \param entry The pointer to the entry to be erased. The entry pointer should |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 113 | * returned by the findExactMatch(), lookup(), or findLongestPrefixMatch() functions. |
| 114 | */ |
| 115 | bool |
| 116 | eraseEntryIfEmpty(shared_ptr<name_tree::Entry> entry); |
| 117 | |
| 118 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 119 | * \brief Longest prefix matching for the given name |
| 120 | * \details Starts from the full name string, reduce the number of name component |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 121 | * by one each time, until an Entry is found. |
| 122 | */ |
| 123 | shared_ptr<name_tree::Entry> |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 124 | findLongestPrefixMatch(const Name& prefix, |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 125 | const name_tree::EntrySelector& entrySelector = |
| 126 | name_tree::AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 127 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame^] | 128 | shared_ptr<name_tree::Entry> |
| 129 | findLongestPrefixMatch(shared_ptr<name_tree::Entry> entry, |
| 130 | const name_tree::EntrySelector& entrySelector = |
| 131 | name_tree::AnyEntry()) const; |
| 132 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 133 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 134 | * \brief Resize the hash table size when its load factor reaches a threshold. |
| 135 | * \details As we are currently using a hand-written hash table implementation |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 136 | * for the Name Tree, the hash table resize() function should be kept in the |
| 137 | * name-tree.hpp file. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 138 | * \param newNBuckets The number of buckets for the new hash table. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 139 | */ |
| 140 | void |
| 141 | resize(size_t newNBuckets); |
| 142 | |
| 143 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 144 | * \brief Enumerate all the name prefixes stored in the Name Tree. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 145 | */ |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 146 | const_iterator |
| 147 | fullEnumerate(const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 148 | |
| 149 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 150 | * \brief Enumerate all the name prefixes that satisfies the EntrySubTreeSelector. |
| 151 | */ |
| 152 | const_iterator |
Junxiao Shi | 4063184 | 2014-03-01 13:52:37 -0700 | [diff] [blame] | 153 | partialEnumerate(const Name& prefix, |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 154 | const name_tree::EntrySubTreeSelector& entrySubTreeSelector = |
| 155 | name_tree::AnyEntrySubTree()) const; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 156 | |
| 157 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 158 | * \brief Enumerate all the name prefixes that satisfy the prefix and entrySelector |
| 159 | */ |
| 160 | const_iterator |
| 161 | findAllMatches(const Name& prefix, |
| 162 | const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry()) const; |
| 163 | |
| 164 | /** |
| 165 | * \brief Dump all the information stored in the Name Tree for debugging. |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 166 | */ |
| 167 | void |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 168 | dump(std::ostream& output) const; |
| 169 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame^] | 170 | shared_ptr<name_tree::Entry> |
| 171 | get(const fib::Entry& fibEntry) const; |
| 172 | |
| 173 | shared_ptr<name_tree::Entry> |
| 174 | get(const pit::Entry& pitEntry) const; |
| 175 | |
| 176 | shared_ptr<name_tree::Entry> |
| 177 | get(const measurements::Entry& measurementsEntry) const; |
| 178 | |
| 179 | shared_ptr<name_tree::Entry> |
| 180 | get(const strategy_choice::Entry& strategeChoiceEntry) const; |
| 181 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 182 | const_iterator |
| 183 | begin() const; |
| 184 | |
| 185 | const_iterator |
| 186 | end() const; |
| 187 | |
| 188 | enum IteratorType |
| 189 | { |
| 190 | FULL_ENUMERATE_TYPE, |
| 191 | PARTIAL_ENUMERATE_TYPE, |
| 192 | FIND_ALL_MATCHES_TYPE |
| 193 | }; |
| 194 | |
| 195 | class const_iterator : public std::iterator<std::forward_iterator_tag, name_tree::Entry> |
| 196 | { |
| 197 | public: |
| 198 | friend class NameTree; |
| 199 | |
| 200 | const_iterator(NameTree::IteratorType type, |
| 201 | const NameTree& nameTree, |
| 202 | shared_ptr<name_tree::Entry> entry, |
| 203 | const name_tree::EntrySelector& entrySelector = name_tree::AnyEntry(), |
| 204 | const name_tree::EntrySubTreeSelector& entrySubTreeSelector = name_tree::AnyEntrySubTree()); |
| 205 | |
| 206 | ~const_iterator(); |
| 207 | |
| 208 | const name_tree::Entry& |
| 209 | operator*() const; |
| 210 | |
| 211 | shared_ptr<name_tree::Entry> |
| 212 | operator->() const; |
| 213 | |
| 214 | const_iterator |
| 215 | operator++(); |
| 216 | |
| 217 | const_iterator |
| 218 | operator++(int); |
| 219 | |
| 220 | bool |
| 221 | operator==(const const_iterator& other) const; |
| 222 | |
| 223 | bool |
| 224 | operator!=(const const_iterator& other) const; |
| 225 | |
| 226 | private: |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 227 | const NameTree& m_nameTree; |
| 228 | shared_ptr<name_tree::Entry> m_entry; |
| 229 | shared_ptr<name_tree::Entry> m_subTreeRoot; |
| 230 | shared_ptr<name_tree::EntrySelector> m_entrySelector; |
| 231 | shared_ptr<name_tree::EntrySubTreeSelector> m_entrySubTreeSelector; |
| 232 | NameTree::IteratorType m_type; |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 233 | bool m_shouldVisitChildren; |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 234 | }; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 235 | |
| 236 | private: |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 237 | size_t m_nItems; // Number of items being stored |
| 238 | size_t m_nBuckets; // Number of hash buckets |
| 239 | double m_loadFactor; |
| 240 | size_t m_resizeThreshold; |
| 241 | int m_resizeFactor; |
| 242 | name_tree::Node** m_buckets; // Name Tree Buckets in the NPHT |
| 243 | shared_ptr<name_tree::Entry> m_end; |
| 244 | const_iterator m_endIterator; |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 245 | |
| 246 | /** |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 247 | * \brief Create a Name Tree Entry if it does not exist, or return the existing |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 248 | * Name Tree Entry address. |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 249 | * \details Called by lookup() only. |
| 250 | * \return The first item is the Name Tree Entry address, the second item is |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 251 | * a bool value indicates whether this is an old entry (false) or a new |
| 252 | * entry (true). |
| 253 | */ |
| 254 | std::pair<shared_ptr<name_tree::Entry>, bool> |
| 255 | insert(const Name& prefix); |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 256 | }; |
| 257 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 258 | inline NameTree::const_iterator::~const_iterator() |
| 259 | { |
| 260 | } |
| 261 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 262 | inline size_t |
| 263 | NameTree::size() const |
| 264 | { |
| 265 | return m_nItems; |
| 266 | } |
| 267 | |
| 268 | inline size_t |
| 269 | NameTree::getNBuckets() const |
| 270 | { |
| 271 | return m_nBuckets; |
| 272 | } |
| 273 | |
Junxiao Shi | efceadc | 2014-03-09 18:52:57 -0700 | [diff] [blame] | 274 | inline shared_ptr<name_tree::Entry> |
| 275 | NameTree::findExactMatch(const fib::Entry& fibEntry) const |
| 276 | { |
| 277 | return fibEntry.m_nameTreeEntry; |
| 278 | } |
| 279 | |
HangZhang | cb4fc83 | 2014-03-11 16:57:11 +0800 | [diff] [blame^] | 280 | inline shared_ptr<name_tree::Entry> |
| 281 | NameTree::get(const fib::Entry& fibEntry) const |
| 282 | { |
| 283 | return fibEntry.m_nameTreeEntry; |
| 284 | } |
| 285 | |
| 286 | inline shared_ptr<name_tree::Entry> |
| 287 | NameTree::get(const pit::Entry& pitEntry) const |
| 288 | { |
| 289 | return pitEntry.m_nameTreeEntry; |
| 290 | } |
| 291 | |
| 292 | inline shared_ptr<name_tree::Entry> |
| 293 | NameTree::get(const measurements::Entry& measurementsEntry) const |
| 294 | { |
| 295 | return measurementsEntry.m_nameTreeEntry; |
| 296 | } |
| 297 | |
| 298 | inline shared_ptr<name_tree::Entry> |
| 299 | NameTree::get(const strategy_choice::Entry& strategeChoiceEntry) const |
| 300 | { |
| 301 | return strategeChoiceEntry.m_nameTreeEntry; |
| 302 | } |
| 303 | |
Haowei Yuan | e1079fc | 2014-03-08 14:41:25 -0600 | [diff] [blame] | 304 | inline NameTree::const_iterator |
| 305 | NameTree::begin() const |
| 306 | { |
| 307 | return fullEnumerate(); |
| 308 | } |
| 309 | |
| 310 | inline NameTree::const_iterator |
| 311 | NameTree::end() const |
| 312 | { |
| 313 | return m_endIterator; |
| 314 | } |
| 315 | |
| 316 | inline const name_tree::Entry& |
| 317 | NameTree::const_iterator::operator*() const |
| 318 | { |
| 319 | return *m_entry; |
| 320 | } |
| 321 | |
| 322 | inline shared_ptr<name_tree::Entry> |
| 323 | NameTree::const_iterator::operator->() const |
| 324 | { |
| 325 | return m_entry; |
| 326 | } |
| 327 | |
| 328 | inline NameTree::const_iterator |
| 329 | NameTree::const_iterator::operator++(int) |
| 330 | { |
| 331 | NameTree::const_iterator temp(*this); |
| 332 | ++(*this); |
| 333 | return temp; |
| 334 | } |
| 335 | |
| 336 | inline bool |
| 337 | NameTree::const_iterator::operator==(const NameTree::const_iterator& other) const |
| 338 | { |
| 339 | return m_entry == other.m_entry; |
| 340 | } |
| 341 | |
| 342 | inline bool |
| 343 | NameTree::const_iterator::operator!=(const NameTree::const_iterator& other) const |
| 344 | { |
| 345 | return m_entry != other.m_entry; |
| 346 | } |
| 347 | |
HYuan | a9b8575 | 2014-02-26 02:32:30 -0600 | [diff] [blame] | 348 | } // namespace nfd |
| 349 | |
| 350 | #endif // NFD_TABLE_NAME_TREE_HPP |