39 compute(
const void* buffer,
size_t length)
49 compute(
const void* buffer,
size_t length)
65 for (
size_t i = 0, last = prefixLen < 0 ? name.
size() : prefixLen; i < last; ++i) {
67 h ^= HashFunc::compute(comp.
wire(), comp.
size());
78 seq.reserve(name.
size() + 1);
84 h ^= HashFunc::compute(comp.wire(), comp.size());
100 BOOST_ASSERT(
prev ==
nullptr);
101 BOOST_ASSERT(
next ==
nullptr);
120 BOOST_ASSERT(m_options.
minSize > 0);
131 this->computeThresholds();
136 for (
size_t i = 0; i < m_buckets.size(); ++i) {
145 Hashtable::attach(
size_t bucket,
Node* node)
147 node->
prev =
nullptr;
148 node->
next = m_buckets[bucket];
150 if (node->
next !=
nullptr) {
151 BOOST_ASSERT(node->
next->
prev ==
nullptr);
155 m_buckets[bucket] = node;
159 Hashtable::detach(
size_t bucket,
Node* node)
161 if (node->
prev !=
nullptr) {
162 BOOST_ASSERT(node->
prev->
next == node);
166 BOOST_ASSERT(m_buckets[bucket] == node);
167 m_buckets[bucket] = node->
next;
170 if (node->
next !=
nullptr) {
171 BOOST_ASSERT(node->
next->
prev == node);
178 std::pair<const Node*, bool>
179 Hashtable::findOrInsert(
const Name&
name,
size_t prefixLen,
HashValue h,
bool allowInsert)
183 for (
const Node* node = m_buckets[bucket]; node !=
nullptr; node = node->
next) {
186 return {node,
false};
192 return {
nullptr,
false};
196 this->attach(bucket, node);
200 if (m_size > m_expandThreshold) {
201 this->resize(static_cast<size_t>(m_options.
expandFactor * this->getNBuckets()));
211 return const_cast<Hashtable*
>(
this)->findOrInsert(name, prefixLen, h,
false).first;
217 BOOST_ASSERT(hashes.at(prefixLen) ==
computeHash(name, prefixLen));
218 return const_cast<Hashtable*
>(
this)->findOrInsert(name, prefixLen, hashes[prefixLen],
false).first;
221 std::pair<const Node*, bool>
224 BOOST_ASSERT(hashes.at(prefixLen) ==
computeHash(name, prefixLen));
225 return this->findOrInsert(name, prefixLen, hashes[prefixLen],
true);
231 BOOST_ASSERT(node !=
nullptr);
237 this->detach(bucket, node);
241 if (m_size < m_shrinkThreshold) {
242 size_t newNBuckets = std::max(m_options.
minSize,
243 static_cast<size_t>(m_options.
shrinkFactor * this->getNBuckets()));
244 this->resize(newNBuckets);
249 Hashtable::computeThresholds()
253 NFD_LOG_TRACE(
"thresholds expand=" << m_expandThreshold <<
" shrink=" << m_shrinkThreshold);
257 Hashtable::resize(
size_t newNBuckets)
264 std::vector<Node*> oldBuckets;
265 oldBuckets.swap(m_buckets);
266 m_buckets.resize(newNBuckets);
268 for (
Node* head : oldBuckets) {
271 this->attach(bucket, node);
275 this->computeThresholds();
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix (PartialName) of the name, containing first nComponents components.
HashSequence computeHashes(const Name &name)
computes hash values for each prefix of name
float expandLoadFactor
if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
size_t getNBuckets() const
void foreachNode(N *head, const F &func)
invoke a function for each node in a doubly linked list
HashtableOptions(size_t size=16)
constructor
std::pair< const Node *, bool > insert(const Name &name, size_t prefixLen, const HashSequence &hashes)
find or insert node for name.getPrefix(prefixLen)
Node(HashValue h, const Name &name)
uint32 CityHash32(const char *s, size_t len)
float expandFactor
when hashtable is expanded, its new size is nBuckets*expandFactor
const uint8_t * wire() const
size_t computeBucketIndex(HashValue h) const
std::conditional<(sizeof(HashValue) > 4), Hash64, Hash32 >::type HashFunc
a type with compute static method to compute hash value from a raw buffer
#define NFD_LOG_DEBUG(expression)
provides options for Hashtable
const Node * find(const Name &name, size_t prefixLen) const
find node for name.getPrefix(prefixLen)
float shrinkLoadFactor
if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
a hashtable for fast exact name lookup
~Hashtable()
deallocates all nodes
uint64 CityHash64(const char *s, size_t len)
#define NFD_LOG_TRACE(expression)
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Copyright (c) 2011-2015 Regents of the University of California.
Name abstraction to represent an absolute name.
size_t HashValue
a single hash value
std::vector< HashValue > HashSequence
a sequence of hash values
Node * getNode(const Entry &entry)
size_t size() const
Get the number of components.
HashValue computeHash(const Name &name, ssize_t prefixLen)
computes a single hash value
size_t initialSize
initial number of buckets
Component holds a read-only name component value.
Hashtable(const Options &options)
float shrinkFactor
when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
size_t minSize
minimal number of buckets
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
static HashValue compute(const void *buffer, size_t length)
static HashValue compute(const void *buffer, size_t length)
Entry * getParent() const
#define NFD_LOG_INIT(name)
const Name & getName() const
an entry in the name tree
void erase(Node *node)
delete node