1 /*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #ifndef ART_RUNTIME_BASE_STL_UTIL_H_
18 #define ART_RUNTIME_BASE_STL_UTIL_H_
19
20 #include <algorithm>
21 #include <sstream>
22
23 namespace art {
24
25 // Sort and remove duplicates of an STL vector or deque.
26 template<class T>
STLSortAndRemoveDuplicates(T * v)27 void STLSortAndRemoveDuplicates(T* v) {
28 std::sort(v->begin(), v->end());
29 v->erase(std::unique(v->begin(), v->end()), v->end());
30 }
31
32 // STLDeleteContainerPointers()
33 // For a range within a container of pointers, calls delete
34 // (non-array version) on these pointers.
35 // NOTE: for these three functions, we could just implement a DeleteObject
36 // functor and then call for_each() on the range and functor, but this
37 // requires us to pull in all of algorithm.h, which seems expensive.
38 // For hash_[multi]set, it is important that this deletes behind the iterator
39 // because the hash_set may call the hash function on the iterator when it is
40 // advanced, which could result in the hash function trying to deference a
41 // stale pointer.
42 template <class ForwardIterator>
STLDeleteContainerPointers(ForwardIterator begin,ForwardIterator end)43 void STLDeleteContainerPointers(ForwardIterator begin,
44 ForwardIterator end) {
45 while (begin != end) {
46 ForwardIterator temp = begin;
47 ++begin;
48 delete *temp;
49 }
50 }
51
52 // STLDeleteElements() deletes all the elements in an STL container and clears
53 // the container. This function is suitable for use with a vector, set,
54 // hash_set, or any other STL container which defines sensible begin(), end(),
55 // and clear() methods.
56 //
57 // If container is null, this function is a no-op.
58 //
59 // As an alternative to calling STLDeleteElements() directly, consider
60 // using a container of std::unique_ptr, which ensures that your container's
61 // elements are deleted when the container goes out of scope.
62 template <class T>
STLDeleteElements(T * container)63 void STLDeleteElements(T *container) {
64 if (container != nullptr) {
65 STLDeleteContainerPointers(container->begin(), container->end());
66 container->clear();
67 }
68 }
69
70 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
71 // deletes all the "value" components and clears the container. Does nothing
72 // in the case it's given a null pointer.
73 template <class T>
STLDeleteValues(T * v)74 void STLDeleteValues(T *v) {
75 if (v != nullptr) {
76 for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
77 delete i->second;
78 }
79 v->clear();
80 }
81 }
82
83 template <class T>
ToString(const T & v)84 std::string ToString(const T& v) {
85 std::ostringstream os;
86 os << "[";
87 for (size_t i = 0; i < v.size(); ++i) {
88 os << v[i];
89 if (i < v.size() - 1) {
90 os << ", ";
91 }
92 }
93 os << "]";
94 return os.str();
95 }
96
97 } // namespace art
98
99 #endif // ART_RUNTIME_BASE_STL_UTIL_H_
100