openMSX
static_vector.hh
Go to the documentation of this file.
1#ifndef STATIC_VECTOR_HH
2#define STATIC_VECTOR_HH
3
4#include "ranges.hh"
5#include <algorithm>
6#include <array>
7#include <cassert>
8#include <cstddef>
9#include <cstdint>
10#include <initializer_list>
11#include <limits>
12#include <span>
13#include <type_traits>
14#include <utility>
15
16// This is a _very_ minimal implementation of the following (we can easily
17// extend this when the need arises).
18//
19// static_vector
20// A dynamically-resizable vector with fixed capacity and embedded storage
21// http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2018/p0843r2.html
22//
23// For us the main purpose of this class is to use it during constexpr
24// (compile-time) calculations. Because for now, it's no yet possible to use
25// heap memory in constexpr functions (there are proposals to loosen this
26// restriction in future C++ standards).
27
28struct from_range_t {};
29inline constexpr from_range_t from_range;
30
31template<typename T, size_t N>
33{
34 using SizeType =
35 std::conditional_t<N <= std::numeric_limits<uint8_t >::max(), uint8_t,
36 std::conditional_t<N <= std::numeric_limits<uint16_t>::max(), uint16_t,
37 std::conditional_t<N <= std::numeric_limits<uint32_t>::max(), uint32_t,
38 uint64_t>>>;
39
40public:
41 using value_type = T;
45 using const_iterator = const value_type*;
47 using const_pointer = const value_type*;
48 using size_type = size_t;
49 using difference_type = ptrdiff_t;
50 using reverse_iterator = std::reverse_iterator<iterator>;
51 using const_reverse_iterator = std::reverse_iterator<const_iterator>;
52
53 constexpr static_vector() = default;
54
55 constexpr static_vector(std::initializer_list<T> list) {
56 assert(list.size() <= N);
57 ranges::copy(list, array);
58 sz = SizeType(list.size());
59 }
60
61 template<typename Range>
62 constexpr static_vector(from_range_t, Range&& range) {
63 for (auto&& elem : range) {
64 push_back(std::forward<decltype(elem)>(elem));
65 }
66 }
67
68 [[nodiscard]] constexpr auto begin() noexcept { return array.begin(); }
69 [[nodiscard]] constexpr auto begin() const noexcept { return array.begin(); }
70 [[nodiscard]] constexpr auto end() noexcept { return array.begin() + sz; }
71 [[nodiscard]] constexpr auto end() const noexcept { return array.begin() + sz; }
72 [[nodiscard]] constexpr auto rbegin() noexcept { return reverse_iterator(end()); }
73 [[nodiscard]] constexpr auto rbegin() const noexcept { return const_reverse_iterator(end()); }
74 [[nodiscard]] constexpr auto rend() noexcept { return reverse_iterator(begin()); }
75 [[nodiscard]] constexpr auto rend() const noexcept { return const_reverse_iterator(begin()); }
76
77 [[nodiscard]] constexpr size_t size() const noexcept { return sz; }
78 [[nodiscard]] constexpr size_t max_size() const noexcept { return array.max_size(); }
79 [[nodiscard]] constexpr bool empty() const noexcept { return sz == 0; }
80
81 [[nodiscard]] constexpr T& operator[](size_t index) noexcept { return array[index]; }
82 [[nodiscard]] constexpr const T& operator[](size_t index) const noexcept { return array[index]; }
83 [[nodiscard]] constexpr T& front() noexcept { assert(sz > 0); return array[0]; }
84 [[nodiscard]] constexpr const T& front() const noexcept { assert(sz > 0); return array[0]; }
85 [[nodiscard]] constexpr T& back() noexcept { assert(sz > 0); return array[sz - 1]; }
86 [[nodiscard]] constexpr const T& back() const noexcept { assert(sz > 0); return array[sz - 1]; }
87 [[nodiscard]] constexpr T* data() noexcept { return array.data(); }
88 [[nodiscard]] constexpr const T* data() const noexcept { return array.data(); }
89
90 constexpr void push_back(const T& a) { assert(sz < N); array[sz++] = a; }
91 constexpr void push_back(T&& a) { assert(sz < N); array[sz++] = std::move(a); }
92 constexpr void pop_back() noexcept { assert(sz > 0); sz--; }
93 constexpr void clear() noexcept { sz = 0; }
94
95 operator std::span< T>() noexcept { return {array.data(), sz}; }
96 operator std::span<const T>() const noexcept { return {array.data(), sz}; }
97
98private:
99 std::array<T, N> array = {};
100 SizeType sz = 0;
101};
102
103#endif
value_type & reference
const value_type * const_iterator
constexpr auto rbegin() noexcept
constexpr T & operator[](size_t index) noexcept
constexpr size_t max_size() const noexcept
constexpr auto begin() const noexcept
ptrdiff_t difference_type
constexpr size_t size() const noexcept
constexpr void clear() noexcept
constexpr void pop_back() noexcept
constexpr static_vector(from_range_t, Range &&range)
constexpr T & front() noexcept
constexpr bool empty() const noexcept
constexpr static_vector(std::initializer_list< T > list)
std::reverse_iterator< iterator > reverse_iterator
constexpr static_vector()=default
constexpr void push_back(T &&a)
constexpr auto rend() const noexcept
constexpr auto rend() noexcept
constexpr T & back() noexcept
const value_type * const_pointer
constexpr const T & back() const noexcept
constexpr auto end() noexcept
constexpr T * data() noexcept
constexpr const T * data() const noexcept
constexpr auto begin() noexcept
constexpr auto end() const noexcept
constexpr auto rbegin() const noexcept
constexpr const T & front() const noexcept
value_type * pointer
const value_type & const_reference
constexpr const T & operator[](size_t index) const noexcept
value_type * iterator
std::reverse_iterator< const_iterator > const_reverse_iterator
constexpr void push_back(const T &a)
constexpr auto copy(InputRange &&range, OutputIter out)
Definition ranges.hh:252
constexpr from_range_t from_range