Chromium Code Reviews| 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 } | |
|
Nico
2016/09/22 15:56:11
The usual comment: I"d put this bit in a separate
Avi (use Gerrit)
2016/09/22 19:17:16
Done.
| |
| 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 29 matching lines...) Expand all Loading... | |
| 103 container->clear(); | 89 container->clear(); |
| 104 } | 90 } |
| 105 | 91 |
| 106 // Given an STL container consisting of (key, value) pairs, STLDeleteValues | 92 // Given an STL container consisting of (key, value) pairs, STLDeleteValues |
| 107 // deletes all the "value" components and clears the container. Does nothing | 93 // deletes all the "value" components and clears the container. Does nothing |
| 108 // in the case it's given a NULL pointer. | 94 // in the case it's given a NULL pointer. |
| 109 template <class T> | 95 template <class T> |
| 110 void STLDeleteValues(T* container) { | 96 void STLDeleteValues(T* container) { |
| 111 if (!container) | 97 if (!container) |
| 112 return; | 98 return; |
| 113 STLDeleteContainerPairSecondPointers(container->begin(), container->end()); | 99 |
| 100 auto it = container->begin(); | |
| 101 while (it != container->end()) { | |
| 102 auto temp = it; | |
| 103 ++it; | |
| 104 delete temp->second; | |
| 105 } | |
| 106 | |
| 114 container->clear(); | 107 container->clear(); |
| 115 } | 108 } |
| 116 | 109 |
| 117 // Test to see if a set, map, hash_set or hash_map contains a particular key. | 110 // Test to see if a set, map, hash_set or hash_map contains a particular key. |
| 118 // Returns true if the key is in the collection. | 111 // Returns true if the key is in the collection. |
| 119 template <typename Collection, typename Key> | 112 template <typename Collection, typename Key> |
| 120 bool ContainsKey(const Collection& collection, const Key& key) { | 113 bool ContainsKey(const Collection& collection, const Key& key) { |
| 121 return collection.find(key) != collection.end(); | 114 return collection.find(key) != collection.end(); |
| 122 } | 115 } |
| 123 | 116 |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 182 bool STLIncludes(const Arg1& a1, const Arg2& a2) { | 175 bool STLIncludes(const Arg1& a1, const Arg2& a2) { |
| 183 DCHECK(STLIsSorted(a1)); | 176 DCHECK(STLIsSorted(a1)); |
| 184 DCHECK(STLIsSorted(a2)); | 177 DCHECK(STLIsSorted(a2)); |
| 185 return std::includes(a1.begin(), a1.end(), | 178 return std::includes(a1.begin(), a1.end(), |
| 186 a2.begin(), a2.end()); | 179 a2.begin(), a2.end()); |
| 187 } | 180 } |
| 188 | 181 |
| 189 } // namespace base | 182 } // namespace base |
| 190 | 183 |
| 191 #endif // BASE_STL_UTIL_H_ | 184 #endif // BASE_STL_UTIL_H_ |
| OLD | NEW |