Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(784)

Unified Diff: base/containers/flat_set.h

Issue 2723853002: Implementing erase/erase_if functions from library fundamentals ts: (Closed)
Patch Set: Review, round 2. Created 3 years, 10 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | base/containers/flat_set_unittest.cc » ('j') | base/containers/flat_set_unittest.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: base/containers/flat_set.h
diff --git a/base/containers/flat_set.h b/base/containers/flat_set.h
index ae444c2c8e21c38ebec295ee023a365b84b3e057..5e59c39924277eb5f1d796b156394a7a9a3ab0cf 100644
--- a/base/containers/flat_set.h
+++ b/base/containers/flat_set.h
@@ -33,12 +33,13 @@ namespace base {
// TODO(dyaroshev): improve the interface to better support this pattern
// (crbug.com/682254).
// * Sets where mutating happens in big bulks: use erase(std::remove()) idiom
-// for erasing many elements. Insertion is harder - consider set operations
-// or building a new vector. Set operations can be slow if one of the sets
-// is considerably bigger. Also be aware that beating performance of
-// sort + unique (implementation of flat_set's constructor) is hard, clever
-// merge of many sets might not win. Generally avoid inserting into flat set
-// without benchmarks.
+// for erasing many elements. If possible - use supplied for flat_set
+// overload of base::EraseIf. Insertion is harder - consider set operations
dyaroshev 2017/03/03 23:16:54 I'm not sure how to formulate this thought.
Peter Kasting 2017/03/03 23:27:30 "Sets where mutating happens in bulk operations: t
dyaroshev 2017/03/04 00:32:49 Done.
+// or building a new vector. Set operations can be slow if one of the sets is
+// considerably bigger. Also be aware that beating performance of sort +
+// unique (implementation of flat_set's constructor) is hard, clever merge of
+// many sets might not win. Generally avoid inserting into flat set without
+// benchmarks.
// * Copying and iterating.
// * Set operations (union/intersect etc).
//
@@ -217,7 +218,8 @@ class flat_set {
// erase(key) may take O(size) + O(log(size)).
//
// Prefer the erase(std::remove(), end()) idiom for deleting multiple
danakj 2017/03/03 23:51:22 Like peter said above just say to use base::EraseI
dyaroshev 2017/03/04 00:32:49 Slightly extended it. Is it ok?
- // elements.
+ // elements. If possible use supplied base::EraseIf overload.
+ //
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
@@ -657,6 +659,18 @@ void flat_set<Key, Compare>::swap(flat_set& other) {
std::swap(impl_, other.impl_);
}
+// ----------------------------------------------------------------------------
+// Free functions.
+
+// Erases all elements that match predicate. It has O(size) complexity.
+
danakj 2017/03/03 23:51:22 no whitespace here
dyaroshev 2017/03/04 00:32:49 Done.
+template <typename Key, typename Compare, typename Predicate>
+void EraseIf(base::flat_set<Key, Compare>& container, Predicate pred) {
+ container.erase(
+ std::remove_if(std::begin(container), std::end(container), pred),
danakj 2017/03/03 23:51:22 it's a bit less verbose to use container.begin() a
dyaroshev 2017/03/04 00:32:49 Done.
+ std::end(container));
+}
+
} // namespace base
#endif // BASE_CONTAINERS_FLAT_SET_H_
« no previous file with comments | « no previous file | base/containers/flat_set_unittest.cc » ('j') | base/containers/flat_set_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698