diff options
Diffstat (limited to 'chromium/base/stl_util.h')
-rw-r--r-- | chromium/base/stl_util.h | 43 |
1 files changed, 40 insertions, 3 deletions
diff --git a/chromium/base/stl_util.h b/chromium/base/stl_util.h index ccad09d7317..3602df0377a 100644 --- a/chromium/base/stl_util.h +++ b/chromium/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_ |