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_LIBARTBASE_BASE_STL_UTIL_H_
18 #define ART_LIBARTBASE_BASE_STL_UTIL_H_
19 
20 #include <algorithm>
21 #include <sstream>
22 
23 #include <android-base/logging.h>
24 
25 namespace art {
26 
27 // STLDeleteContainerPointers()
28 //  For a range within a container of pointers, calls delete
29 //  (non-array version) on these pointers.
30 // NOTE: for these three functions, we could just implement a DeleteObject
31 // functor and then call for_each() on the range and functor, but this
32 // requires us to pull in all of algorithm.h, which seems expensive.
33 // For hash_[multi]set, it is important that this deletes behind the iterator
34 // because the hash_set may call the hash function on the iterator when it is
35 // advanced, which could result in the hash function trying to deference a
36 // stale pointer.
37 template <class ForwardIterator>
STLDeleteContainerPointers(ForwardIterator begin,ForwardIterator end)38 void STLDeleteContainerPointers(ForwardIterator begin,
39                                 ForwardIterator end) {
40   while (begin != end) {
41     ForwardIterator temp = begin;
42     ++begin;
43     delete *temp;
44   }
45 }
46 
47 // STLDeleteElements() deletes all the elements in an STL container and clears
48 // the container.  This function is suitable for use with a vector, set,
49 // hash_set, or any other STL container which defines sensible begin(), end(),
50 // and clear() methods.
51 //
52 // If container is null, this function is a no-op.
53 //
54 // As an alternative to calling STLDeleteElements() directly, consider
55 // using a container of std::unique_ptr, which ensures that your container's
56 // elements are deleted when the container goes out of scope.
57 template <class T>
STLDeleteElements(T * container)58 void STLDeleteElements(T *container) {
59   if (container != nullptr) {
60     STLDeleteContainerPointers(container->begin(), container->end());
61     container->clear();
62   }
63 }
64 
65 // Given an STL container consisting of (key, value) pairs, STLDeleteValues
66 // deletes all the "value" components and clears the container.  Does nothing
67 // in the case it's given a null pointer.
68 template <class T>
STLDeleteValues(T * v)69 void STLDeleteValues(T *v) {
70   if (v != nullptr) {
71     for (typename T::iterator i = v->begin(); i != v->end(); ++i) {
72       delete i->second;
73     }
74     v->clear();
75   }
76 }
77 
78 // Deleter using free() for use with std::unique_ptr<>. See also UniqueCPtr<> below.
79 struct FreeDelete {
80   // NOTE: Deleting a const object is valid but free() takes a non-const pointer.
operatorFreeDelete81   void operator()(const void* ptr) const {
82     free(const_cast<void*>(ptr));
83   }
84 };
85 
86 // Alias for std::unique_ptr<> that uses the C function free() to delete objects.
87 template <typename T>
88 using UniqueCPtr = std::unique_ptr<T, FreeDelete>;
89 
90 // Find index of the first element with the specified value known to be in the container.
91 template <typename Container, typename T>
IndexOfElement(const Container & container,const T & value)92 size_t IndexOfElement(const Container& container, const T& value) {
93   auto it = std::find(container.begin(), container.end(), value);
94   DCHECK(it != container.end());  // Must exist.
95   return std::distance(container.begin(), it);
96 }
97 
98 // Remove the first element with the specified value known to be in the container.
99 template <typename Container, typename T>
RemoveElement(Container & container,const T & value)100 void RemoveElement(Container& container, const T& value) {
101   auto it = std::find(container.begin(), container.end(), value);
102   DCHECK(it != container.end());  // Must exist.
103   container.erase(it);
104 }
105 
106 // Replace the first element with the specified old_value known to be in the container.
107 template <typename Container, typename T>
ReplaceElement(Container & container,const T & old_value,const T & new_value)108 void ReplaceElement(Container& container, const T& old_value, const T& new_value) {
109   auto it = std::find(container.begin(), container.end(), old_value);
110   DCHECK(it != container.end());  // Must exist.
111   *it = new_value;
112 }
113 
114 // Search for an element with the specified value and return true if it was found, false otherwise.
115 template <typename Container, typename T>
116 bool ContainsElement(const Container& container, const T& value, size_t start_pos = 0u) {
117   DCHECK_LE(start_pos, container.size());
118   auto start = container.begin();
119   std::advance(start, start_pos);
120   auto it = std::find(start, container.end(), value);
121   return it != container.end();
122 }
123 
124 // 32-bit FNV-1a hash function suitable for std::unordered_map.
125 // It can be used with any container which works with range-based for loop.
126 // See http://en.wikipedia.org/wiki/Fowler%E2%80%93Noll%E2%80%93Vo_hash_function
127 template <typename Vector>
128 struct FNVHash {
operatorFNVHash129   size_t operator()(const Vector& vector) const {
130     uint32_t hash = 2166136261u;
131     for (const auto& value : vector) {
132       hash = (hash ^ value) * 16777619u;
133     }
134     return hash;
135   }
136 };
137 
138 // Returns a copy of the passed vector that doesn't memory-own its entries.
139 template <typename T>
MakeNonOwningPointerVector(const std::vector<std::unique_ptr<T>> & src)140 static inline std::vector<T*> MakeNonOwningPointerVector(const std::vector<std::unique_ptr<T>>& src) {
141   std::vector<T*> result;
142   result.reserve(src.size());
143   for (const std::unique_ptr<T>& t : src) {
144     result.push_back(t.get());
145   }
146   return result;
147 }
148 
149 }  // namespace art
150 
151 #endif  // ART_LIBARTBASE_BASE_STL_UTIL_H_
152