-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtarray.hpp
275 lines (263 loc) · 6.76 KB
/
tarray.hpp
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#ifndef TARRAY_HPP
#define TARRAY_HPP
#include <cstddef>
#include <stdexcept>
#include <limits>
#include <tstl_uninitialized.hpp>
#include <type_traits>
#include <initializer_list>
#include <cassert>
namespace tstd
{
// array is an aggregate class
template<typename T, std::size_t N>
class array
{
public:
using value_type = T;
using size_type= std::size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type&;
using const_reference = const value_type&;
using pointer = T*;
using const_pointer = const T*;
using iterator = T*;
using const_iterator = const T*;
using reverse_iterator = tstd::reverse_iterator<iterator>;
using const_reverse_iterator = tstd::reverse_iterator<const_iterator>;
public:
// constructors are implicit
// elements access
constexpr reference at(size_type pos)
{
assert(pos < N);
if (pos >= N)
{
throw std::out_of_range("array::at index out of range");
}
return elements[pos];
}
constexpr const_reference at(size_type pos) const
{
assert(pos < N);
if (pos >= N)
{
throw std::out_of_range("array::at index out of range");
}
return elements[pos];
}
constexpr reference operator[](size_type pos)
{
assert(pos < N);
return elements[pos];
}
constexpr const_reference operator[](size_type pos) const
{
assert(pos < N);
return elements[pos];
}
constexpr reference front()
{
assert(!empty());
return elements[0];
}
constexpr const_reference front() const
{
assert(!empty());
return elements[0];
}
constexpr reference back()
{
assert(!empty());
return elements[N-1];
}
constexpr const_reference back() const
{
assert(!empty());
return elements[N-1];
}
// iterators
constexpr iterator begin() noexcept
{
return elements;
}
constexpr const_iterator begin() const noexcept
{
return elements;
}
constexpr const_iterator cbegin() const noexcept
{
return elements;
}
constexpr iterator end() noexcept
{
return elements + N;
}
constexpr const_iterator end() const noexcept
{
return elements + N;
}
constexpr const_iterator cend() const noexcept
{
return elements + N;
}
constexpr reverse_iterator rbegin() noexcept
{
return reverse_iterator(elements + N);
}
constexpr const_reverse_iterator rbegin() const noexcept
{
return const_reverse_iterator(elements + N);
}
constexpr const_reverse_iterator crbegin() const noexcept
{
return const_reverse_iterator(elements + N);
}
constexpr reverse_iterator rend() noexcept
{
return reverse_iterator(elements);
}
constexpr const_reverse_iterator rend() const noexcept
{
return const_reverse_iterator(elements);
}
constexpr const_reverse_iterator crend() const noexcept
{
return const_reverse_iterator(elements);
}
// size and capacity
[[nodiscard]] constexpr bool empty() const noexcept
{
return N == 0;
}
constexpr size_type size() const noexcept
{
return N;
}
constexpr size_type max_size() const noexcept
{
return N;
}
// modifiers
constexpr void fill(const T& value)
{
for (size_type i = 0; i < N; ++i)
{
elements[i] = value;
}
}
constexpr void swap(array& other) noexcept(std::is_nothrow_swappable_v<T>)
{
T tmp;
for (size_type i = 0; i < N; ++i)
{
tmp = elements[i];
elements[i] = other.elements[i];
other.elements[i] = tmp;
}
}
public:
T elements[N];
};
// non-member functions
// a non-standard compare function for tstd::array
// equal 0 less -1 greater 1
template<typename T, std::size_t N>
constexpr int _cmp_array(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
for (std::size_t i = 0; i < N; ++i)
{
if (lhs[i] == rhs[i])
{
continue;
}
return lhs[i] > rhs[i] ? 1 : -1;
}
return 0;
}
// comparisons
template<typename T, std::size_t N>
constexpr bool operator==(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
return _cmp_array(lhs, rhs) == 0;
}
template<typename T, std::size_t N>
constexpr bool operator!=(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
return _cmp_array(lhs, rhs) != 0;
}
template<typename T, std::size_t N>
constexpr bool operator<(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
return _cmp_array(lhs, rhs) < 0;
}
template<typename T, std::size_t N>
constexpr bool operator<=(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
return _cmp_array(lhs, rhs) <= 0;
}
template<typename T, std::size_t N>
constexpr bool operator>(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
return _cmp_array(lhs, rhs) > 0;
}
template<typename T, std::size_t N>
constexpr bool operator>=(const tstd::array<T, N>& lhs, const tstd::array<T, N>& rhs)
{
return _cmp_array(lhs, rhs) >= 0;
}
// tstd::get
template<std::size_t I, typename T, std::size_t N>
constexpr T& get(tstd::array<T, N>& a) noexcept
{
return a[I];
}
template<std::size_t I, typename T, std::size_t N>
constexpr T&& get(tstd::array<T, N>&& a) noexcept
{
return a[I];
}
template<std::size_t I, typename T, std::size_t N>
constexpr const T& get(const tstd::array<T, N>& a) noexcept
{
return a[I];
}
template<std::size_t I, typename T, std::size_t N>
constexpr const T&& get(const tstd::array<T, N>&& a) noexcept
{
return a[I];
}
// tstd::swap
template<class T, std::size_t N,
typename = std::enable_if<N == 0 || std::is_swappable_v<T>>>
constexpr void swap(tstd::array<T, N>& lhs, tstd::array<T, N>& rhs) noexcept(noexcept(lhs.swap(rhs)))
{
lhs.swap(rhs);
}
// built-in array to tstd::array
template<typename T, std::size_t N,
typename = std::enable_if<std::is_constructible_v<T, T&> && !std::is_array_v<T>>>
constexpr tstd::array<std::remove_cv_t<T>, N> to_array(T (&a)[N])
{
tstd::array<std::remove_cv_t<T>, N> ret;
for (std::size_t i = 0; i < N; ++i)
{
ret[i] = a[i];
}
return ret;
}
template<typename T, std::size_t N,
typename std::enable_if<std::is_move_constructible_v<T> && !std::is_array_v<T>>>
constexpr tstd::array<std::remove_cv_t<T>, N> to_array(T (&&a)[N])
{
tstd::array<std::remove_cv_t<T>, N> ret;
for (std::size_t i = 0; i < N; ++i)
{
ret[i] = std::move(a[i]);
}
return ret;
}
// auxiliary classes
// todo: specilization of (t?)std::tuple_size and (t?)std::tuple_element
} // namespace tstd
#endif // TARRAY_HPP