Index: base/stl_util.h |
diff --git a/base/stl_util.h b/base/stl_util.h |
index ccad09d7317b4825045b41a6dbc8c48b14169d67..3602df0377ade5e461c73aad16f672743050b98d 100644 |
--- a/base/stl_util.h |
+++ b/base/stl_util.h |
@@ -201,9 +201,11 @@ 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(); |
+ // Note: Use reverse iterator on container to ensure we only require |
+ // value_type to implement operator<. |
+ return std::adjacent_find(cont.rbegin(), cont.rend(), |
+ std::less<typename Container::value_type>()) |
+ == cont.rend(); |
} |
// Returns a new ResultType containing the difference of two sorted containers. |
@@ -218,6 +220,41 @@ ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) { |
return difference; |
} |
+// Returns a new ResultType containing the union of two sorted containers. |
+template <typename ResultType, typename Arg1, typename Arg2> |
+ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) { |
+ DCHECK(STLIsSorted(a1)); |
+ DCHECK(STLIsSorted(a2)); |
+ ResultType result; |
+ std::set_union(a1.begin(), a1.end(), |
+ a2.begin(), a2.end(), |
+ std::inserter(result, result.end())); |
+ return result; |
+} |
+ |
+// Returns a new ResultType containing the intersection of two sorted |
+// containers. |
+template <typename ResultType, typename Arg1, typename Arg2> |
+ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) { |
+ DCHECK(STLIsSorted(a1)); |
+ DCHECK(STLIsSorted(a2)); |
+ ResultType result; |
+ std::set_intersection(a1.begin(), a1.end(), |
+ a2.begin(), a2.end(), |
+ std::inserter(result, result.end())); |
+ return result; |
+} |
+ |
+// Returns true if the sorted container |a1| contains all elements of the sorted |
+// container |a2|. |
+template <typename Arg1, typename Arg2> |
+bool STLIncludes(const Arg1& a1, const Arg2& a2) { |
+ DCHECK(STLIsSorted(a1)); |
+ DCHECK(STLIsSorted(a2)); |
+ return std::includes(a1.begin(), a1.end(), |
+ a2.begin(), a2.end()); |
+} |
+ |
} // namespace base |
#endif // BASE_STL_UTIL_H_ |