-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathts_array.hpp
More file actions
264 lines (218 loc) · 5.37 KB
/
ts_array.hpp
File metadata and controls
264 lines (218 loc) · 5.37 KB
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
#ifndef TS_ARRAY_HPP
#define TS_ARRAY_HPP
#include "ts_uninitialized.hpp"
#include <algorithm>
#include <cstddef>
#include <initializer_list>
#include <stdexcept>
#include <utility>
namespace TS
{
template <typename T, std::size_t N> class array;
template <typename T, std::size_t N>
bool operator==(const array<T, N> &lhs, const array<T, N> &rhs);
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 = value_type *;
using const_pointer = const value_type *;
using iterator = value_type *;
using const_iterator = const value_type *;
protected:
using self = array<T, N>;
public:
~array() = default;
array() : _data{}
{
}
array(const self &other)
{
uninitialized_copy_n(other._data, other.size(), _data);
}
array(self &&other) noexcept
{
for (size_type i = 0; i < N; i++)
{
_data[i] = std::move(other._data[i]);
}
}
array(std::initializer_list<T> init)
{
if (init.size() > N)
{
throw std::out_of_range("Too many elements in initializer list");
}
uninitialized_copy_n(init.begin(), init.size(), _data);
uninitialized_fill_n(_data + init.size(), N - init.size(), T());
}
self &operator=(const self &other)
{
if (this != &other)
{
uninitialized_copy_n(other._data, other.size(), _data);
}
return *this;
}
self &operator=(self &&other) noexcept
{
if (this != &other)
{
for (size_type i = 0; i < N; ++i)
{
_data[i] = std::move(other._data[i]);
}
}
return *this;
}
reference at(size_type pos)
{
return const_cast<reference>(static_cast<const self *>(this)->at(pos));
}
const_reference at(size_type pos) const
{
if (pos < 0 || pos >= N)
{
throw std::out_of_range("out of range");
}
return operator[](pos);
}
reference operator[](size_type pos)
{
return const_cast<reference>(static_cast<const self *>(this)->operator[](pos));
}
const_reference operator[](size_type pos) const
{
return _data[pos];
}
reference front()
{
return const_cast<reference>(static_cast<const self *>(this)->front());
}
const_reference front() const
{
if (empty())
{
throw std::logic_error("empty vector");
}
return *_data;
}
reference back()
{
return const_cast<reference>(static_cast<const self *>(this)->back());
}
const_reference back() const
{
if (empty())
{
throw std::logic_error("empty vector");
}
return _data[N - 1];
}
pointer data() noexcept
{
return const_cast<pointer>(static_cast<const self *>(this)->data());
}
const_pointer data() const noexcept
{
return _data;
}
iterator begin() noexcept
{
return const_cast<iterator>(static_cast<const self *>(this)->begin());
}
const_iterator begin() const noexcept
{
return _data;
}
const_iterator cbegin() const noexcept
{
return _data;
}
iterator end() noexcept
{
return const_cast<iterator>(static_cast<const self *>(this)->end());
}
const_iterator end() const noexcept
{
return _data + N;
}
const_iterator cend() const noexcept
{
return _data + N;
}
constexpr bool empty() const
{
return N == 0;
}
constexpr size_type size() const
{
return N;
}
constexpr size_type max_size() const
{
return N;
}
void fill(const T &val)
{
uninitialized_fill_n(_data, N, val);
}
void swap(self &other) noexcept
{
if (this == &other)
{
return;
}
for (size_type i = 0; i < N; ++i)
{
std::swap(_data[i], other._data[i]);
}
}
friend bool operator== <T, N>(const array<T, N> &lhs, const array<T, N> &rhs);
protected:
T _data[N];
};
template <typename T, std::size_t N> bool operator==(const array<T, N> &lhs, const array<T, N> &rhs)
{
if (lhs.size() != rhs.size())
{
return false;
}
for (std::size_t i = 0; i < N; ++i)
{
if (lhs[i] != rhs[i])
{
return false;
}
}
return true;
}
template <std::size_t I, typename T, std::size_t N> T &get(array<T, N> &a) noexcept
{
return const_cast<T &>(get<I>(static_cast<const array<T, N> &>(a)));
}
template <std::size_t I, typename T, std::size_t N> T &&get(array<T, N> &&a) noexcept
{
static_assert(I >= 0 && I < N);
return std::move(a[I]);
}
template <std::size_t I, typename T, std::size_t N> const T &get(const array<T, N> &a) noexcept
{
static_assert(I >= 0 && I < N);
return a[I];
}
template <std::size_t I, typename T, std::size_t N> const T &&get(const array<T, N> &&a) noexcept
{
static_assert(I >= 0 && I < N);
return std::move(a[I]);
}
template <typename T, std::size_t N> void swap(array<T, N> &lhs, array<T, N> &rhs)
{
lhs.swap(rhs);
}
} // namespace TS
#endif