OLD | NEW |
---|---|
(Empty) | |
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 | |
3 // found in the LICENSE file. | |
4 | |
5 // Derived from google3/util/gtl/stl_util.h | |
6 | |
7 #ifndef BASE_STL_UTIL_H_ | |
8 #define BASE_STL_UTIL_H_ | |
9 #pragma once | |
10 | |
11 #include <assert.h> | |
12 #include <string.h> // for memcpy | |
13 | |
14 #include <set> | |
15 #include <string> | |
16 #include <vector> | |
17 | |
18 // Clear internal memory of an STL object. | |
19 // STL clear()/reserve(0) does not always free internal memory allocated | |
20 // This function uses swap/destructor to ensure the internal memory is freed. | |
21 template<class T> void STLClearObject(T* obj) { | |
22 T tmp; | |
23 tmp.swap(*obj); | |
24 // Sometimes "T tmp" allocates objects with memory (arena implementation?). | |
25 // Hence using additional reserve(0) even if it doesn't always work. | |
26 obj->reserve(0); | |
27 } | |
28 | |
29 // STLDeleteContainerPointers() | |
30 // For a range within a container of pointers, calls delete | |
31 // (non-array version) on these pointers. | |
32 // NOTE: for these three functions, we could just implement a DeleteObject | |
33 // functor and then call for_each() on the range and functor, but this | |
34 // requires us to pull in all of algorithm.h, which seems expensive. | |
35 // For hash_[multi]set, it is important that this deletes behind the iterator | |
36 // because the hash_set may call the hash function on the iterator when it is | |
37 // advanced, which could result in the hash function trying to deference a | |
38 // stale pointer. | |
39 template <class ForwardIterator> | |
40 void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) { | |
41 while (begin != end) { | |
42 ForwardIterator temp = begin; | |
43 ++begin; | |
44 delete *temp; | |
45 } | |
46 } | |
47 | |
48 // STLDeleteContainerPairPointers() | |
49 // For a range within a container of pairs, calls delete | |
50 // (non-array version) on BOTH items in the pairs. | |
51 // NOTE: Like STLDeleteContainerPointers, it is important that this deletes | |
52 // behind the iterator because if both the key and value are deleted, the | |
53 // container may call the hash function on the iterator when it is advanced, | |
54 // which could result in the hash function trying to dereference a stale | |
55 // pointer. | |
56 template <class ForwardIterator> | |
57 void STLDeleteContainerPairPointers(ForwardIterator begin, | |
58 ForwardIterator end) { | |
59 while (begin != end) { | |
60 ForwardIterator temp = begin; | |
61 ++begin; | |
62 delete temp->first; | |
63 delete temp->second; | |
64 } | |
65 } | |
66 | |
67 // STLDeleteContainerPairFirstPointers() | |
68 // For a range within a container of pairs, calls delete (non-array version) | |
69 // on the FIRST item in the pairs. | |
70 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator. | |
71 template <class ForwardIterator> | |
72 void STLDeleteContainerPairFirstPointers(ForwardIterator begin, | |
73 ForwardIterator end) { | |
74 while (begin != end) { | |
75 ForwardIterator temp = begin; | |
76 ++begin; | |
77 delete temp->first; | |
78 } | |
79 } | |
80 | |
81 // STLDeleteContainerPairSecondPointers() | |
82 // For a range within a container of pairs, calls delete | |
83 // (non-array version) on the SECOND item in the pairs. | |
84 template <class ForwardIterator> | |
85 void STLDeleteContainerPairSecondPointers(ForwardIterator begin, | |
86 ForwardIterator end) { | |
87 while (begin != end) { | |
88 delete begin->second; | |
89 ++begin; | |
90 } | |
91 } | |
92 | |
93 // 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() | |
95 // directly, but that is undefined behaviour if v is empty. | |
96 | |
97 template<typename T> | |
98 inline T* vector_as_array(std::vector<T>* v) { | |
99 # ifdef NDEBUG | |
100 return &*v->begin(); | |
101 # else | |
102 return v->empty() ? NULL : &*v->begin(); | |
103 # endif | |
104 } | |
105 | |
106 template<typename T> | |
107 inline const T* vector_as_array(const std::vector<T>* v) { | |
108 # ifdef NDEBUG | |
109 return &*v->begin(); | |
110 # else | |
111 return v->empty() ? NULL : &*v->begin(); | |
112 # endif | |
113 } | |
114 | |
115 // Return a mutable char* pointing to a string's internal buffer, | |
116 // which may not be null-terminated. Writing through this pointer will | |
117 // modify the string. | |
118 // | |
119 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the | |
120 // next call to a string method that invalidates iterators. | |
121 // | |
122 // As of 2006-04, there is no standard-blessed way of getting a | |
123 // mutable reference to a string's internal buffer. However, issue 530 | |
124 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530) | |
125 // proposes this as the method. According to Matt Austern, this should | |
126 // already work on all current implementations. | |
127 inline char* string_as_array(std::string* str) { | |
128 // DO NOT USE const_cast<char*>(str->data()) | |
129 return str->empty() ? NULL : &*str->begin(); | |
130 } | |
131 | |
132 // These are methods that test two hash maps/sets for equality. These exist | |
133 // because the == operator in the STL can return false when the maps/sets | |
134 // contain identical elements. This is because it compares the internal hash | |
135 // tables which may be different if the order of insertions and deletions | |
136 // differed. | |
137 | |
138 template <class HashSet> | |
139 inline bool HashSetEquality(const HashSet& set_a, const HashSet& set_b) { | |
brettw
2011/07/19 16:01:04
It looks like nobody uses this and it can be delet
Denis Lagno
2011/07/19 18:31:00
Done.
| |
140 if (set_a.size() != set_b.size()) return false; | |
141 for (typename HashSet::const_iterator i = set_a.begin(); | |
142 i != set_a.end(); ++i) { | |
143 if (set_b.find(*i) == set_b.end()) | |
144 return false; | |
145 } | |
146 return true; | |
147 } | |
148 | |
149 template <class HashMap> | |
150 inline bool HashMapEquality(const HashMap& map_a, const HashMap& map_b) { | |
brettw
2011/07/19 16:01:04
It looks like nobody uses this and it can be delet
Denis Lagno
2011/07/19 18:31:00
Done.
| |
151 if (map_a.size() != map_b.size()) return false; | |
152 for (typename HashMap::const_iterator i = map_a.begin(); | |
153 i != map_a.end(); ++i) { | |
154 typename HashMap::const_iterator j = map_b.find(i->first); | |
155 if (j == map_b.end()) return false; | |
156 if (i->second != j->second) return false; | |
157 } | |
158 return true; | |
159 } | |
160 | |
161 // The following functions are useful for cleaning up STL containers | |
162 // whose elements point to allocated memory. | |
163 | |
164 // STLDeleteElements() deletes all the elements in an STL container and clears | |
165 // the container. This function is suitable for use with a vector, set, | |
166 // hash_set, or any other STL container which defines sensible begin(), end(), | |
167 // and clear() methods. | |
168 // | |
169 // If container is NULL, this function is a no-op. | |
170 // | |
171 // As an alternative to calling STLDeleteElements() directly, consider | |
172 // STLElementDeleter (defined below), which ensures that your container's | |
173 // elements are deleted when the STLElementDeleter goes out of scope. | |
174 template <class T> | |
175 void STLDeleteElements(T *container) { | |
176 if (!container) return; | |
177 STLDeleteContainerPointers(container->begin(), container->end()); | |
178 container->clear(); | |
179 } | |
180 | |
181 // Given an STL container consisting of (key, value) pairs, STLDeleteValues | |
182 // deletes all the "value" components and clears the container. Does nothing | |
183 // in the case it's given a NULL pointer. | |
184 | |
185 template <class T> | |
186 void STLDeleteValues(T *v) { | |
187 if (!v) return; | |
188 for (typename T::iterator i = v->begin(); i != v->end(); ++i) { | |
189 delete i->second; | |
190 } | |
191 v->clear(); | |
192 } | |
193 | |
194 | |
195 // The following classes provide a convenient way to delete all elements or | |
196 // values from STL containers when they goes out of scope. This greatly | |
197 // simplifies code that creates temporary objects and has multiple return | |
198 // statements. Example: | |
199 // | |
200 // vector<MyProto *> tmp_proto; | |
201 // STLElementDeleter<vector<MyProto *> > d(&tmp_proto); | |
202 // if (...) return false; | |
203 // ... | |
204 // return success; | |
205 | |
206 // Given a pointer to an STL container this class will delete all the element | |
207 // pointers when it goes out of scope. | |
208 | |
209 template<class STLContainer> class STLElementDeleter { | |
210 public: | |
211 STLElementDeleter<STLContainer>(STLContainer *ptr) : container_ptr_(ptr) {} | |
212 ~STLElementDeleter<STLContainer>() { STLDeleteElements(container_ptr_); } | |
213 private: | |
214 STLContainer *container_ptr_; | |
215 }; | |
216 | |
217 // Given a pointer to an STL container this class will delete all the value | |
218 // pointers when it goes out of scope. | |
219 | |
220 template<class STLContainer> class STLValueDeleter { | |
221 public: | |
222 STLValueDeleter<STLContainer>(STLContainer *ptr) : container_ptr_(ptr) {} | |
223 ~STLValueDeleter<STLContainer>() { STLDeleteValues(container_ptr_); } | |
224 private: | |
225 STLContainer *container_ptr_; | |
226 }; | |
227 | |
228 | |
229 // Translates a set into a vector. | |
230 template<typename T> | |
231 std::vector<T> SetToVector(const std::set<T>& values) { | |
brettw
2011/07/19 16:01:04
Can you remove this? It looks like nobody calls it
Denis Lagno
2011/07/19 18:31:00
it was used once in printing/page_range.cc
Remove
| |
232 std::vector<T> result; | |
233 result.reserve(values.size()); | |
234 result.insert(result.begin(), values.begin(), values.end()); | |
235 return result; | |
236 } | |
237 | |
238 // Test to see if a set, map, hash_set or hash_map contains a particular key. | |
239 // Returns true if the key is in the collection. | |
240 template <typename Collection, typename Key> | |
241 bool ContainsKey(const Collection& collection, const Key& key) { | |
242 return collection.find(key) != collection.end(); | |
243 } | |
244 | |
245 #endif // BASE_STL_UTIL_H_ | |
OLD | NEW |