-
Notifications
You must be signed in to change notification settings - Fork 17
/
Array.hpp
277 lines (212 loc) · 6.26 KB
/
Array.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
276
277
#pragma once
#include <cstddef> // size_t
#include <stdexcept> // std::out_of_range
#include <iterator> // std::reverse_iterator
#include <algorithm> // std::equal
#include "_Common.hpp"
// C++ 标准规定:单下划线+大写字母(_Identifier)或 双下划线+小写字母(__identifier)的标识符是保留字。理论上用户不得使用,只允许标准库和编译器使用。此处小彭老师打算只在最简单的 array 容器中严格服从一下这个规则作为演示,正经标准库里的代码都是这样的(为避免和用户定义的符号产生冲突),此后其他容器的课程都会用日常的写法,不给同学平添阅读难度
template <class _Tp, size_t _N>
struct Array {
using value_type = _Tp;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
using pointer = _Tp *;
using const_pointer = _Tp const *;
using reference = _Tp &;
using const_reference = _Tp const &;
using iterator = _Tp *;
using const_iterator = _Tp const *;
using reverse_iterator = std::reverse_iterator<_Tp *>;
using const_reverse_iterator = std::reverse_iterator<_Tp const *>;
_Tp _M_elements[_N];
_Tp &operator[](size_t __i) noexcept {
return _M_elements[__i];
}
_Tp const &operator[](size_t __i) const noexcept {
return _M_elements[__i];
}
_Tp &at(size_t __i) {
if (__i >= _N) [[__unlikely__]]
throw std::out_of_range("array::at");
return _M_elements[__i];
}
_Tp const &at(size_t __i) const {
if (__i >= _N) [[__unlikely__]]
throw std::out_of_range("array::at");
return _M_elements[__i];
}
void fill(_Tp const &__val) noexcept(std::is_nothrow_copy_assignable_v<_Tp>) {
for (size_t __i = 0; __i < _N; __i++) {
_M_elements[__i] = __val;
}
}
void swap(Array &__that) noexcept(std::is_nothrow_swappable_v<_Tp>) {
for (size_t __i = 0; __i < _N; __i++) {
std::swap(_M_elements[__i], __that._M_elements[__i]);
}
}
_Tp &front() noexcept {
return _M_elements[0];
}
_Tp const &front() const noexcept {
return _M_elements[0];
}
_Tp &back() noexcept {
return _M_elements[_N - 1];
}
_Tp const &back() const noexcept {
return _M_elements[_N - 1];
}
static constexpr bool empty() noexcept {
return false;
}
static constexpr size_t size() noexcept {
return _N;
}
static constexpr size_t max_size() noexcept {
return _N;
}
_Tp const *cdata() const noexcept {
return _M_elements;
}
_Tp const *data() const noexcept {
return _M_elements;
}
_Tp *data() noexcept {
return _M_elements;
}
_Tp const *cbegin() const noexcept {
return _M_elements;
}
_Tp const *cend() const noexcept {
return _M_elements + _N;
}
_Tp const *begin() const noexcept {
return _M_elements;
}
_Tp const *end() const noexcept {
return _M_elements + _N;
}
_Tp *begin() noexcept {
return _M_elements;
}
_Tp *end() noexcept {
return _M_elements + _N;
}
const_reverse_iterator crbegin() const noexcept {
return const_reverse_iterator(end());
}
const_reverse_iterator crend() const noexcept {
return const_reverse_iterator(begin());
}
const_reverse_iterator rbegin() const noexcept {
return const_reverse_iterator(end());
}
const_reverse_iterator rend() const noexcept {
return const_reverse_iterator(begin());
}
reverse_iterator rbegin() noexcept {
return reverse_iterator(end());
}
reverse_iterator rend() noexcept {
return reverse_iterator(begin());
}
_LIBPENGCXX_DEFINE_COMPARISON(Array);
};
template <class _Tp>
struct Array<_Tp, 0> {
using value_type = _Tp;
using pointer = _Tp *;
using const_pointer = _Tp const *;
using reference = _Tp &;
using const_reference = _Tp const &;
using iterator = _Tp *;
using const_iterator = _Tp const *;
using reverse_iterator = _Tp *;
using const_reverse_iterator = _Tp const *;
_Tp &operator[](size_t __i) noexcept {
_LIBPENGCXX_UNREACHABLE();
}
_Tp const &operator[](size_t __i) const noexcept {
_LIBPENGCXX_UNREACHABLE();
}
_Tp &at(size_t __i) {
throw std::out_of_range("array::at");
}
_Tp const &at(size_t __i) const {
throw std::out_of_range("array::at");
}
void fill(_Tp const &) noexcept {
}
void swap(Array &) noexcept {
}
_Tp &front() noexcept {
_LIBPENGCXX_UNREACHABLE();
}
_Tp const &front() const noexcept {
_LIBPENGCXX_UNREACHABLE();
}
_Tp &back() noexcept {
_LIBPENGCXX_UNREACHABLE();
}
_Tp const &back() const noexcept {
_LIBPENGCXX_UNREACHABLE();
}
static constexpr bool empty() noexcept {
return true;
}
static constexpr size_t size() noexcept {
return 0;
}
static constexpr size_t max_size() noexcept {
return 0;
}
_Tp const *cdata() const noexcept {
return nullptr;
}
_Tp const *data() const noexcept {
return nullptr;
}
_Tp *data() noexcept {
return nullptr;
}
_Tp const *cbegin() const noexcept {
return nullptr;
}
_Tp const *cend() const noexcept {
return nullptr;
}
_Tp const *begin() const noexcept {
return nullptr;
}
_Tp const *end() const noexcept {
return nullptr;
}
_Tp *begin() noexcept {
return nullptr;
}
_Tp *end() noexcept {
return nullptr;
}
_Tp const *crbegin() const noexcept {
return nullptr;
}
_Tp const *crend() const noexcept {
return nullptr;
}
_Tp const *rbegin() const noexcept {
return nullptr;
}
_Tp const *rend() const noexcept {
return nullptr;
}
_Tp *rbegin() noexcept {
return nullptr;
}
_Tp *rend() noexcept {
return nullptr;
}
_LIBPENGCXX_DEFINE_COMPARISON(Array);
};
template <class _Tp, class ..._Ts>
Array(_Tp, _Ts...) -> Array<std::enable_if_t<(std::is_same_v<_Tp, _Ts> && ...), _Tp>, 1 + sizeof...(_Ts)>;