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

Side by Side Diff: base/stl_util.h

Issue 1010973013: Refactor Uses of std::set to std::vector in SimpleFeature (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: IsIdInArray and other Cleanup Created 5 years, 9 months 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
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 <algorithm>
(...skipping 72 matching lines...) Expand 10 before | Expand all | Expand 10 after
83 template <class ForwardIterator> 83 template <class ForwardIterator>
84 void STLDeleteContainerPairSecondPointers(ForwardIterator begin, 84 void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
85 ForwardIterator end) { 85 ForwardIterator end) {
86 while (begin != end) { 86 while (begin != end) {
87 ForwardIterator temp = begin; 87 ForwardIterator temp = begin;
88 ++begin; 88 ++begin;
89 delete temp->second; 89 delete temp->second;
90 } 90 }
91 } 91 }
92 92
93 // Counts the number of instances of val in a container.
94 template <typename Container, typename T>
95 typename std::iterator_traits<
96 typename Container::const_iterator>::difference_type
97 STLCount(const Container* const container, const T& val) {
Lei Zhang 2015/03/26 21:48:45 Can |container| be a const ref instead?
robliao 2015/03/26 21:51:48 My initial iteration started out like that, but I
miu 2015/03/26 22:24:43 I gotta agree with thestig@ here. "const Containe
robliao 2015/03/26 23:15:50 Changed and Updated.
98 const typename Container::const_iterator begin = container->begin();
99 const typename Container::const_iterator end = container->end();
100 return std::count(begin, end, val);
101 }
102
93 // To treat a possibly-empty vector as an array, use these functions. 103 // To treat a possibly-empty vector as an array, use these functions.
94 // If you know the array will never be empty, you can use &*v.begin() 104 // If you know the array will never be empty, you can use &*v.begin()
95 // directly, but that is undefined behaviour if |v| is empty. 105 // directly, but that is undefined behaviour if |v| is empty.
96 template<typename T> 106 template<typename T>
97 inline T* vector_as_array(std::vector<T>* v) { 107 inline T* vector_as_array(std::vector<T>* v) {
98 return v->empty() ? NULL : &*v->begin(); 108 return v->empty() ? NULL : &*v->begin();
99 } 109 }
100 110
101 template<typename T> 111 template<typename T>
102 inline const T* vector_as_array(const std::vector<T>* v) { 112 inline const T* vector_as_array(const std::vector<T>* v) {
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after
188 T* container_; 198 T* container_;
189 }; 199 };
190 200
191 // Test to see if a set, map, hash_set or hash_map contains a particular key. 201 // Test to see if a set, map, hash_set or hash_map contains a particular key.
192 // Returns true if the key is in the collection. 202 // Returns true if the key is in the collection.
193 template <typename Collection, typename Key> 203 template <typename Collection, typename Key>
194 bool ContainsKey(const Collection& collection, const Key& key) { 204 bool ContainsKey(const Collection& collection, const Key& key) {
195 return collection.find(key) != collection.end(); 205 return collection.find(key) != collection.end();
196 } 206 }
197 207
208 // Test to see if a collection like a vector contains a particular value.
209 // Returns true if the value is in the collection.
210 template <typename Collection, typename Value>
211 bool ContainsValue(const Collection& collection, const Value& value) {
212 return std::find(collection.begin(), collection.end(), value) !=
213 collection.end();
214 }
215
198 namespace base { 216 namespace base {
199 217
200 // Returns true if the container is sorted. 218 // Returns true if the container is sorted.
201 template <typename Container> 219 template <typename Container>
202 bool STLIsSorted(const Container& cont) { 220 bool STLIsSorted(const Container& cont) {
203 // Note: Use reverse iterator on container to ensure we only require 221 // Note: Use reverse iterator on container to ensure we only require
204 // value_type to implement operator<. 222 // value_type to implement operator<.
205 return std::adjacent_find(cont.rbegin(), cont.rend(), 223 return std::adjacent_find(cont.rbegin(), cont.rend(),
206 std::less<typename Container::value_type>()) 224 std::less<typename Container::value_type>())
207 == cont.rend(); 225 == cont.rend();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 bool STLIncludes(const Arg1& a1, const Arg2& a2) { 268 bool STLIncludes(const Arg1& a1, const Arg2& a2) {
251 DCHECK(STLIsSorted(a1)); 269 DCHECK(STLIsSorted(a1));
252 DCHECK(STLIsSorted(a2)); 270 DCHECK(STLIsSorted(a2));
253 return std::includes(a1.begin(), a1.end(), 271 return std::includes(a1.begin(), a1.end(),
254 a2.begin(), a2.end()); 272 a2.begin(), a2.end());
255 } 273 }
256 274
257 } // namespace base 275 } // namespace base
258 276
259 #endif // BASE_STL_UTIL_H_ 277 #endif // BASE_STL_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698