diff options
author | Henning Baldersheim <balder@oath.com> | 2018-10-27 11:19:13 +0200 |
---|---|---|
committer | Henning Baldersheim <balder@oath.com> | 2018-11-01 15:14:38 +0100 |
commit | d31a1b7f2df4ea94821a9d98754f4ae186bbf2d8 (patch) | |
tree | 75515a07b65e9225ac3a43f7830d825e5cbb916d /vespalib | |
parent | c3982d42b9d5ca9aea7600f5bc5fc6cceb78505a (diff) |
Use a templated find() to enable lookup without object creation when objects are comparable.
Diffstat (limited to 'vespalib')
-rw-r--r-- | vespalib/src/tests/stllike/hash_test.cpp | 6 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/hash_fun.h | 14 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/hash_map.h | 19 | ||||
-rw-r--r-- | vespalib/src/vespa/vespalib/stllike/hash_set.h | 8 |
4 files changed, 40 insertions, 7 deletions
diff --git a/vespalib/src/tests/stllike/hash_test.cpp b/vespalib/src/tests/stllike/hash_test.cpp index e7fe729c1ba..4feeaef8349 100644 --- a/vespalib/src/tests/stllike/hash_test.cpp +++ b/vespalib/src/tests/stllike/hash_test.cpp @@ -154,14 +154,14 @@ TEST("test hash set with simple type") TEST("test hash map iterator stability") { - hash_map<int, int> h; + hash_map<uint32_t, uint32_t> h; EXPECT_EQUAL(1ul, h.capacity()); for (size_t i(0); i < 100; i++) { EXPECT_TRUE(h.find(i) == h.end()); h[i] = i; EXPECT_TRUE(h.find(i) != h.end()); - int * p1 = & h.find(i)->second; - int * p2 = & h[i]; + uint32_t * p1 = & h.find(i)->second; + uint32_t * p2 = & h[i]; EXPECT_EQUAL(p1, p2); } EXPECT_EQUAL(128ul, h.capacity()); diff --git a/vespalib/src/vespa/vespalib/stllike/hash_fun.h b/vespalib/src/vespa/vespalib/stllike/hash_fun.h index f708f49081e..de849b8924e 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_fun.h +++ b/vespalib/src/vespa/vespalib/stllike/hash_fun.h @@ -6,6 +6,20 @@ namespace vespalib { +template<typename T, typename F> +struct cast_static { + T operator() (const F & o) const { + return static_cast<T>(o); + } +}; + +template<typename L, typename R> +struct equal_to : public std::binary_function<L, R, bool> +{ + bool operator()(const L & __x, const R & __y) const { return __x == __y; } + bool operator()(const R & __x, const L & __y) const { return __x == __y; } +}; + template<typename K> struct hash { // specializations operate as functor for known key types size_t operator() (const K & v) const { diff --git a/vespalib/src/vespa/vespalib/stllike/hash_map.h b/vespalib/src/vespa/vespalib/stllike/hash_map.h index 6d6498f8e78..929a3740a1d 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_map.h +++ b/vespalib/src/vespa/vespalib/stllike/hash_map.h @@ -50,6 +50,25 @@ public: void erase(const_iterator it) { return erase(it->first); } iterator find(const K & key) { return _ht.find(key); } const_iterator find(const K & key) const { return _ht.find(key); } + + template< typename AltKey, typename AltExtract=std::_Identity<K>, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > + const_iterator find(const AltKey & key) const { + return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key); + } + template< typename AltKey, typename AltExtract=std::_Identity<K>, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > + iterator find(const AltKey & key) { + return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key); + } + template< typename AltKey, typename AltExtract, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > + const_iterator find(const AltKey & key, const AltExtract & altExtract) const { + return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key, altExtract); + } + + template< typename AltKey, typename AltExtract, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > + iterator find(const AltKey & key, const AltExtract & altExtract) { + return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key, altExtract); + } + void clear(); void resize(size_t newSize); void swap(hash_map & rhs); diff --git a/vespalib/src/vespa/vespalib/stllike/hash_set.h b/vespalib/src/vespa/vespalib/stllike/hash_set.h index c4ccc662787..9756a3083df 100644 --- a/vespalib/src/vespa/vespalib/stllike/hash_set.h +++ b/vespalib/src/vespa/vespalib/stllike/hash_set.h @@ -48,18 +48,18 @@ public: template <typename Func> void for_each(Func func) const { _ht.for_each(func); } - template< typename AltKey, typename AltExtract, typename AltHash, typename AltEqual > + template< typename AltKey, typename AltExtract=std::_Identity<K>, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > const_iterator find(const AltKey & key) const { return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key); } - template< typename AltKey, typename AltExtract, typename AltHash, typename AltEqual > + template< typename AltKey, typename AltExtract=std::_Identity<K>, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > iterator find(const AltKey & key) { return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key); } - template< typename AltKey, typename AltExtract, typename AltHash, typename AltEqual > + template< typename AltKey, typename AltExtract, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > const_iterator find(const AltKey & key, const AltExtract & altExtract) const { return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key, altExtract); } - template< typename AltKey, typename AltExtract, typename AltHash, typename AltEqual > + template< typename AltKey, typename AltExtract, typename AltHash=vespalib::hash<AltKey>, typename AltEqual=equal_to<AltKey, K> > iterator find(const AltKey & key, const AltExtract & altExtract) { return _ht.template find<AltKey, AltExtract, AltHash, AltEqual>(key, altExtract); } |