blob: 3c5565cdc00fdfc49e77e7e3d904b1aa56eb9f7f [file] [log] [blame]
Elliott Hughes2faa5f12012-01-30 14:42:07 -08001/*
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 */
Carl Shapiro58551df2011-07-24 03:09:51 -070016
Brian Carlstromfc0e3212013-07-17 14:40:12 -070017#ifndef ART_RUNTIME_BASE_STL_UTIL_H_
18#define ART_RUNTIME_BASE_STL_UTIL_H_
Carl Shapiro58551df2011-07-24 03:09:51 -070019
Elliott Hughes08fc03a2012-06-26 17:34:00 -070020#include <algorithm>
Elliott Hughes1aa246d2012-12-13 09:29:36 -080021#include <sstream>
Elliott Hughes14134a12011-09-30 16:55:51 -070022
Carl Shapiro58551df2011-07-24 03:09:51 -070023namespace art {
24
Elliott Hughes08fc03a2012-06-26 17:34:00 -070025// Sort and remove duplicates of an STL vector or deque.
26template<class T>
27void STLSortAndRemoveDuplicates(T* v) {
28 std::sort(v->begin(), v->end());
29 v->erase(std::unique(v->begin(), v->end()), v->end());
30}
31
Carl Shapiro58551df2011-07-24 03:09:51 -070032// 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.
42template <class ForwardIterator>
43void 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
Richard Uhler3c43f8d2015-01-15 10:27:22 -080060// using a container of std::unique_ptr, which ensures that your container's
61// elements are deleted when the container goes out of scope.
Carl Shapiro58551df2011-07-24 03:09:51 -070062template <class T>
63void STLDeleteElements(T *container) {
64 if (!container) return;
65 STLDeleteContainerPointers(container->begin(), container->end());
66 container->clear();
67}
68
Elliott Hughesc31664f2011-09-29 15:58:28 -070069// Given an STL container consisting of (key, value) pairs, STLDeleteValues
70// deletes all the "value" components and clears the container. Does nothing
71// in the case it's given a NULL pointer.
72template <class T>
73void STLDeleteValues(T *v) {
74 if (!v) return;
75 for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
76 delete i->second;
77 }
78 v->clear();
79}
80
Elliott Hughes14134a12011-09-30 16:55:51 -070081template <class T>
82std::string ToString(const T& v) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070083 std::ostringstream os;
Elliott Hughes14134a12011-09-30 16:55:51 -070084 os << "[";
85 for (size_t i = 0; i < v.size(); ++i) {
86 os << v[i];
87 if (i < v.size() - 1) {
88 os << ", ";
89 }
90 }
91 os << "]";
92 return os.str();
93}
94
Carl Shapiro58551df2011-07-24 03:09:51 -070095} // namespace art
96
Brian Carlstromfc0e3212013-07-17 14:40:12 -070097#endif // ART_RUNTIME_BASE_STL_UTIL_H_