-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAlignmentPracticableRepa.cpp
More file actions
424 lines (407 loc) · 13.3 KB
/
AlignmentPracticableRepa.cpp
File metadata and controls
424 lines (407 loc) · 13.3 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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "AlignmentPracticableRepa.h"
using namespace Alignment;
// parametersSystemsBuilderTupleNoSumlayerMultiEffectiveRepa_ui ::
// Integer -> Integer -> Integer -> Integer -> [VariableRepa] -> Fud ->
// HistoryRepa -> HistogramRepaRed -> HistoryRepa -> HistogramRepaRed ->
// ([[VariableRepa]],Integer)
std::tuple<std::unique_ptr<SizeListList>, std::size_t> Alignment::parametersSystemsBuilderTupleNoSumlayerMultiEffectiveRepa_ui(std::size_t xmax, std::size_t omax, std::size_t bmax, std::size_t mmax, const SizeList& vv, const FudRepa& fr, const HistoryRepa& hh, const HistogramRepaRed& hhx, const HistoryRepa& hhrr, const HistogramRepaRed& hhrrx)
{
auto frvars = fudRepasSetVar;
auto frder = fudRepasDerived;
auto cross = parametersSetVarsHistoryRepasSetSetVarsAlignedTop_u;
auto append = parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop_u;
SizeList vv1;
vv1.reserve(vv.size());
{
auto n = hh.dimension;
auto& mvv = hh.mapVarInt();
auto sh = hh.shape;
auto pxx1 = hhx.arr;
std::size_t sz = 0;
SizeList xx;
xx.reserve(n);
for (std::size_t i = 0; i < n; i++)
{
xx.push_back(sz);
sz += sh[i];
}
for (auto& w : vv)
{
auto p = mvv[w];
auto e = sh[p];
bool first = false;
bool second = false;
for (std::size_t i = 0; !second && i < e; i++)
{
bool nz = pxx1[xx[p] + i] > 0.0;
second = second || (first && nz);
first = first || nz;
}
if (second)
vv1.push_back(w);
}
}
std::size_t s = 0;
auto xx1 = std::make_unique<SizeListList>();
if (vv1.size() < 2)
return std::tuple<std::unique_ptr<SizeListList>, std::size_t>(std::move(xx1), s);
DoubleSizeListPairList xx;
xx.reserve(omax*10);
if (!fr.layers.size())
{
auto t = cross(xmax, omax, vv1, hh, hhx, hhrr, hhrrx);
auto& xc = std::get<0>(t);
s += std::get<1>(t);
xx.insert(xx.end(), xc->begin(), xc->end());
xx1->reserve(xc->size());
for (auto& pp : *xc)
xx1->push_back(pp.second);
}
else
{
auto vvf = frvars(fr);
vv1.reserve(vv1.size() + vvf->size());
vv1.insert(vv1.end(), vvf->begin(), vvf->end());
auto wwf = frder(fr);
xx1->reserve(wwf->size());
for (auto& w : *wwf)
xx1->push_back(SizeList{ w });
}
while (xx1->size())
{
auto t = append(xmax, omax, vv1, *xx1, hh, hhx, hhrr, hhrrx);
auto& xa = std::get<0>(t);
s += std::get<1>(t);
xx.insert(xx.end(), xa->begin(), xa->end());
xx1->clear();
xx1->reserve(xa->size());
for (auto& pp : *xa)
xx1->push_back(pp.second);
}
if (!xx.size())
return std::tuple<std::unique_ptr<SizeListList>, std::size_t>(std::move(xx1), s);
std::sort(xx.begin(), xx.end());
xx1->reserve(bmax / mmax);
std::size_t start = xx.size() - 1;
std::size_t end = xx.size() > bmax/mmax ? xx.size() - bmax/mmax : 0;
for (long long i = start; i >= (long long)end; i--)
xx1->push_back(xx[i].second);
return std::tuple<std::unique_ptr<SizeListList>, std::size_t>(std::move(xx1), s);
}
// parametersSystemsBuilderTupleNoSumlayerMultiEffectiveRepa_uip ::
// Integer -> Integer -> Integer -> Integer -> Integer -> [VariableRepa] -> Fud ->
// HistoryRepa -> HistogramRepaRed -> HistoryRepa -> HistogramRepaRed ->
// ([[VariableRepa]],Integer)
std::tuple<std::unique_ptr<SizeListList>, std::size_t> Alignment::parametersSystemsBuilderTupleNoSumlayerMultiEffectiveRepa_uip(std::size_t xmax, std::size_t omax, std::size_t bmax, std::size_t mmax, std::size_t tint, const SizeList& vv, const FudRepa& fr, const HistoryRepa& hh, const HistogramRepaRed& hhx, const HistoryRepa& hhrr, const HistogramRepaRed& hhrrx)
{
auto frvars = fudRepasSetVar;
auto frder = fudRepasDerived;
auto cross = parametersSetVarsHistoryRepasSetSetVarsAlignedTop_up;
auto append = parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedTop_up;
SizeList vv1;
vv1.reserve(vv.size());
{
auto n = hh.dimension;
auto& mvv = hh.mapVarInt();
auto sh = hh.shape;
auto pxx1 = hhx.arr;
std::size_t sz = 0;
SizeList xx;
xx.reserve(n);
for (std::size_t i = 0; i < n; i++)
{
xx.push_back(sz);
sz += sh[i];
}
for (auto& w : vv)
{
auto p = mvv[w];
auto e = sh[p];
bool first = false;
bool second = false;
for (std::size_t i = 0; !second && i < e; i++)
{
bool nz = pxx1[xx[p] + i] > 0.0;
second = second || (first && nz);
first = first || nz;
}
if (second)
vv1.push_back(w);
}
}
std::size_t s = 0;
auto xx1 = std::make_unique<SizeListList>();
if (vv1.size() < 2)
return std::tuple<std::unique_ptr<SizeListList>, std::size_t>(std::move(xx1), s);
DoubleSizeListPairList xx;
xx.reserve(omax * 10);
if (!fr.layers.size())
{
auto t = cross(xmax, omax, tint, vv1, hh, hhx, hhrr, hhrrx);
auto& xc = std::get<0>(t);
s += std::get<1>(t);
xx.insert(xx.end(), xc->begin(), xc->end());
xx1->reserve(xc->size());
for (auto& pp : *xc)
xx1->push_back(pp.second);
}
else
{
auto vvf = frvars(fr);
vv1.reserve(vv1.size() + vvf->size());
vv1.insert(vv1.end(), vvf->begin(), vvf->end());
auto wwf = frder(fr);
xx1->reserve(wwf->size());
for (auto& w : *wwf)
xx1->push_back(SizeList{ w });
}
while (xx1->size())
{
auto t = append(xmax, omax, tint, vv1, *xx1, hh, hhx, hhrr, hhrrx);
auto& xa = std::get<0>(t);
s += std::get<1>(t);
xx.insert(xx.end(), xa->begin(), xa->end());
xx1->clear();
xx1->reserve(xa->size());
for (auto& pp : *xa)
xx1->push_back(pp.second);
}
if (!xx.size())
return std::tuple<std::unique_ptr<SizeListList>, std::size_t>(std::move(xx1), s);
std::sort(xx.begin(), xx.end());
xx1->reserve(bmax / mmax);
std::size_t start = xx.size() - 1;
std::size_t end = xx.size() > bmax / mmax ? xx.size() - bmax / mmax : 0;
for (long long i = start; i >= (long long)end; i--)
xx1->push_back(xx[i].second);
return std::tuple<std::unique_ptr<SizeListList>, std::size_t>(std::move(xx1), s);
}
// parametersSystemsPartitionerMaxRollByMRepa_ui ::
// Integer -> Integer -> Integer -> [VariableRepa] -> HistoryRepa -> HistoryRepa ->
// ([[[VariableRepa]]],Integer)
std::tuple<std::unique_ptr<SizeListListList>, std::size_t> Alignment::parametersSystemsPartitionerMaxRollByMRepa_ui(std::size_t mmax, std::size_t umax, std::size_t pmax, const SizeList& kk, const HistoryRepa& hh, const HistoryRepa& hhrr)
{
auto hrred = [](double f, const HistoryRepa& hr, const SizeList& kk)
{
return setVarsHistoryRepasReduce_u(f, kk.size(), kk.data(), hr);
};
auto rrvqqy = parametersHistogramRepaVecsSetTuplePartitionTopByM_u;
auto z = (double)hh.size;
auto zr = (double)hhrr.size;
auto aa = hrred(1.0, hh, kk);
auto aarr = hrred(z/zr, hhrr, kk);
double y1 = aa->facLn() - aarr->facLn();
return rrvqqy(mmax, umax, pmax, *aa, *aarr, z, y1);
}
// parametersRollerMaximumRollExcludedSelfRepa_i ::
// [[VariableRepa]] -> -> HistogramRepa -> -> HistogramRepa ->
// ([[[Int]]],Integer)
std::tuple<std::unique_ptr<SizeListListList>, std::size_t> Alignment::parametersRollerMaximumRollExcludedSelfRepa_i(const SizeListList& pp, const HistogramRepa& aa, const HistogramRepa& aarr, double z)
{
auto t = histogramRepaVecsRollMax(pp, aa, aarr, z);
auto tt = std::move(std::get<0>(t));
auto s = std::get<1>(t);
auto tt1 = std::make_unique<SizeListListList>();
bool any = false;
for (auto& vv : *tt)
if (*max_element(vv.begin(), vv.end()) < vv.size() - 1)
{
tt1->push_back(*tt);
break;
}
return std::tuple<std::unique_ptr<SizeListListList>, std::size_t>(std::move(tt1), s);
}
// parametersSystemsBuilderDerivedVarsHighestNoSumlayerRepa_ui ::
// Integer -> Integer -> [VariableRepa] -> Fud ->
// HistoryRepa -> HistogramRepaRed -> HistoryRepa -> HistogramRepaRed ->
// ([(Double, [VariableRepa]],Integer)
std::tuple<std::unique_ptr<DoubleSizeListPairList>, std::size_t> Alignment::parametersSystemsBuilderDerivedVarsHighestNoSumlayerRepa_ui(std::size_t wmax, std::size_t omax, const FudRepa& fr, const HistoryRepa& hh, const HistogramRepaRed& hhx, const HistoryRepa& hhrr, const HistogramRepaRed& hhrrx)
{
auto fsize = fudRepasSize;
auto append = parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedExcludeHiddenDenseTop_u;
auto l = fsize(fr);
SizeUSet wwf;
wwf.reserve(l);
SizeList yy;
yy.reserve(l);
SizeSizeSetMap mm;
bool first = true;
for (auto& ll : fr.layers)
{
for (auto& tt : ll)
{
auto& w = tt->derived;
yy.push_back(w);
wwf.insert(w);
auto& nn = mm[w];
if (!first)
{
auto m = tt->dimension;
auto& zz = tt->vectorVar;
for (std::size_t i = 0; i < m; i++)
{
auto& v = zz[i];
wwf.erase(v);
nn.insert(v);
auto it = mm.find(v);
if (it != mm.end())
nn.insert(it->second.begin(), it->second.end());
}
}
}
first = false;
}
SizeSizePairList cc;
for (auto& pp : mm)
{
auto& w = pp.first;
for (auto& v : pp.second)
cc.push_back(SizeSizePair(w, v));
}
std::size_t s = 0;
DoubleSizeListPairList xx;
xx.reserve(omax * 10);
SizeListList xx1;
xx1.reserve(wwf.size());
for (auto& w : wwf)
xx1.push_back(SizeList{ w });
while (xx1.size())
{
auto t = append(wmax, omax, cc, yy, xx1, hh, hhx, hhrr, hhrrx);
auto& xa = std::get<0>(t);
s += std::get<1>(t);
xx.insert(xx.end(), xa->begin(), xa->end());
xx1.clear();
xx1.reserve(xa->size());
for (auto& pp : *xa)
xx1.push_back(pp.second);
}
std::sort(xx.begin(), xx.end());
auto xx2 = std::make_unique<DoubleSizeListPairList>();
xx2->reserve(1);
if (xx.size())
xx2->push_back(xx.back());
return std::tuple<std::unique_ptr<DoubleSizeListPairList>, std::size_t>(std::move(xx2), s);
}
// parametersSystemsBuilderDerivedVarsHighestNoSumlayerRepa_uip ::
// Integer -> Integer -> Integer -> [VariableRepa] -> Fud ->
// HistoryRepa -> HistogramRepaRed -> HistoryRepa -> HistogramRepaRed ->
// ([(Double, [VariableRepa]],Integer)
std::tuple<std::unique_ptr<DoubleSizeListPairList>, std::size_t> Alignment::parametersSystemsBuilderDerivedVarsHighestNoSumlayerRepa_uip(std::size_t wmax, std::size_t omax, std::size_t tint, const FudRepa& fr, const HistoryRepa& hh, const HistogramRepaRed& hhx, const HistoryRepa& hhrr, const HistogramRepaRed& hhrrx)
{
auto fsize = fudRepasSize;
auto append = parametersSetVarsSetSetVarsHistoryRepasSetSetVarsAlignedExcludeHiddenDenseTop_up;
auto l = fsize(fr);
SizeUSet wwf;
wwf.reserve(l);
SizeList yy;
yy.reserve(l);
SizeSizeSetMap mm;
bool first = true;
for (auto& ll : fr.layers)
{
for (auto& tt : ll)
{
auto& w = tt->derived;
yy.push_back(w);
wwf.insert(w);
auto& nn = mm[w];
if (!first)
{
auto m = tt->dimension;
auto& zz = tt->vectorVar;
for (std::size_t i = 0; i < m; i++)
{
auto& v = zz[i];
wwf.erase(v);
nn.insert(v);
auto it = mm.find(v);
if (it != mm.end())
nn.insert(it->second.begin(), it->second.end());
}
}
}
first = false;
}
SizeSizePairList cc;
for (auto& pp : mm)
{
auto& w = pp.first;
for (auto& v : pp.second)
cc.push_back(SizeSizePair(w, v));
}
std::size_t s = 0;
DoubleSizeListPairList xx;
xx.reserve(omax * 10);
SizeListList xx1;
xx1.reserve(wwf.size());
for (auto& w : wwf)
xx1.push_back(SizeList{ w });
while (xx1.size())
{
auto t = append(wmax, omax, tint, cc, yy, xx1, hh, hhx, hhrr, hhrrx);
auto& xa = std::get<0>(t);
s += std::get<1>(t);
xx.insert(xx.end(), xa->begin(), xa->end());
xx1.clear();
xx1.reserve(xa->size());
for (auto& pp : *xa)
xx1.push_back(pp.second);
}
std::sort(xx.begin(), xx.end());
auto xx2 = std::make_unique<DoubleSizeListPairList>();
xx2->reserve(1);
if (xx.size())
xx2->push_back(xx.back());
return std::tuple<std::unique_ptr<DoubleSizeListPairList>, std::size_t>(std::move(xx2), s);
}
// systemsHistoryRepasApplicationsHistoryHistoryPartitionedRepa_u ::
// HistoryRepa -> ApplicationRepa ->
// (SystemRepa, HistoryRepa)
std::unique_ptr<HistoryRepa> Alignment::systemsHistoryRepasApplicationsHistoryHistoryPartitionedRepa_u(const HistoryRepa& hr, const ApplicationRepa& dr, SystemRepa& ur)
{
auto frmul = historyRepasFudRepasMultiply_u;
auto& llu1 = ur.listVarSizePair;
auto hr2 = frmul(hr, *dr.fud);
auto sl = treesLeafElements(*dr.slices);
auto m = sl->size();
auto z = hr2->size;
auto& mvv2 = hr2->mapVarInt();
SizeList pvv2;
for (auto v : *sl)
pvv2.push_back(mvv2[v]);
auto hr3 = std::make_unique<HistoryRepa>();
std::size_t n3 = 2;
std::size_t s0 = (m-1)/256 + 1;
std::size_t s1 = m > 256 ? 256 : m;
llu1.push_back(VarSizePair(std::make_shared<Variable>("partition0"), s0));
llu1.push_back(VarSizePair(std::make_shared<Variable>("partition1"), s1));
std::size_t v0 = llu1.size() - 2;
std::size_t v1 = llu1.size() - 1;
hr3->dimension = n3;
hr3->vectorVar = new std::size_t[n3];
auto vv3 = hr3->vectorVar;
hr3->shape = new std::size_t[n3];
auto sh3 = hr3->shape;
vv3[0] = v0;
sh3[0] = s0;
vv3[1] = v1;
sh3[1] = s1;
hr3->size = z;
hr3->evient = false;
hr3->arr = new unsigned char[z*n3];
auto rr2 = hr2->arr;
auto rr3 = hr3->arr;
for (std::size_t j = 0; j < z; j++)
for (std::size_t i = 0; i < m; i++)
{
std::size_t u = rr2[pvv2[i]*z + j];
if (u)
{
rr3[j] = (unsigned char)(i/256);
rr3[z+j] = (unsigned char)(i%256);
break;
}
}
return hr3;
}