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

Side by Side Diff: base/stl_util.h

Issue 1124763003: Update from https://crrev.com/327068 (Closed) Base URL: git@github.com:domokit/mojo.git@master
Patch Set: update nacl, buildtools, fix display_change_notifier_unittest Created 5 years, 7 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& container, const T& val) {
98 return std::count(container.begin(), container.end(), val);
99 }
100
93 // To treat a possibly-empty vector as an array, use these functions. 101 // 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() 102 // 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. 103 // directly, but that is undefined behaviour if |v| is empty.
96 template<typename T> 104 template<typename T>
97 inline T* vector_as_array(std::vector<T>* v) { 105 inline T* vector_as_array(std::vector<T>* v) {
98 return v->empty() ? NULL : &*v->begin(); 106 return v->empty() ? NULL : &*v->begin();
99 } 107 }
100 108
101 template<typename T> 109 template<typename T>
102 inline const T* vector_as_array(const std::vector<T>* v) { 110 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_; 196 T* container_;
189 }; 197 };
190 198
191 // Test to see if a set, map, hash_set or hash_map contains a particular key. 199 // 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. 200 // Returns true if the key is in the collection.
193 template <typename Collection, typename Key> 201 template <typename Collection, typename Key>
194 bool ContainsKey(const Collection& collection, const Key& key) { 202 bool ContainsKey(const Collection& collection, const Key& key) {
195 return collection.find(key) != collection.end(); 203 return collection.find(key) != collection.end();
196 } 204 }
197 205
206 // Test to see if a collection like a vector contains a particular value.
207 // Returns true if the value is in the collection.
208 template <typename Collection, typename Value>
209 bool ContainsValue(const Collection& collection, const Value& value) {
210 return std::find(collection.begin(), collection.end(), value) !=
211 collection.end();
212 }
213
198 namespace base { 214 namespace base {
199 215
200 // Returns true if the container is sorted. 216 // Returns true if the container is sorted.
201 template <typename Container> 217 template <typename Container>
202 bool STLIsSorted(const Container& cont) { 218 bool STLIsSorted(const Container& cont) {
203 // Note: Use reverse iterator on container to ensure we only require 219 // Note: Use reverse iterator on container to ensure we only require
204 // value_type to implement operator<. 220 // value_type to implement operator<.
205 return std::adjacent_find(cont.rbegin(), cont.rend(), 221 return std::adjacent_find(cont.rbegin(), cont.rend(),
206 std::less<typename Container::value_type>()) 222 std::less<typename Container::value_type>())
207 == cont.rend(); 223 == cont.rend();
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after
250 bool STLIncludes(const Arg1& a1, const Arg2& a2) { 266 bool STLIncludes(const Arg1& a1, const Arg2& a2) {
251 DCHECK(STLIsSorted(a1)); 267 DCHECK(STLIsSorted(a1));
252 DCHECK(STLIsSorted(a2)); 268 DCHECK(STLIsSorted(a2));
253 return std::includes(a1.begin(), a1.end(), 269 return std::includes(a1.begin(), a1.end(),
254 a2.begin(), a2.end()); 270 a2.begin(), a2.end());
255 } 271 }
256 272
257 } // namespace base 273 } // namespace base
258 274
259 #endif // BASE_STL_UTIL_H_ 275 #endif // BASE_STL_UTIL_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698