From 281371bb0c9543ca156d82bdce5d7e48ec4ca711 Mon Sep 17 00:00:00 2001 From: q66 Date: Wed, 17 Jun 2015 21:33:49 +0100 Subject: [PATCH] reduce code duplication --- octa/internal/hashtable.h | 15 +++++++++++++++ octa/map.h | 40 ++++++--------------------------------- octa/set.h | 40 ++++++--------------------------------- 3 files changed, 27 insertions(+), 68 deletions(-) diff --git a/octa/internal/hashtable.h b/octa/internal/hashtable.h index fac1f22..bdd1c06 100644 --- a/octa/internal/hashtable.h +++ b/octa/internal/hashtable.h @@ -21,6 +21,21 @@ namespace detail { HashChain *next; T value; }; + + template + static inline octa::Size estimate_hrsize(const R &range, + octa::EnableIf::value, bool> = true + ) { + return range.size(); + } + + template + static inline octa::Size estimate_hrsize(const R &, + octa::EnableIf::value, bool> = true + ) { + /* we have no idea how big the range actually is */ + return 16; + } } template diff --git a/octa/map.h b/octa/map.h index de04a63..ff41504 100644 --- a/octa/map.h +++ b/octa/map.h @@ -51,21 +51,6 @@ private: >; Base p_table; - template - octa::Size estimate_rsize(const R &range, - octa::EnableIf::value, bool> = true - ) { - return range.size(); - } - - template - octa::Size estimate_rsize(const R &, - octa::EnableIf::value, bool> = true - ) { - /* we have no idea how big the range actually is */ - return 16; - } - public: using Key = K; @@ -110,7 +95,8 @@ public: octa::IsConvertible, Value>::value, bool > = true - ): p_table(size ? size : estimate_rsize(range), hf, eqf, alloc) { + ): p_table(size ? size : octa::detail::estimate_hrsize(range), + hf, eqf, alloc) { for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); @@ -152,7 +138,7 @@ public: Map & > operator=(R range) { clear(); - p_table.reserve_at_least(estimate_rsize(range)); + p_table.reserve_at_least(octa::detail::estimate_hrsize(range)); for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); @@ -260,21 +246,6 @@ private: >; Base p_table; - template - octa::Size estimate_rsize(const R &range, - octa::EnableIf::value, bool> = true - ) { - return range.size(); - } - - template - octa::Size estimate_rsize(const R &, - octa::EnableIf::value, bool> = true - ) { - /* we have no idea how big the range actually is */ - return 16; - } - public: using Key = K; @@ -322,7 +293,8 @@ public: octa::IsConvertible, Value>::value, bool > = true - ): p_table(size ? size : estimate_rsize(range), hf, eqf, alloc) { + ): p_table(size ? size : octa::detail::estimate_hrsize(range), + hf, eqf, alloc) { for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); @@ -364,7 +336,7 @@ public: Multimap & > operator=(R range) { clear(); - p_table.reserve_at_least(estimate_rsize(range)); + p_table.reserve_at_least(octa::detail::estimate_hrsize(range)); for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); diff --git a/octa/set.h b/octa/set.h index 1fa9abf..ce153ac 100644 --- a/octa/set.h +++ b/octa/set.h @@ -42,21 +42,6 @@ private: >; Base p_table; - template - octa::Size estimate_rsize(const R &range, - octa::EnableIf::value, bool> = true - ) { - return range.size(); - } - - template - octa::Size estimate_rsize(const R &, - octa::EnableIf::value, bool> = true - ) { - /* we have no idea how big the range actually is */ - return 16; - } - public: using Key = T; @@ -100,7 +85,8 @@ public: octa::IsConvertible, Value>::value, bool > = true - ): p_table(size ? size : estimate_rsize(range), hf, eqf, alloc) { + ): p_table(size ? size : octa::detail::estimate_hrsize(range), + hf, eqf, alloc) { for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); @@ -142,7 +128,7 @@ public: Set & > operator=(R range) { clear(); - p_table.reserve_at_least(estimate_rsize(range)); + p_table.reserve_at_least(octa::detail::estimate_hrsize(range)); for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); @@ -227,21 +213,6 @@ private: >; Base p_table; - template - octa::Size estimate_rsize(const R &range, - octa::EnableIf::value, bool> = true - ) { - return range.size(); - } - - template - octa::Size estimate_rsize(const R &, - octa::EnableIf::value, bool> = true - ) { - /* we have no idea how big the range actually is */ - return 16; - } - public: using Key = T; @@ -288,7 +259,8 @@ public: octa::IsConvertible, Value>::value, bool > = true - ): p_table(size ? size : estimate_rsize(range), hf, eqf, alloc) { + ): p_table(size ? size : octa::detail::estimate_hrsize(range), + hf, eqf, alloc) { for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up(); @@ -330,7 +302,7 @@ public: Multiset & > operator=(R range) { clear(); - p_table.reserve_at_least(estimate_rsize(range)); + p_table.reserve_at_least(octa::detail::estimate_hrsize(range)); for (; !range.empty(); range.pop_front()) emplace(range.front()); p_table.rehash_up();