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);
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) {
188 return {node,
false};
194 return {
nullptr,
false};
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 expandLoadFactor
if hashtable has more than nBuckets*expandLoadFactor nodes, it will be expanded
size_t HashValue
a single hash value
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)
HashSequence computeHashes(const Name &name, size_t prefixLen)
computes hash values for each prefix of name.getPrefix(prefixLen)
float expandFactor
when hashtable is expanded, its new size is nBuckets*expandFactor
const Name & getName() const
HashValue computeHash(const Name &name, size_t prefixLen)
computes hash value of name.getPrefix(prefixLen)
size_t wireEncode(EncodingImpl< TAG > &encoder) const
Fast encoding or block size estimation.
#define NFD_LOG_DEBUG(expression)
provides options for Hashtable
std::vector< HashValue > HashSequence
a sequence of hash values
float shrinkLoadFactor
if hashtable has less than nBuckets*shrinkLoadFactor nodes, it will be shrunk
a hashtable for fast exact name lookup
Entry * getParent() const
~Hashtable()
deallocates all nodes
size_t size() const
Get size of encoded wire, including Type-Length-Value.
uint64 CityHash64(const char *s, size_t len)
#define NFD_LOG_TRACE(expression)
Copyright (c) 2011-2015 Regents of the University of California.
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
size_t size() const
Get number of components.
Represents an absolute name.
void push_back(const T &component)
Append a component.
Node * getNode(const Entry &entry)
size_t initialSize
initial number of buckets
Component holds a read-only name component value.
int compare(const Name &other) const
Compare this to the other Name using NDN canonical ordering.
Hashtable(const Options &options)
float shrinkFactor
when hashtable is shrunk, its new size is max(nBuckets*shrinkFactor, minSize)
const uint8_t * wire() const
Get pointer to encoded wire.
size_t minSize
minimal number of buckets
PartialName getPrefix(ssize_t nComponents) const
Extract a prefix of the name.
size_t getNBuckets() const
static HashValue compute(const void *buffer, size_t length)
static HashValue compute(const void *buffer, size_t length)
#define NFD_LOG_INIT(name)
an entry in the name tree
void erase(Node *node)
delete node
const Node * find(const Name &name, size_t prefixLen) const
find node for name.getPrefix(prefixLen)