Repository navigation
Expand file tree
/
Copy pathResResultContainers.h
More file actions
245 lines (200 loc) · 7.91 KB
/
Copy pathResResultContainers.h
File metadata and controls
245 lines (200 loc) · 7.91 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
#ifndef SROTHMAN_EEC_V2_RES_CONTAINERS_H
#define SROTHMAN_EEC_V2_RES_CONTAINERS_H
#include "usings.h"
#include <vector>
#include <numeric>
#include <boost/multi_array.hpp>
namespace EEC{
template <typename T>
class ResVectorContainer{
public:
struct entry{
T iR;
T ir;
T ic;
double wt;
entry(T iR, T ir,
T ic, double wt) noexcept :
iR(iR), ir(ir), ic(ic), wt(wt) {}
};
using data_t = std::vector<entry>;
using TYPE = T;
constexpr static bool SHOULD_BIN = std::is_same<T, unsigned>::value;
constexpr static bool IS_ARRAY = false;
ResVectorContainer(
const size_t nR,
const size_t nr,
const size_t nc) :
nR(nR), nr(nr), nc(nc){
data.reserve(100);
}
ResVectorContainer() :
ResVectorContainer(0, 0, 0) {}
ResVectorContainer(ResVectorContainer&& other) :
nR(other.nR), nr(other.nr), nc(other.nc),
data(std::move(other.data)) {}
ResVectorContainer(const ResVectorContainer& other) :
nR(other.nR), nr(other.nr), nc(other.nc),
data(other.data) {}
void fill(T iR, T ir,
T ic, double wt) noexcept{
data.emplace_back(iR, ir, ic, wt);
}
const data_t& get_data() const noexcept{
return data;
}
ResVectorContainer& operator+=(const ResVectorContainer& other) noexcept{
data.insert(data.end(),
other.data.begin(),
other.data.end());
return *this;
}
ResVectorContainer operator+(const ResVectorContainer& other) const noexcept{
ResVectorContainer result(*this);
result += other;
return result;
}
double total_weight() const noexcept{
return std::accumulate(
data.begin(), data.end(), 0.0,
[](double acc, const entry& e){
return acc + e.wt;
});
}
const size_t nR, nr, nc;
private:
data_t data;
};
class ResMultiArrayContainer{
public:
using data_t = multi_array<double, 3>;
using TYPE = unsigned;
constexpr static bool SHOULD_BIN = true;
constexpr static bool IS_ARRAY = true;
ResMultiArrayContainer() :
ResMultiArrayContainer(0, 0, 0) {}
ResMultiArrayContainer(
const size_t nR,
const size_t nr,
const size_t nc) :
nR(nR), nr(nr), nc(nc){
data.resize(boost::extents[nR][nr][nc]);
std::fill(data.data(),
data.data() + data.num_elements(), 0);
}
ResMultiArrayContainer(
const ResVectorContainer<unsigned>& other):
ResMultiArrayContainer(other.nR,
other.nr,
other.nc){
for (const auto& e : other.get_data()){
fill(e.iR, e.ir, e.ic, e.wt);
}
}
ResMultiArrayContainer(
const ResMultiArrayContainer& other):
nR(other.nR), nr(other.nr), nc(other.nc),
data() {
data.resize(boost::extents[nR][nr][nc]);
data = other.get_data();
}
ResMultiArrayContainer(
ResMultiArrayContainer&& other):
nR(other.nR), nr(other.nr), nc(other.nc),
data() {
//multi array doesn't have move constructor
//so we have to do this manually
data.resize(boost::extents[nR][nr][nc]);
data = other.get_data();
}
void fill(unsigned iR, unsigned ir,
unsigned ic, double wt) noexcept {
data[iR][ir][ic] += wt;
}
const data_t& get_data() const noexcept{
return data;
}
ResMultiArrayContainer& operator+=(const ResMultiArrayContainer& other) {
assert(data.num_elements() == other.data.num_elements());
std::transform(data.data(),
data.data() + data.num_elements(),
other.data.data(), data.data(),
std::plus<double>());
return *this;
}
ResMultiArrayContainer operator+(const ResMultiArrayContainer& other) const noexcept{
ResMultiArrayContainer result(*this);
result += other;
return result;
}
ResMultiArrayContainer& operator-=(
const ResMultiArrayContainer& other) noexcept{
assert(data.num_elements() == other.data.num_elements());
std::transform(data.data(),
data.data() + data.num_elements(),
other.data.data(), data.data(),
std::minus<double>());
return *this;
}
ResMultiArrayContainer operator-(const ResMultiArrayContainer& other) const noexcept{
ResMultiArrayContainer result(*this);
result -= other;
return result;
}
double total_weight() const noexcept{
return std::accumulate(
data.data(),
data.data() + data.num_elements(), 0.0);
}
bool operator==(const ResMultiArrayContainer& other) const noexcept{
return std::equal(data.data(),
data.data() + data.num_elements(),
other.data.data(),
[](double a, double b){
return std::abs(a - b) < 1e-6;
});
}
const size_t nR, nr, nc;
private:
data_t data;
};
inline bool operator==(const ResVectorContainer<unsigned>& a, const ResVectorContainer<unsigned>& b) noexcept{
return ResMultiArrayContainer(a) == ResMultiArrayContainer(b);
}
inline bool operator==(const ResVectorContainer<unsigned>& a, const ResMultiArrayContainer& b) noexcept{
return ResMultiArrayContainer(a) == b;
}
inline bool operator==(const ResMultiArrayContainer& a, const ResVectorContainer<unsigned>& b) noexcept{
return a == ResMultiArrayContainer(b);
}
inline ResMultiArrayContainer operator+(const ResVectorContainer<unsigned>& a, const ResMultiArrayContainer& b) noexcept{
return ResMultiArrayContainer(a) + b;
}
inline ResMultiArrayContainer operator+(const ResMultiArrayContainer& a, const ResVectorContainer<unsigned>& b) noexcept{
return a + ResMultiArrayContainer(b);
}
inline ResMultiArrayContainer operator-(const ResVectorContainer<unsigned>& a, const ResMultiArrayContainer& b) noexcept{
return ResMultiArrayContainer(a) - b;
}
inline ResMultiArrayContainer operator-(const ResMultiArrayContainer& a, const ResVectorContainer<unsigned>& b) noexcept{
return a - ResMultiArrayContainer(b);
}
inline ResMultiArrayContainer operator-(const ResVectorContainer<unsigned>& a, const ResVectorContainer<unsigned>& b) noexcept{
return ResMultiArrayContainer(a) - ResMultiArrayContainer(b);
}
inline void print_nonzero(const ResMultiArrayContainer& a){
for(size_t iR=0; iR<a.nR; ++iR){
for(size_t ir=0; ir<a.nr; ++ir){
for(size_t ic=0; ic<a.nc; ++ic){
if (a.get_data()[iR][ir][ic] > 0){
printf("(%lu, %lu, %lu): %g\n", iR, ir, ic, a.get_data()[iR][ir][ic]);
}
}
}
}
}
inline void print_nonzero(const ResVectorContainer<unsigned>& a){
print_nonzero(ResMultiArrayContainer(a));
}
};
#endif