forked from celphato/mda
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmda.hpp
187 lines (173 loc) · 4.47 KB
/
mda.hpp
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
#pragma once
#include <vector>
#include <stdexcept>
#include <memory>
namespace mda {
template <typename T>
class mda
{
std::vector< mda<T> > children;
size_t d;
size_t s;
std::unique_ptr<T> content;
mda(std::vector<size_t>::iterator p_it, size_t p_d)
: d(p_d)
{
if (d != 0)
{
s = *p_it;
children.reserve(s);
if (d != 1)
{
p_it++;
}
for (size_t i=0; i<s; i++)
{
children.push_back(mda<T>(p_it, d-1));
}
content = nullptr;
}
else
{
s = 0;
content = std::make_unique<T>();
}
}
public:
mda()
: d(0), s(0), content(std::make_unique<T>())
{}
mda(const mda<T>& p_mda)
: d(p_mda.d), s(p_mda.s)
{
if (d == 0)
{
content = std::make_unique<T>(*(p_mda.content));
}
else
{
children.clear();
children.reserve(s);
for (size_t i=0; i<s; i++)
{
children.push_back(mda<T>(p_mda[i]));
}
}
}
template <typename Any> friend class mda;
template <typename U>
mda(const mda<U>& p_mda)
: d(p_mda.d), s(p_mda.s)
{
if (d == 0)
{
content = std::make_unique<T>(*(p_mda.content));
}
else
{
children.clear();
children.reserve(s);
for (size_t i=0; i<s; i++)
{
children.push_back(mda<T>(p_mda[i]));
}
}
}
mda(std::vector<size_t> p_shape)
: d(p_shape.size())
{
if (d != 0)
{
std::vector<size_t>::iterator it = p_shape.begin();
s = *it;
children.reserve(s);
if (d != 1)
{
it++;
}
for (size_t i=0; i<s; i++)
{
children.push_back(mda<T>(it, d-1));
}
content = nullptr;
}
else
{
s = 0;
content = std::make_unique<T>();
}
}
const mda<T>& operator[](size_t p_i) const
{
if (d == 0)
{
std::string outerr = "attempted subscript of mda with dim==0";
throw std::invalid_argument(outerr);
}
return children[p_i];
}
operator T&() const
{
if (d != 0)
{
std::string outerr = "attempted cast from mda with dim>0";
throw std::invalid_argument(outerr);
}
return *content;
}
void operator=(const mda<T>& p_mda)
{
s = p_mda.s;
d = p_mda.d;
if (d == 0)
{
content = std::make_unique<T>(*(p_mda.content));
}
else
{
children.clear();
children.reserve(s);
for (size_t i=0; i<s; i++)
{
children.push_back(mda<T>(p_mda[i]));
}
}
}
void operator=(mda<T>&& p_mda)
{
s = p_mda.s;
d = p_mda.d;
if (d == 0)
{
content = std::make_unique<T>();
*content = *(p_mda.content);
}
else
{
children.clear();
children.reserve(s);
for (size_t i=0; i<s; i++)
{
children.push_back(mda<T>(p_mda[i]));
}
}
}
void operator=(const T& p_content) const
{
if (d != 0)
{
std::string outerr = "attempted assignment of value to mda with dim>0";
throw std::invalid_argument(outerr);
}
*content = p_content;
}
size_t size() const
{
return s;
}
size_t dim() const
{
return d;
}
};
}