-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.h
127 lines (98 loc) · 3.14 KB
/
test.h
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
#include <cstdio>
#include <array>
template<size_t _Sz>
using tensor_size = std::array<size_t,_Sz>;
template<typename _Ts>
class array_iterator
{
public:
using value_type = _Ts;
using pointer = value_type*;
using reference = value_type&;
using difference_type = typename value_type::difference_type;
using const_self = array_iterator<typename value_type::const_self>;
using normal_self = array_iterator<typename value_type::normal_self>;
using elm_type = typename value_type::elm_type;
using elm_pointer = elm_type*;
static constexpr size_t dim = value_type::dim + 1;
using deep_size_type = tensor_size<dim - 1>;
private:
value_type _value;
difference_type _step = 0;
public:
};
template<typename _Elm>
class array_ptr
{
public:
using normal_elm_type = typename std::remove_const<_Elm>::type;
using normal_self = array_ptr<normal_elm_type>;
using const_self = array_ptr<const _Elm>;
using array_type = array_ptr;
using size_type = size_t;
using elm_type = _Elm;
using elm_pointer = elm_type *;
using value_type = _Elm;
using pointer = value_type * ;
using reference = value_type & ;
using iterator = pointer;
using reverse_iterator = std::reverse_iterator<iterator>;
using difference_type = size_t;
static constexpr unsigned int dim = 1;
};
template<typename _Base>
class base_tensor_ptr
{
public:
using value_type = _Base;
using normal_self = base_tensor_ptr<typename value_type::normal_self>;
using const_self = base_tensor_ptr<typename value_type::const_self>;
using elm_type = typename value_type::elm_type;
using elm_pointer = elm_type*;
using array_type = typename value_type::array_type;
using size_type = typename value_type::size_type;
//using reference = value_type & ;
//using pointer = value_type * ;
using difference_type = typename value_type::difference_type;
using iterator = array_iterator<value_type>;
using const_iterator = array_iterator<typename value_type::const_self>;
static constexpr size_t dim = value_type::dim + 1;
using deep_size_type = tensor_size<dim>;
private:
iterator _pointer;
size_t _size = 0;
public:
base_tensor_ptr(elm_pointer _elm, deep_size_type _size) :
_pointer(value_type(_elm, sub_size(_size))),
_size(_size[0]) {}
const iterator& begin() const
{
return _pointer;
}
value_type at(size_t i) const
{
return *(begin() + (difference_type)i);
}
value_type operator[](size_t i) const
{
return at(i);
}
};
template<typename _Elm, size_t _Dim>
struct _tensor_traits
{
using type = base_tensor_ptr<
typename _tensor_traits<_Elm, _Dim - 1>::type>;
};
template<typename _Elm>
struct _tensor_traits<_Elm, 1>
{
using type = array_ptr<_Elm>;
};
template<typename _Elm>
struct _tensor_traits<_Elm, 0>
{
using type = _Elm;
};
template<typename _Elm, size_t _Dim>
using tensor_ptr = typename _tensor_traits<_Elm, _Dim>::type;