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> | |
| 11 #include <set> | |
| 10 #include <string> | 12 #include <string> |
| 11 #include <vector> | 13 #include <vector> |
| 12 | 14 |
| 13 // Clears internal memory of an STL object. | 15 // Clears internal memory of an STL object. |
| 14 // STL clear()/reserve(0) does not always free internal memory allocated | 16 // STL clear()/reserve(0) does not always free internal memory allocated |
| 15 // This function uses swap/destructor to ensure the internal memory is freed. | 17 // This function uses swap/destructor to ensure the internal memory is freed. |
| 16 template<class T> | 18 template<class T> |
| 17 void STLClearObject(T* obj) { | 19 void STLClearObject(T* obj) { |
| 18 T tmp; | 20 T tmp; |
| 19 tmp.swap(*obj); | 21 tmp.swap(*obj); |
| (...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 184 T* container_; | 186 T* container_; |
| 185 }; | 187 }; |
| 186 | 188 |
| 187 // Test to see if a set, map, hash_set or hash_map contains a particular key. | 189 // Test to see if a set, map, hash_set or hash_map contains a particular key. |
| 188 // Returns true if the key is in the collection. | 190 // Returns true if the key is in the collection. |
| 189 template <typename Collection, typename Key> | 191 template <typename Collection, typename Key> |
| 190 bool ContainsKey(const Collection& collection, const Key& key) { | 192 bool ContainsKey(const Collection& collection, const Key& key) { |
| 191 return collection.find(key) != collection.end(); | 193 return collection.find(key) != collection.end(); |
| 192 } | 194 } |
| 193 | 195 |
| 196 // Returns a new std::set containing the difference of two sets. | |
| 197 // A convenient wrapper around std::set_difference. | |
| 198 template <typename T> | |
| 199 std::set<T> STLSetDifference(const std::set<T>& a, const std::set<T>& b) { | |
|
Jeffrey Yasskin
2012/11/30 23:00:16
I agree with the decision to only provide one over
Jeffrey Yasskin
2012/11/30 23:06:21
I'm just being dumb. insert_iterator uses 'iter =
| |
| 200 std::set<T> difference; | |
| 201 std::set_difference(a.begin(), a.end(), | |
| 202 b.begin(), b.end(), | |
| 203 std::inserter(difference, difference.end())); | |
| 204 return difference; | |
| 205 } | |
| 206 | |
| 194 #endif // BASE_STL_UTIL_H_ | 207 #endif // BASE_STL_UTIL_H_ |
| OLD | NEW |