-
Notifications
You must be signed in to change notification settings - Fork 3
/
span.cpp
806 lines (658 loc) · 31.2 KB
/
span.cpp
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
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
// -*- C++ -*-
//===------------------------------ span ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is dual licensed under the MIT and the University of Illinois Open
// Source Licenses. See LICENSE.TXT for details.
//
//===---------------------------------------------------------------------===//
#ifndef _LIBCPP_SPAN
#define _LIBCPP_SPAN
/*
span synopsis
namespace std {
// constants
inline constexpr ptrdiff_t dynamic_extent = -1;
// [views.span], class template span
template <class ElementType, ptrdiff_t Extent = dynamic_extent> class span;
// [span.comparison], span comparison operators
template <class T, ptrdiff_t X, class U, ptrdiff_t Y>
constexpr bool operator==(span<T, X> l, span<U, Y> r);
template <class T, ptrdiff_t X, class U, ptrdiff_t Y>
constexpr bool operator!=(span<T, X> l, span<U, Y> r);
template <class T, ptrdiff_t X, class U, ptrdiff_t Y>
constexpr bool operator<(span<T, X> l, span<U, Y> r);
template <class T, ptrdiff_t X, class U, ptrdiff_t Y>
constexpr bool operator<=(span<T, X> l, span<U, Y> r);
template <class T, ptrdiff_t X, class U, ptrdiff_t Y>
constexpr bool operator>(span<T, X> l, span<U, Y> r);
template <class T, ptrdiff_t X, class U, ptrdiff_t Y>
constexpr bool operator>=(span<T, X> l, span<U, Y> r);
// [span.objectrep], views of object representation
template <class ElementType, ptrdiff_t Extent>
span<const byte, ((Extent == dynamic_extent) ? dynamic_extent :
(static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent))> as_bytes(span<ElementType, Extent> s) noexcept;
template <class ElementType, ptrdiff_t Extent>
span< byte, ((Extent == dynamic_extent) ? dynamic_extent :
(static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent))> as_writable_bytes(span<ElementType, Extent> s) noexcept;
namespace std {
template <class ElementType, ptrdiff_t Extent = dynamic_extent>
class span {
public:
// constants and types
using element_type = ElementType;
using value_type = remove_cv_t<ElementType>;
using index_type = ptrdiff_t;
using difference_type = ptrdiff_t;
using pointer = element_type*;
using reference = element_type&;
using iterator = implementation-defined;
using const_iterator = implementation-defined;
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = Extent;
// [span.cons], span constructors, copy, assignment, and destructor
constexpr span() noexcept;
constexpr span(pointer ptr, index_type count);
constexpr span(pointer firstElem, pointer lastElem);
template <size_t N>
constexpr span(element_type (&arr)[N]) noexcept;
template <size_t N>
constexpr span(array<value_type, N>& arr) noexcept;
template <size_t N>
constexpr span(const array<value_type, N>& arr) noexcept;
template <class Container>
constexpr span(Container& cont);
template <class Container>
constexpr span(const Container& cont);
constexpr span(const span& other) noexcept = default;
template <class OtherElementType, ptrdiff_t OtherExtent>
constexpr span(const span<OtherElementType, OtherExtent>& s) noexcept;
~span() noexcept = default;
constexpr span& operator=(const span& other) noexcept = default;
// [span.sub], span subviews
template <ptrdiff_t Count>
constexpr span<element_type, Count> first() const;
template <ptrdiff_t Count>
constexpr span<element_type, Count> last() const;
template <ptrdiff_t Offset, ptrdiff_t Count = dynamic_extent>
constexpr span<element_type, see below> subspan() const;
constexpr span<element_type, dynamic_extent> first(index_type count) const;
constexpr span<element_type, dynamic_extent> last(index_type count) const;
constexpr span<element_type, dynamic_extent> subspan(index_type offset, index_type count = dynamic_extent) const;
// [span.obs], span observers
constexpr index_type size() const noexcept;
constexpr index_type size_bytes() const noexcept;
constexpr bool empty() const noexcept;
// [span.elem], span element access
constexpr reference operator[](index_type idx) const;
constexpr reference operator()(index_type idx) const;
constexpr pointer data() const noexcept;
// [span.iterators], span iterator support
constexpr iterator begin() const noexcept;
constexpr iterator end() const noexcept;
constexpr const_iterator cbegin() const noexcept;
constexpr const_iterator cend() const noexcept;
constexpr reverse_iterator rbegin() const noexcept;
constexpr reverse_iterator rend() const noexcept;
constexpr const_reverse_iterator crbegin() const noexcept;
constexpr const_reverse_iterator crend() const noexcept;
private:
pointer data_; // exposition only
index_type size_; // exposition only
};
template<class T, size_t N>
span(T (&)[N]) -> span<T, N>;
template<class T, size_t N>
span(array<T, N>&) -> span<T, N>;
template<class T, size_t N>
span(const array<T, N>&) -> span<const T, N>;
template<class Container>
span(Container&) -> span<typename Container::value_type>;
template<class Container>
span(const Container&) -> span<const typename Container::value_type>;
} // namespace std
*/
#include <cstddef> // for std::ptrdiff_t
#include <iterator> // for iterators
#include <array> // for std::array
#include <type_traits> // for std::remove_cv, etc
#include <cstddef> // for std::byte
#include <cassert>
inline constexpr std::ptrdiff_t dynamic_extent = -1;
template <typename _Tp, ptrdiff_t _Extent = dynamic_extent> class span;
template <class _Tp>
struct __is_span_impl : public std::false_type {};
template <class _Tp, ptrdiff_t _Extent>
struct __is_span_impl<span<_Tp, _Extent>> : public std::true_type {};
template <class _Tp>
struct __is_span : public __is_span_impl<std::remove_cv_t<_Tp>> {};
template <class _Tp>
struct __is_std_array_impl : public std::false_type {};
template <class _Tp, size_t _N>
struct __is_std_array_impl<std::array<_Tp, _N>> : public std::true_type {};
template <class _Tp>
struct __is_std_array : public __is_std_array_impl<std::remove_cv_t<_Tp>> {};
template <class _Tp, class _ElementType>
struct __is_span_compatible_ptr :
public std::bool_constant<std::is_convertible_v<_Tp(*)[], _ElementType(*)[]>> {};
template <class _Tp, class _ElementType, class = void>
struct __is_span_compatible_container : public std::false_type {};
template <class _Tp, class _ElementType>
struct __is_span_compatible_container<_Tp, _ElementType,
std::void_t<
// is not a specialization of span
typename std::enable_if<!__is_span<_Tp>::value, std::nullptr_t>::type,
// is not a specialization of array
typename std::enable_if<!__is_std_array<_Tp>::value, std::nullptr_t>::type,
// data(cont) and size(cont) are well formed
decltype(std::data(std::declval<_Tp>())),
decltype(std::size(std::declval<_Tp>())),
// remove_pointer_t<decltype(data(cont))>(*)[] is convertible to ElementType(*)[]
typename std::enable_if<
__is_span_compatible_ptr<
std::remove_pointer_t<decltype(std::data(std::declval<_Tp &>()))>,
_ElementType>
::value,
std::nullptr_t>::type
>>
: public std::true_type {};
template <typename _Tp, ptrdiff_t _Extent>
class span {
public:
// constants and types
using element_type = _Tp;
using value_type = std::remove_cv_t<_Tp>;
using index_type = std::ptrdiff_t;
using difference_type = std::ptrdiff_t;
using pointer = _Tp *;
using reference = _Tp &;
using iterator = pointer; // TODO libc++ wrap iterator
using const_iterator = const _Tp *; // TODO libc++ wrap iterator
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = _Extent;
static_assert (_Extent >= 0, "Can't have a span with an extent < 0");
// [span.cons], span constructors, copy, assignment, and destructor
constexpr span() : __data{nullptr}
{ static_assert(_Extent == 0); }
constexpr span (const span& __rhs) noexcept = default;
constexpr span& operator=(const span& __rhs) noexcept = default;
constexpr span(pointer __ptr, index_type __count) : __data{__ptr}
{ assert(_Extent == __count); }
constexpr span(pointer __f, pointer __l) : __data{__f}
{ assert(_Extent == std::distance(__f, __l)); }
constexpr span(element_type (&__arr)[_Extent]) : __data{__arr} {}
// TODO This should not be a template - make _N and _Extent the same
template <size_t _N>
constexpr span(std::array<std::remove_const_t<element_type>, _N>& __arr)
: __data{__arr.data()}
{ static_assert(_Extent == _N); }
// TODO This should not be a template - make _N and _Extent the same
template <size_t _N>
constexpr span(const std::array<std::remove_const_t<element_type>, _N>& __arr)
: __data{__arr.data()}
{ static_assert(_Extent == _N); }
template <class _Container>
constexpr span( _Container& __c,
const std::enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, std::nullptr_t> = nullptr)
: __data{std::data(__c)}
{ assert(_Extent == std::size(__c)); }
template <class _Container>
constexpr span(const _Container& __c,
const std::enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, std::nullptr_t> = nullptr)
: __data{std::data(__c)}
{ assert(_Extent == std::size(__c)); }
// TODO This should not be a template - make _N and _Extent the same
// Note: Weird to make a static extent span from a dynamic extent one
template <class _OtherElementType, std::ptrdiff_t _OtherExtent>
constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
const std::enable_if_t<
std::is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
std::nullptr_t> = nullptr)
: __data{__other.data()}
{
static_assert(_Extent == _OtherExtent || _OtherExtent == dynamic_extent);
assert(_Extent == __other.size());
}
~span() noexcept = default;
// TODO make noexcept in libc++
template <std::ptrdiff_t _Count>
constexpr span<element_type, _Count> first() const
{
static_assert(_Count >= 0);
assert(_Count <= size());
return {data(), _Count};
}
// TODO make noexcept in libc++
template <std::ptrdiff_t _Count>
constexpr span<element_type, _Count> last() const
{
static_assert(_Count >= 0);
assert(_Count <= size());
return {data() + size() - _Count, _Count};
}
// TODO make noexcept in libc++
constexpr span<element_type, dynamic_extent> first(index_type __count) const
{
assert(__count >= 0 && __count <= size());
return {data(), __count};
}
// TODO make noexcept in libc++
constexpr span<element_type, dynamic_extent> last(index_type __count) const
{
assert(__count >= 0 && __count <= size());
return {data() + size() - __count, __count};
}
// TODO make noexcept in libc++
template <std::ptrdiff_t _Offset, std::ptrdiff_t _Count = dynamic_extent>
constexpr auto subspan() const
-> span<element_type, _Count != dynamic_extent ? _Count : _Extent - _Offset>
{
assert(_Offset >= 0 && _Offset <= size());
return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
}
// TODO make noexcept in libc++
constexpr span<element_type, dynamic_extent> subspan(index_type __offset,
index_type __count = dynamic_extent) const
{
// Still have to deal with count == -1 here
assert( __offset >= 0 && __offset <= size());
assert((__count >= 0 && __count <= size()) || __count == dynamic_extent);
if (__count == dynamic_extent)
return {data() + __offset, size() - __offset};
assert(__offset + __count <= size());
return {data() + __offset, __count};
}
constexpr index_type size() const noexcept { return _Extent; }
constexpr index_type size_bytes() const noexcept { return _Extent * sizeof(element_type); }
constexpr bool empty() const noexcept { return _Extent == 0; }
constexpr reference operator[](index_type __idx) const { return __data[__idx]; } // TODO make noexcept in libc++
constexpr reference operator()(index_type __idx) const { return __data[__idx]; } // TODO make noexcept in libc++
constexpr pointer data() const noexcept { return __data; }
// [span.iter], span iterator support
constexpr iterator begin() const noexcept { return data(); }
constexpr iterator end() const noexcept { return data() + size(); }
constexpr const_iterator cbegin() const noexcept { return data(); }
constexpr const_iterator cend() const noexcept { return data() + size(); }
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
constexpr void swap(span &__other) noexcept
{
pointer __p = __data;
__data = __other.__data;
__other.__data = __p;
}
span<const std::byte, _Extent * sizeof(element_type)> __as_bytes() const noexcept
{ return {reinterpret_cast<const std::byte *>(data()), size_bytes()}; }
span<std::byte, _Extent * sizeof(element_type)> __as_writeable_bytes() const noexcept
{ return {reinterpret_cast<std::byte *>(data()), size_bytes()}; }
private:
pointer __data;
};
template <typename _Tp>
class span<_Tp, dynamic_extent> {
private:
public:
// constants and types
using element_type = _Tp;
using value_type = std::remove_cv_t<_Tp>;
using index_type = std::ptrdiff_t;
using difference_type = std::ptrdiff_t;
using pointer = _Tp*;
using reference = _Tp&;
using iterator = pointer; // TODO libc++ wrap iterator
using const_iterator = const _Tp *; // TODO libc++ wrap iterator
using reverse_iterator = std::reverse_iterator<iterator>;
using const_reverse_iterator = std::reverse_iterator<const_iterator>;
static constexpr index_type extent = dynamic_extent;
// [span.cons], span constructors, copy, assignment, and destructor
constexpr span() : __data{nullptr}, __size{0} {}
constexpr span (const span& __rhs) noexcept = default;
constexpr span& operator=(const span& __rhs) noexcept = default;
constexpr span(pointer __ptr, index_type __count) : __data{__ptr}, __size{__count} {}
constexpr span(pointer __f, pointer __l) : __data{__f}, __size{std::distance(__f, __l)} {}
template <size_t _N>
constexpr span(element_type (&__arr)[_N]) : __data{__arr}, __size{_N} {}
template <size_t _N>
constexpr span(std::array<std::remove_const_t<element_type>, _N>& __arr)
: __data{__arr.data()}, __size{_N} {}
template <size_t _N>
constexpr span(const std::array<std::remove_const_t<element_type>, _N>& __arr)
: __data{__arr.data()}, __size{_N} {}
template <class _Container>
constexpr span( _Container& __c,
const std::enable_if_t<__is_span_compatible_container<_Container, _Tp>::value, std::nullptr_t> = nullptr)
: __data{std::data(__c)}, __size{(index_type) std::size(__c)} {}
template <class _Container>
constexpr span(const _Container& __c,
const std::enable_if_t<__is_span_compatible_container<const _Container, _Tp>::value, std::nullptr_t> = nullptr)
: __data{std::data(__c)}, __size{(index_type) std::size(__c)} {}
template <class _OtherElementType, std::ptrdiff_t _OtherExtent>
constexpr span(const span<_OtherElementType, _OtherExtent>& __other,
const std::enable_if_t<
std::is_convertible_v<_OtherElementType(*)[], element_type (*)[]>,
std::nullptr_t> = nullptr)
: __data{__other.data()}, __size{__other.size()} {}
~span() noexcept = default;
// TODO make noexcept in libc++
template <std::ptrdiff_t _Count>
constexpr span<element_type, _Count> first() const
{
static_assert(_Count >= 0);
assert(_Count <= size());
return {data(), _Count};
}
// TODO make noexcept in libc++
template <std::ptrdiff_t _Count>
constexpr span<element_type, _Count> last() const
{
static_assert(_Count >= 0);
assert(_Count <= size());
return {data() + size() - _Count, _Count};
}
// TODO make noexcept in libc++
constexpr span<element_type, dynamic_extent> first(index_type __count) const
{
assert(__count >= 0 && __count <= size());
return {data(), __count};
}
constexpr span<element_type, dynamic_extent> last (index_type __count) const
{
assert(__count >= 0 && __count <= size());
return {data() + size() - __count, __count};
}
// TODO make noexcept in libc++
template <std::ptrdiff_t _Offset, std::ptrdiff_t _Count = dynamic_extent>
constexpr span<_Tp, dynamic_extent> subspan() const
{
assert(_Offset >= 0 && _Offset <= size());
assert(_Count == dynamic_extent || _Offset + _Count <= size());
return {data() + _Offset, _Count == dynamic_extent ? size() - _Offset : _Count};
}
// TODO make noexcept in libc++
constexpr span<element_type, dynamic_extent> subspan(index_type __offset,
index_type __count = dynamic_extent) const
{
assert( __offset >= 0 && __offset <= size());
assert((__count >= 0 && __count <= size()) || __count == dynamic_extent);
if (__count == dynamic_extent)
return {data() + __offset, size() - __offset};
assert(__offset + __count <= size());
return {data() + __offset, __count};
}
constexpr index_type size() const noexcept { return __size; }
constexpr index_type size_bytes() const noexcept { return __size * sizeof(element_type); }
constexpr bool empty() const noexcept { return __size == 0; }
constexpr reference operator[](index_type __idx) const { return __data[__idx]; } // TODO make noexcept in libc++
constexpr reference operator()(index_type __idx) const { return __data[__idx]; } // TODO make noexcept in libc++
constexpr pointer data() const noexcept { return __data; }
// [span.iter], span iterator support
constexpr iterator begin() const noexcept { return data(); }
constexpr iterator end() const noexcept { return data() + size(); }
constexpr const_iterator cbegin() const noexcept { return data(); }
constexpr const_iterator cend() const noexcept { return data() + size(); }
constexpr reverse_iterator rbegin() const noexcept { return reverse_iterator(end()); }
constexpr reverse_iterator rend() const noexcept { return reverse_iterator(begin()); }
constexpr const_reverse_iterator crbegin() const noexcept { return const_reverse_iterator(cend()); }
constexpr const_reverse_iterator crend() const noexcept { return const_reverse_iterator(cbegin()); }
constexpr void swap(span &__other) noexcept
{
pointer __p = __data;
__data = __other.__data;
__other.__data = __p;
index_type __sz = __size;
__size = __other.__size;
__other.__size = __sz;
}
span<const std::byte, dynamic_extent> __as_bytes() const noexcept
{ return {reinterpret_cast<const std::byte *>(data()), size_bytes()}; }
span<std::byte, dynamic_extent> __as_writeable_bytes() const noexcept
{ return {reinterpret_cast<std::byte *>(data()), size_bytes()}; }
private:
pointer __data;
index_type __size;
};
template <class _Tp1, class _Tp2, class = void>
struct __is_equality_comparable : public std::false_type {};
template <class _Tp1, class _Tp2>
struct __is_equality_comparable<
_Tp1, _Tp2,
std::void_t<std::enable_if_t<std::is_convertible_v<bool,
decltype(std::declval<const _Tp1&>() == std::declval<const _Tp2 &>())>>, std::nullptr_t>>
: public std::true_type {};
template <class _Tp1, class _Tp2, class = void>
struct __is_less_than_comparable : public std::false_type {};
template <class _Tp1, class _Tp2>
struct __is_less_than_comparable<
_Tp1, _Tp2,
std::void_t<std::enable_if_t<std::is_convertible_v<bool,
decltype(std::declval<const _Tp1&>() < std::declval<const _Tp2 &>())>>, std::nullptr_t>>
: public std::true_type {};
// TODO - update P0805 - SFINAE away the comparisons if the underlying types are not comparable
template <class _Tp1, std::ptrdiff_t _Extent1, class _Tp2, std::ptrdiff_t _Extent2>
constexpr typename std::enable_if<__is_equality_comparable<_Tp1, _Tp2>::value, bool>::type
operator==(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs)
{ return std::equal(__lhs.begin(), __lhs.end(), __rhs.begin(), __rhs.end()); }
template <class _Tp1, std::ptrdiff_t _Extent1, class _Tp2, std::ptrdiff_t _Extent2>
constexpr typename std::enable_if<__is_equality_comparable<_Tp1, _Tp2>::value, bool>::type
operator!=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs)
{ return !(__rhs == __lhs); }
template <class _Tp1, std::ptrdiff_t _Extent1, class _Tp2, std::ptrdiff_t _Extent2>
constexpr typename std::enable_if<__is_less_than_comparable<_Tp1, _Tp2>::value, bool>::type
operator< (const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs)
{ return std::lexicographical_compare (__lhs.begin(), __lhs.end(), __rhs.begin(), __rhs.end()); }
template <class _Tp1, std::ptrdiff_t _Extent1, class _Tp2, std::ptrdiff_t _Extent2>
constexpr typename std::enable_if<__is_less_than_comparable<_Tp2, _Tp1>::value, bool>::type
operator<=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs)
{ return !(__rhs < __lhs); }
template <class _Tp1, std::ptrdiff_t _Extent1, class _Tp2, std::ptrdiff_t _Extent2>
constexpr typename std::enable_if<__is_less_than_comparable<_Tp2, _Tp1>::value, bool>::type
operator> (const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs)
{ return __rhs < __lhs; }
template <class _Tp1, std::ptrdiff_t _Extent1, class _Tp2, std::ptrdiff_t _Extent2>
constexpr typename std::enable_if<__is_less_than_comparable<_Tp1, _Tp2>::value, bool>::type
operator>=(const span<_Tp1, _Extent1>& __lhs, const span<_Tp2, _Extent2>& __rhs)
{ return !(__lhs < __rhs); }
// as_bytes & as_writeable_bytes
template <class _Tp, ptrdiff_t _Extent>
auto as_bytes(span<_Tp, _Extent> __s) noexcept
-> decltype(__s.__as_bytes())
{ return __s.__as_bytes(); }
template <class _Tp, ptrdiff_t _Extent>
auto as_writeable_bytes(span<_Tp, _Extent> __s) noexcept
-> typename std::enable_if<!std::is_const_v<_Tp>, decltype(__s.__as_writeable_bytes())>::type
{ return __s.__as_writeable_bytes(); }
template <class _Tp, ptrdiff_t _Extent>
constexpr void swap(span<_Tp, _Extent> &__lhs, span<_Tp, _Extent> &__rhs) noexcept
{ __lhs.swap(__rhs); }
// Deduction guides
template<class _Tp, size_t _N>
span(_Tp (&)[_N]) -> span<_Tp, _N>;
template<class _Tp, size_t _N>
span(std::array<_Tp, _N>&) -> span<_Tp, _N>;
template<class _Tp, size_t _N>
span(const std::array<_Tp, _N>&) -> span<const _Tp, _N>;
template<class _Container>
span(_Container&) -> span<typename _Container::value_type>;
template<class _Container>
span(const _Container&) -> span<const typename _Container::value_type>;
#endif // _LIBCPP_SPAN
#include <string>
#include <array>
#include <vector>
#include <list>
constexpr int global1 = 1;
constexpr bool test_swap() {
span<const int> s1;
span<const int> s2{&global1, 1};
using std::swap; swap(s1, s2);
return s1.size() == 1 && s2.size() == 0
&& s1.data() == &global1 && s2.data() == nullptr;
}
int main ()
{
span<int> s0;
assert(s0.size() == 0);
span<int, 0> s1;
assert(s1.size() == 0);
assert (s0 == s1);
assert (!(s0 < s1));
int i = 23;
span<int, 1> s2{&i, 1};
assert(s2.size() == 1);
assert(s2.data() == &i);
assert(s2[0] == 23);
auto it2 = s2.begin();
assert(*it2 == 23);
++it2;
assert(it2 == s2.end());
span<int> s3{&i, 1};
assert(s3.size() == 1);
assert(s3.data() == &i);
assert(s3[0] == 23);
auto it3 = s2.rbegin();
assert(*it3 == 23);
++it3;
assert(it3 == s2.rend());
assert(s2 == s3);
auto b2 = as_bytes(s2);
auto b2w = as_writeable_bytes(s2);
assert((void *) b2.data() == (void *)s2.data());
assert(b2.size()/sizeof(int) == s2.size());
assert((void *) b2w.data() == (void *) s3.data());
assert(b2w.size()/sizeof(int) == s3.size());
auto b3 = as_bytes(s3);
auto b3w = as_writeable_bytes(s3);
assert((void *) b3.data() == (void *) s3.data());
assert(b3.size()/sizeof(int) == s3.size());
assert((void *) b3w.data() == (void *) s3.data());
assert(b3w.size()/sizeof(int) == s3.size());
span<const int, 0> s4;
span<const int> s5;
auto b4 = as_bytes(s4);
auto b5 = as_bytes(s5);
assert(b4.data() == nullptr);
assert(b5.data() == nullptr);
assert(b4.size() == 0);
assert(b5.size() == 0);
// auto b4w = as_writeable_bytes(s4);
// auto b5w = as_writeable_bytes(s5);
auto ss1 = s2.subspan<0, -1>(); assert(ss1.size() == 1);
auto ss2 = s2.subspan<0, 1>(); assert(ss2.size() == 1);
auto ss3 = s2.subspan<1, 0>(); assert(ss3.size() == 0);
auto ss4 = s2.subspan<1, -1>(); assert(ss4.size() == 0);
auto ss5 = s3.subspan<0, -1>(); assert(ss5.size() == 1);
auto ss6 = s3.subspan<0, 1>(); assert(ss6.size() == 1);
auto ss7 = s3.subspan<1, 0>(); assert(ss7.size() == 0);
auto ss8 = s3.subspan<1, -1>(); assert(ss8.size() == 0);
// Converting
span<int, 0> convs0 = s0; // dynamic -> static extent
span<const int, 0> convs1 = s1; (void) convs1.size(); // non-const -> const
span<int> convs2 = convs0; (void) convs2.size(); // static -> dynamic extent
// span<int, 1> convs4 = s4; // const -> non-const FAILS
// Deduce from array
{
int arr[] = { 1, 2, 3, 4, 5};
span s6 = arr;
static_assert(std::is_same_v<int, decltype(s6)::value_type>);
static_assert(std::is_same_v<int, decltype(s6)::element_type>);
assert(s6.size() == std::size(arr));
assert(s6.data() == arr);
span<int, 5> s6f = arr; (void) s6f.size();
span<int> s6d = arr; (void) s6d.size();
span<const int, 5> s6cf = arr; (void) s6cf.size();
span<const int> s6cd = arr; (void) s6cd.size();
const int carr[] = { 1, 2, 3, 4};
span s7 = carr;
static_assert(std::is_same_v< int, decltype(s7)::value_type>);
static_assert(std::is_same_v<const int, decltype(s7)::element_type>);
assert(s7.size() == std::size(carr));
assert(s7.data() == carr);
// span<int, 4> s7f = carr;
// span<int> s7d = carr;
span<const int, 4> s7cf = carr; (void) s7cf.size();
span<const int> s7cd = carr; (void) s7cd.size();
}
// Deduce from std::array
{
std::array<int, 5> arr = {1, 2, 3, 4, 5};
span s6 = arr;
static_assert(std::is_same_v<int, decltype(s6)::value_type>);
static_assert(std::is_same_v<int, decltype(s6)::element_type>);
assert(s6.size() == std::size(arr));
assert(s6.data() == arr.data());
span<int, 5> s6f = arr; (void) s6f.size();
span<int> s6d = arr; (void) s6d.size();
span<const int, 5> s6cf = arr; (void) s6cf.size();
span<const int> s6cd = arr; (void) s6cd.size();
const std::array<int, 4> carr = {1, 2, 3, 4};
span s7 = carr;
static_assert(std::is_same_v< int, decltype(s7)::value_type>);
static_assert(std::is_same_v<const int, decltype(s7)::element_type>);
assert(s7.size() == std::size(carr));
assert(s7.data() == carr.data());
// span<int, 4> s7f = carr;
// span<int> s7d = carr;
span<const int, 4> s7cf = carr; (void) s7cf.size();
span<const int> s7cd = carr; (void) s7cd.size();
}
{
static_assert(!__is_equality_comparable<char, std::string>::value, "" );
static_assert( __is_equality_comparable<char, int>::value, "" );
static_assert(!__is_equality_comparable<span<char>, span<std::string>>::value, "");
static_assert( __is_equality_comparable<span<char>, span<int>>::value, "" );
}
{
static_assert( __is_span_compatible_container<std::vector<int>, int>::value);
static_assert( __is_span_compatible_container<const std::vector<int>, const int>::value);
static_assert(!__is_std_array<std::list<int>>::value);
static_assert( __is_std_array<std::array<int, 5>>::value);
static_assert(!__is_std_array<span<int, 5>>::value);
static_assert(!__is_std_array<span<int>>::value);
static_assert(!__is_span<std::list<int>>::value);
static_assert(!__is_span<std::array<int, 5>>::value);
static_assert( __is_span<span<int, 5>>::value);
static_assert( __is_span<span<int>>::value);
static_assert(!__is_span_compatible_container<std::array<int, 5>, int>::value);
static_assert(!__is_span_compatible_container<std::vector<int>, std::string>::value);
static_assert(!__is_span_compatible_container<std::list<int>, int>::value);
}
// {
// std::vector<int> v1;
// const std::vector<int> v2;
// span<int> s1 = v1; (void) s1.size();
// span<const int> sc1 = v1; (void) sc1.size();
// span<int, 0> s1f = v1; (void) s1f.size();
// span<const int, 0> sc1f = v1; (void) sc1f.size();
// span sd1 = v1; (void) sd1.size();
//
// // span<int> s2 = v2; // fails
// // span<int, 0> s2f = v2; // fails
// span<const int> s2 = v2; (void) s2. size();
// span<const int, 0> s2f = v2; (void) s2f.size();
// span s2d = v2; (void) s2d.size();
// }
{
class A; // incomplete
typedef span<A> SA;
static_assert(std::is_same<ptrdiff_t, decltype(std::declval<const SA &>().size())>::value, "");
}
{
span<const int> s1;
span<const int> s2{&global1, 1};
assert(s1.size() == 0);
assert(s1.data() == nullptr);
assert(s2.size() == 1);
assert(s2.data() == &global1);
std::swap(s1, s2);
assert(s1.size() == 1);
assert(s1.data() == &global1);
assert(s2.size() == 0);
assert(s2.data() == nullptr);
static_assert(test_swap());
}
}