summaryrefslogtreecommitdiffstats
path: root/vespalib
diff options
context:
space:
mode:
authorHenning Baldersheim <balder@yahoo-inc.com>2017-12-28 00:04:08 +0100
committerHenning Baldersheim <balder@yahoo-inc.com>2018-01-03 09:08:48 +0100
commitf5e8faf58ead944a57364cbff5dd2411b452570f (patch)
treeb64d47d48a92f6773b0e0d2b646e04df3890f66e /vespalib
parente77379005bccd3d5009b1484038183a05856b4cd (diff)
Add a for_each method for more efficient iteration.
Diffstat (limited to 'vespalib')
-rw-r--r--vespalib/src/vespa/vespalib/stllike/hash_map.h4
-rw-r--r--vespalib/src/vespa/vespalib/stllike/hashtable.h3
-rw-r--r--vespalib/src/vespa/vespalib/stllike/hashtable.hpp15
3 files changed, 22 insertions, 0 deletions
diff --git a/vespalib/src/vespa/vespalib/stllike/hash_map.h b/vespalib/src/vespa/vespalib/stllike/hash_map.h
index 023594d3018..321f92a419f 100644
--- a/vespalib/src/vespa/vespalib/stllike/hash_map.h
+++ b/vespalib/src/vespa/vespalib/stllike/hash_map.h
@@ -38,6 +38,10 @@ public:
insert_result insert(const value_type & value) { return _ht.insert(value); }
template <typename InputIt>
void insert(InputIt first, InputIt last);
+ template <typename Func>
+ void for_each(Func func) const {
+ _ht.for_each(func);
+ }
const V & operator [] (const K & key) const { return _ht.find(key)->second; }
V & operator [] (const K & key) { return _ht.insert(value_type(key, V())).first->second; }
void erase(const K & key);
diff --git a/vespalib/src/vespa/vespalib/stllike/hashtable.h b/vespalib/src/vespa/vespalib/stllike/hashtable.h
index 15949067a60..41781e649f3 100644
--- a/vespalib/src/vespa/vespalib/stllike/hashtable.h
+++ b/vespalib/src/vespa/vespalib/stllike/hashtable.h
@@ -249,6 +249,9 @@ public:
insert_result insert(V && node) {
return insertInternal(std::forward<V>(node));
}
+ template <typename Func>
+ void for_each(Func func) const;
+
void erase(const Key & key);
void reserve(size_t sz) {
if (sz > _nodes.capacity()) {
diff --git a/vespalib/src/vespa/vespalib/stllike/hashtable.hpp b/vespalib/src/vespa/vespalib/stllike/hashtable.hpp
index f499ba35f3f..2fbe83eb226 100644
--- a/vespalib/src/vespa/vespalib/stllike/hashtable.hpp
+++ b/vespalib/src/vespa/vespalib/stllike/hashtable.hpp
@@ -201,6 +201,21 @@ void hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::reclaim(MoveHand
}
template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
+template <typename Func>
+void hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::for_each(Func func) const
+{
+ uint32_t i(0);
+ for (; i < _modulator.getTableSize(); i++) {
+ if (_nodes[i].valid()) {
+ func(_nodes[i].getValue());
+ }
+ }
+ for (; i < _nodes.size(); i++) {
+ func(_nodes[i].getValue());
+ }
+}
+
+template< typename Key, typename Value, typename Hash, typename Equal, typename KeyExtract, typename Modulator >
template <typename MoveHandler>
void
hashtable<Key, Value, Hash, Equal, KeyExtract, Modulator>::erase(MoveHandler & moveHandler, next_t h, const const_iterator & it)