hash fixes/cleanups and make at() return a pointer

master
Daniel Kolesa 2015-07-20 22:04:52 +01:00
parent 56629ff358
commit 8e40fca1ce
2 changed files with 11 additions and 16 deletions

View File

@ -283,13 +283,6 @@ private:
}
}
T *access_base(const K &key, Size &h) const {
if (!p_size) return NULL;
Chain *c = find(key, h);
if (c) return &B::get_data(c->value);
return nullptr;
}
void rehash_ahead(Size n) {
if (!bucket_count())
reserve(n);
@ -307,23 +300,25 @@ protected:
T &access_or_insert(const K &key) {
Size h = 0;
T *v = access_base(key, h);
if (v) return *v;
Chain *c = find(key, h);
if (c) return B::get_data(c->value);
rehash_ahead(1);
return insert(h, key);
}
T &access_or_insert(K &&key) {
Size h = 0;
T *v = access_base(key, h);
if (v) return *v;
Chain *c = find(key, h);
if (c) return B::get_data(c->value);
rehash_ahead(1);
return insert(h, move(key));
}
T &access(const K &key) const {
T *access(const K &key) const {
Size h;
return *access_base(key, h);
Chain *c = find(key, h);
if (c) return &B::get_data(c->value);
return nullptr;
}
template<typename R>
@ -505,7 +500,7 @@ public:
Size bucket_size(Size n) const {
Size ret = 0;
if (ret >= p_size) return ret;
if (n >= p_size) return ret;
Chain **cp = p_data.first();
for (Chain *c = cp[n], *e = cp[n + 1]; c != e; c = c->next)
++ret;

View File

@ -139,11 +139,11 @@ namespace detail {
return *this;
}
T &at(const K &key) {
T *at(const K &key) {
static_assert(!IsMultihash, "at() only allowed on regular maps");
return Base::access(key);
}
const T &at(const K &key) const {
const T *at(const K &key) const {
static_assert(!IsMultihash, "at() only allowed on regular maps");
return Base::access(key);
}