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