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.
5 // Derived from google3/util/gtl/stl_util.h
7 #ifndef BASE_STL_UTIL_H_
8 #define BASE_STL_UTIL_H_
16 #include "base/logging.h"
18 // Clears 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.
22 void STLClearObject(T
* obj
) {
25 // Sometimes "T tmp" allocates objects with memory (arena implementation?).
26 // Hence using additional reserve(0) even if it doesn't always work.
30 // For a range within a container of pointers, calls delete (non-array version)
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
39 template <class ForwardIterator
>
40 void STLDeleteContainerPointers(ForwardIterator begin
, ForwardIterator end
) {
41 while (begin
!= end
) {
42 ForwardIterator temp
= begin
;
48 // For a range within a container of pairs, calls delete (non-array version) on
49 // BOTH items in the pairs.
50 // NOTE: Like STLDeleteContainerPointers, it is important that this deletes
51 // behind the iterator because if both the key and value are deleted, the
52 // container may call the hash function on the iterator when it is advanced,
53 // which could result in the hash function trying to dereference a stale
55 template <class ForwardIterator
>
56 void STLDeleteContainerPairPointers(ForwardIterator begin
,
57 ForwardIterator end
) {
58 while (begin
!= end
) {
59 ForwardIterator temp
= begin
;
66 // For a range within a container of pairs, calls delete (non-array version) on
67 // the FIRST item in the pairs.
68 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
69 template <class ForwardIterator
>
70 void STLDeleteContainerPairFirstPointers(ForwardIterator begin
,
71 ForwardIterator end
) {
72 while (begin
!= end
) {
73 ForwardIterator temp
= begin
;
79 // For a range within a container of pairs, calls delete.
80 // NOTE: Like STLDeleteContainerPointers, deleting behind the iterator.
81 // Deleting the value does not always invalidate the iterator, but it may
82 // do so if the key is a pointer into the value object.
83 template <class ForwardIterator
>
84 void STLDeleteContainerPairSecondPointers(ForwardIterator begin
,
85 ForwardIterator end
) {
86 while (begin
!= end
) {
87 ForwardIterator temp
= begin
;
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.
97 inline T
* vector_as_array(std::vector
<T
>* v
) {
98 return v
->empty() ? NULL
: &*v
->begin();
102 inline const T
* vector_as_array(const std::vector
<T
>* v
) {
103 return v
->empty() ? NULL
: &*v
->begin();
106 // Return a mutable char* pointing to a string's internal buffer,
107 // which may not be null-terminated. Writing through this pointer will
108 // modify the string.
110 // string_as_array(&str)[i] is valid for 0 <= i < str.size() until the
111 // next call to a string method that invalidates iterators.
113 // As of 2006-04, there is no standard-blessed way of getting a
114 // mutable reference to a string's internal buffer. However, issue 530
115 // (http://www.open-std.org/JTC1/SC22/WG21/docs/lwg-active.html#530)
116 // proposes this as the method. According to Matt Austern, this should
117 // already work on all current implementations.
118 inline char* string_as_array(std::string
* str
) {
119 // DO NOT USE const_cast<char*>(str->data())
120 return str
->empty() ? NULL
: &*str
->begin();
123 // The following functions are useful for cleaning up STL containers whose
124 // elements point to allocated memory.
126 // STLDeleteElements() deletes all the elements in an STL container and clears
127 // the container. This function is suitable for use with a vector, set,
128 // hash_set, or any other STL container which defines sensible begin(), end(),
129 // and clear() methods.
131 // If container is NULL, this function is a no-op.
133 // As an alternative to calling STLDeleteElements() directly, consider
134 // STLElementDeleter (defined below), which ensures that your container's
135 // elements are deleted when the STLElementDeleter goes out of scope.
137 void STLDeleteElements(T
* container
) {
140 STLDeleteContainerPointers(container
->begin(), container
->end());
144 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
145 // deletes all the "value" components and clears the container. Does nothing
146 // in the case it's given a NULL pointer.
148 void STLDeleteValues(T
* container
) {
151 STLDeleteContainerPairSecondPointers(container
->begin(), container
->end());
156 // The following classes provide a convenient way to delete all elements or
157 // values from STL containers when they goes out of scope. This greatly
158 // simplifies code that creates temporary objects and has multiple return
159 // statements. Example:
161 // vector<MyProto *> tmp_proto;
162 // STLElementDeleter<vector<MyProto *> > d(&tmp_proto);
163 // if (...) return false;
167 // Given a pointer to an STL container this class will delete all the element
168 // pointers when it goes out of scope.
170 class STLElementDeleter
{
172 STLElementDeleter
<T
>(T
* container
) : container_(container
) {}
173 ~STLElementDeleter
<T
>() { STLDeleteElements(container_
); }
179 // Given a pointer to an STL container this class will delete all the value
180 // pointers when it goes out of scope.
182 class STLValueDeleter
{
184 STLValueDeleter
<T
>(T
* container
) : container_(container
) {}
185 ~STLValueDeleter
<T
>() { STLDeleteValues(container_
); }
191 // 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.
193 template <typename Collection
, typename Key
>
194 bool ContainsKey(const Collection
& collection
, const Key
& key
) {
195 return collection
.find(key
) != collection
.end();
200 // Returns true if the container is sorted.
201 template <typename Container
>
202 bool STLIsSorted(const Container
& cont
) {
203 // Note: Use reverse iterator on container to ensure we only require
204 // value_type to implement operator<.
205 return std::adjacent_find(cont
.rbegin(), cont
.rend(),
206 std::less
<typename
Container::value_type
>())
210 // Returns a new ResultType containing the difference of two sorted containers.
211 template <typename ResultType
, typename Arg1
, typename Arg2
>
212 ResultType
STLSetDifference(const Arg1
& a1
, const Arg2
& a2
) {
213 DCHECK(STLIsSorted(a1
));
214 DCHECK(STLIsSorted(a2
));
215 ResultType difference
;
216 std::set_difference(a1
.begin(), a1
.end(),
217 a2
.begin(), a2
.end(),
218 std::inserter(difference
, difference
.end()));
222 // Returns a new ResultType containing the union of two sorted containers.
223 template <typename ResultType
, typename Arg1
, typename Arg2
>
224 ResultType
STLSetUnion(const Arg1
& a1
, const Arg2
& a2
) {
225 DCHECK(STLIsSorted(a1
));
226 DCHECK(STLIsSorted(a2
));
228 std::set_union(a1
.begin(), a1
.end(),
229 a2
.begin(), a2
.end(),
230 std::inserter(result
, result
.end()));
234 // Returns a new ResultType containing the intersection of two sorted
236 template <typename ResultType
, typename Arg1
, typename Arg2
>
237 ResultType
STLSetIntersection(const Arg1
& a1
, const Arg2
& a2
) {
238 DCHECK(STLIsSorted(a1
));
239 DCHECK(STLIsSorted(a2
));
241 std::set_intersection(a1
.begin(), a1
.end(),
242 a2
.begin(), a2
.end(),
243 std::inserter(result
, result
.end()));
247 // Returns true if the sorted container |a1| contains all elements of the sorted
249 template <typename Arg1
, typename Arg2
>
250 bool STLIncludes(const Arg1
& a1
, const Arg2
& a2
) {
251 DCHECK(STLIsSorted(a1
));
252 DCHECK(STLIsSorted(a2
));
253 return std::includes(a1
.begin(), a1
.end(),
254 a2
.begin(), a2
.end());
259 #endif // BASE_STL_UTIL_H_