Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 2 | /* |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 3 | * Copyright (c) 2014-2024, Regents of the University of California, |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [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. |
| 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_HASHTABLE_HPP |
| 27 | #define NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP |
| 28 | |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 29 | #include "name-tree-entry.hpp" |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 30 | |
Davide Pesavento | 2c9d2ca | 2024-01-27 16:36:51 -0500 | [diff] [blame] | 31 | #include <limits> |
| 32 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 33 | namespace nfd::name_tree { |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 34 | |
| 35 | class Entry; |
| 36 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 37 | /** \brief A single hash value. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 38 | */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 39 | using HashValue = size_t; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 40 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 41 | /** \brief A sequence of hash values. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 42 | * \sa computeHashes |
| 43 | */ |
Junxiao Shi | f54d030 | 2017-09-07 18:16:38 +0000 | [diff] [blame] | 44 | using HashSequence = std::vector<HashValue>; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 45 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 46 | /** \brief Computes hash value of \p name.getPrefix(prefixLen). |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 47 | */ |
| 48 | HashValue |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 49 | computeHash(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 50 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 51 | /** \brief Computes hash values for each prefix of \p name.getPrefix(prefixLen). |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 52 | * \return a hash sequence, where the i-th hash value equals computeHash(name, i) |
| 53 | */ |
| 54 | HashSequence |
Junxiao Shi | 042a331 | 2017-09-15 02:51:20 +0000 | [diff] [blame] | 55 | computeHashes(const Name& name, size_t prefixLen = std::numeric_limits<size_t>::max()); |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 56 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 57 | /** \brief A hashtable node. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 58 | * |
| 59 | * Zero or more nodes can be added to a hashtable bucket. They are organized as |
| 60 | * a doubly linked list through prev and next pointers. |
| 61 | */ |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 62 | class Node : noncopyable |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 63 | { |
| 64 | public: |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 65 | /** \post entry.getName() == name |
| 66 | * \post getNode(entry) == this |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 67 | */ |
| 68 | Node(HashValue h, const Name& name); |
| 69 | |
| 70 | /** \pre prev == nullptr |
| 71 | * \pre next == nullptr |
| 72 | */ |
| 73 | ~Node(); |
| 74 | |
| 75 | public: |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 76 | const HashValue hash; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 77 | Node* prev; |
| 78 | Node* next; |
Junxiao Shi | 340d553 | 2016-08-13 04:00:35 +0000 | [diff] [blame] | 79 | mutable Entry entry; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 80 | }; |
| 81 | |
| 82 | /** \return node associated with entry |
| 83 | * \note This function is for NameTree internal use. |
| 84 | */ |
| 85 | Node* |
| 86 | getNode(const Entry& entry); |
| 87 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 88 | /** \brief Invoke a function for each node in a doubly linked list. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 89 | * \tparam N either Node or const Node |
| 90 | * \tparam F a functor with signature void F(N*) |
| 91 | * \note It's safe to delete the node in the function. |
| 92 | */ |
| 93 | template<typename N, typename F> |
| 94 | void |
| 95 | foreachNode(N* head, const F& func) |
| 96 | { |
| 97 | N* node = head; |
| 98 | while (node != nullptr) { |
| 99 | N* next = node->next; |
| 100 | func(node); |
| 101 | node = next; |
| 102 | } |
| 103 | } |
| 104 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 105 | /** |
| 106 | * \brief Provides options for Hashtable. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 107 | */ |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 108 | struct HashtableOptions |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 109 | { |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 110 | /** \brief Constructor. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 111 | * \post initialSize == size |
| 112 | * \post minSize == size |
| 113 | */ |
| 114 | explicit |
| 115 | HashtableOptions(size_t size = 16); |
| 116 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 117 | /** \brief Initial number of buckets. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 118 | */ |
| 119 | size_t initialSize; |
| 120 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 121 | /** \brief Minimal number of buckets. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 122 | */ |
| 123 | size_t minSize; |
| 124 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 125 | /** \brief If the hashtable has more than `nBuckets*expandLoadFactor` nodes, it will be expanded. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 126 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 127 | float expandLoadFactor = 0.5f; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 128 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 129 | /** \brief When the hashtable is expanded, its new size will be `nBuckets*expandFactor`. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 130 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 131 | float expandFactor = 2.0f; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 132 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 133 | /** \brief If the hashtable has less than `nBuckets*shrinkLoadFactor` nodes, it will be shrunk. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 134 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 135 | float shrinkLoadFactor = 0.1f; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 136 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 137 | /** \brief When the hashtable is shrunk, its new size will be `max(nBuckets*shrinkFactor, minSize)`. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 138 | */ |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 139 | float shrinkFactor = 0.5f; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 142 | /** |
| 143 | * \brief A hashtable for fast exact name lookup. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 144 | * |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 145 | * The Hashtable contains a number of buckets. |
| 146 | * Each node is placed into a bucket determined by a hash value computed from its name. |
| 147 | * Hash collision is resolved through a doubly linked list in each bucket. |
| 148 | * The number of buckets is adjusted according to how many nodes are stored. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 149 | */ |
| 150 | class Hashtable |
| 151 | { |
| 152 | public: |
Davide Pesavento | b7bfcb9 | 2022-05-22 23:55:23 -0400 | [diff] [blame] | 153 | using Options = HashtableOptions; |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 154 | |
| 155 | explicit |
| 156 | Hashtable(const Options& options); |
| 157 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 158 | /** \brief Deallocates all nodes. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 159 | */ |
| 160 | ~Hashtable(); |
| 161 | |
| 162 | /** \return number of nodes |
| 163 | */ |
| 164 | size_t |
| 165 | size() const |
| 166 | { |
| 167 | return m_size; |
| 168 | } |
| 169 | |
| 170 | /** \return number of buckets |
| 171 | */ |
| 172 | size_t |
| 173 | getNBuckets() const |
| 174 | { |
| 175 | return m_buckets.size(); |
| 176 | } |
| 177 | |
| 178 | /** \return bucket index for hash value h |
| 179 | */ |
| 180 | size_t |
| 181 | computeBucketIndex(HashValue h) const |
| 182 | { |
| 183 | return h % this->getNBuckets(); |
| 184 | } |
| 185 | |
| 186 | /** \return i-th bucket |
| 187 | * \pre bucket < getNBuckets() |
| 188 | */ |
| 189 | const Node* |
| 190 | getBucket(size_t bucket) const |
| 191 | { |
| 192 | BOOST_ASSERT(bucket < this->getNBuckets()); |
| 193 | return m_buckets[bucket]; // don't use m_bucket.at() for better performance |
| 194 | } |
| 195 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 196 | /** \brief Find node for name.getPrefix(prefixLen). |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 197 | * \pre name.size() > prefixLen |
| 198 | */ |
| 199 | const Node* |
| 200 | find(const Name& name, size_t prefixLen) const; |
| 201 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 202 | /** \brief Find node for name.getPrefix(prefixLen). |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 203 | * \pre name.size() > prefixLen |
| 204 | * \pre hashes == computeHashes(name) |
| 205 | */ |
| 206 | const Node* |
| 207 | find(const Name& name, size_t prefixLen, const HashSequence& hashes) const; |
| 208 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 209 | /** \brief Find or insert node for name.getPrefix(prefixLen). |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 210 | * \pre name.size() > prefixLen |
| 211 | * \pre hashes == computeHashes(name) |
| 212 | */ |
| 213 | std::pair<const Node*, bool> |
| 214 | insert(const Name& name, size_t prefixLen, const HashSequence& hashes); |
| 215 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 216 | /** \brief Delete node. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 217 | * \pre node exists in this hashtable |
| 218 | */ |
| 219 | void |
| 220 | erase(Node* node); |
| 221 | |
| 222 | private: |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 223 | /** \brief Attach node to bucket. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 224 | */ |
| 225 | void |
| 226 | attach(size_t bucket, Node* node); |
| 227 | |
Davide Pesavento | aa9e3b2 | 2022-10-21 17:00:07 -0400 | [diff] [blame] | 228 | /** \brief Detach node from bucket. |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 229 | */ |
| 230 | void |
| 231 | detach(size_t bucket, Node* node); |
| 232 | |
| 233 | std::pair<const Node*, bool> |
| 234 | findOrInsert(const Name& name, size_t prefixLen, HashValue h, bool allowInsert); |
| 235 | |
| 236 | void |
| 237 | computeThresholds(); |
| 238 | |
| 239 | void |
| 240 | resize(size_t newNBuckets); |
| 241 | |
| 242 | private: |
| 243 | std::vector<Node*> m_buckets; |
| 244 | Options m_options; |
| 245 | size_t m_size; |
| 246 | size_t m_expandThreshold; |
| 247 | size_t m_shrinkThreshold; |
| 248 | }; |
| 249 | |
Davide Pesavento | e422f9e | 2022-06-03 01:30:23 -0400 | [diff] [blame] | 250 | } // namespace nfd::name_tree |
Junxiao Shi | b660b4c | 2016-08-06 20:47:44 +0000 | [diff] [blame] | 251 | |
| 252 | #endif // NFD_DAEMON_TABLE_NAME_TREE_HASHTABLE_HPP |