Elliott Hughes | 2faa5f1 | 2012-01-30 14:42:07 -0800 | [diff] [blame] | 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 | */ |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 16 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 17 | #ifndef ART_SRC_BASE_STL_UTIL_H_ |
| 18 | #define ART_SRC_BASE_STL_UTIL_H_ |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 19 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 20 | #include <algorithm> |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 21 | #include <sstream> |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 22 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 23 | namespace art { |
| 24 | |
Elliott Hughes | 08fc03a | 2012-06-26 17:34:00 -0700 | [diff] [blame] | 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 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | c31664f | 2011-09-29 15:58:28 -0700 | [diff] [blame] | 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 | |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 81 | template <class T> |
| 82 | std::string ToString(const T& v) { |
Elliott Hughes | 3b6baaa | 2011-10-14 19:13:56 -0700 | [diff] [blame] | 83 | std::ostringstream os; |
Elliott Hughes | 14134a1 | 2011-09-30 16:55:51 -0700 | [diff] [blame] | 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 | |
Carl Shapiro | 58551df | 2011-07-24 03:09:51 -0700 | [diff] [blame] | 95 | } // namespace art |
| 96 | |
Elliott Hughes | 1aa246d | 2012-12-13 09:29:36 -0800 | [diff] [blame] | 97 | #endif // ART_SRC_BASE_STL_UTIL_H_ |