Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 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 | |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 19 | #include <ftl/array_traits.h> |
| 20 | #include <ftl/initializer_list.h> |
Dominik Laskowski | 0bacf27 | 2020-10-22 14:08:27 -0700 | [diff] [blame] | 21 | |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 22 | #include <algorithm> |
| 23 | #include <cassert> |
| 24 | #include <iterator> |
| 25 | #include <memory> |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 26 | #include <type_traits> |
| 27 | #include <utility> |
| 28 | |
| 29 | namespace android::ftl { |
| 30 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 31 | constexpr struct IteratorRangeTag { |
| 32 | } kIteratorRange; |
Dominik Laskowski | 0357237 | 2020-10-27 22:36:00 -0700 | [diff] [blame] | 33 | |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 34 | // Fixed-capacity, statically allocated counterpart of std::vector. Like std::array, StaticVector |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 35 | // allocates contiguous storage for N elements of type T at compile time, but stores at most (rather |
| 36 | // than exactly) N elements. Unlike std::array, its default constructor does not require T to have a |
Dominik Laskowski | ccd50a4 | 2020-10-30 19:56:38 -0700 | [diff] [blame] | 37 | // default constructor, since elements are constructed in place as the vector grows. Operations that |
Dominik Laskowski | 0357237 | 2020-10-27 22:36:00 -0700 | [diff] [blame] | 38 | // insert an element (emplace_back, push_back, etc.) fail when the vector is full. The API otherwise |
| 39 | // adheres to standard containers, except the unstable_erase operation that does not preserve order, |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 40 | // and the replace operation that destructively emplaces. |
| 41 | // |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 42 | // StaticVector<T, 1> is analogous to an iterable std::optional. |
| 43 | // StaticVector<T, 0> is an error. |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 44 | // |
| 45 | // Example usage: |
| 46 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 47 | // ftl::StaticVector<char, 3> vector; |
| 48 | // assert(vector.empty()); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 49 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 50 | // vector = {'a', 'b'}; |
| 51 | // assert(vector.size() == 2u); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 52 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 53 | // vector.push_back('c'); |
| 54 | // assert(vector.full()); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 55 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 56 | // assert(!vector.push_back('d')); |
| 57 | // assert(vector.size() == 3u); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 58 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 59 | // vector.unstable_erase(vector.begin()); |
| 60 | // assert(vector == (ftl::StaticVector{'c', 'b'})); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 61 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 62 | // vector.pop_back(); |
| 63 | // assert(vector.back() == 'c'); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 64 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 65 | // const char array[] = "hi"; |
| 66 | // vector = ftl::StaticVector(array); |
| 67 | // assert(vector == (ftl::StaticVector{'h', 'i', '\0'})); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 68 | // |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 69 | // ftl::StaticVector strings = ftl::init::list<std::string>("abc")("123456", 3u)(3u, '?'); |
| 70 | // assert(strings.size() == 3u); |
| 71 | // assert(strings[0] == "abc"); |
| 72 | // assert(strings[1] == "123"); |
| 73 | // assert(strings[2] == "???"); |
Dominik Laskowski | ccd50a4 | 2020-10-30 19:56:38 -0700 | [diff] [blame] | 74 | // |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 75 | template <typename T, std::size_t N> |
Dominik Laskowski | 0bacf27 | 2020-10-22 14:08:27 -0700 | [diff] [blame] | 76 | class StaticVector final : ArrayTraits<T>, |
| 77 | ArrayIterators<StaticVector<T, N>, T>, |
| 78 | ArrayComparators<StaticVector> { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 79 | static_assert(N > 0); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 80 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 81 | using ArrayTraits<T>::construct_at; |
Dominik Laskowski | 0bacf27 | 2020-10-22 14:08:27 -0700 | [diff] [blame] | 82 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 83 | using Iter = ArrayIterators<StaticVector, T>; |
| 84 | friend Iter; |
Dominik Laskowski | 0bacf27 | 2020-10-22 14:08:27 -0700 | [diff] [blame] | 85 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 86 | // There is ambiguity when constructing from two iterator-like elements like pointers: |
| 87 | // they could be an iterator range, or arguments for in-place construction. Assume the |
| 88 | // latter unless they are input iterators and cannot be used to construct elements. If |
| 89 | // the former is intended, the caller can pass an IteratorRangeTag to disambiguate. |
| 90 | template <typename I, typename Traits = std::iterator_traits<I>> |
| 91 | using is_input_iterator = |
| 92 | std::conjunction<std::is_base_of<std::input_iterator_tag, typename Traits::iterator_category>, |
| 93 | std::negation<std::is_constructible<T, I>>>; |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 94 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 95 | public: |
| 96 | FTL_ARRAY_TRAIT(T, value_type); |
| 97 | FTL_ARRAY_TRAIT(T, size_type); |
| 98 | FTL_ARRAY_TRAIT(T, difference_type); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 99 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 100 | FTL_ARRAY_TRAIT(T, pointer); |
| 101 | FTL_ARRAY_TRAIT(T, reference); |
| 102 | FTL_ARRAY_TRAIT(T, iterator); |
| 103 | FTL_ARRAY_TRAIT(T, reverse_iterator); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 104 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 105 | FTL_ARRAY_TRAIT(T, const_pointer); |
| 106 | FTL_ARRAY_TRAIT(T, const_reference); |
| 107 | FTL_ARRAY_TRAIT(T, const_iterator); |
| 108 | FTL_ARRAY_TRAIT(T, const_reverse_iterator); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 109 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 110 | // Creates an empty vector. |
| 111 | StaticVector() = default; |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 112 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 113 | // Copies and moves a vector, respectively. |
| 114 | StaticVector(const StaticVector& other) |
| 115 | : StaticVector(kIteratorRange, other.begin(), other.end()) {} |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 116 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 117 | StaticVector(StaticVector&& other) { swap<true>(other); } |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 118 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 119 | // Copies at most N elements from a smaller convertible vector. |
| 120 | template <typename U, std::size_t M, typename = std::enable_if_t<M <= N>> |
| 121 | StaticVector(const StaticVector<U, M>& other) |
| 122 | : StaticVector(kIteratorRange, other.begin(), other.end()) {} |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 123 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 124 | // Copies at most N elements from an array. |
| 125 | template <typename U, std::size_t M> |
| 126 | explicit StaticVector(U (&array)[M]) |
| 127 | : StaticVector(kIteratorRange, std::begin(array), std::end(array)) {} |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 128 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 129 | // Copies at most N elements from the range [first, last). |
| 130 | // |
| 131 | // IteratorRangeTag disambiguates with initialization from two iterator-like elements. |
| 132 | // |
| 133 | template <typename Iterator, typename = std::enable_if_t<is_input_iterator<Iterator>{}>> |
| 134 | StaticVector(Iterator first, Iterator last) : StaticVector(kIteratorRange, first, last) { |
| 135 | using V = typename std::iterator_traits<Iterator>::value_type; |
| 136 | static_assert(std::is_constructible_v<value_type, V>, "Incompatible iterator range"); |
| 137 | } |
| 138 | |
| 139 | template <typename Iterator> |
| 140 | StaticVector(IteratorRangeTag, Iterator first, Iterator last) |
| 141 | : size_(std::min(max_size(), static_cast<size_type>(std::distance(first, last)))) { |
| 142 | std::uninitialized_copy(first, first + size_, begin()); |
| 143 | } |
| 144 | |
| 145 | // Constructs at most N elements. The template arguments T and N are inferred using the |
| 146 | // deduction guide defined below. Note that T is determined from the first element, and |
| 147 | // subsequent elements must have convertible types: |
| 148 | // |
| 149 | // ftl::StaticVector vector = {1, 2, 3}; |
| 150 | // static_assert(std::is_same_v<decltype(vector), ftl::StaticVector<int, 3>>); |
| 151 | // |
| 152 | // const auto copy = "quince"s; |
| 153 | // auto move = "tart"s; |
| 154 | // ftl::StaticVector vector = {copy, std::move(move)}; |
| 155 | // |
| 156 | // static_assert(std::is_same_v<decltype(vector), ftl::StaticVector<std::string, 2>>); |
| 157 | // |
| 158 | template <typename E, typename... Es, |
| 159 | typename = std::enable_if_t<std::is_constructible_v<value_type, E>>> |
| 160 | StaticVector(E&& element, Es&&... elements) |
| 161 | : StaticVector(std::index_sequence<0>{}, std::forward<E>(element), |
| 162 | std::forward<Es>(elements)...) { |
| 163 | static_assert(sizeof...(elements) < N, "Too many elements"); |
| 164 | } |
| 165 | |
| 166 | // Constructs at most N elements in place by forwarding per-element constructor arguments. The |
| 167 | // template arguments T and N are inferred using the deduction guide defined below. The syntax |
| 168 | // for listing arguments is as follows: |
| 169 | // |
| 170 | // ftl::StaticVector vector = ftl::init::list<std::string>("abc")()(3u, '?'); |
| 171 | // |
| 172 | // static_assert(std::is_same_v<decltype(vector), ftl::StaticVector<std::string, 3>>); |
| 173 | // assert(vector.full()); |
| 174 | // assert(vector[0] == "abc"); |
| 175 | // assert(vector[1].empty()); |
| 176 | // assert(vector[2] == "???"); |
| 177 | // |
| 178 | template <typename U, std::size_t Size, std::size_t... Sizes, typename... Types> |
| 179 | StaticVector(InitializerList<U, std::index_sequence<Size, Sizes...>, Types...>&& list) |
| 180 | : StaticVector(std::index_sequence<0, 0, Size>{}, std::make_index_sequence<Size>{}, |
| 181 | std::index_sequence<Sizes...>{}, list.tuple) {} |
| 182 | |
| 183 | ~StaticVector() { std::destroy(begin(), end()); } |
| 184 | |
| 185 | StaticVector& operator=(const StaticVector& other) { |
| 186 | StaticVector copy(other); |
| 187 | swap(copy); |
| 188 | return *this; |
| 189 | } |
| 190 | |
| 191 | StaticVector& operator=(StaticVector&& other) { |
Dominik Laskowski | 44828ce | 2021-09-13 11:00:22 -0700 | [diff] [blame] | 192 | clear(); |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 193 | swap<true>(other); |
| 194 | return *this; |
| 195 | } |
| 196 | |
| 197 | // IsEmpty enables a fast path when the vector is known to be empty at compile time. |
| 198 | template <bool IsEmpty = false> |
| 199 | void swap(StaticVector&); |
| 200 | |
| 201 | static constexpr size_type max_size() { return N; } |
| 202 | size_type size() const { return size_; } |
| 203 | |
| 204 | bool empty() const { return size() == 0; } |
| 205 | bool full() const { return size() == max_size(); } |
| 206 | |
| 207 | iterator begin() { return std::launder(reinterpret_cast<pointer>(data_)); } |
| 208 | iterator end() { return begin() + size(); } |
| 209 | |
| 210 | using Iter::begin; |
| 211 | using Iter::end; |
| 212 | |
| 213 | using Iter::cbegin; |
| 214 | using Iter::cend; |
| 215 | |
| 216 | using Iter::rbegin; |
| 217 | using Iter::rend; |
| 218 | |
| 219 | using Iter::crbegin; |
| 220 | using Iter::crend; |
| 221 | |
| 222 | using Iter::last; |
| 223 | |
| 224 | using Iter::back; |
| 225 | using Iter::front; |
| 226 | |
| 227 | using Iter::operator[]; |
| 228 | |
| 229 | // Replaces an element, and returns a reference to it. The iterator must be dereferenceable, so |
| 230 | // replacing at end() is erroneous. |
| 231 | // |
| 232 | // The element is emplaced via move constructor, so type T does not need to define copy/move |
| 233 | // assignment, e.g. its data members may be const. |
| 234 | // |
| 235 | // The arguments may directly or indirectly refer to the element being replaced. |
| 236 | // |
| 237 | // Iterators to the replaced element point to its replacement, and others remain valid. |
| 238 | // |
| 239 | template <typename... Args> |
| 240 | reference replace(const_iterator it, Args&&... args) { |
| 241 | value_type element{std::forward<Args>(args)...}; |
| 242 | std::destroy_at(it); |
| 243 | // This is only safe because exceptions are disabled. |
| 244 | return *construct_at(it, std::move(element)); |
| 245 | } |
| 246 | |
| 247 | // Appends an element, and returns an iterator to it. If the vector is full, the element is not |
| 248 | // inserted, and the end() iterator is returned. |
| 249 | // |
| 250 | // On success, the end() iterator is invalidated. |
| 251 | // |
| 252 | template <typename... Args> |
| 253 | iterator emplace_back(Args&&... args) { |
| 254 | if (full()) return end(); |
| 255 | const iterator it = construct_at(end(), std::forward<Args>(args)...); |
| 256 | ++size_; |
| 257 | return it; |
| 258 | } |
| 259 | |
| 260 | // Appends an element unless the vector is full, and returns whether the element was inserted. |
| 261 | // |
| 262 | // On success, the end() iterator is invalidated. |
| 263 | // |
| 264 | bool push_back(const value_type& v) { |
| 265 | // Two statements for sequence point. |
| 266 | const iterator it = emplace_back(v); |
| 267 | return it != end(); |
| 268 | } |
| 269 | |
| 270 | bool push_back(value_type&& v) { |
| 271 | // Two statements for sequence point. |
| 272 | const iterator it = emplace_back(std::move(v)); |
| 273 | return it != end(); |
| 274 | } |
| 275 | |
| 276 | // Removes the last element. The vector must not be empty, or the call is erroneous. |
| 277 | // |
| 278 | // The last() and end() iterators are invalidated. |
| 279 | // |
| 280 | void pop_back() { unstable_erase(last()); } |
| 281 | |
Dominik Laskowski | 44828ce | 2021-09-13 11:00:22 -0700 | [diff] [blame] | 282 | // Removes all elements. |
| 283 | // |
| 284 | // All iterators are invalidated. |
| 285 | // |
| 286 | void clear() { |
| 287 | std::destroy(begin(), end()); |
| 288 | size_ = 0; |
| 289 | } |
| 290 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 291 | // Erases an element, but does not preserve order. Rather than shifting subsequent elements, |
| 292 | // this moves the last element to the slot of the erased element. |
| 293 | // |
| 294 | // The last() and end() iterators, as well as those to the erased element, are invalidated. |
| 295 | // |
| 296 | void unstable_erase(const_iterator it) { |
| 297 | std::destroy_at(it); |
| 298 | if (it != last()) { |
| 299 | // Move last element and destroy its source for destructor side effects. This is only |
| 300 | // safe because exceptions are disabled. |
| 301 | construct_at(it, std::move(back())); |
| 302 | std::destroy_at(last()); |
Dominik Laskowski | 0357237 | 2020-10-27 22:36:00 -0700 | [diff] [blame] | 303 | } |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 304 | --size_; |
| 305 | } |
Dominik Laskowski | 0357237 | 2020-10-27 22:36:00 -0700 | [diff] [blame] | 306 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 307 | private: |
| 308 | // Recursion for variadic constructor. |
| 309 | template <std::size_t I, typename E, typename... Es> |
| 310 | StaticVector(std::index_sequence<I>, E&& element, Es&&... elements) |
| 311 | : StaticVector(std::index_sequence<I + 1>{}, std::forward<Es>(elements)...) { |
| 312 | construct_at(begin() + I, std::forward<E>(element)); |
| 313 | } |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 314 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 315 | // Base case for variadic constructor. |
| 316 | template <std::size_t I> |
| 317 | explicit StaticVector(std::index_sequence<I>) : size_(I) {} |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 318 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 319 | // Recursion for in-place constructor. |
| 320 | // |
| 321 | // Construct element I by extracting its arguments from the InitializerList tuple. ArgIndex |
| 322 | // is the position of its first argument in Args, and ArgCount is the number of arguments. |
| 323 | // The Indices sequence corresponds to [0, ArgCount). |
| 324 | // |
| 325 | // The Sizes sequence lists the argument counts for elements after I, so Size is the ArgCount |
| 326 | // for the next element. The recursion stops when Sizes is empty for the last element. |
| 327 | // |
| 328 | template <std::size_t I, std::size_t ArgIndex, std::size_t ArgCount, std::size_t... Indices, |
| 329 | std::size_t Size, std::size_t... Sizes, typename... Args> |
| 330 | StaticVector(std::index_sequence<I, ArgIndex, ArgCount>, std::index_sequence<Indices...>, |
| 331 | std::index_sequence<Size, Sizes...>, std::tuple<Args...>& tuple) |
| 332 | : StaticVector(std::index_sequence<I + 1, ArgIndex + ArgCount, Size>{}, |
| 333 | std::make_index_sequence<Size>{}, std::index_sequence<Sizes...>{}, tuple) { |
| 334 | construct_at(begin() + I, std::move(std::get<ArgIndex + Indices>(tuple))...); |
| 335 | } |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 336 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 337 | // Base case for in-place constructor. |
| 338 | template <std::size_t I, std::size_t ArgIndex, std::size_t ArgCount, std::size_t... Indices, |
| 339 | typename... Args> |
| 340 | StaticVector(std::index_sequence<I, ArgIndex, ArgCount>, std::index_sequence<Indices...>, |
| 341 | std::index_sequence<>, std::tuple<Args...>& tuple) |
| 342 | : size_(I + 1) { |
| 343 | construct_at(begin() + I, std::move(std::get<ArgIndex + Indices>(tuple))...); |
| 344 | } |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 345 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 346 | size_type size_ = 0; |
| 347 | std::aligned_storage_t<sizeof(value_type), alignof(value_type)> data_[N]; |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 348 | }; |
| 349 | |
| 350 | // Deduction guide for array constructor. |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 351 | template <typename T, std::size_t N> |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 352 | StaticVector(T (&)[N]) -> StaticVector<std::remove_cv_t<T>, N>; |
| 353 | |
| 354 | // Deduction guide for variadic constructor. |
| 355 | template <typename T, typename... Us, typename V = std::decay_t<T>, |
| 356 | typename = std::enable_if_t<(std::is_constructible_v<V, Us> && ...)>> |
| 357 | StaticVector(T&&, Us&&...) -> StaticVector<V, 1 + sizeof...(Us)>; |
| 358 | |
| 359 | // Deduction guide for in-place constructor. |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 360 | template <typename T, std::size_t... Sizes, typename... Types> |
Dominik Laskowski | ccd50a4 | 2020-10-30 19:56:38 -0700 | [diff] [blame] | 361 | StaticVector(InitializerList<T, std::index_sequence<Sizes...>, Types...>&&) |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 362 | -> StaticVector<T, sizeof...(Sizes)>; |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 363 | |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 364 | template <typename T, std::size_t N> |
| 365 | template <bool IsEmpty> |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 366 | void StaticVector<T, N>::swap(StaticVector& other) { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 367 | auto [to, from] = std::make_pair(this, &other); |
| 368 | if (from == this) return; |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 369 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 370 | // Assume this vector has fewer elements, so the excess of the other vector will be moved to it. |
| 371 | auto [min, max] = std::make_pair(size(), other.size()); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 372 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 373 | // No elements to swap if moving into an empty vector. |
| 374 | if constexpr (IsEmpty) { |
| 375 | assert(min == 0); |
| 376 | } else { |
| 377 | if (min > max) { |
| 378 | std::swap(from, to); |
| 379 | std::swap(min, max); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 382 | // Swap elements [0, min). |
| 383 | std::swap_ranges(begin(), begin() + min, other.begin()); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 384 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 385 | // No elements to move if sizes are equal. |
| 386 | if (min == max) return; |
| 387 | } |
| 388 | |
| 389 | // Move elements [min, max) and destroy their source for destructor side effects. |
| 390 | const auto [first, last] = std::make_pair(from->begin() + min, from->begin() + max); |
| 391 | std::uninitialized_move(first, last, to->begin() + min); |
| 392 | std::destroy(first, last); |
| 393 | |
| 394 | std::swap(size_, other.size_); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 395 | } |
| 396 | |
Dominik Laskowski | 5444fc8 | 2020-11-24 13:41:10 -0800 | [diff] [blame] | 397 | template <typename T, std::size_t N> |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 398 | inline void swap(StaticVector<T, N>& lhs, StaticVector<T, N>& rhs) { |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 399 | lhs.swap(rhs); |
Dominik Laskowski | 6fdf114 | 2020-10-07 12:09:09 -0700 | [diff] [blame] | 400 | } |
| 401 | |
Dominik Laskowski | e21dbed | 2020-12-04 20:51:43 -0800 | [diff] [blame] | 402 | } // namespace android::ftl |