diff options
author | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:20:29 +0200 |
---|---|---|
committer | Daniel Wilhelm <daniel@wili.li> | 2014-04-18 17:20:29 +0200 |
commit | b8f13e45be884dc12884ebe8f3dcd9eecb23a106 (patch) | |
tree | 22a6d8b96815d626061ff3e2d432c13078fca5c4 /zen/stl_tools.h | |
parent | 5.4 (diff) | |
download | FreeFileSync-b8f13e45be884dc12884ebe8f3dcd9eecb23a106.tar.gz FreeFileSync-b8f13e45be884dc12884ebe8f3dcd9eecb23a106.tar.bz2 FreeFileSync-b8f13e45be884dc12884ebe8f3dcd9eecb23a106.zip |
5.5
Diffstat (limited to 'zen/stl_tools.h')
-rw-r--r-- | zen/stl_tools.h | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/zen/stl_tools.h b/zen/stl_tools.h index 78d99832..ace6ebaa 100644 --- a/zen/stl_tools.h +++ b/zen/stl_tools.h @@ -33,6 +33,9 @@ void set_remove_if(S& set, Predicate p); template <class M, class Predicate> void map_remove_if(M& map, Predicate p); +template <class M, class K, class V> +V& map_add_or_update(M& map, const K& key, const V& value); //efficient add or update without "default-constructible" requirement (Effective STL, item 24) + //binary search returning an iterator template <class ForwardIterator, class T, typename Compare> ForwardIterator binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp); @@ -98,6 +101,20 @@ template <class M, class Predicate> inline void map_remove_if(M& map, Predicate p) { set_remove_if(map, p); } +template <class M, class K, class V> inline +V& map_add_or_update(M& map, const K& key, const V& value) //efficient add or update without "default-constructible" requirement (Effective STL, item 24) +{ + auto iter = map.lower_bound(key); + if (iter != map.end() && !(map.key_comp()(key, iter->first))) + { + iter->second = value; + return iter->second; + } + else + return map.insert(iter, typename M::value_type(key, value))->second; +} + + template <class ForwardIterator, class T, typename Compare> inline ForwardIterator binary_search(ForwardIterator first, ForwardIterator last, const T& value, Compare comp) { |