39 compute(
const void* buffer,
size_t length)
49 compute(
const void* buffer,
size_t length)
65 for (
size_t i = 0, last = std::min(prefixLen,
name.size()); i < last; ++i) {
67 h ^= HashFunc::compute(comp.
wire(), comp.
size());
77 size_t last = std::min(prefixLen,
name.size());
79 seq.reserve(last + 1);
84 for (
size_t i = 0; i < last; ++i) {
86 h ^= HashFunc::compute(comp.
wire(), comp.
size());
102 BOOST_ASSERT(
prev ==
nullptr);
103 BOOST_ASSERT(
next ==
nullptr);
122 BOOST_ASSERT(m_options.
minSize > 0);
133 this->computeThresholds();
138 for (
size_t i = 0; i < m_buckets.size(); ++i) {
147 Hashtable::attach(
size_t bucket,
Node* node)
149 node->
prev =
nullptr;
150 node->
next = m_buckets[bucket];
152 if (node->
next !=
nullptr) {
153 BOOST_ASSERT(node->
next->
prev ==
nullptr);
157 m_buckets[bucket] = node;
161 Hashtable::detach(
size_t bucket, Node* node)
163 if (node->prev !=
nullptr) {
164 BOOST_ASSERT(node->prev->next == node);
165 node->prev->next = node->next;
168 BOOST_ASSERT(m_buckets[bucket] == node);
169 m_buckets[bucket] = node->next;
172 if (node->next !=
nullptr) {
173 BOOST_ASSERT(node->next->prev == node);
174 node->next->prev = node->prev;
177 node->prev = node->next =
nullptr;
180 std::pair<const Node*, bool>
181 Hashtable::findOrInsert(
const Name&
name,
size_t prefixLen,
HashValue h,
bool allowInsert)
185 for (
const Node* node = m_buckets[bucket]; node !=
nullptr; node = node->next) {
186 if (node->hash == h &&
name.compare(0, prefixLen, node->entry.getName()) == 0) {
187 NFD_LOG_TRACE(
"found " <<
name.getPrefix(prefixLen) <<
" hash=" << h <<
" bucket=" << bucket);
188 return {node,
false};
193 NFD_LOG_TRACE(
"not-found " <<
name.getPrefix(prefixLen) <<
" hash=" << h <<
" bucket=" << bucket);
194 return {
nullptr,
false};
197 Node* node =
new Node(h,
name.getPrefix(prefixLen));
198 this->attach(bucket, node);
199 NFD_LOG_TRACE(
"insert " << node->entry.getName() <<
" hash=" << h <<
" bucket=" << bucket);
202 if (m_size > m_expandThreshold) {
203 this->resize(
static_cast<size_t>(m_options.
expandFactor * this->getNBuckets()));
213 return const_cast<Hashtable*
>(
this)->findOrInsert(
name, prefixLen, h,
false).first;
220 return const_cast<Hashtable*
>(
this)->findOrInsert(
name, prefixLen, hashes[prefixLen],
false).first;
223 std::pair<const Node*, bool>
227 return this->findOrInsert(
name, prefixLen, hashes[prefixLen],
true);
233 BOOST_ASSERT(node !=
nullptr);
239 this->detach(bucket, node);
243 if (m_size < m_shrinkThreshold) {
244 size_t newNBuckets = std::max(m_options.
minSize,
245 static_cast<size_t>(m_options.
shrinkFactor * this->getNBuckets()));
246 this->resize(newNBuckets);
251 Hashtable::computeThresholds()
255 NFD_LOG_TRACE(
"thresholds expand=" << m_expandThreshold <<
" shrink=" << m_shrinkThreshold);
259 Hashtable::resize(
size_t newNBuckets)
266 std::vector<Node*> oldBuckets;
267 oldBuckets.swap(m_buckets);
268 m_buckets.resize(newNBuckets);
270 for (Node* head : oldBuckets) {
273 this->attach(bucket, node);
277 this->computeThresholds();