Avi Drissman | e4622aa | 2022-09-08 20:36:06 | [diff] [blame] | 1 | // Copyright 2011 The Chromium Authors |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | // Derived from google3/util/gtl/stl_util.h |
| 6 | |
| 7 | #ifndef BASE_STL_UTIL_H_ |
| 8 | #define BASE_STL_UTIL_H_ |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 9 | |
[email protected] | 1dea757 | 2012-12-05 21:40:27 | [diff] [blame] | 10 | #include <algorithm> |
[email protected] | c014f2b3 | 2013-09-03 23:29:12 | [diff] [blame] | 11 | #include <iterator> |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 12 | |
Hans Wennborg | 7b53371 | 2020-06-22 20:52:27 | [diff] [blame] | 13 | #include "base/check.h" |
[email protected] | 1dea757 | 2012-12-05 21:40:27 | [diff] [blame] | 14 | |
skyostil | 68be715 | 2016-08-09 22:18:00 | [diff] [blame] | 15 | namespace base { |
| 16 | |
Dan Sanders | 97c1374 | 2018-05-17 23:12:32 | [diff] [blame] | 17 | // Returns a const reference to the underlying container of a container adapter. |
| 18 | // Works for std::priority_queue, std::queue, and std::stack. |
| 19 | template <class A> |
| 20 | const typename A::container_type& GetUnderlyingContainer(const A& adapter) { |
| 21 | struct ExposedAdapter : A { |
| 22 | using A::c; |
| 23 | }; |
| 24 | return adapter.*&ExposedAdapter::c; |
| 25 | } |
| 26 | |
[email protected] | 6ee951a | 2012-06-26 17:24:05 | [diff] [blame] | 27 | // Clears internal memory of an STL object. |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 28 | // STL clear()/reserve(0) does not always free internal memory allocated |
| 29 | // This function uses swap/destructor to ensure the internal memory is freed. |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 30 | template <class T> |
[email protected] | 6ee951a | 2012-06-26 17:24:05 | [diff] [blame] | 31 | void STLClearObject(T* obj) { |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 32 | T tmp; |
| 33 | tmp.swap(*obj); |
| 34 | // Sometimes "T tmp" allocates objects with memory (arena implementation?). |
| 35 | // Hence using additional reserve(0) even if it doesn't always work. |
| 36 | obj->reserve(0); |
| 37 | } |
| 38 | |
[email protected] | 1dea757 | 2012-12-05 21:40:27 | [diff] [blame] | 39 | // Returns a new ResultType containing the difference of two sorted containers. |
| 40 | template <typename ResultType, typename Arg1, typename Arg2> |
| 41 | ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) { |
Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 42 | DCHECK(std::ranges::is_sorted(a1)); |
| 43 | DCHECK(std::ranges::is_sorted(a2)); |
[email protected] | 1dea757 | 2012-12-05 21:40:27 | [diff] [blame] | 44 | ResultType difference; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 45 | std::set_difference(a1.begin(), a1.end(), a2.begin(), a2.end(), |
[email protected] | 1dea757 | 2012-12-05 21:40:27 | [diff] [blame] | 46 | std::inserter(difference, difference.end())); |
| 47 | return difference; |
| 48 | } |
| 49 | |
[email protected] | 9a53ade | 2014-01-29 17:13:27 | [diff] [blame] | 50 | // Returns a new ResultType containing the union of two sorted containers. |
| 51 | template <typename ResultType, typename Arg1, typename Arg2> |
| 52 | ResultType STLSetUnion(const Arg1& a1, const Arg2& a2) { |
Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 53 | DCHECK(std::ranges::is_sorted(a1)); |
| 54 | DCHECK(std::ranges::is_sorted(a2)); |
[email protected] | 9a53ade | 2014-01-29 17:13:27 | [diff] [blame] | 55 | ResultType result; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 56 | std::set_union(a1.begin(), a1.end(), a2.begin(), a2.end(), |
[email protected] | 9a53ade | 2014-01-29 17:13:27 | [diff] [blame] | 57 | std::inserter(result, result.end())); |
| 58 | return result; |
| 59 | } |
| 60 | |
| 61 | // Returns a new ResultType containing the intersection of two sorted |
| 62 | // containers. |
| 63 | template <typename ResultType, typename Arg1, typename Arg2> |
| 64 | ResultType STLSetIntersection(const Arg1& a1, const Arg2& a2) { |
Peter Kasting | 025a9425 | 2025-01-29 21:28:37 | [diff] [blame] | 65 | DCHECK(std::ranges::is_sorted(a1)); |
| 66 | DCHECK(std::ranges::is_sorted(a2)); |
[email protected] | 9a53ade | 2014-01-29 17:13:27 | [diff] [blame] | 67 | ResultType result; |
Peter Kasting | 134ef9af | 2024-12-28 02:30:09 | [diff] [blame] | 68 | std::set_intersection(a1.begin(), a1.end(), a2.begin(), a2.end(), |
[email protected] | 9a53ade | 2014-01-29 17:13:27 | [diff] [blame] | 69 | std::inserter(result, result.end())); |
| 70 | return result; |
| 71 | } |
| 72 | |
Kevin Bailey | 156ff6d | 2017-10-26 17:36:00 | [diff] [blame] | 73 | // A helper class to be used as the predicate with |EraseIf| to implement |
| 74 | // in-place set intersection. Helps implement the algorithm of going through |
| 75 | // each container an element at a time, erasing elements from the first |
| 76 | // container if they aren't in the second container. Requires each container be |
| 77 | // sorted. Note that the logic below appears inverted since it is returning |
| 78 | // whether an element should be erased. |
| 79 | template <class Collection> |
| 80 | class IsNotIn { |
| 81 | public: |
| 82 | explicit IsNotIn(const Collection& collection) |
Lei Zhang | 56887e2 | 2024-11-21 18:48:06 | [diff] [blame] | 83 | : it_(collection.begin()), end_(collection.end()) {} |
Kevin Bailey | 156ff6d | 2017-10-26 17:36:00 | [diff] [blame] | 84 | |
| 85 | bool operator()(const typename Collection::value_type& x) { |
Lei Zhang | 56887e2 | 2024-11-21 18:48:06 | [diff] [blame] | 86 | while (it_ != end_ && *it_ < x) { |
| 87 | ++it_; |
Kevin Bailey | 156ff6d | 2017-10-26 17:36:00 | [diff] [blame] | 88 | } |
Lei Zhang | 56887e2 | 2024-11-21 18:48:06 | [diff] [blame] | 89 | if (it_ == end_ || *it_ != x) { |
| 90 | return true; |
| 91 | } |
| 92 | ++it_; |
| 93 | return false; |
Kevin Bailey | 156ff6d | 2017-10-26 17:36:00 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | private: |
Lei Zhang | 56887e2 | 2024-11-21 18:48:06 | [diff] [blame] | 97 | typename Collection::const_iterator it_; |
Kevin Bailey | 156ff6d | 2017-10-26 17:36:00 | [diff] [blame] | 98 | const typename Collection::const_iterator end_; |
| 99 | }; |
| 100 | |
[email protected] | 1dea757 | 2012-12-05 21:40:27 | [diff] [blame] | 101 | } // namespace base |
| 102 | |
[email protected] | 7286e3fc | 2011-07-19 22:13:24 | [diff] [blame] | 103 | #endif // BASE_STL_UTIL_H_ |