| 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> |
| 11 #include <functional> | 11 #include <functional> |
| 12 #include <iterator> | 12 #include <iterator> |
| 13 #include <string> | 13 #include <string> |
| 14 #include <vector> | 14 #include <vector> |
| 15 | 15 |
| 16 #include "base/logging.h" | 16 #include "base/logging.h" |
| 17 | 17 |
| 18 namespace base { |
| 19 |
| 18 // Clears internal memory of an STL object. | 20 // Clears internal memory of an STL object. |
| 19 // STL clear()/reserve(0) does not always free internal memory allocated | 21 // STL clear()/reserve(0) does not always free internal memory allocated |
| 20 // This function uses swap/destructor to ensure the internal memory is freed. | 22 // This function uses swap/destructor to ensure the internal memory is freed. |
| 21 template<class T> | 23 template<class T> |
| 22 void STLClearObject(T* obj) { | 24 void STLClearObject(T* obj) { |
| 23 T tmp; | 25 T tmp; |
| 24 tmp.swap(*obj); | 26 tmp.swap(*obj); |
| 25 // Sometimes "T tmp" allocates objects with memory (arena implementation?). | 27 // Sometimes "T tmp" allocates objects with memory (arena implementation?). |
| 26 // Hence using additional reserve(0) even if it doesn't always work. | 28 // Hence using additional reserve(0) even if it doesn't always work. |
| 27 obj->reserve(0); | 29 obj->reserve(0); |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 191 } | 193 } |
| 192 | 194 |
| 193 // Test to see if a collection like a vector contains a particular value. | 195 // Test to see if a collection like a vector contains a particular value. |
| 194 // Returns true if the value is in the collection. | 196 // Returns true if the value is in the collection. |
| 195 template <typename Collection, typename Value> | 197 template <typename Collection, typename Value> |
| 196 bool ContainsValue(const Collection& collection, const Value& value) { | 198 bool ContainsValue(const Collection& collection, const Value& value) { |
| 197 return std::find(collection.begin(), collection.end(), value) != | 199 return std::find(collection.begin(), collection.end(), value) != |
| 198 collection.end(); | 200 collection.end(); |
| 199 } | 201 } |
| 200 | 202 |
| 201 namespace base { | |
| 202 | |
| 203 // Returns true if the container is sorted. | 203 // Returns true if the container is sorted. |
| 204 template <typename Container> | 204 template <typename Container> |
| 205 bool STLIsSorted(const Container& cont) { | 205 bool STLIsSorted(const Container& cont) { |
| 206 // Note: Use reverse iterator on container to ensure we only require | 206 // Note: Use reverse iterator on container to ensure we only require |
| 207 // value_type to implement operator<. | 207 // value_type to implement operator<. |
| 208 return std::adjacent_find(cont.rbegin(), cont.rend(), | 208 return std::adjacent_find(cont.rbegin(), cont.rend(), |
| 209 std::less<typename Container::value_type>()) | 209 std::less<typename Container::value_type>()) |
| 210 == cont.rend(); | 210 == cont.rend(); |
| 211 } | 211 } |
| 212 | 212 |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 template <typename Arg1, typename Arg2> | 252 template <typename Arg1, typename Arg2> |
| 253 bool STLIncludes(const Arg1& a1, const Arg2& a2) { | 253 bool STLIncludes(const Arg1& a1, const Arg2& a2) { |
| 254 DCHECK(STLIsSorted(a1)); | 254 DCHECK(STLIsSorted(a1)); |
| 255 DCHECK(STLIsSorted(a2)); | 255 DCHECK(STLIsSorted(a2)); |
| 256 return std::includes(a1.begin(), a1.end(), | 256 return std::includes(a1.begin(), a1.end(), |
| 257 a2.begin(), a2.end()); | 257 a2.begin(), a2.end()); |
| 258 } | 258 } |
| 259 | 259 |
| 260 } // namespace base | 260 } // namespace base |
| 261 | 261 |
| 262 // TODO(skyostil): Remove these global aliases once all call sites have been |
| 263 // fixed. |
| 264 using base::ContainsKey; |
| 265 using base::ContainsValue; |
| 266 using base::STLClearObject; |
| 267 using base::STLCount; |
| 268 using base::STLDeleteContainerPairFirstPointers; |
| 269 using base::STLDeleteContainerPairPointers; |
| 270 using base::STLDeleteContainerPairSecondPointers; |
| 271 using base::STLDeleteContainerPointers; |
| 272 using base::STLDeleteElements; |
| 273 using base::STLDeleteValues; |
| 274 using base::STLElementDeleter; |
| 275 using base::STLValueDeleter; |
| 276 using base::string_as_array; |
| 277 |
| 262 #endif // BASE_STL_UTIL_H_ | 278 #endif // BASE_STL_UTIL_H_ |
| OLD | NEW |