39 compute(
const void* buffer,
size_t length)
49 compute(
const void* buffer,
size_t length)
57 using HashFunc = std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32>::type;
65 for (
size_t i = 0, last = std::min(prefixLen, name.size()); i < last; ++i) {
66 const name::Component& comp = name[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) {
85 const name::Component& comp = name[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);
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);
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);
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;
219 BOOST_ASSERT(hashes.at(prefixLen) ==
computeHash(name, prefixLen));
220 return const_cast<Hashtable*
>(
this)->findOrInsert(name, prefixLen, hashes[prefixLen],
false).first;
223 std::pair<const Node*, bool>
226 BOOST_ASSERT(hashes.at(prefixLen) ==
computeHash(name, prefixLen));
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();
float shrinkFactor
when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
size_t initialSize
initial number of buckets
size_t HashValue
a single hash value
HashtableOptions(size_t size=16)
constructor
void foreachNode(N *head, const F &func)
invoke a function for each node in a doubly linked list
uint32 CityHash32(const char *s, size_t len)
size_t getNBuckets() const
HashSequence computeHashes(const Name &name, size_t prefixLen)
computes hash values for each prefix of name.getPrefix(prefixLen)
const Node * find(const Name &name, size_t prefixLen) const
find node for name.getPrefix(prefixLen)
Node(HashValue h, const Name &name)
HashValue computeHash(const Name &name, size_t prefixLen)
computes hash value of name.getPrefix(prefixLen)
size_t computeBucketIndex(HashValue h) const
float expandFactor
when hashtable is expanded, its new size is nBuckets*expandFactor
std::vector< HashValue > HashSequence
a sequence of hash values
uint64 CityHash64(const char *s, size_t len)
float shrinkLoadFactor
if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
provides options for Hashtable
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...
Entry * getParent() const
std::pair< const Node *, bool > insert(const Name &name, size_t prefixLen, const HashSequence &hashes)
find or insert node for name.getPrefix(prefixLen)
std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32 >::type HashFunc
a type with compute static method to compute hash value from a raw buffer
const Name & getName() const
Hashtable(const Options &options)
Node * getNode(const Entry &entry)
An entry in the name tree.
float expandLoadFactor
if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
~Hashtable()
deallocates all nodes
#define NFD_LOG_INIT(name)
void erase(Node *node)
delete node
size_t minSize
minimal number of buckets
a hashtable for fast exact name lookup