blob: a4cf24986146d31bef34641c10443504ebac4861 [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
Andreas Gampebda1d602016-08-29 17:43:45 -070023// This header is used in the disassembler with libbase's logging. Only include ART logging
24// when no other logging macros are available. b/15436106, b/31338270
25#ifndef CHECK
Vladimir Marko637ee0b2015-09-04 12:47:41 +010026#include "base/logging.h"
Andreas Gampebda1d602016-08-29 17:43:45 -070027#endif
Vladimir Marko637ee0b2015-09-04 12:47:41 +010028
Carl Shapiro58551df2011-07-24 03:09:51 -070029namespace art {
30
Elliott Hughes08fc03a2012-06-26 17:34:00 -070031// Sort and remove duplicates of an STL vector or deque.
32template<class T>
33void STLSortAndRemoveDuplicates(T* v) {
34 std::sort(v->begin(), v->end());
35 v->erase(std::unique(v->begin(), v->end()), v->end());
36}
37
Carl Shapiro58551df2011-07-24 03:09:51 -070038// STLDeleteContainerPointers()
39// For a range within a container of pointers, calls delete
40// (non-array version) on these pointers.
41// NOTE: for these three functions, we could just implement a DeleteObject
42// functor and then call for_each() on the range and functor, but this
43// requires us to pull in all of algorithm.h, which seems expensive.
44// For hash_[multi]set, it is important that this deletes behind the iterator
45// because the hash_set may call the hash function on the iterator when it is
46// advanced, which could result in the hash function trying to deference a
47// stale pointer.
48template <class ForwardIterator>
49void STLDeleteContainerPointers(ForwardIterator begin,
50 ForwardIterator end) {
51 while (begin != end) {
52 ForwardIterator temp = begin;
53 ++begin;
54 delete *temp;
55 }
56}
57
58// STLDeleteElements() deletes all the elements in an STL container and clears
59// the container. This function is suitable for use with a vector, set,
60// hash_set, or any other STL container which defines sensible begin(), end(),
61// and clear() methods.
62//
Mathieu Chartier2cebb242015-04-21 16:50:40 -070063// If container is null, this function is a no-op.
Carl Shapiro58551df2011-07-24 03:09:51 -070064//
65// As an alternative to calling STLDeleteElements() directly, consider
Richard Uhler3c43f8d2015-01-15 10:27:22 -080066// using a container of std::unique_ptr, which ensures that your container's
67// elements are deleted when the container goes out of scope.
Carl Shapiro58551df2011-07-24 03:09:51 -070068template <class T>
69void STLDeleteElements(T *container) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070070 if (container != nullptr) {
71 STLDeleteContainerPointers(container->begin(), container->end());
72 container->clear();
73 }
Carl Shapiro58551df2011-07-24 03:09:51 -070074}
75
Elliott Hughesc31664f2011-09-29 15:58:28 -070076// Given an STL container consisting of (key, value) pairs, STLDeleteValues
77// deletes all the "value" components and clears the container. Does nothing
Mathieu Chartier2cebb242015-04-21 16:50:40 -070078// in the case it's given a null pointer.
Elliott Hughesc31664f2011-09-29 15:58:28 -070079template <class T>
80void STLDeleteValues(T *v) {
Mathieu Chartier2cebb242015-04-21 16:50:40 -070081 if (v != nullptr) {
82 for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
83 delete i->second;
84 }
85 v->clear();
Elliott Hughesc31664f2011-09-29 15:58:28 -070086 }
Elliott Hughesc31664f2011-09-29 15:58:28 -070087}
88
Elliott Hughes14134a12011-09-30 16:55:51 -070089template <class T>
90std::string ToString(const T& v) {
Elliott Hughes3b6baaa2011-10-14 19:13:56 -070091 std::ostringstream os;
Elliott Hughes14134a12011-09-30 16:55:51 -070092 os << "[";
93 for (size_t i = 0; i < v.size(); ++i) {
94 os << v[i];
95 if (i < v.size() - 1) {
96 os << ", ";
97 }
98 }
99 os << "]";
100 return os.str();
101}
102
Vladimir Marko637ee0b2015-09-04 12:47:41 +0100103// Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below.
104struct FreeDelete {
105 // NOTE: Deleting a const object is valid but free() takes a non-const pointer.
106 void operator()(const void* ptr) const {
107 free(const_cast<void*>(ptr));
108 }
109};
110
111// Alias for std::unique_ptr<> that uses the C function free() to delete objects.
112template <typename T>
113using UniqueCPtr = std::unique_ptr<T, FreeDelete>;
114
115// C++14 from-the-future import (std::make_unique)
116// Invoke the constructor of 'T' with the provided args, and wrap the result in a unique ptr.
117template <typename T, typename ... Args>
118std::unique_ptr<T> MakeUnique(Args&& ... args) {
119 return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
120}
121
122// Find index of the first element with the specified value known to be in the container.
123template <typename Container, typename T>
124size_t IndexOfElement(const Container& container, const T& value) {
125 auto it = std::find(container.begin(), container.end(), value);
126 DCHECK(it != container.end()); // Must exist.
127 return std::distance(container.begin(), it);
128}
129
130// Remove the first element with the specified value known to be in the container.
131template <typename Container, typename T>
132void RemoveElement(Container& container, const T& value) {
133 auto it = std::find(container.begin(), container.end(), value);
134 DCHECK(it != container.end()); // Must exist.
135 container.erase(it);
136}
137
138// Replace the first element with the specified old_value known to be in the container.
139template <typename Container, typename T>
140void ReplaceElement(Container& container, const T& old_value, const T& new_value) {
141 auto it = std::find(container.begin(), container.end(), old_value);
142 DCHECK(it != container.end()); // Must exist.
143 *it = new_value;
144}
145
146// Search for an element with the specified value and return true if it was found, false otherwise.
147template <typename Container, typename T>
148bool ContainsElement(const Container& container, const T& value, size_t start_pos = 0u) {
149 DCHECK_LE(start_pos, container.size());
150 auto start = container.begin();
151 std::advance(start, start_pos);
152 auto it = std::find(start, container.end(), value);
153 return it != container.end();
154}
155
David Srbecky04b05262015-11-09 18:05:48 +0000156// const char* compare function suitable for std::map or std::set.
157struct CStringLess {
158 bool operator()(const char* lhs, const char* rhs) const {
159 return strcmp(lhs, rhs) < 0;
160 }
161};
162
David Srbecky24868a12016-01-29 18:59:56 +0000163// 32-bit FNV-1a hash function suitable for std::unordered_map.
164// It can be used with any container which works with range-based for loop.
165// See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
166template <typename Vector>
167struct FNVHash {
168 size_t operator()(const Vector& vector) const {
169 uint32_t hash = 2166136261u;
170 for (const auto& value : vector) {
171 hash = (hash ^ value) * 16777619u;
172 }
173 return hash;
174 }
175};
176
Vladimir Marko88b2b802015-12-04 14:19:04 +0000177// Use to suppress type deduction for a function argument.
178// See std::identity<> for more background:
179// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2005/n1856.html#20.2.2 - move/forward helpers
180//
181// e.g. "template <typename X> void bar(identity<X>::type foo);
182// bar(5); // compilation error
183// bar<int>(5); // ok
184// or "template <typename T> void foo(T* x, typename Identity<T*>::type y);
185// Base b;
186// Derived d;
187// foo(&b, &d); // Use implicit Derived* -> Base* conversion.
188// If T was deduced from both &b and &d, there would be a mismatch, i.e. deduction failure.
189template <typename T>
190struct Identity {
191 using type = T;
192};
193
Carl Shapiro58551df2011-07-24 03:09:51 -0700194} // namespace art
195
Brian Carlstromfc0e3212013-07-17 14:40:12 -0700196#endif // ART_RUNTIME_BASE_STL_UTIL_H_