Home | History | Annotate | Download | only in base
      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>
     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>
     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 // ElementDeleter (defined below), which ensures that your container's elements
     61 // are deleted when the ElementDeleter goes out of scope.
     62 template <class T>
     63 void STLDeleteElements(T *container) {
     64   if (!container) return;
     65   STLDeleteContainerPointers(container->begin(), container->end());
     66   container->clear();
     67 }
     68 
     69 // 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.
     72 template <class T>
     73 void 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 
     81 template <class T>
     82 std::string ToString(const T& v) {
     83   std::ostringstream os;
     84   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 
     95 }  // namespace art
     96 
     97 #endif  // ART_RUNTIME_BASE_STL_UTIL_H_
     98