Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(214)

Side by Side Diff: base/stl_util.h

Issue 11415239: Add STLSetDifference to stl_util.h, because std::set_difference is extremely (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: blah Created 8 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 <string> 11 #include <string>
11 #include <vector> 12 #include <vector>
12 13
14 #include "base/logging.h"
15
13 // Clears internal memory of an STL object. 16 // Clears internal memory of an STL object.
14 // STL clear()/reserve(0) does not always free internal memory allocated 17 // STL clear()/reserve(0) does not always free internal memory allocated
15 // This function uses swap/destructor to ensure the internal memory is freed. 18 // This function uses swap/destructor to ensure the internal memory is freed.
16 template<class T> 19 template<class T>
17 void STLClearObject(T* obj) { 20 void STLClearObject(T* obj) {
18 T tmp; 21 T tmp;
19 tmp.swap(*obj); 22 tmp.swap(*obj);
20 // Sometimes "T tmp" allocates objects with memory (arena implementation?). 23 // Sometimes "T tmp" allocates objects with memory (arena implementation?).
21 // Hence using additional reserve(0) even if it doesn't always work. 24 // Hence using additional reserve(0) even if it doesn't always work.
22 obj->reserve(0); 25 obj->reserve(0);
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 T* container_; 187 T* container_;
185 }; 188 };
186 189
187 // Test to see if a set, map, hash_set or hash_map contains a particular key. 190 // 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. 191 // Returns true if the key is in the collection.
189 template <typename Collection, typename Key> 192 template <typename Collection, typename Key>
190 bool ContainsKey(const Collection& collection, const Key& key) { 193 bool ContainsKey(const Collection& collection, const Key& key) {
191 return collection.find(key) != collection.end(); 194 return collection.find(key) != collection.end();
192 } 195 }
193 196
197 namespace base {
198
199 // Returns true if the container is sorted.
200 template <typename Container>
201 bool STLIsSorted(const Container& cont) {
202 return std::adjacent_find(cont.begin(), cont.end(),
203 std::greater<typename Container::value_type>())
204 == cont.end();
205 }
206
207 // Returns a new ResultType containing the difference of two sorted containers.
208 template <typename ResultType, typename Arg1, typename Arg2>
209 ResultType STLSetDifference(const Arg1& a1, const Arg2& a2) {
210 DCHECK(STLIsSorted(a1));
211 DCHECK(STLIsSorted(a2));
212 ResultType difference;
213 std::set_difference(a1.begin(), a1.end(),
214 a2.begin(), a2.end(),
215 std::inserter(difference, difference.end()));
216 return difference;
217 }
218
219 } // namespace base
220
194 #endif // BASE_STL_UTIL_H_ 221 #endif // BASE_STL_UTIL_H_
OLDNEW
« no previous file with comments | « base/base.gyp ('k') | base/stl_util_unittest.cc » ('j') | base/stl_util_unittest.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698