This repository was archived by the owner on Aug 31, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 226
Expand file tree
/
Copy pathfoundation-span.h
More file actions
472 lines (398 loc) · 14.6 KB
/
foundation-span.h
File metadata and controls
472 lines (398 loc) · 14.6 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
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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
/* -*-C++-*-
* Copyright (C) 2016 LiveCode Ltd.
*
* This file is part of LiveCode.
*
* LiveCode is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License v3 as published
* by the Free Software Foundation.
*
* LiveCode is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with LiveCode. If not see
* <http://www.gnu.org/licenses/>.
*/
#ifndef __MC_FOUNDATION_SPAN_H__
#define __MC_FOUNDATION_SPAN_H__
/* An MCSpan<T> represents a "view" onto an array of values of type T.
* It is intended to be used anywhere where a pointer & length would
* otherwise be stored on the stack, either in function parameters or
* in local variables. Note that an MCSpan<T> does _not_ own the
* buffer to which it points; you should use an `MCAutoArray` or
* similar when you require an owned array.
*
* MCSpan<T> behaves like a pointer + length in many respects, but the
* key differences are that:
*
* - All accesses are checked
*
* - Its view onto the underlying array can only be narrowed, not
* widened
*
* MCSpan is heavily inspired by gsl::span from
* https://github.com/Microsoft/GSL
*/
/* In the C++ standard, operator[] for pointers is defined to take an
* argument of std::ptrdiff_t. This is used throughout this class in
* order to make sure that there is as much commonality as possible
* between the behaviour of an MCSpan and an array pointer. */
/* TODO[C++14] Some of the constexpr methods in MCSpan use assertions,
* and have to do ugly chaining using the comma operator in a return
* statement in order to comply with the C++11 restrictions on
* constexpr functions. When LiveCode is built with C++14, these
* methods should be refactored. */
#include "foundation.h"
#include <cstddef>
#include <iterator>
#include <type_traits>
static const std::ptrdiff_t kMCSpanDynamicExtent = -1;
namespace MC
{
namespace Details
{
template <typename Span, bool IsConst>
class MCSpanIterator
{
typedef typename Span::element_type element_type_;
public:
/* These type members are required in order to allow
* std::iterator_traits to work with this class. In C++17, this
* is a requirement for conformance with the Iterator concept. */
typedef typename std::random_access_iterator_tag iterator_category;
typedef typename Span::index_type difference_type;
typedef typename std::remove_const<element_type_>::type value_type;
typedef typename std::conditional<IsConst,
const element_type_,
element_type_>::type & reference;
typedef typename std::add_pointer<reference>::type pointer;
/* The const version of this MCSpanIterator type should be a
* friend so that its fields are accessible. N.b. the converse is
* _not_ true. */
friend class MCSpanIterator<Span, true>;
/* ---------- Constructors */
constexpr MCSpanIterator() : m_span(nullptr), m_index(0) {}
constexpr MCSpanIterator(const Span* p_span,
typename Span::index_type p_index)
: m_span(p_span),
m_index(p_index)
{
/* TODO[C++14] Some compilers don't allow statements in
* constexpr constructors yet */
#if NEEDS_CPP_14
MCAssert(p_span == nullptr ||
(p_index >= 0 && p_index < p_span->length())),
#endif
}
/* Allow the creation of an iterator over a const span from an
* iterator over a non-const span. */
constexpr MCSpanIterator(const MCSpanIterator<Span, false>& other)
: m_span(other.m_span), m_index(other.m_index) {}
/* ---------- Assignment ops */
/* TODO[C++11] MCSpanIterator& operator=(const MCSpanIterator& other) = default; */
MCSpanIterator& operator=(const MCSpanIterator& other)
{
m_span = other.m_span;
m_index = other.m_index;
return *this;
}
/* ---------- Element access */
constexpr reference operator*() const
{
return MCAssert(m_span != nullptr), (*m_span)[m_index];
}
constexpr pointer operator->() const
{
return MCAssert(m_span != nullptr), &((*m_span)[m_index]);
}
/* ---------- Traversal ops */
/* TODO[C++14] Make these operators constexpr */
MCSpanIterator& operator++()
{
return
MCAssert(m_span != nullptr &&
m_index >= 0 && m_index < m_span->length()),
++m_index,
*this;
}
MCSpanIterator operator++(int)
{
auto t_iter = *this;
++(*this);
return t_iter;
}
MCSpanIterator& operator--()
{
return
MCAssert(m_span != nullptr &&
m_index > 0 && m_index <= m_span->length()),
--m_index,
*this;
}
MCSpanIterator operator--(int)
{
auto t_iter = *this;
--(*this);
return t_iter;
}
MCSpanIterator operator+(difference_type p_offset) const
{
auto t_iter = *this;
return t_iter += p_offset;
}
MCSpanIterator& operator+=(difference_type p_offset)
{
return
MCAssert(m_span != nullptr &&
(m_index + p_offset) >= 0 &&
(m_index + p_offset) < m_span->length()),
m_index += p_offset,
*this;
}
MCSpanIterator operator-(difference_type p_offset) const
{
auto t_iter = *this;
return t_iter -= p_offset;
}
MCSpanIterator& operator-=(difference_type p_offset)
{
return *this += -p_offset;
}
/* ---------- Iterator comparisons */
/* Measure the distance between two iterators over the same
* span */
constexpr difference_type operator-(const MCSpanIterator& p_rhs) const
{
return MCAssert(m_span == p_rhs.m_span), m_index - p_rhs.m_index;
}
constexpr friend bool operator==(const MCSpanIterator& p_lhs,
const MCSpanIterator& p_rhs)
{
return p_lhs.m_span == p_rhs.m_span && p_lhs.m_index == p_rhs.m_index;
}
constexpr friend bool operator!=(const MCSpanIterator& p_lhs,
const MCSpanIterator& p_rhs)
{
return !(p_lhs == p_rhs);
}
constexpr friend bool operator<(const MCSpanIterator& p_lhs,
const MCSpanIterator& p_rhs)
{
return MCAssert(p_lhs.m_span == p_rhs.m_span),
p_lhs.m_index < p_rhs.m_index;
}
constexpr friend bool operator <=(const MCSpanIterator& p_lhs,
const MCSpanIterator& p_rhs)
{
return !(p_rhs < p_lhs);
}
constexpr friend bool operator >(const MCSpanIterator& p_lhs,
const MCSpanIterator& p_rhs)
{
return p_rhs < p_lhs;
}
constexpr friend bool operator >=(const MCSpanIterator& p_lhs,
const MCSpanIterator& p_rhs)
{
return !(p_rhs > p_lhs);
}
void swap(MCSpanIterator& p_other)
{
std::swap(m_index, p_other.m_index);
std::swap(m_span, p_other.m_span);
}
protected:
const Span* m_span;
difference_type m_index;
};
template <typename Span, bool IsConst>
constexpr MCSpanIterator<Span, IsConst>
operator+(typename MCSpanIterator<Span, IsConst>::difference_type n,
const MCSpanIterator<Span, IsConst>& p_rhs)
{
return p_rhs + n;
}
template <typename Span, bool IsConst>
constexpr MCSpanIterator<Span, IsConst>
operator-(typename MCSpanIterator<Span, IsConst>::difference_type n,
const MCSpanIterator<Span, IsConst>& p_rhs)
{
return p_rhs - n;
}
} /* namespace Details */
} /* namespace MC */
template <typename ElementType>
class MCSpan
{
public:
typedef ElementType element_type;
typedef std::ptrdiff_t index_type;
typedef element_type* pointer;
typedef element_type& reference;
typedef MC::Details::MCSpanIterator<MCSpan<element_type>, false> iterator;
typedef MC::Details::MCSpanIterator<MCSpan<element_type>, true> const_iterator;
typedef std::reverse_iterator<iterator> reverse_iterator;
typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
/* ---------- Constructors */
constexpr MCSpan() = default;
constexpr MCSpan(decltype(nullptr)) {}
constexpr MCSpan(pointer p_ptr, index_type p_count)
: m_data(p_ptr), m_length(p_count) {}
template <size_t N>
constexpr MCSpan(ElementType (&arr)[N])
: m_data(&arr[0]), m_length(N) {}
constexpr MCSpan(const MCSpan& other) = default;
constexpr MCSpan(MCSpan&& other) = default;
~MCSpan() = default;
/* ---------- Assignment ops */
MCSpan& operator=(const MCSpan& other) = default;
MCSpan& operator=(MCSpan&& other) = default;
/* ---------- Subspans */
constexpr MCSpan first(index_type p_count) const
{
return MCAssert(p_count >= 0 && p_count <= size()),
MCSpan(data(), p_count);
}
constexpr MCSpan last(index_type p_count) const
{
return MCAssert(p_count >= 0 && p_count <= size()),
MCSpan(data() + (size() - p_count), p_count);
}
constexpr MCSpan subspan(index_type p_offset,
index_type p_count = kMCSpanDynamicExtent) const
{
return
MCAssert(p_offset == 0 || (p_offset > 0 && p_offset <= size())),
MCAssert(p_count == kMCSpanDynamicExtent ||
(p_count >= 0 && p_offset + p_count <= size())),
MCSpan(data() + p_offset,
p_count == kMCSpanDynamicExtent ? size() - p_offset : p_count);
}
/* ---------- Observers */
constexpr index_type length() const { return size(); }
constexpr index_type size() const { return m_length; }
constexpr index_type lengthBytes() const { return sizeBytes(); }
constexpr index_type sizeBytes() const
{
return m_length * sizeof(ElementType);
}
constexpr bool empty() const { return size() == 0; }
/* ---------- Element access */
constexpr reference operator[](index_type p_index) const
{
return MCAssert(p_index >= 0 && p_index < size()),
data()[p_index];
}
constexpr reference operator*() const
{
return (*this)[0];
}
constexpr pointer operator->() const
{
return &((*this)[0]);
}
constexpr pointer data() const { return m_data; }
/* ---------- Iterator support */
iterator begin() const { return iterator(this, 0); }
iterator end() const { return iterator(this, length()); }
const_iterator cbegin() const { return const_iterator(this, 0); }
const_iterator cend() const { return const_iterator(this, length()); }
reverse_iterator rbegin() const { return reverse_iterator(end()); }
reverse_iterator rend() const { return reverse_iterator(begin()); }
const_reverse_iterator crbegin() const { return const_reverse_iterator(cend()); }
const_reverse_iterator crend() const { return const_reverse_iterator(cbegin()); }
protected:
MCSpan& advance(index_type p_offset)
{
MCAssert(p_offset == 0 || (p_offset > 0 && p_offset <= size()));
m_data += p_offset;
m_length -= p_offset;
return *this;
}
pointer m_data = nullptr;
index_type m_length = 0;
};
/* TODO[C++17] Remove when we have class and struct template type inference */
template<typename ElementType, size_t N>
constexpr MCSpan<ElementType>
MCMakeSpan(ElementType (&arr)[N])
{
return MCSpan<ElementType>(arr, N);
}
template <typename ElementType>
constexpr MCSpan<ElementType>
MCMakeSpan(ElementType *p_ptr,
typename MCSpan<ElementType>::index_type p_count)
{
return MCSpan<ElementType>(p_ptr, p_count);
}
template <typename ElementType = byte_t>
inline MCSpan<ElementType> MCDataGetSpan(MCDataRef p_data)
{
MCAssert(MCDataGetLength(p_data) % sizeof(ElementType) == 0);
auto t_ptr = reinterpret_cast<ElementType*>(MCDataGetBytePtr(p_data));
size_t t_length = MCDataGetLength(p_data) / sizeof(ElementType);
return MCMakeSpan(t_ptr, t_length);
}
/* ----------------------------------------------------------------
* Span-based overloads for pointer+range libfoundation functions
* ---------------------------------------------------------------- */
template <typename ElementType>
inline void MCMemoryClearSecure(MCSpan<ElementType> x_span)
{
MCMemoryClearSecure(reinterpret_cast<byte_t*>(x_span.data()),
x_span.sizeBytes());
}
MC_DLLEXPORT
bool MCArrayStoreValueOnPath(MCArrayRef array,
bool case_sensitive,
MCSpan<MCNameRef> path,
MCValueRef value);
MC_DLLEXPORT
bool MCArrayFetchValueOnPath(MCArrayRef array,
bool case_sensitive,
MCSpan<MCNameRef> path,
MCValueRef& r_value);
MC_DLLEXPORT
bool MCArrayRemoveValueOnPath(MCArrayRef array,
bool case_sensitive,
MCSpan<MCNameRef> path);
MC_DLLEXPORT
hash_t MCHashBytes(MCSpan<const byte_t> bytes);
MC_DLLEXPORT
hash_t MCHashBytesStream(hash_t previous,
MCSpan<const byte_t> bytes);
MC_DLLEXPORT
hash_t MCHashNativeChars(MCSpan<const char_t> chars);
MC_DLLEXPORT
hash_t MCHashChars(MCSpan<const unichar_t> chars);
template <typename ElementType>
inline hash_t MCHashSpan(MCSpan<ElementType> p_span)
{
/* TODO[C++11] Enable this assertion once all our C++ compilers support it. */
/* static_assert(std::is_trivially_copyable<ElementType>::value,
"MCHashObjectSpan can only be used with trivially copyable types"); */
return MCHashBytes(p_span.data(), p_span.sizeBytes());
}
template <typename ElementType>
inline hash_t MCHashSpanStream(hash_t p_previous,
MCSpan<ElementType> p_span)
{
/* TODO[C++11] Enable this assertion once all our C++ compilers support it. */
/* static_assert(std::is_trivially_copyable<ElementType>::value,
"MCHashObjectSpanStream can only be used with trivially copyable types"); */
return MCHashBytesStream(p_previous,
p_span.data(), p_span.sizeBytes());
}
template <typename ElementType>
inline bool MCProperListCreateWithForeignValues(MCTypeInfoRef p_typeinfo, const MCSpan<ElementType> p_values, MCProperListRef& r_list)
{
return MCProperListCreateWithForeignValues(p_typeinfo,
p_values.data(),
p_values.length(),
r_list);
}
#endif /* !__MC_FOUNDATION_SPAN_H__ */