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 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 // As of 2006-04, there is no standard-blessed way of getting a | 47 // As of 2006-04, there is no standard-blessed way of getting a |
48 // mutable reference to a string's internal buffer. However, issue 530 | 48 // mutable reference to a string's internal buffer. However, issue 530 |
49 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) | 49 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) |
50 // proposes this as the method. According to Matt Austern, this should | 50 // proposes this as the method. According to Matt Austern, this should |
51 // already work on all current implementations. | 51 // already work on all current implementations. |
52 inline char* string_as_array(std::string* str) { | 52 inline char* string_as_array(std::string* str) { |
53 // DO NOT USE const_cast<char*>(str->data()) | 53 // DO NOT USE const_cast<char*>(str->data()) |
54 return str->empty() ? NULL : &*str->begin(); | 54 return str->empty() ? NULL : &*str->begin(); |
55 } | 55 } |
56 | 56 |
57 // The following functions are useful for cleaning up STL containers whose | 57 // The following function is useful for cleaning up STL containers whose |
58 // elements point to allocated memory. | 58 // elements point to allocated memory. |
59 | 59 |
60 // STLDeleteElements() deletes all the elements in an STL container and clears | |
61 // the container. This function is suitable for use with a vector, set, | |
62 // hash_set, or any other STL container which defines sensible begin(), end(), | |
63 // and clear() methods. | |
64 // | |
65 // If container is NULL, this function is a no-op. | |
66 template <class T> | |
67 void STLDeleteElements(T* container) { | |
68 if (!container) | |
69 return; | |
70 | |
71 for (auto it = container->begin(); it != container->end();) { | |
72 auto temp = it; | |
73 ++it; | |
74 delete *temp; | |
75 } | |
76 | |
77 container->clear(); | |
78 } | |
79 | |
80 // Given an STL container consisting of (key, value) pairs, STLDeleteValues | 60 // Given an STL container consisting of (key, value) pairs, STLDeleteValues |
81 // deletes all the "value" components and clears the container. Does nothing | 61 // deletes all the "value" components and clears the container. Does nothing |
82 // in the case it's given a NULL pointer. | 62 // in the case it's given a NULL pointer. |
83 template <class T> | 63 template <class T> |
84 void STLDeleteValues(T* container) { | 64 void STLDeleteValues(T* container) { |
85 if (!container) | 65 if (!container) |
86 return; | 66 return; |
87 | 67 |
88 for (auto it = container->begin(); it != container->end();) { | 68 for (auto it = container->begin(); it != container->end();) { |
89 auto temp = it; | 69 auto temp = it; |
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
162 bool STLIncludes(const Arg1& a1, const Arg2& a2) { | 142 bool STLIncludes(const Arg1& a1, const Arg2& a2) { |
163 DCHECK(STLIsSorted(a1)); | 143 DCHECK(STLIsSorted(a1)); |
164 DCHECK(STLIsSorted(a2)); | 144 DCHECK(STLIsSorted(a2)); |
165 return std::includes(a1.begin(), a1.end(), | 145 return std::includes(a1.begin(), a1.end(), |
166 a2.begin(), a2.end()); | 146 a2.begin(), a2.end()); |
167 } | 147 } |
168 | 148 |
169 } // namespace base | 149 } // namespace base |
170 | 150 |
171 #endif // BASE_STL_UTIL_H_ | 151 #endif // BASE_STL_UTIL_H_ |
OLD | NEW |