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

Side by Side Diff: base/stl_util.h

Issue 2457343002: Remove stl_util's STLDeleteContainerPointers. (Closed)
Patch Set: for Created 4 years, 1 month 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
« no previous file with comments | « base/containers/scoped_ptr_hash_map.h ('k') | 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 9
10 #include <algorithm> 10 #include <algorithm>
(...skipping 11 matching lines...) Expand all
22 // 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.
23 template<class T> 23 template<class T>
24 void STLClearObject(T* obj) { 24 void STLClearObject(T* obj) {
25 T tmp; 25 T tmp;
26 tmp.swap(*obj); 26 tmp.swap(*obj);
27 // Sometimes "T tmp" allocates objects with memory (arena implementation?). 27 // Sometimes "T tmp" allocates objects with memory (arena implementation?).
28 // Hence using additional reserve(0) even if it doesn't always work. 28 // Hence using additional reserve(0) even if it doesn't always work.
29 obj->reserve(0); 29 obj->reserve(0);
30 } 30 }
31 31
32 // For a range within a container of pointers, calls delete (non-array version)
33 // on these pointers.
34 // NOTE: for these three functions, we could just implement a DeleteObject
35 // functor and then call for_each() on the range and functor, but this
36 // requires us to pull in all of algorithm.h, which seems expensive.
37 // For hash_[multi]set, it is important that this deletes behind the iterator
38 // because the hash_set may call the hash function on the iterator when it is
39 // advanced, which could result in the hash function trying to deference a
40 // stale pointer.
41 template <class ForwardIterator>
42 void STLDeleteContainerPointers(ForwardIterator begin, ForwardIterator end) {
43 while (begin != end) {
44 ForwardIterator temp = begin;
45 ++begin;
46 delete *temp;
47 }
48 }
49
50 // Counts the number of instances of val in a container. 32 // Counts the number of instances of val in a container.
51 template <typename Container, typename T> 33 template <typename Container, typename T>
52 typename std::iterator_traits< 34 typename std::iterator_traits<
53 typename Container::const_iterator>::difference_type 35 typename Container::const_iterator>::difference_type
54 STLCount(const Container& container, const T& val) { 36 STLCount(const Container& container, const T& val) {
55 return std::count(container.begin(), container.end(), val); 37 return std::count(container.begin(), container.end(), val);
56 } 38 }
57 39
58 // Return a mutable char* pointing to a string's internal buffer, 40 // Return a mutable char* pointing to a string's internal buffer,
59 // which may not be null-terminated. Writing through this pointer will 41 // which may not be null-terminated. Writing through this pointer will
(...skipping 18 matching lines...) Expand all
78 // STLDeleteElements() deletes all the elements in an STL container and clears 60 // STLDeleteElements() deletes all the elements in an STL container and clears
79 // the container. This function is suitable for use with a vector, set, 61 // the container. This function is suitable for use with a vector, set,
80 // hash_set, or any other STL container which defines sensible begin(), end(), 62 // hash_set, or any other STL container which defines sensible begin(), end(),
81 // and clear() methods. 63 // and clear() methods.
82 // 64 //
83 // If container is NULL, this function is a no-op. 65 // If container is NULL, this function is a no-op.
84 template <class T> 66 template <class T>
85 void STLDeleteElements(T* container) { 67 void STLDeleteElements(T* container) {
86 if (!container) 68 if (!container)
87 return; 69 return;
88 STLDeleteContainerPointers(container->begin(), container->end()); 70
71 for (auto it = container->begin(); it != container->end();) {
72 auto temp = it;
73 ++it;
74 delete *temp;
75 }
76
89 container->clear(); 77 container->clear();
90 } 78 }
91 79
92 // Given an STL container consisting of (key, value) pairs, STLDeleteValues 80 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
93 // deletes all the "value" components and clears the container. Does nothing 81 // deletes all the "value" components and clears the container. Does nothing
94 // in the case it's given a NULL pointer. 82 // in the case it's given a NULL pointer.
95 template <class T> 83 template <class T>
96 void STLDeleteValues(T* container) { 84 void STLDeleteValues(T* container) {
97 if (!container) 85 if (!container)
98 return; 86 return;
99 87
100 auto it = container->begin(); 88 for (auto it = container->begin(); it != container->end();) {
101 while (it != container->end()) {
102 auto temp = it; 89 auto temp = it;
103 ++it; 90 ++it;
104 delete temp->second; 91 delete temp->second;
105 } 92 }
106 93
107 container->clear(); 94 container->clear();
108 } 95 }
109 96
110 // Test to see if a set, map, hash_set or hash_map contains a particular key. 97 // Test to see if a set, map, hash_set or hash_map contains a particular key.
111 // Returns true if the key is in the collection. 98 // Returns true if the key is in the collection.
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
175 bool STLIncludes(const Arg1& a1, const Arg2& a2) { 162 bool STLIncludes(const Arg1& a1, const Arg2& a2) {
176 DCHECK(STLIsSorted(a1)); 163 DCHECK(STLIsSorted(a1));
177 DCHECK(STLIsSorted(a2)); 164 DCHECK(STLIsSorted(a2));
178 return std::includes(a1.begin(), a1.end(), 165 return std::includes(a1.begin(), a1.end(),
179 a2.begin(), a2.end()); 166 a2.begin(), a2.end());
180 } 167 }
181 168
182 } // namespace base 169 } // namespace base
183 170
184 #endif // BASE_STL_UTIL_H_ 171 #endif // BASE_STL_UTIL_H_
OLDNEW
« no previous file with comments | « base/containers/scoped_ptr_hash_map.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698