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

Unified Diff: base/stl_util.h

Issue 11415239: Add STLSetDifference to stl_util.h, because std::set_difference is extremely (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: blah Created 8 years 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
Index: base/stl_util.h
diff --git a/base/stl_util.h b/base/stl_util.h
index b3ddfa3b4fe8f85bb9568768d1a2af9fd7c75d19..47c0ce1a7009b22686aa07e65556d3c0dbd552b0 100644
--- a/base/stl_util.h
+++ b/base/stl_util.h
@@ -7,9 +7,12 @@
#ifndef BASE_STL_UTIL_H_
#define BASE_STL_UTIL_H_
+#include <algorithm>
#include <string>
#include <vector>
+#include "base/logging.h"
+
// Clears internal memory of an STL object.
// STL clear()/reserve(0) does not always free internal memory allocated
// This function uses swap/destructor to ensure the internal memory is freed.
@@ -191,4 +194,28 @@ bool ContainsKey(const Collection& collection, const Key& key) {
return collection.find(key) != collection.end();
}
+namespace base {
+
+// Returns true if the container is sorted.
+template <typename Container>
+bool STLIsSorted(const Container& cont) {
+ return std::adjacent_find(cont.begin(), cont.end(),
+ std::greater<typename Container::value_type>())
+ == cont.end();
+}
+
+// Returns a new ResultType containing the difference of two sorted containers.
+template <typename ResultType, typename Arg1, typename Arg2>
+ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
+ DCHECK(STLIsSorted(a1));
+ DCHECK(STLIsSorted(a2));
+ ResultType difference;
+ std::set_difference(a1.begin(), a1.end(),
+ a2.begin(), a2.end(),
+ std::inserter(difference, difference.end()));
+ return difference;
+}
+
+} // namespace base
+
#endif // BASE_STL_UTIL_H_
« no previous file with comments | « base/base.gyp ('k') | base/stl_util_unittest.cc » ('j') | base/stl_util_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698