• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2020 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 #pragma once
18 
19 #include <ftl/array_traits.h>
20 #include <ftl/static_vector.h>
21 
22 #include <algorithm>
23 #include <iterator>
24 #include <type_traits>
25 #include <utility>
26 #include <variant>
27 #include <vector>
28 
29 namespace android::ftl {
30 
31 template <typename>
32 struct is_small_vector;
33 
34 // ftl::StaticVector that promotes to std::vector when full. SmallVector is a drop-in replacement
35 // for std::vector with statically allocated storage for N elements, whose goal is to improve run
36 // time by avoiding heap allocation and increasing probability of cache hits. The standard API is
37 // augmented by an unstable_erase operation that does not preserve order, and a replace operation
38 // that destructively emplaces.
39 //
40 // SmallVector<T, 0> is a specialization that thinly wraps std::vector.
41 //
42 // Example usage:
43 //
44 //   ftl::SmallVector<char, 3> vector;
45 //   assert(vector.empty());
46 //   assert(!vector.dynamic());
47 //
48 //   vector = {'a', 'b', 'c'};
49 //   assert(vector.size() == 3u);
50 //   assert(!vector.dynamic());
51 //
52 //   vector.push_back('d');
53 //   assert(vector.dynamic());
54 //
55 //   vector.unstable_erase(vector.begin());
56 //   assert(vector == (ftl::SmallVector{'d', 'b', 'c'}));
57 //
58 //   vector.pop_back();
59 //   assert(vector.back() == 'b');
60 //   assert(vector.dynamic());
61 //
62 //   const char array[] = "hi";
63 //   vector = ftl::SmallVector(array);
64 //   assert(vector == (ftl::SmallVector{'h', 'i', '\0'}));
65 //   assert(!vector.dynamic());
66 //
67 //   ftl::SmallVector strings = ftl::init::list<std::string>("abc")("123456", 3u)(3u, '?');
68 //   assert(strings.size() == 3u);
69 //   assert(!strings.dynamic());
70 //
71 //   assert(strings[0] == "abc");
72 //   assert(strings[1] == "123");
73 //   assert(strings[2] == "???");
74 //
75 template <typename T, std::size_t N>
76 class SmallVector final : ArrayTraits<T>, ArrayComparators<SmallVector> {
77   using Static = StaticVector<T, N>;
78   using Dynamic = SmallVector<T, 0>;
79 
80   // TODO: Replace with std::remove_cvref_t in C++20.
81   template <typename U>
82   using remove_cvref_t = std::remove_cv_t<std::remove_reference_t<U>>;
83 
84  public:
85   FTL_ARRAY_TRAIT(T, value_type);
86   FTL_ARRAY_TRAIT(T, size_type);
87   FTL_ARRAY_TRAIT(T, difference_type);
88 
89   FTL_ARRAY_TRAIT(T, pointer);
90   FTL_ARRAY_TRAIT(T, reference);
91   FTL_ARRAY_TRAIT(T, iterator);
92   FTL_ARRAY_TRAIT(T, reverse_iterator);
93 
94   FTL_ARRAY_TRAIT(T, const_pointer);
95   FTL_ARRAY_TRAIT(T, const_reference);
96   FTL_ARRAY_TRAIT(T, const_iterator);
97   FTL_ARRAY_TRAIT(T, const_reverse_iterator);
98 
99   // Creates an empty vector.
100   SmallVector() = default;
101 
102   // Constructs at most N elements. See StaticVector for underlying constructors.
103   template <typename Arg, typename... Args,
104             typename = std::enable_if_t<!is_small_vector<remove_cvref_t<Arg>>{}>>
SmallVector(Arg && arg,Args &&...args)105   SmallVector(Arg&& arg, Args&&... args)
106       : vector_(std::in_place_type<Static>, std::forward<Arg>(arg), std::forward<Args>(args)...) {}
107 
108   // Copies at most N elements from a smaller convertible vector.
109   template <typename U, std::size_t M, typename = std::enable_if_t<M <= N>>
SmallVector(const SmallVector<U,M> & other)110   SmallVector(const SmallVector<U, M>& other)
111       : SmallVector(kIteratorRange, other.begin(), other.end()) {}
112 
swap(SmallVector & other)113   void swap(SmallVector& other) { vector_.swap(other.vector_); }
114 
115   // Returns whether the vector is backed by static or dynamic storage.
dynamic()116   bool dynamic() const { return std::holds_alternative<Dynamic>(vector_); }
117 
118   // Avoid std::visit as it generates a dispatch table.
119 #define DISPATCH(T, F, ...)                                                            \
120   T F() __VA_ARGS__ {                                                                  \
121     return dynamic() ? std::get<Dynamic>(vector_).F() : std::get<Static>(vector_).F(); \
122   }
123 
DISPATCH(size_type,max_size,const)124   DISPATCH(size_type, max_size, const)
125   DISPATCH(size_type, size, const)
126   DISPATCH(bool, empty, const)
127 
128   // noexcept to suppress warning about zero variadic macro arguments.
129   DISPATCH(iterator, begin, noexcept)
130   DISPATCH(const_iterator, begin, const)
131   DISPATCH(const_iterator, cbegin, const)
132 
133   DISPATCH(iterator, end, noexcept)
134   DISPATCH(const_iterator, end, const)
135   DISPATCH(const_iterator, cend, const)
136 
137   DISPATCH(reverse_iterator, rbegin, noexcept)
138   DISPATCH(const_reverse_iterator, rbegin, const)
139   DISPATCH(const_reverse_iterator, crbegin, const)
140 
141   DISPATCH(reverse_iterator, rend, noexcept)
142   DISPATCH(const_reverse_iterator, rend, const)
143   DISPATCH(const_reverse_iterator, crend, const)
144 
145   DISPATCH(iterator, last, noexcept)
146   DISPATCH(const_iterator, last, const)
147 
148   DISPATCH(reference, front, noexcept)
149   DISPATCH(const_reference, front, const)
150 
151   DISPATCH(reference, back, noexcept)
152   DISPATCH(const_reference, back, const)
153 
154 #undef DISPATCH
155 
156   reference operator[](size_type i) {
157     return dynamic() ? std::get<Dynamic>(vector_)[i] : std::get<Static>(vector_)[i];
158   }
159 
160   const_reference operator[](size_type i) const { return const_cast<SmallVector&>(*this)[i]; }
161 
162   // Replaces an element, and returns a reference to it. The iterator must be dereferenceable, so
163   // replacing at end() is erroneous.
164   //
165   // The element is emplaced via move constructor, so type T does not need to define copy/move
166   // assignment, e.g. its data members may be const.
167   //
168   // The arguments may directly or indirectly refer to the element being replaced.
169   //
170   // Iterators to the replaced element point to its replacement, and others remain valid.
171   //
172   template <typename... Args>
replace(const_iterator it,Args &&...args)173   reference replace(const_iterator it, Args&&... args) {
174     if (dynamic()) {
175       return std::get<Dynamic>(vector_).replace(it, std::forward<Args>(args)...);
176     } else {
177       return std::get<Static>(vector_).replace(it, std::forward<Args>(args)...);
178     }
179   }
180 
181   // Appends an element, and returns a reference to it.
182   //
183   // If the vector reaches its static or dynamic capacity, then all iterators are invalidated.
184   // Otherwise, only the end() iterator is invalidated.
185   //
186   template <typename... Args>
emplace_back(Args &&...args)187   reference emplace_back(Args&&... args) {
188     constexpr auto kInsertStatic = &Static::template emplace_back<Args...>;
189     constexpr auto kInsertDynamic = &Dynamic::template emplace_back<Args...>;
190     return *insert<kInsertStatic, kInsertDynamic>(std::forward<Args>(args)...);
191   }
192 
193   // Appends an element.
194   //
195   // If the vector reaches its static or dynamic capacity, then all iterators are invalidated.
196   // Otherwise, only the end() iterator is invalidated.
197   //
push_back(const value_type & v)198   void push_back(const value_type& v) {
199     constexpr auto kInsertStatic =
200         static_cast<bool (Static::*)(const value_type&)>(&Static::push_back);
201     constexpr auto kInsertDynamic =
202         static_cast<bool (Dynamic::*)(const value_type&)>(&Dynamic::push_back);
203     insert<kInsertStatic, kInsertDynamic>(v);
204   }
205 
push_back(value_type && v)206   void push_back(value_type&& v) {
207     constexpr auto kInsertStatic = static_cast<bool (Static::*)(value_type &&)>(&Static::push_back);
208     constexpr auto kInsertDynamic =
209         static_cast<bool (Dynamic::*)(value_type &&)>(&Dynamic::push_back);
210     insert<kInsertStatic, kInsertDynamic>(std::move(v));
211   }
212 
213   // Removes the last element. The vector must not be empty, or the call is erroneous.
214   //
215   // The last() and end() iterators are invalidated.
216   //
pop_back()217   void pop_back() {
218     if (dynamic()) {
219       std::get<Dynamic>(vector_).pop_back();
220     } else {
221       std::get<Static>(vector_).pop_back();
222     }
223   }
224 
225   // Erases an element, but does not preserve order. Rather than shifting subsequent elements,
226   // this moves the last element to the slot of the erased element.
227   //
228   // The last() and end() iterators, as well as those to the erased element, are invalidated.
229   //
unstable_erase(iterator it)230   void unstable_erase(iterator it) {
231     if (dynamic()) {
232       std::get<Dynamic>(vector_).unstable_erase(it);
233     } else {
234       std::get<Static>(vector_).unstable_erase(it);
235     }
236   }
237 
238  private:
239   template <auto InsertStatic, auto InsertDynamic, typename... Args>
insert(Args &&...args)240   auto insert(Args&&... args) {
241     if (Dynamic* const vector = std::get_if<Dynamic>(&vector_)) {
242       return (vector->*InsertDynamic)(std::forward<Args>(args)...);
243     }
244 
245     auto& vector = std::get<Static>(vector_);
246     if (vector.full()) {
247       return (promote(vector).*InsertDynamic)(std::forward<Args>(args)...);
248     } else {
249       return (vector.*InsertStatic)(std::forward<Args>(args)...);
250     }
251   }
252 
promote(Static & static_vector)253   Dynamic& promote(Static& static_vector) {
254     assert(static_vector.full());
255 
256     // Allocate double capacity to reduce probability of reallocation.
257     Dynamic vector;
258     vector.reserve(Static::max_size() * 2);
259     std::move(static_vector.begin(), static_vector.end(), std::back_inserter(vector));
260 
261     return vector_.template emplace<Dynamic>(std::move(vector));
262   }
263 
264   std::variant<Static, Dynamic> vector_;
265 };
266 
267 // Partial specialization without static storage.
268 template <typename T>
269 class SmallVector<T, 0> final : ArrayTraits<T>,
270                                 ArrayIterators<SmallVector<T, 0>, T>,
271                                 std::vector<T> {
272   using ArrayTraits<T>::construct_at;
273 
274   using Iter = ArrayIterators<SmallVector, T>;
275   using Impl = std::vector<T>;
276 
277   friend Iter;
278 
279  public:
280   FTL_ARRAY_TRAIT(T, value_type);
281   FTL_ARRAY_TRAIT(T, size_type);
282   FTL_ARRAY_TRAIT(T, difference_type);
283 
284   FTL_ARRAY_TRAIT(T, pointer);
285   FTL_ARRAY_TRAIT(T, reference);
286   FTL_ARRAY_TRAIT(T, iterator);
287   FTL_ARRAY_TRAIT(T, reverse_iterator);
288 
289   FTL_ARRAY_TRAIT(T, const_pointer);
290   FTL_ARRAY_TRAIT(T, const_reference);
291   FTL_ARRAY_TRAIT(T, const_iterator);
292   FTL_ARRAY_TRAIT(T, const_reverse_iterator);
293 
294   using Impl::Impl;
295 
296   using Impl::empty;
297   using Impl::max_size;
298   using Impl::size;
299 
300   using Impl::reserve;
301 
302   // std::vector iterators are not necessarily raw pointers.
begin()303   iterator begin() { return Impl::data(); }
end()304   iterator end() { return Impl::data() + size(); }
305 
306   using Iter::begin;
307   using Iter::end;
308 
309   using Iter::cbegin;
310   using Iter::cend;
311 
312   using Iter::rbegin;
313   using Iter::rend;
314 
315   using Iter::crbegin;
316   using Iter::crend;
317 
318   using Iter::last;
319 
320   using Iter::back;
321   using Iter::front;
322 
323   using Iter::operator[];
324 
325   template <typename... Args>
replace(const_iterator it,Args &&...args)326   reference replace(const_iterator it, Args&&... args) {
327     value_type element{std::forward<Args>(args)...};
328     std::destroy_at(it);
329     // This is only safe because exceptions are disabled.
330     return *construct_at(it, std::move(element));
331   }
332 
333   template <typename... Args>
emplace_back(Args &&...args)334   iterator emplace_back(Args&&... args) {
335     return &Impl::emplace_back(std::forward<Args>(args)...);
336   }
337 
push_back(const value_type & v)338   bool push_back(const value_type& v) {
339     Impl::push_back(v);
340     return true;
341   }
342 
push_back(value_type && v)343   bool push_back(value_type&& v) {
344     Impl::push_back(std::move(v));
345     return true;
346   }
347 
348   using Impl::pop_back;
349 
unstable_erase(iterator it)350   void unstable_erase(iterator it) {
351     if (it != last()) std::iter_swap(it, last());
352     pop_back();
353   }
354 
swap(SmallVector & other)355   void swap(SmallVector& other) { Impl::swap(other); }
356 };
357 
358 template <typename>
359 struct is_small_vector : std::false_type {};
360 
361 template <typename T, std::size_t N>
362 struct is_small_vector<SmallVector<T, N>> : std::true_type {};
363 
364 // Deduction guide for array constructor.
365 template <typename T, std::size_t N>
366 SmallVector(T (&)[N]) -> SmallVector<std::remove_cv_t<T>, N>;
367 
368 // Deduction guide for variadic constructor.
369 template <typename T, typename... Us, typename V = std::decay_t<T>,
370           typename = std::enable_if_t<(std::is_constructible_v<V, Us> && ...)>>
371 SmallVector(T&&, Us&&...) -> SmallVector<V, 1 + sizeof...(Us)>;
372 
373 // Deduction guide for in-place constructor.
374 template <typename T, std::size_t... Sizes, typename... Types>
375 SmallVector(InitializerList<T, std::index_sequence<Sizes...>, Types...>&&)
376     -> SmallVector<T, sizeof...(Sizes)>;
377 
378 // Deduction guide for StaticVector conversion.
379 template <typename T, std::size_t N>
380 SmallVector(StaticVector<T, N>&&) -> SmallVector<T, N>;
381 
382 template <typename T, std::size_t N>
383 inline void swap(SmallVector<T, N>& lhs, SmallVector<T, N>& rhs) {
384   lhs.swap(rhs);
385 }
386 
387 }  // namespace android::ftl
388