| 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 function is useful for cleaning up STL containers whose | |
| 58 // elements point to allocated memory. | |
| 59 | |
| 60 // Given an STL container consisting of (key, value) pairs, STLDeleteValues | |
| 61 // deletes all the "value" components and clears the container. Does nothing | |
| 62 // in the case it's given a NULL pointer. | |
| 63 template <class T> | |
| 64 void STLDeleteValues(T* container) { | |
| 65 if (!container) | |
| 66 return; | |
| 67 | |
| 68 for (auto it = container->begin(); it != container->end();) { | |
| 69 auto temp = it; | |
| 70 ++it; | |
| 71 delete temp->second; | |
| 72 } | |
| 73 | |
| 74 container->clear(); | |
| 75 } | |
| 76 | |
| 77 // Test to see if a set, map, hash_set or hash_map contains a particular key. | 57 // Test to see if a set, map, hash_set or hash_map contains a particular key. |
| 78 // Returns true if the key is in the collection. | 58 // Returns true if the key is in the collection. |
| 79 template <typename Collection, typename Key> | 59 template <typename Collection, typename Key> |
| 80 bool ContainsKey(const Collection& collection, const Key& key) { | 60 bool ContainsKey(const Collection& collection, const Key& key) { |
| 81 return collection.find(key) != collection.end(); | 61 return collection.find(key) != collection.end(); |
| 82 } | 62 } |
| 83 | 63 |
| 84 // Test to see if a collection like a vector contains a particular value. | 64 // Test to see if a collection like a vector contains a particular value. |
| 85 // Returns true if the value is in the collection. | 65 // Returns true if the value is in the collection. |
| 86 template <typename Collection, typename Value> | 66 template <typename Collection, typename Value> |
| (...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 bool STLIncludes(const Arg1& a1, const Arg2& a2) { | 122 bool STLIncludes(const Arg1& a1, const Arg2& a2) { |
| 143 DCHECK(STLIsSorted(a1)); | 123 DCHECK(STLIsSorted(a1)); |
| 144 DCHECK(STLIsSorted(a2)); | 124 DCHECK(STLIsSorted(a2)); |
| 145 return std::includes(a1.begin(), a1.end(), | 125 return std::includes(a1.begin(), a1.end(), |
| 146 a2.begin(), a2.end()); | 126 a2.begin(), a2.end()); |
| 147 } | 127 } |
| 148 | 128 |
| 149 } // namespace base | 129 } // namespace base |
| 150 | 130 |
| 151 #endif // BASE_STL_UTIL_H_ | 131 #endif // BASE_STL_UTIL_H_ |
| OLD | NEW |