12faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes/*
22faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Copyright (C) 2011 The Android Open Source Project
32faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
42faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
52faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * you may not use this file except in compliance with the License.
62faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * You may obtain a copy of the License at
72faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
82faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
92faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes *
102faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * Unless required by applicable law or agreed to in writing, software
112faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
122faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
132faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * See the License for the specific language governing permissions and
142faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes * limitations under the License.
152faa5f1271587cda765f26bcf2951065300a01ffElliott Hughes */
1658551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro
17fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#ifndef ART_RUNTIME_BASE_STL_UTIL_H_
18fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#define ART_RUNTIME_BASE_STL_UTIL_H_
1958551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro
2008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes#include <algorithm>
211aa246dec5abe212f699de1413a0c4a191ca364aElliott Hughes#include <sstream>
2214134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes
2358551df3c2646ed507feec9e9eb3768085a76059Carl Shapironamespace art {
2458551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro
2508fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes// Sort and remove duplicates of an STL vector or deque.
2608fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughestemplate<class T>
2708fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughesvoid STLSortAndRemoveDuplicates(T* v) {
2808fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  std::sort(v->begin(), v->end());
2908fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes  v->erase(std::unique(v->begin(), v->end()), v->end());
3008fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes}
3108fc03ae5dded4adc9b45b7014a4b9dfedbe95a6Elliott Hughes
3258551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// STLDeleteContainerPointers()
3358551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro//  For a range within a container of pointers, calls delete
3458551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro//  (non-array version) on these pointers.
3558551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// NOTE: for these three functions, we could just implement a DeleteObject
3658551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// functor and then call for_each() on the range and functor, but this
3758551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// requires us to pull in all of algorithm.h, which seems expensive.
3858551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// For hash_[multi]set, it is important that this deletes behind the iterator
3958551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// because the hash_set may call the hash function on the iterator when it is
4058551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// advanced, which could result in the hash function trying to deference a
4158551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// stale pointer.
4258551df3c2646ed507feec9e9eb3768085a76059Carl Shapirotemplate <class ForwardIterator>
4358551df3c2646ed507feec9e9eb3768085a76059Carl Shapirovoid STLDeleteContainerPointers(ForwardIterator begin,
4458551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro                                ForwardIterator end) {
4558551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro  while (begin != end) {
4658551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro    ForwardIterator temp = begin;
4758551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro    ++begin;
4858551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro    delete *temp;
4958551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro  }
5058551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro}
5158551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro
5258551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// STLDeleteElements() deletes all the elements in an STL container and clears
5358551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// the container.  This function is suitable for use with a vector, set,
5458551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// hash_set, or any other STL container which defines sensible begin(), end(),
5558551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// and clear() methods.
5658551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro//
5758551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// If container is NULL, this function is a no-op.
5858551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro//
5958551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// As an alternative to calling STLDeleteElements() directly, consider
6058551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// ElementDeleter (defined below), which ensures that your container's elements
6158551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro// are deleted when the ElementDeleter goes out of scope.
6258551df3c2646ed507feec9e9eb3768085a76059Carl Shapirotemplate <class T>
6358551df3c2646ed507feec9e9eb3768085a76059Carl Shapirovoid STLDeleteElements(T *container) {
6458551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro  if (!container) return;
6558551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro  STLDeleteContainerPointers(container->begin(), container->end());
6658551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro  container->clear();
6758551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro}
6858551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro
69c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes// Given an STL container consisting of (key, value) pairs, STLDeleteValues
70c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes// deletes all the "value" components and clears the container.  Does nothing
71c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes// in the case it's given a NULL pointer.
72c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughestemplate <class T>
73c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughesvoid STLDeleteValues(T *v) {
74c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes  if (!v) return;
75c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes  for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
76c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes    delete i->second;
77c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes  }
78c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes  v->clear();
79c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes}
80c31664f3d82e6cd68275a529a8a73f067a52e8beElliott Hughes
8114134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughestemplate <class T>
8214134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughesstd::string ToString(const T& v) {
833b6baaa203fa63f1522b2172a1645f90412afdaeElliott Hughes  std::ostringstream os;
8414134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes  os << "[";
8514134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes  for (size_t i = 0; i < v.size(); ++i) {
8614134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes    os << v[i];
8714134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes    if (i < v.size() - 1) {
8814134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes      os << ", ";
8914134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes    }
9014134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes  }
9114134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes  os << "]";
9214134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes  return os.str();
9314134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes}
9414134a10e9bbaff0faf314dc00c1a1aeef8ef86bElliott Hughes
9558551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro}  // namespace art
9658551df3c2646ed507feec9e9eb3768085a76059Carl Shapiro
97fc0e3219edc9a5bf81b166e82fd5db2796eb6a0dBrian Carlstrom#endif  // ART_RUNTIME_BASE_STL_UTIL_H_
98