-
Notifications
You must be signed in to change notification settings - Fork 76
/
Copy pathextent1.cxx
79 lines (64 loc) · 2.18 KB
/
extent1.cxx
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
#include <type_traits>
#include <limits>
#include <iostream>
constexpr size_t dynamic_extent = size_t.max;
template<typename Type>
concept SizeType = std::is_convertible_v<Type, size_t>;
template<size_t index, size_t Extent>
struct _storage_t {
// static storage.
constexpr _storage_t(size_t extent) noexcept { }
static constexpr size_t extent = Extent;
};
template<size_t index>
struct _storage_t<index, dynamic_extent> {
// dynamic storage.
constexpr _storage_t(size_t extent) noexcept : extent(extent) { }
size_t extent;
};
template<size_t... Extents>
struct extents {
// Partial static storage.
[[no_unique_address]] _storage_t<int..., Extents> ...m;
// Count the rank (number of Extents).
static constexpr size_t rank() noexcept {
return sizeof... Extents;
}
// Count the dynamic rank (number of Extents equal to dynamic_equal).
static constexpr size_t rank_dynamic() noexcept {
return (0 + ... + (dynamic_extent == Extents));
}
// Dynamic access to extents.
constexpr size_t extent(size_t i) const noexcept {
return i == int... ...? m.extent : 0;
}
// Construct from one index per extent.
template<SizeType... IndexTypes>
requires(sizeof...(IndexTypes) == rank())
constexpr extents(IndexTypes... exts) noexcept : m(exts)... { }
// Map index I to index of dynamic extent J.
template<size_t I>
static constexpr size_t find_dynamic_index =
(0 + ... + (dynamic_extent == Extents...[:I]));
// Construct from one index per *dynamic extent*.
template<SizeType... IndexTypes>
requires(
sizeof...(IndexTypes) != rank() &&
sizeof...(IndexTypes) == rank_dynamic()
)
constexpr extents(IndexTypes... exts) noexcept : m(
dynamic_extent == Extents ??
exts...[find_dynamic_index<int...>] :
Extents
)... { }
};
int main() {
using Extents = extents<3, 4, dynamic_extent, dynamic_extent, 7>;
// Initialize extents with one value per extent.
Extents e1(3, 4, 5, 6, 7);
// Initialize extents with one value per *dynamic extent*. The static extents
// are inherited from the template arguments.
Extents e2(5, 6);
for(int i : Extents::rank())
std::cout<< i<< ": "<< e1.extent(i)<< " - "<< e2.extent(i)<< "\n";
}