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

Unified Diff: base/containers/flat_set.h

Issue 2723853002: Implementing erase/erase_if functions from library fundamentals ts: (Closed)
Patch Set: Compilation on linux. Created 3 years, 9 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') | no next file with comments »
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..aa7e52eddbdfca8758ae32a22479ccccd046f7cd 100644
--- a/base/containers/flat_set.h
+++ b/base/containers/flat_set.h
@@ -32,13 +32,13 @@ namespace base {
// collecting all data in a vector and then building flat_set out of it.
// 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.
+// * Sets where mutating happens in big bulks: to erase multiple elements, use
+// base::EraseIf() rather than repeated single-element removal. 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.
// * Copying and iterating.
// * Set operations (union/intersect etc).
//
@@ -216,8 +216,8 @@ class flat_set {
// erase(position), erase(first, last) can take O(size).
// erase(key) may take O(size) + O(log(size)).
//
- // Prefer the erase(std::remove(), end()) idiom for deleting multiple
- // elements.
+ // Prefer base::EraseIf() or some other variation on erase(remove(), end())
+ // idiom when deleting multiple non-consecutive elements.
iterator erase(const_iterator position);
iterator erase(const_iterator first, const_iterator last);
@@ -657,6 +657,16 @@ 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.
+template <typename Key, typename Compare, typename Predicate>
+void EraseIf(base::flat_set<Key, Compare>& container, Predicate pred) {
+ container.erase(std::remove_if(container.begin(), container.end(), pred),
+ container.end());
+}
+
} // namespace base
#endif // BASE_CONTAINERS_FLAT_SET_H_
« no previous file with comments | « no previous file | base/containers/flat_set_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698