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

Side by Side Diff: base/stl_util.h

Issue 7465001: Small stl_util tidy up (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: rebase Created 9 years, 5 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 | Annotate | Revision Log
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 #pragma once 9 #pragma once
10 10
11 #include <assert.h>
12 #include <string.h> // for memcpy
13
14 #include <string> 11 #include <string>
15 #include <vector> 12 #include <vector>
16 13
17 // Clear internal memory of an STL object. 14 // Clear internal memory of an STL object.
18 // STL clear()/reserve(0) does not always free internal memory allocated 15 // STL clear()/reserve(0) does not always free internal memory allocated
19 // This function uses swap/destructor to ensure the internal memory is freed. 16 // This function uses swap/destructor to ensure the internal memory is freed.
20 template<class T> void STLClearObject(T* obj) { 17 template<class T> void STLClearObject(T* obj) {
21 T tmp; 18 T tmp;
22 tmp.swap(*obj); 19 tmp.swap(*obj);
23 // Sometimes "T tmp" allocates objects with memory (arena implementation?). 20 // Sometimes "T tmp" allocates objects with memory (arena implementation?).
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
72 ForwardIterator end) { 69 ForwardIterator end) {
73 while (begin != end) { 70 while (begin != end) {
74 ForwardIterator temp = begin; 71 ForwardIterator temp = begin;
75 ++begin; 72 ++begin;
76 delete temp->first; 73 delete temp->first;
77 } 74 }
78 } 75 }
79 76
80 // STLDeleteContainerPairSecondPointers() 77 // STLDeleteContainerPairSecondPointers()
81 // For a range within a container of pairs, calls delete 78 // For a range within a container of pairs, calls delete
79 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
80 // Deleting the value does not always invalidate the iterator, but it may
81 // do so if the key is a pointer into the value object.
82 // (non-array version) on the SECOND item in the pairs. 82 // (non-array version) on the SECOND item in the pairs.
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 delete begin->second; 87 ForwardIterator temp = begin;
88 ++begin; 88 ++begin;
89 delete temp->second;
89 } 90 }
90 } 91 }
91 92
92 // To treat a possibly-empty vector as an array, use these functions. 93 // To treat a possibly-empty vector as an array, use these functions.
93 // If you know the array will never be empty, you can use &*v.begin() 94 // If you know the array will never be empty, you can use &*v.begin()
94 // directly, but that is undefined behaviour if v is empty. 95 // directly, but that is undefined behaviour if v is empty.
95 96
96 template<typename T> 97 template<typename T>
97 inline T* vector_as_array(std::vector<T>* v) { 98 inline T* vector_as_array(std::vector<T>* v) {
98 # ifdef NDEBUG
99 return &*v->begin();
100 # else
101 return v->empty() ? NULL : &*v->begin(); 99 return v->empty() ? NULL : &*v->begin();
102 # endif
103 } 100 }
104 101
105 template<typename T> 102 template<typename T>
106 inline const T* vector_as_array(const std::vector<T>* v) { 103 inline const T* vector_as_array(const std::vector<T>* v) {
107 # ifdef NDEBUG
108 return &*v->begin();
109 # else
110 return v->empty() ? NULL : &*v->begin(); 104 return v->empty() ? NULL : &*v->begin();
111 # endif
112 } 105 }
113 106
114 // Return a mutable char* pointing to a string's internal buffer, 107 // Return a mutable char* pointing to a string's internal buffer,
115 // which may not be null-terminated. Writing through this pointer will 108 // which may not be null-terminated. Writing through this pointer will
116 // modify the string. 109 // modify the string.
117 // 110 //
118 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the 111 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
119 // next call to a string method that invalidates iterators. 112 // next call to a string method that invalidates iterators.
120 // 113 //
121 // As of 2006-04, there is no standard-blessed way of getting a 114 // As of 2006-04, there is no standard-blessed way of getting a
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
196 }; 189 };
197 190
198 // Test to see if a set, map, hash_set or hash_map contains a particular key. 191 // Test to see if a set, map, hash_set or hash_map contains a particular key.
199 // Returns true if the key is in the collection. 192 // Returns true if the key is in the collection.
200 template <typename Collection, typename Key> 193 template <typename Collection, typename Key>
201 bool ContainsKey(const Collection& collection, const Key& key) { 194 bool ContainsKey(const Collection& collection, const Key& key) {
202 return collection.find(key) != collection.end(); 195 return collection.find(key) != collection.end();
203 } 196 }
204 197
205 #endif // BASE_STL_UTIL_H_ 198 #endif // BASE_STL_UTIL_H_
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698