OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 // Derived from google3/util/gtl/stl_util.h | 5 // Derived from google3/util/gtl/stl_util.h |
6 | 6 |
7 #ifndef BASE_STL_UTIL_H_ | 7 #ifndef BASE_STL_UTIL_H_ |
8 #define BASE_STL_UTIL_H_ | 8 #define BASE_STL_UTIL_H_ |
9 | 9 |
10 #include <algorithm> | 10 #include <algorithm> |
(...skipping 29 matching lines...) Expand all Loading... |
40 // stale pointer. | 40 // stale pointer. |
41 template <class ForwardIterator> | 41 template <class ForwardIterator> |
42 void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) { | 42 void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) { |
43 while (begin != end) { | 43 while (begin != end) { |
44 ForwardIterator temp = begin; | 44 ForwardIterator temp = begin; |
45 ++begin; | 45 ++begin; |
46 delete *temp; | 46 delete *temp; |
47 } | 47 } |
48 } | 48 } |
49 | 49 |
50 // For a range within a container of pairs, calls delete. | |
51 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. | |
52 // Deleting the value does not always invalidate the iterator, but it may | |
53 // do so if the key is a pointer into the value object. | |
54 template <class ForwardIterator> | |
55 void STLDeleteContainerPairSecondPointers(ForwardIterator begin, | |
56 ForwardIterator end) { | |
57 while (begin != end) { | |
58 ForwardIterator temp = begin; | |
59 ++begin; | |
60 delete temp->second; | |
61 } | |
62 } | |
63 | |
64 // Counts the number of instances of val in a container. | 50 // Counts the number of instances of val in a container. |
65 template <typename Container, typename T> | 51 template <typename Container, typename T> |
66 typename std::iterator_traits< | 52 typename std::iterator_traits< |
67 typename Container::const_iterator>::difference_type | 53 typename Container::const_iterator>::difference_type |
68 STLCount(const Container& container, const T& val) { | 54 STLCount(const Container& container, const T& val) { |
69 return std::count(container.begin(), container.end(), val); | 55 return std::count(container.begin(), container.end(), val); |
70 } | 56 } |
71 | 57 |
72 // Return a mutable char* pointing to a string's internal buffer, | 58 // Return a mutable char* pointing to a string's internal buffer, |
73 // which may not be null-terminated. Writing through this pointer will | 59 // which may not be null-terminated. Writing through this pointer will |
(...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
189 bool STLIncludes(const Arg1& a1, const Arg2& a2) { | 175 bool STLIncludes(const Arg1& a1, const Arg2& a2) { |
190 DCHECK(STLIsSorted(a1)); | 176 DCHECK(STLIsSorted(a1)); |
191 DCHECK(STLIsSorted(a2)); | 177 DCHECK(STLIsSorted(a2)); |
192 return std::includes(a1.begin(), a1.end(), | 178 return std::includes(a1.begin(), a1.end(), |
193 a2.begin(), a2.end()); | 179 a2.begin(), a2.end()); |
194 } | 180 } |
195 | 181 |
196 } // namespace base | 182 } // namespace base |
197 | 183 |
198 #endif // BASE_STL_UTIL_H_ | 184 #endif // BASE_STL_UTIL_H_ |
OLD | NEW |