-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathP635.ExpressionTemplates.cpp
More file actions
255 lines (240 loc) · 6.08 KB
/
Copy pathP635.ExpressionTemplates.cpp
File metadata and controls
255 lines (240 loc) · 6.08 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
#include <iostream>
#include <type_traits>
#include <cstddef>
#include <cassert>
template<typename T>
class SArray
{
private:
T* storage;
std::size_t storage_size;
protected:
void init()
{
for (std::size_t idx = 0; idx < size(); ++idx)
{
storage[idx] = T();
}
}
void copy(const SArray<T>& source)
{
assert(size() == source.size());
for (std::size_t idx = 0; idx < size(); ++idx)
{
storage[idx] = source.storage[idx];
}
}
public:
explicit SArray(std::size_t s) : storage(new T[s]), storage_size(s)
{
init();
}
SArray(const SArray<T>& source) : storage(new T[source.size()]), storage_size(source.size())
{
copy(source);
}
~SArray()
{
delete [] storage;
}
SArray& operator=(const SArray<T>& source)
{
if (&source != this)
{
copy(source);
}
return *this;
}
std::size_t size() const
{
return storage_size;
}
const T& operator[](std::size_t idx) const
{
return storage[idx];
}
T& operator[](std::size_t idx)
{
return storage[idx];
}
};
template<typename T> class A_Scalar;
// for array
template<typename T>
struct A_Traits
{
using ExprRef = const T&;
};
// for scalar type
template<typename T>
struct A_Traits<A_Scalar<T>>
{
using ExprRef = A_Scalar<T>;
};
// class that representat the addition of two operands
template<typename T, typename OP1, typename OP2>
class A_Add
{
private:
typename A_Traits<OP1>::ExprRef op1; // first operand
typename A_Traits<OP2>::ExprRef op2; // second operand
public:
A_Add(const OP1& a, const OP2& b) : op1(a), op2(b) {}
// compute sum when value requested
T operator[](std::size_t idx) const
{
return op1[idx] + op2[idx];
}
// maximum size, size of scalar is 0
std::size_t size() const
{
assert(op1.size() == 0 || op2.size() == 0 || op1.size() == op2.size());
return op1.size() != 0 ? op1.size() : op2.size();
}
};
// class that representat the multiplication of two operands
template<typename T, typename OP1, typename OP2>
class A_Mult
{
private:
typename A_Traits<OP1>::ExprRef op1; // first operand
typename A_Traits<OP2>::ExprRef op2; // second operand
public:
A_Mult(const OP1& a, const OP2& b) : op1(a), op2(b) {}
// compute product when value requested
T operator[](std::size_t idx) const
{
return op1[idx] * op2[idx];
}
// maximum size
std::size_t size() const
{
assert(op1.size() == 0 || op2.size() == 0 || op1.size() == op2.size());
return op1.size() != 0 ? op1.size() : op2.size();
}
};
template<typename T>
class A_Scalar
{
private:
const T& s; // value of scalar
public:
constexpr A_Scalar(const T& v) : s(v) {}
// for index operations, always return the scalar itself
constexpr const T& operator[](std::size_t) const
{
return s;
}
// scalars has size of 0
constexpr std::size_t size() const
{
return 0;
}
};
template<typename T, typename Rep = SArray<T>>
class Array
{
private:
Rep arr; // data of array
public:
// create array with initial size
explicit Array(std::size_t s) : arr(s) {}
// create array from possible representation
Array(const Rep& r) : arr(r) {}
// assignment for same type array
Array& operator=(const Array& rhs)
{
assert(size() == rhs.size());
for (std::size_t i = 0; i < rhs.size(); ++i)
{
arr[i] = rhs[i];
}
return *this;
}
// assignment for arrays of different type
template<typename T2, typename Rep2>
Array& operator=(const Array<T2, Rep2>& rhs)
{
assert(size() == rhs.size());
for (std::size_t i = 0; i < rhs.size(); ++i)
{
arr[i] = rhs[i];
}
return *this;
}
// size
std::size_t size() const
{
return arr.size();
}
// index operator
decltype(auto) operator[](std::size_t idx) const
{
assert(idx < size());
return arr[idx];
}
T& operator[](std::size_t idx)
{
assert(idx < size());
return arr[idx];
}
// underlying array
const Rep& rep() const
{
return arr;
}
Rep& rep()
{
return arr;
}
};
// operators
template<typename T, typename R1, typename R2>
Array<T, A_Add<T, R1, R2>> operator+(const Array<T, R1>& a, const Array<T, R2>& b)
{
return Array<T, A_Add<T, R1, R2>>(A_Add<T, R1, R2>(a.rep(), b.rep()));
}
template<typename T, typename R1>
Array<T, A_Add<T, R1, A_Scalar<T>>> operator+(const Array<T, R1>& a, const T& b)
{
return Array<T, A_Add<T, R1, A_Scalar<T>>>(A_Add<T, R1, A_Scalar<T>>(a.rep(), A_Scalar<T>(b)));
}
template<typename T, typename R2>
Array<T, A_Add<T, A_Scalar<T>, R2>> operator+(const T& a, const Array<T, R2>& b)
{
return Array<T, A_Add<T, A_Scalar<T>, R2>>(A_Add<T, A_Scalar<T>, R2>(A_Scalar<T>(a), b.rep()));
}
template<typename T, typename R1, typename R2>
Array<T, A_Mult<T, R1, R2>> operator*(const Array<T, R1>& a, const Array<T, R2>& b)
{
return Array<T, A_Mult<T, R1, R2>>(A_Mult<T, R1, R2>(a.rep(), b.rep()));
}
template<typename T, typename R1>
Array<T, A_Mult<T, R1, A_Scalar<T>>> operator*(const Array<T, R1>& a, const T& b)
{
return Array<T, A_Mult<T, R1, A_Scalar<T>>>(A_Mult<T, R1, A_Scalar<T>>(a.rep(), A_Scalar<T>(b)));
}
template<typename T, typename R2>
Array<T, A_Mult<T, A_Scalar<T>, R2>> operator*(const T& a, const Array<T, R2>& b)
{
return Array<T, A_Mult<T, A_Scalar<T>, R2>>(A_Mult<T, A_Scalar<T>, R2>(A_Scalar<T>(a), b.rep()));
}
template<typename T, typename Rep>
void printArray(const Array<T, Rep>& arr)
{
assert(arr.size() > 0);
std::cout << "SArray[" << arr.size() << "]: ";
for (std::size_t i = 0; i < arr.size(); ++i)
{
std::cout << arr[i] << ", ";
}
std::cout << std::endl;
}
int main(int argc, char const *argv[])
{
Array<double> x(10);
Array<double> y(10);
auto res = (x+1.0)*1.2 + (x+1.0)*(y+1.0);
printArray(res);
return 0;
}