Commit de0ddc0e authored by Kenton Varda's avatar Kenton Varda

Add findEntry() methods to HashMap and TreeMap.

parent cffc710e
...@@ -87,6 +87,14 @@ public: ...@@ -87,6 +87,14 @@ public:
// Like find() but if the key isn't present then call createEntry() to create the corresponding // Like find() but if the key isn't present then call createEntry() to create the corresponding
// entry and insert it. createEntry() must return type `Entry`. // entry and insert it. createEntry() must return type `Entry`.
template <typename KeyLike>
kj::Maybe<Entry&> findEntry(KeyLike&& key);
template <typename KeyLike>
kj::Maybe<const Entry&> findEntry(KeyLike&& key) const;
template <typename KeyLike, typename Func>
Entry& findOrCreateEntry(KeyLike&& key, Func&& createEntry);
// Sometimes you need to see the whole matching Entry, not just the Value.
template <typename KeyLike> template <typename KeyLike>
bool erase(KeyLike&& key); bool erase(KeyLike&& key);
// Erase the entry with the matching key. // Erase the entry with the matching key.
...@@ -176,6 +184,14 @@ public: ...@@ -176,6 +184,14 @@ public:
// Like find() but if the key isn't present then call createEntry() to create the corresponding // Like find() but if the key isn't present then call createEntry() to create the corresponding
// entry and insert it. createEntry() must return type `Entry`. // entry and insert it. createEntry() must return type `Entry`.
template <typename KeyLike>
kj::Maybe<Entry&> findEntry(KeyLike&& key);
template <typename KeyLike>
kj::Maybe<const Entry&> findEntry(KeyLike&& key) const;
template <typename KeyLike, typename Func>
Entry& findOrCreateEntry(KeyLike&& key, Func&& createEntry);
// Sometimes you need to see the whole matching Entry, not just the Value.
template <typename K1, typename K2> template <typename K1, typename K2>
auto range(K1&& k1, K2&& k2); auto range(K1&& k1, K2&& k2);
template <typename K1, typename K2> template <typename K1, typename K2>
...@@ -353,6 +369,25 @@ Value& HashMap<Key, Value>::findOrCreate(KeyLike&& key, Func&& createEntry) { ...@@ -353,6 +369,25 @@ Value& HashMap<Key, Value>::findOrCreate(KeyLike&& key, Func&& createEntry) {
return table.findOrCreate(key, kj::fwd<Func>(createEntry)).value; return table.findOrCreate(key, kj::fwd<Func>(createEntry)).value;
} }
template <typename Key, typename Value>
template <typename KeyLike>
kj::Maybe<typename HashMap<Key, Value>::Entry&>
HashMap<Key, Value>::findEntry(KeyLike&& key) {
return table.find(kj::fwd<KeyLike>(key));
}
template <typename Key, typename Value>
template <typename KeyLike>
kj::Maybe<const typename HashMap<Key, Value>::Entry&>
HashMap<Key, Value>::findEntry(KeyLike&& key) const {
return table.find(kj::fwd<KeyLike>(key));
}
template <typename Key, typename Value>
template <typename KeyLike, typename Func>
typename HashMap<Key, Value>::Entry&
HashMap<Key, Value>::findOrCreateEntry(KeyLike&& key, Func&& createEntry) {
return table.findOrCreate(kj::fwd<KeyLike>(key), kj::fwd<Func>(createEntry));
}
template <typename Key, typename Value> template <typename Key, typename Value>
template <typename KeyLike> template <typename KeyLike>
bool HashMap<Key, Value>::erase(KeyLike&& key) { bool HashMap<Key, Value>::erase(KeyLike&& key) {
...@@ -445,6 +480,25 @@ Value& TreeMap<Key, Value>::findOrCreate(KeyLike&& key, Func&& createEntry) { ...@@ -445,6 +480,25 @@ Value& TreeMap<Key, Value>::findOrCreate(KeyLike&& key, Func&& createEntry) {
return table.findOrCreate(key, kj::fwd<Func>(createEntry)).value; return table.findOrCreate(key, kj::fwd<Func>(createEntry)).value;
} }
template <typename Key, typename Value>
template <typename KeyLike>
kj::Maybe<typename TreeMap<Key, Value>::Entry&>
TreeMap<Key, Value>::findEntry(KeyLike&& key) {
return table.find(kj::fwd<KeyLike>(key));
}
template <typename Key, typename Value>
template <typename KeyLike>
kj::Maybe<const typename TreeMap<Key, Value>::Entry&>
TreeMap<Key, Value>::findEntry(KeyLike&& key) const {
return table.find(kj::fwd<KeyLike>(key));
}
template <typename Key, typename Value>
template <typename KeyLike, typename Func>
typename TreeMap<Key, Value>::Entry&
TreeMap<Key, Value>::findOrCreateEntry(KeyLike&& key, Func&& createEntry) {
return table.findOrCreate(kj::fwd<KeyLike>(key), kj::fwd<Func>(createEntry));
}
template <typename Key, typename Value> template <typename Key, typename Value>
template <typename K1, typename K2> template <typename K1, typename K2>
auto TreeMap<Key, Value>::range(K1&& k1, K2&& k2) { auto TreeMap<Key, Value>::range(K1&& k1, K2&& k2) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment