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

View File

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