1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
// -*- C++ -*-
//===----------------------------------------------------------------------===//
//
// Part of the BastionOS freestanding C++ standard library.
//
// std::array — fixed-size aggregate container.
//
//===----------------------------------------------------------------------===//
#ifndef _LIBBASTION_ARRAY
#define _LIBBASTION_ARRAY
#include <__config>
#include <__type_traits/integral_constant.h>
#include <__type_traits/type_modifications.h>
#include <__utility/move.h>
#include <__utility/swap.h>
#include <__algorithm/fill.h>
#include <__algorithm/comparison.h>
#include <cstddef>
_LIBBASTION_BEGIN_NAMESPACE_STD
template<class _Tp, size_t _Np>
struct array {
using value_type = _Tp;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = _Tp&;
using const_reference = const _Tp&;
using pointer = _Tp*;
using const_pointer = const _Tp*;
using iterator = _Tp*;
using const_iterator = const _Tp*;
// Aggregate — public data member.
_Tp __data_[_Np];
// Element access
_LIBBASTION_NODISCARD constexpr reference at(size_type __pos) { _LIBBASTION_ASSERT(__pos < _Np, "array::at out of range"); return __data_[__pos]; }
_LIBBASTION_NODISCARD constexpr const_reference at(size_type __pos) const { _LIBBASTION_ASSERT(__pos < _Np, "array::at out of range"); return __data_[__pos]; }
_LIBBASTION_NODISCARD constexpr reference operator[](size_type __pos) { return __data_[__pos]; }
_LIBBASTION_NODISCARD constexpr const_reference operator[](size_type __pos) const { return __data_[__pos]; }
_LIBBASTION_NODISCARD constexpr reference front() { return __data_[0]; }
_LIBBASTION_NODISCARD constexpr const_reference front() const { return __data_[0]; }
_LIBBASTION_NODISCARD constexpr reference back() { return __data_[_Np - 1]; }
_LIBBASTION_NODISCARD constexpr const_reference back() const { return __data_[_Np - 1]; }
_LIBBASTION_NODISCARD constexpr pointer data() noexcept { return __data_; }
_LIBBASTION_NODISCARD constexpr const_pointer data() const noexcept { return __data_; }
// Iterators
_LIBBASTION_NODISCARD constexpr iterator begin() noexcept { return __data_; }
_LIBBASTION_NODISCARD constexpr const_iterator begin() const noexcept { return __data_; }
_LIBBASTION_NODISCARD constexpr const_iterator cbegin() const noexcept { return __data_; }
_LIBBASTION_NODISCARD constexpr iterator end() noexcept { return __data_ + _Np; }
_LIBBASTION_NODISCARD constexpr const_iterator end() const noexcept { return __data_ + _Np; }
_LIBBASTION_NODISCARD constexpr const_iterator cend() const noexcept { return __data_ + _Np; }
// Capacity
_LIBBASTION_NODISCARD constexpr bool empty() const noexcept { return _Np == 0; }
_LIBBASTION_NODISCARD constexpr size_type size() const noexcept { return _Np; }
_LIBBASTION_NODISCARD constexpr size_type max_size() const noexcept { return _Np; }
// Operations
constexpr void fill(const _Tp& __val) { std::fill(begin(), end(), __val); }
constexpr void swap(array& __other) noexcept(is_nothrow_swappable_v<_Tp>) {
for (size_type __i = 0; __i < _Np; ++__i)
std::swap(__data_[__i], __other.__data_[__i]);
}
};
// Zero-size specialization
template<class _Tp>
struct array<_Tp, 0> {
using value_type = _Tp;
using size_type = size_t;
using difference_type = ptrdiff_t;
using reference = _Tp&;
using const_reference = const _Tp&;
using pointer = _Tp*;
using const_pointer = const _Tp*;
using iterator = _Tp*;
using const_iterator = const _Tp*;
// No __data_ member for zero-size array.
_LIBBASTION_NODISCARD constexpr reference at(size_type) { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr const_reference at(size_type) const { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr reference operator[](size_type) { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr const_reference operator[](size_type) const { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr reference front() { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr const_reference front() const { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr reference back() { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr const_reference back() const { _LIBBASTION_TRAP(); }
_LIBBASTION_NODISCARD constexpr pointer data() noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr const_pointer data() const noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr iterator begin() noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr const_iterator begin() const noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr const_iterator cbegin() const noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr iterator end() noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr const_iterator end() const noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr const_iterator cend() const noexcept { return nullptr; }
_LIBBASTION_NODISCARD constexpr bool empty() const noexcept { return true; }
_LIBBASTION_NODISCARD constexpr size_type size() const noexcept { return 0; }
_LIBBASTION_NODISCARD constexpr size_type max_size() const noexcept { return 0; }
constexpr void fill(const _Tp&) {}
constexpr void swap(array&) noexcept {}
};
// Deduction guide
template<class _Tp, class... _Up>
array(_Tp, _Up...) -> array<_Tp, 1 + sizeof...(_Up)>;
// Non-member swap
template<class _Tp, size_t _Np>
inline constexpr void swap(array<_Tp, _Np>& __a, array<_Tp, _Np>& __b) noexcept(noexcept(__a.swap(__b))) {
__a.swap(__b);
}
// Comparison
template<class _Tp, size_t _Np>
_LIBBASTION_NODISCARD constexpr bool operator==(const array<_Tp, _Np>& __a, const array<_Tp, _Np>& __b) {
return std::equal(__a.begin(), __a.end(), __b.begin());
}
template<class _Tp, size_t _Np>
_LIBBASTION_NODISCARD constexpr bool operator!=(const array<_Tp, _Np>& __a, const array<_Tp, _Np>& __b) {
return !(__a == __b);
}
// get<I> for structured bindings
template<size_t _Ip, class _Tp, size_t _Np>
_LIBBASTION_NODISCARD constexpr _Tp& get(array<_Tp, _Np>& __a) noexcept {
static_assert(_Ip < _Np, "array index out of range");
return __a.__data_[_Ip];
}
template<size_t _Ip, class _Tp, size_t _Np>
_LIBBASTION_NODISCARD constexpr const _Tp& get(const array<_Tp, _Np>& __a) noexcept {
static_assert(_Ip < _Np, "array index out of range");
return __a.__data_[_Ip];
}
template<size_t _Ip, class _Tp, size_t _Np>
_LIBBASTION_NODISCARD constexpr _Tp&& get(array<_Tp, _Np>&& __a) noexcept {
static_assert(_Ip < _Np, "array index out of range");
return std::move(__a.__data_[_Ip]);
}
// tuple_size / tuple_element for structured bindings
template<class _Tp, size_t _Np>
struct tuple_size<array<_Tp, _Np>> : integral_constant<size_t, _Np> {};
template<size_t _Ip, class _Tp, size_t _Np>
struct tuple_element<_Ip, array<_Tp, _Np>> {
static_assert(_Ip < _Np, "array index out of range");
using type = _Tp;
};
// to_array (C++20)
namespace __detail {
template<class _Tp, size_t _Np, size_t... _Idx>
constexpr array<remove_cv_t<_Tp>, _Np> __to_array_impl(_Tp (&__a)[_Np], index_sequence<_Idx...>) {
return {{__a[_Idx]...}};
}
template<class _Tp, size_t _Np, size_t... _Idx>
constexpr array<remove_cv_t<_Tp>, _Np> __to_array_impl(_Tp (&&__a)[_Np], index_sequence<_Idx...>) {
return {{std::move(__a[_Idx])...}};
}
} // namespace __detail
template<class _Tp, size_t _Np>
constexpr array<remove_cv_t<_Tp>, _Np> to_array(_Tp (&__a)[_Np]) {
return __detail::__to_array_impl(__a, make_index_sequence<_Np>{});
}
template<class _Tp, size_t _Np>
constexpr array<remove_cv_t<_Tp>, _Np> to_array(_Tp (&&__a)[_Np]) {
return __detail::__to_array_impl(std::move(__a), make_index_sequence<_Np>{});
}
_LIBBASTION_END_NAMESPACE_STD
#endif // _LIBBASTION_ARRAY
|