Add STLSetDifference to stl_util.h, because std::set_difference is extremely
awkward to work with.


Review URL: https://chromiumcodereview.appspot.com/11415239

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@171321 0039d316-1c4b-4281-b951-d872f2087c98
diff --git a/base/stl_util.h b/base/stl_util.h
index b3ddfa3..47c0ce1a 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 @@
   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_