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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
|
/*
* This source code is released into the public domain.
*/
module;
#include <cassert>
#include <cstdint>
#include <cstdlib>
#include <string>
#include <ucl.h>
export module nihil.ucl:array;
import :object;
namespace nihil::ucl {
export template<datatype T>
struct array;
template<datatype T>
struct array_iterator {
using difference_type = std::ptrdiff_t;
using value_type = T;
using reference = T&;
using pointer = T*;
array_iterator() = default;
auto operator* (this array_iterator const &self) -> T
{
auto uobj = ::ucl_array_find_index(self._array, self._idx);
if (uobj == nullptr)
throw error("failed to fetch UCL array index");
return T(nihil::ucl::ref, uobj);
}
auto operator[] (this array_iterator const &self,
difference_type idx)
-> T
{
return *(self + idx);
}
auto operator++ (this array_iterator &self) -> array_iterator&
{
++self._idx;
return self;
}
auto operator++ (this array_iterator &self, int) -> array_iterator
{
auto copy = self;
++self;
return copy;
}
auto operator-- (this array_iterator &self) -> array_iterator&
{
if (self._idx == 0)
throw std::out_of_range("attempt to iterate before "
"start of UCL array");
--self._idx;
return self;
}
auto operator-- (this array_iterator &self, int) -> array_iterator
{
auto copy = self;
--self;
return copy;
}
auto operator== (this array_iterator const &lhs,
array_iterator const &rhs)
-> bool
{
return lhs._idx == rhs._idx;
}
auto operator<=> (this array_iterator const &lhs,
array_iterator const &rhs)
{
return lhs._idx <=> rhs._idx;
}
auto operator+= (this array_iterator &lhs,
difference_type rhs)
-> array_iterator &
{
lhs._idx += rhs;
return lhs;
}
auto operator-= (this array_iterator &lhs,
difference_type rhs)
-> array_iterator &
{
lhs._idx -= rhs;
return lhs;
}
auto operator- (this array_iterator const &lhs,
array_iterator const &rhs)
-> difference_type
{
return lhs._idx - rhs._idx;
}
private:
friend struct array<T>;
::ucl_object_t const *_array{};
std::size_t _idx{};
array_iterator(::ucl_object_t const *array, std::size_t idx)
: _array(array)
, _idx(idx)
{}
};
export template<datatype T>
auto operator+(array_iterator<T> const &lhs,
typename array_iterator<T>::difference_type rhs)
-> array_iterator<T>
{
auto copy = lhs;
copy += rhs;
return copy;
}
export template<datatype T>
auto operator+(typename array_iterator<T>::difference_type lhs,
array_iterator<T> const &rhs)
-> array_iterator<T>
{
return rhs - lhs;
}
export template<datatype T>
auto operator-(array_iterator<T> const &lhs,
typename array_iterator<T>::difference_type rhs)
-> array_iterator<T>
{
auto copy = lhs;
copy -= rhs;
return copy;
}
export template<datatype T = object>
struct array final : object {
inline static constexpr object_type ucl_type = object_type::array;
using value_type = T;
using size_type = std::size_t;
using difference_type = std::ptrdiff_t;
// Create an empty array.
array() : object(noref, ::ucl_object_typed_new(UCL_ARRAY))
{
if (_object == nullptr)
throw error("failed to create UCL object");
}
// Create a new array from a UCL object.
array(ref_t, ::ucl_object_t const *uobj)
: object(nihil::ucl::ref, uobj)
{
if (type() != ucl_type)
throw type_mismatch(ucl_type, type());
}
array(noref_t, ::ucl_object_t *uobj)
: object(noref, uobj)
{
if (type() != ucl_type)
throw type_mismatch(ucl_type, type());
}
/*
* Create an array from an iterator pair.
*/
template<std::input_iterator Iterator>
requires(std::convertible_to<std::iter_value_t<Iterator>, T>)
array(Iterator first, Iterator last)
: array()
{
if (_object == nullptr)
throw error("failed to create UCL object");
// This is exception safe, because if we throw here the
// base class destructor will free the array.
while (first != last) {
push_back(*first);
++first;
}
}
/*
* Create an array from a range.
*/
template<std::ranges::range Range>
requires(std::convertible_to<std::ranges::range_value_t<Range>, T>)
array(std::from_range_t, Range &&range)
: array(std::ranges::begin(range),
std::ranges::end(range))
{
}
/*
* Create an array from an initializer_list.
*/
array(std::initializer_list<T> const &list)
: array(std::ranges::begin(list),
std::ranges::end(list))
{
}
/*
* Array iterator access.
*/
auto begin(this array const &self) -> array_iterator<T>
{
return {self.get_ucl_object(), 0};
}
auto end(this array const &self) -> array_iterator<T>
{
return {self.get_ucl_object(), self.size()};
}
/*
* Return the size of this array.
*/
auto size(this array const &self) -> size_type
{
return ::ucl_array_size(self.get_ucl_object());
}
/*
* Test if this array is empty.
*/
auto empty(this array const &self) -> bool
{
return self.size() == 0;
}
/*
* Reserve space for future insertions.
*/
auto reserve(this array &self, size_type nelems) -> void
{
::ucl_object_reserve(self.get_ucl_object(), nelems);
}
/*
* Append an element to the array.
*/
auto push_back(this array &self, value_type const &v) -> void
{
auto uobj = ::ucl_object_ref(v.get_ucl_object());
::ucl_array_append(self.get_ucl_object(), uobj);
}
/*
* Prepend an element to the array.
*/
auto push_front(this array &self, value_type const &v) -> void
{
auto uobj = ::ucl_object_ref(v.get_ucl_object());
::ucl_array_prepend(self.get_ucl_object(), uobj);
}
/*
* Access an array element by index.
*/
auto at(this array const &self, size_type idx) -> T
{
if (idx >= self.size())
throw std::out_of_range("UCL array index out of range");
auto uobj = ::ucl_array_find_index(self.get_ucl_object(), idx);
if (uobj == nullptr)
throw error("failed to fetch UCL array index");
return T(nihil::ucl::ref, uobj);
}
auto operator[] (this array const &self, size_type idx) -> T
{
return self.at(idx);
}
/*
* Return the first element.
*/
auto front(this array const &self) -> T
{
return self.at(0);
}
/*
* Return the last element.
*/
auto back(this array const &self) -> T
{
if (self.empty())
throw std::out_of_range("attempt to access back() on "
"empty UCL array");
return self.at(self.size() - 1);
}
};
/*
* Comparison operators.
*/
export template<datatype T>
auto operator==(array<T> const &a, array<T> const &b) -> bool
{
if (a.size() != b.size())
return false;
for (typename array<T>::size_type i = 0; i < a.size(); ++i)
if (a.at(i) != b.at(i))
return false;
return true;
}
} // namespace nihil::ucl
|