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

Side by Side Diff: base/stl_util-inl.h

Issue 7342047: Cleanup base/stl_util (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: c 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) 2010 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2010 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 // STL utility functions. Usually, these replace built-in, but slow(!), 5 // STL utility functions. Usually, these replace built-in, but slow(!),
6 // STL functions with more efficient versions. 6 // STL functions with more efficient versions.
7 7
8 #ifndef BASE_STL_UTIL_INL_H_ 8 #ifndef BASE_STL_UTIL_INL_H_
9 #define BASE_STL_UTIL_INL_H_ 9 #define BASE_STL_UTIL_INL_H_
10 #pragma once 10 #pragma once
11 11
12 #include <assert.h>
brettw 2011/07/14 15:10:42 What was the reason for this assert change? Wouldn
Denis Lagno 2011/07/14 16:45:26 1st reason is that it is inconsistent: using <cass
12 #include <string.h> // for memcpy 13 #include <string.h> // for memcpy
14
13 #include <functional> 15 #include <functional>
14 #include <set> 16 #include <set>
15 #include <string> 17 #include <string>
16 #include <vector> 18 #include <vector>
17 #include <cassert>
18 19
19 // Clear internal memory of an STL object. 20 // Clear internal memory of an STL object.
20 // STL clear()/reserve(0) does not always free internal memory allocated 21 // STL clear()/reserve(0) does not always free internal memory allocated
21 // This function uses swap/destructor to ensure the internal memory is freed. 22 // This function uses swap/destructor to ensure the internal memory is freed.
22 template<class T> void STLClearObject(T* obj) { 23 template<class T> void STLClearObject(T* obj, size_t limit = 1 << 6) {
brettw 2011/07/14 15:10:42 I don't understand what your goal with this change
Denis Lagno 2011/07/14 16:45:26 My main goal was to get rid of STLClearIfBig funct
23 T tmp; 24 if (obj->capacity() > limit) {
24 tmp.swap(*obj); 25 T tmp;
25 obj->reserve(0); // this is because sometimes "T tmp" allocates objects with 26 tmp.swap(*obj);
26 // memory (arena implementation?). use reserve() 27 // Sometimes "T tmp" allocates objects with memory (arena implementation?).
27 // to clear() even if it doesn't always work 28 // Hence using additional reserve(0) even if it doesn't always work.
28 } 29 obj->reserve(0);
29
30 // Reduce memory usage on behalf of object if it is using more than
31 // "bytes" bytes of space. By default, we clear objects over 1MB.
32 template <class T> inline void STLClearIfBig(T* obj, size_t limit = 1<<20) {
33 if (obj->capacity() >= limit) {
34 STLClearObject(obj);
35 } else { 30 } else {
36 obj->clear(); 31 obj->clear();
37 } 32 }
38 } 33 }
39 34
40 // Reserve space for STL object.
41 // STL's reserve() will always copy.
42 // This function avoid the copy if we already have capacity
43 template<class T> void STLReserveIfNeeded(T* obj, int new_size) {
44 if (obj->capacity() < new_size) // increase capacity
45 obj->reserve(new_size);
46 else if (obj->size() > new_size) // reduce size
47 obj->resize(new_size);
48 }
49
50 // STLDeleteContainerPointers() 35 // STLDeleteContainerPointers()
51 // For a range within a container of pointers, calls delete 36 // For a range within a container of pointers, calls delete
52 // (non-array version) on these pointers. 37 // (non-array version) on these pointers.
53 // NOTE: for these three functions, we could just implement a DeleteObject 38 // NOTE: for these three functions, we could just implement a DeleteObject
54 // functor and then call for_each() on the range and functor, but this 39 // functor and then call for_each() on the range and functor, but this
55 // requires us to pull in all of algorithm.h, which seems expensive. 40 // requires us to pull in all of algorithm.h, which seems expensive.
56 // For hash_[multi]set, it is important that this deletes behind the iterator 41 // For hash_[multi]set, it is important that this deletes behind the iterator
57 // because the hash_set may call the hash function on the iterator when it is 42 // because the hash_set may call the hash function on the iterator when it is
58 // advanced, which could result in the hash function trying to deference a 43 // advanced, which could result in the hash function trying to deference a
59 // stale pointer. 44 // stale pointer.
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 // (non-array version) on the SECOND item in the pairs. 89 // (non-array version) on the SECOND item in the pairs.
105 template <class ForwardIterator> 90 template <class ForwardIterator>
106 void STLDeleteContainerPairSecondPointers(ForwardIterator begin, 91 void STLDeleteContainerPairSecondPointers(ForwardIterator begin,
107 ForwardIterator end) { 92 ForwardIterator end) {
108 while (begin != end) { 93 while (begin != end) {
109 delete begin->second; 94 delete begin->second;
110 ++begin; 95 ++begin;
111 } 96 }
112 } 97 }
113 98
114 template<typename T>
115 inline void STLAssignToVector(std::vector<T>* vec,
116 const T* ptr,
117 size_t n) {
118 vec->resize(n);
119 memcpy(&vec->front(), ptr, n*sizeof(T));
120 }
121
122 /***** Hack to allow faster assignment to a vector *****/
123
124 // This routine speeds up an assignment of 32 bytes to a vector from
125 // about 250 cycles per assignment to about 140 cycles.
126 //
127 // Usage:
128 // STLAssignToVectorChar(&vec, ptr, size);
129 // STLAssignToString(&str, ptr, size);
130
131 inline void STLAssignToVectorChar(std::vector<char>* vec,
132 const char* ptr,
133 size_t n) {
134 STLAssignToVector(vec, ptr, n);
135 }
136
137 inline void STLAssignToString(std::string* str, const char* ptr, size_t n) {
138 str->resize(n);
139 memcpy(&*str->begin(), ptr, n);
140 }
141
142 // To treat a possibly-empty vector as an array, use these functions. 99 // To treat a possibly-empty vector as an array, use these functions.
143 // If you know the array will never be empty, you can use &*v.begin() 100 // If you know the array will never be empty, you can use &v.front()
144 // directly, but that is allowed to dump core if v is empty. This 101 // directly, but that is undefined behaviour if v is empty.
145 // function is the most efficient code that will work, taking into
146 // account how our STL is actually implemented. THIS IS NON-PORTABLE
147 // CODE, so call us instead of repeating the nonportable code
148 // everywhere. If our STL implementation changes, we will need to
149 // change this as well.
150 102
151 template<typename T> 103 template<typename T>
152 inline T* vector_as_array(std::vector<T>* v) { 104 inline T* vector_as_array(std::vector<T>* v) {
153 # ifdef NDEBUG 105 # ifdef NDEBUG
154 return &*v->begin(); 106 return &v->front();
brettw 2011/07/14 15:10:42 I wouldn't change these, I think the generated cod
Denis Lagno 2011/07/14 16:45:26 Done.
155 # else 107 # else
156 return v->empty() ? NULL : &*v->begin(); 108 return v->empty() ? NULL : &v->front();
157 # endif 109 # endif
158 } 110 }
159 111
160 template<typename T> 112 template<typename T>
161 inline const T* vector_as_array(const std::vector<T>* v) { 113 inline const T* vector_as_array(const std::vector<T>* v) {
162 # ifdef NDEBUG 114 # ifdef NDEBUG
163 return &*v->begin(); 115 return &v->front();
164 # else 116 # else
165 return v->empty() ? NULL : &*v->begin(); 117 return v->empty() ? NULL : &v->front();
166 # endif 118 # endif
167 } 119 }
168 120
169 // Return a mutable char* pointing to a string's internal buffer, 121 // Return a mutable char* pointing to a string's internal buffer,
170 // which may not be null-terminated. Writing through this pointer will 122 // which may be not null-terminated. Writing through this pointer will
171 // modify the string. 123 // modify the string.
172 // 124 //
173 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the 125 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
174 // next call to a string method that invalidates iterators. 126 // next call to a string method that invalidates iterators.
175 // 127 //
176 // As of 2006-04, there is no standard-blessed way of getting a 128 // As of 2006-04, there is no standard-blessed way of getting a
177 // mutable reference to a string's internal buffer. However, issue 530 129 // mutable reference to a string's internal buffer. However, issue 530
178 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) 130 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
179 // proposes this as the method. According to Matt Austern, this should 131 // proposes this as the method. According to Matt Austern, this should
180 // already work on all current implementations. 132 // already work on all current implementations.
181 inline char* string_as_array(std::string* str) { 133 inline char* string_as_array(std::string* str) {
182 // DO NOT USE const_cast<char*>(str->data())! See the unittest for why. 134 // DO NOT USE const_cast<char*>(str->data())
183 return str->empty() ? NULL : &*str->begin(); 135 return str->empty() ? NULL : &*str->begin();
184 } 136 }
185 137
138 template<typename T>
139 inline void STLAssignToVector(std::vector<T>* vec, const T* ptr, size_t n) {
140 vec->resize(n);
141 memcpy(vector_as_array(vec), ptr, n*sizeof(T));
142 }
143
144 inline void STLAssignToString(std::string* str, const char* ptr, size_t n) {
145 str->resize(n);
146 memcpy(string_as_array(str), ptr, n);
147 }
148
186 // These are methods that test two hash maps/sets for equality. These exist 149 // These are methods that test two hash maps/sets for equality. These exist
187 // because the == operator in the STL can return false when the maps/sets 150 // because the == operator in the STL can return false when the maps/sets
188 // contain identical elements. This is because it compares the internal hash 151 // contain identical elements. This is because it compares the internal hash
189 // tables which may be different if the order of insertions and deletions 152 // tables which may be different if the order of insertions and deletions
190 // differed. 153 // differed.
191 154
192 template <class HashSet> 155 template <class HashSet>
193 inline bool HashSetEquality(const HashSet& set_a, const HashSet& set_b) { 156 inline bool HashSetEquality(const HashSet& set_a, const HashSet& set_b) {
194 if (set_a.size() != set_b.size()) return false; 157 if (set_a.size() != set_b.size()) return false;
195 for (typename HashSet::const_iterator i = set_a.begin(); 158 for (typename HashSet::const_iterator i = set_a.begin();
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
443 } 406 }
444 407
445 // Test to see if a set, map, hash_set or hash_map contains a particular key. 408 // Test to see if a set, map, hash_set or hash_map contains a particular key.
446 // Returns true if the key is in the collection. 409 // Returns true if the key is in the collection.
447 template <typename Collection, typename Key> 410 template <typename Collection, typename Key>
448 bool ContainsKey(const Collection& collection, const Key& key) { 411 bool ContainsKey(const Collection& collection, const Key& key) {
449 return collection.find(key) != collection.end(); 412 return collection.find(key) != collection.end();
450 } 413 }
451 414
452 #endif // BASE_STL_UTIL_INL_H_ 415 #endif // BASE_STL_UTIL_INL_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