Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 41 additions & 15 deletions kernel/hashlib.h
Original file line number Diff line number Diff line change
Expand Up @@ -451,16 +451,21 @@ class dict {
return 1;
}

int do_lookup(const K &key, Hasher::hash_t &hash) const
int do_lookup(const K &key, Hasher::hash_t &hash)
{
if (hashtable.empty())
return -1;

if (entries.size() * hashtable_size_trigger > hashtable.size()) {
((dict*)this)->do_rehash();
do_rehash();
hash = do_hash(key);
}

return do_lookup_internal(key, hash);
}

int do_lookup_internal(const K &key, Hasher::hash_t hash) const
{
int index = hashtable[hash];

while (index >= 0 && !ops.cmp(entries[index].udata.first, key)) {
Expand All @@ -471,6 +476,14 @@ class dict {
return index;
}

int do_lookup_no_rehash(const K &key, Hasher::hash_t hash) const
{
if (hashtable.empty())
return -1;

return do_lookup_internal(key, hash);
}

int do_insert(const K &key, Hasher::hash_t &hash)
{
if (hashtable.empty()) {
Expand Down Expand Up @@ -694,14 +707,14 @@ class dict {
int count(const K &key) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
return i < 0 ? 0 : 1;
}

int count(const K &key, const_iterator it) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
return i < 0 || i > it.index ? 0 : 1;
}

Expand All @@ -717,7 +730,7 @@ class dict {
const_iterator find(const K &key) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
if (i < 0)
return end();
return const_iterator(this, i);
Expand All @@ -735,7 +748,7 @@ class dict {
const T& at(const K &key) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
if (i < 0)
throw std::out_of_range("dict::at()");
return entries[i].udata.second;
Expand All @@ -744,7 +757,7 @@ class dict {
const T& at(const K &key, const T &defval) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
if (i < 0)
return defval;
return entries[i].udata.second;
Expand Down Expand Up @@ -906,16 +919,21 @@ class pool
return 1;
}

int do_lookup(const K &key, Hasher::hash_t &hash) const
int do_lookup(const K &key, Hasher::hash_t &hash)
{
if (hashtable.empty())
return -1;

if (entries.size() * hashtable_size_trigger > hashtable.size()) {
((pool*)this)->do_rehash();
do_rehash();
hash = do_hash(key);
}

return do_lookup_internal(key, hash);
}

int do_lookup_internal(const K &key, Hasher::hash_t hash) const
{
int index = hashtable[hash];

while (index >= 0 && !ops.cmp(entries[index].udata, key)) {
Expand All @@ -926,6 +944,14 @@ class pool
return index;
}

int do_lookup_no_rehash(const K &key, Hasher::hash_t hash) const
{
if (hashtable.empty())
return -1;

return do_lookup_internal(key, hash);
}

int do_insert(const K &value, Hasher::hash_t &hash)
{
if (hashtable.empty()) {
Expand Down Expand Up @@ -1087,14 +1113,14 @@ class pool
int count(const K &key) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
return i < 0 ? 0 : 1;
}

int count(const K &key, const_iterator it) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
return i < 0 || i > it.index ? 0 : 1;
}

Expand All @@ -1110,7 +1136,7 @@ class pool
const_iterator find(const K &key) const
{
Hasher::hash_t hash = do_hash(key);
int i = do_lookup(key, hash);
int i = do_lookup_no_rehash(key, hash);
if (i < 0)
return end();
return const_iterator(this, i);
Expand Down Expand Up @@ -1222,7 +1248,7 @@ class idict
int at(const K &key) const
{
Hasher::hash_t hash = database.do_hash(key);
int i = database.do_lookup(key, hash);
int i = database.do_lookup_no_rehash(key, hash);
if (i < 0)
throw std::out_of_range("idict::at()");
return i + offset;
Expand All @@ -1231,7 +1257,7 @@ class idict
int at(const K &key, int defval) const
{
Hasher::hash_t hash = database.do_hash(key);
int i = database.do_lookup(key, hash);
int i = database.do_lookup_no_rehash(key, hash);
if (i < 0)
return defval;
return i + offset;
Expand All @@ -1240,7 +1266,7 @@ class idict
int count(const K &key) const
{
Hasher::hash_t hash = database.do_hash(key);
int i = database.do_lookup(key, hash);
int i = database.do_lookup_no_rehash(key, hash);
return i < 0 ? 0 : 1;
}

Expand Down
Loading