From 2b05cb9297452fa15a26323f3f4346bf10c3c2d6 Mon Sep 17 00:00:00 2001 From: q66 Date: Tue, 16 Jun 2015 22:51:57 +0100 Subject: [PATCH] Map::find, Set::find --- octa/internal/hashtable.h | 39 +++++++++++++++++++++++++++++++++++++++ octa/map.h | 3 +++ octa/set.h | 3 +++ 3 files changed, 45 insertions(+) diff --git a/octa/internal/hashtable.h b/octa/internal/hashtable.h index 25aaff4..d534736 100644 --- a/octa/internal/hashtable.h +++ b/octa/internal/hashtable.h @@ -387,6 +387,35 @@ namespace detail { return octa::move(ret); } + template + bool find(const U &key, octa::Size &h, Chain *&oc) const { + if (!p_size) return false; + h = get_hash()(key) & (p_size - 1); + for (Chain *c = p_data.first()[h]; c; c = c->next) { + if (get_eq()(key, B::get_key(c->value))) { + oc = c; + return true; + } + } + return false; + } + + template + Range find(const U &key) { + octa::Size h; + Chain *c; + if (find(key, h, c)) return each_from(c, h); + return Range(); + } + + template + ConstRange find(const U &key) const { + octa::Size h; + Chain *c; + if (find(key, h, c)) return each_from(c, h); + return ConstRange(); + } + float load_factor() const { return float(p_len) / p_size; } float max_load_factor() const { return p_maxlf; } void max_load_factor(float lf) { p_maxlf = lf; } @@ -486,6 +515,16 @@ namespace detail { return ConstLocalRange((Chain *)p_data.first()[n]); } + Range each_from(Chain *c, octa::Size h) { + return Range(p_data.first() + h + 1, + p_data.first() + bucket_count(), c); + } + ConstRange each_from(Chain *c, octa::Size h) const { + using RChain = octa::detail::HashChain; + return Range((RChain *)(p_data.first() + h + 1), + (RChain *)(p_data.first() + bucket_count()), c); + } + void swap(Hashtable &ht) { octa::swap(p_size, ht.p_size); octa::swap(p_len, ht.p_len); diff --git a/octa/map.h b/octa/map.h index 5e8f877..f49c351 100644 --- a/octa/map.h +++ b/octa/map.h @@ -223,6 +223,9 @@ public: return 0; } + Range find(const K &key) { return p_table.find(key); } + ConstRange find(const K &key) const { return p_table.find(key); } + float load_factor() const { return p_table.load_factor(); } float max_load_factor() const { return p_table.max_load_factor(); } void max_load_factor(float lf) { p_table.max_load_factor(lf); } diff --git a/octa/set.h b/octa/set.h index 140d6d7..52dacfe 100644 --- a/octa/set.h +++ b/octa/set.h @@ -191,6 +191,9 @@ public: return 0; } + Range find(const Key &key) { return p_table.find(key); } + ConstRange find(const Key &key) const { return p_table.find(key); } + float load_factor() const { return p_table.load_factor(); } float max_load_factor() const { return p_table.max_load_factor(); } void max_load_factor(float lf) { p_table.max_load_factor(lf); }