forked from MineFlowCSM/MineFlow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmineflow.cpp
More file actions
4724 lines (3976 loc) · 133 KB
/
mineflow.cpp
File metadata and controls
4724 lines (3976 loc) · 133 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* mineflow.cpp
Contact: matthewvdeutsch@gmail.com
You should also have 'mineflow.h', or else you'll have trouble. See that file
for more information.
Citation:
@article{deutsch2022mineflow,
author={Deutsch, Matthew and Da{\u{g}}delen, Kadri and Johnson, Thys},
title={An Open-Source Program for Efficiently Computing Ultimate Pit Limits: MineFlow},
journal={Natural Resources Research},
year={2022},
month={Mar},
day={17},
issn={1573-8981},
doi={10.1007/s11053-022-10035-w},
url={https://doi.org/10.1007/s11053-022-10035-w}
}
This file contains roughly 3 sections:
- The actual implementation of the library
- A testing framework and tests: Access with compile definition: MVD_MINEFLOW_TESTS
- The executable: Access with compile definition: MVD_MINEFLOW_EXE
It is not great practice to recompile the same implementation multiple times
with different #defines to get at what you want, but note that the decision here
was to optimize for integration, not elegance.
LICENSE
-------
Copyright 2022 Matthew Deutsch
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
the Software, and to permit persons to whom the Software is furnished to do so,
subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <iomanip>
#include <functional>
#include <numeric>
#include <chrono>
#include <unordered_map>
#include <unordered_set>
#ifdef MVD_USE_GMP
#include <forward_list>
#include <cstring>
#endif
#include "mineflow.h"
using namespace mvd::mineflow;
using namespace mvd::mineflow::impl;
#ifndef MVD_ASSERT
#ifdef NDEBUG
#undef NDEBUG
#endif
#include <cassert>
#define MVD_ASSERT(x) assert(x)
#endif
namespace mvd::mineflow::impl {
class NodePool;
struct AntecedentsInfo {
std::vector<Node*> OutOfTree;
IndexType NextArc;
NodePool* Init;
};
struct Node {
ValueType Excess; // Positive for excess, negative for deficit
Arc* ToRoot; // Normalized Tree arcs
IndexType Label; // The 'distance' label
Node* FirstChild;
Node* NextChild;
Node* NextScan;
AntecedentsInfo Antecedents;
// Operations
void AddChild(Node* child);
void RemoveChild(Node* child);
void IncrementLabel();
void ForNodeAndChildren(std::function<void(Node*)> cback);
Node* FindWeakAbove();
void InitPrecedence(Node* node);
};
struct Arc {
Node* Tail; // null for main 'root'
Node* Head; // null for main 'root'
ValueType Flow;
};
class PrecedenceArcPool {
public:
PrecedenceArcPool();
~PrecedenceArcPool();
Arc* NewArc(Node* from, Node* to);
void DeleteArc(Arc* arc);
IndexType NumUsed() const;
private:
#ifndef MVD_USE_GMP
ObjectPoolBase<8192, 16> m_ObjectPool; // tunable
#else
template <size_t N>
struct ArcSet {
ArcSet() {
for (size_t i = 0; i < N; i++) {
mpz_init(Arcs[i].Flow);
}
}
~ArcSet() {
for (size_t i = 0; i < N; i++) {
mpz_clear(Arcs[i].Flow);
}
}
Arc Arcs[N];
};
static constexpr inline int N = 1024;
std::forward_list<ArcSet<N>> m_ArcSets; // tunable
int m_Remaining;
ArcSet<N>* m_Set;
#endif
IndexType m_NumUsed;
};
class NodePool {
public:
NodePool(std::shared_ptr<const IPrecedenceConstraints> pre);
~NodePool();
void InitializeNodeValue(IndexType nodeIndex, std::function<void(ValueType*)> getValue);
void GetNodeValue(IndexType nodeIndex, ValueType* value) const;
Node* GetNode(IndexType nodeIndex);
IndexType GetNodeIndex(const Node* node) const;
void ReconnectToRoot(Node* node);
void PushStrongRoot(Node* node);
bool NextStrongRoot(Node** nodep);
void IncrementLabel(Node* node);
// Output
IndexType NumNodes() const;
bool InMinimumCut(IndexType nodeIndex) const;
void InitPrecedence(Node* node);
private:
std::shared_ptr<const IPrecedenceConstraints> m_PrecedenceConstraints;
IndexType m_NumNodes;
std::vector<IndexType> m_LabelCount;
std::vector<std::queue<Node*>> m_Buckets;
std::vector<Node> m_Nodes;
std::vector<Arc> m_RootArcs;
};
}
IBlockValues::IBlockValues()
{
}
IBlockValues::~IBlockValues() {
}
////////////////////////////////////////////////////////////////////////////////
#ifndef MVD_USE_GMP
VecBlockValues::VecBlockValues(IndexType numBlocks)
{
m_Values.assign(numBlocks, 0);
}
VecBlockValues::VecBlockValues(std::vector<ValueType>&& values)
: m_Values(values)
{
}
VecBlockValues::VecBlockValues(std::initializer_list<int> values)
{
m_Values.reserve(values.size());
for (auto & v : values) {
m_Values.push_back(static_cast<ValueType>(v));
}
}
VecBlockValues::VecBlockValues(IBlockValues* valuesToCopy)
{
mineflow::IndexType n = valuesToCopy->NumBlocks();
m_Values.resize(n);
for (mineflow::IndexType blockIndex = 0; blockIndex < n; blockIndex++) {
valuesToCopy->BlockValue(blockIndex, &m_Values[blockIndex]);
}
}
VecBlockValues::~VecBlockValues()
{
}
IndexType VecBlockValues::NumBlocks() const
{
return m_Values.size();
}
void VecBlockValues::BlockValue(IndexType blockIndex, ValueType* value) const
{
*value = m_Values.at(blockIndex);
}
VecBlockValues::const_iterator VecBlockValues::begin() const
{
return m_Values.begin();
}
VecBlockValues::const_iterator VecBlockValues::end() const
{
return m_Values.end();
}
VecBlockValues::iterator VecBlockValues::begin()
{
return m_Values.begin();
}
VecBlockValues::iterator VecBlockValues::end()
{
return m_Values.end();
}
ValueType VecBlockValues::operator[](IndexType blockIndex) const
{
return m_Values[blockIndex];
}
ValueType& VecBlockValues::operator[](IndexType blockIndex)
{
return m_Values[blockIndex];
}
void VecBlockValues::SetBlockValueSI(IndexType blockIndex, int64_t si)
{
m_Values[blockIndex] = static_cast<ValueType>(si);
}
#else
GMPBlockValues::GMPBlockValues(IndexType numBlocks)
: m_NumBlocks(numBlocks)
, m_BlockValues(std::make_unique<ValueType[]>(numBlocks))
{
for (IndexType i = 0; i < m_NumBlocks; i++) {
mpz_init(m_BlockValues[i]);
}
}
GMPBlockValues::GMPBlockValues(IndexType numBlocks, int64_t initialValue)
: m_NumBlocks(numBlocks)
, m_BlockValues(std::make_unique<ValueType[]>(numBlocks))
{
for (IndexType i = 0; i < m_NumBlocks; i++) {
mpz_init_set_si(m_BlockValues[i], initialValue);
}
}
GMPBlockValues::GMPBlockValues(const std::vector<int64_t>& initialValues)
: GMPBlockValues(initialValues.size())
{
for (IndexType i = 0; i < m_NumBlocks; i++) {
SetBlockValueSI(i, initialValues[i]);
}
}
GMPBlockValues::~GMPBlockValues()
{
for (IndexType i = 0; i < m_NumBlocks; i++) {
mpz_clear(m_BlockValues[i]);
}
}
IndexType GMPBlockValues::NumBlocks() const
{
return m_NumBlocks;
}
void GMPBlockValues::BlockValue(IndexType blockIndex, ValueType* value) const
{
mpz_set(*value, m_BlockValues[blockIndex]);
}
void GMPBlockValues::SetBlockValueSI(IndexType blockIndex, int64_t si)
{
mpz_set_si(m_BlockValues[blockIndex], si);
}
#endif
////////////////////////////////////////////////////////////////////////////////
BlockDefinition::BlockDefinition()
{
}
BlockDefinition::~BlockDefinition()
{
}
BlockDefinition::BlockDefinition(
IndexType iNumX, IndexType iNumY, IndexType iNumZ,
double iMinX, double iMinY, double iMinZ,
double iSizeX, double iSizeY, double iSizeZ)
: NumX(iNumX), NumY(iNumY), NumZ(iNumZ)
, MinX(iMinX), MinY(iMinY), MinZ(iMinZ)
, SizeX(iSizeX), SizeY(iSizeY), SizeZ(iSizeZ)
{
}
IndexType BlockDefinition::GridIndex(IndexType x, IndexType y, IndexType z) const
{
return x + y * NumX + z * NumX * NumY;
}
IndexType BlockDefinition::XIndex(IndexType idx) const
{
return idx % NumX;
}
IndexType BlockDefinition::YIndex(IndexType idx) const
{
return (idx / NumX) % NumY;
}
IndexType BlockDefinition::ZIndex(IndexType idx) const
{
return idx / (NumX * NumY);
}
std::tuple<IndexType, IndexType, IndexType> BlockDefinition::XYZIndices(IndexType idx) const
{
return std::make_tuple(XIndex(idx), YIndex(idx), ZIndex(idx));
}
IndexType BlockDefinition::NumBlocks() const
{
return NumX * NumY * NumZ;
}
IndexType BlockDefinition::OffsetIndex(IndexType idx, IndexType ox, IndexType oy, IndexType oz) const
{
return idx + ox + oy * NumX + oz * NumX * NumY;
}
bool BlockDefinition::InDef(IndexType x, IndexType y, IndexType z) const
{
if (x < 0 || x >= NumX ||
y < 0 || y >= NumY ||
z < 0 || z >= NumZ) {
return false;
}
return true;
}
bool BlockDefinition::InDef(IndexType idx) const
{
if (idx < 0 || idx >= NumBlocks()) {
return false;
}
return true;
}
bool BlockDefinition::OffsetInDef(IndexType x, IndexType y, IndexType z,
IndexType ox, IndexType oy, IndexType oz) const
{
return InDef(x + ox, y + oy, z + oz);
}
bool BlockDefinition::OffsetInDef(IndexType idx, IndexType ox, IndexType oy, IndexType oz) const
{
return InDef(OffsetIndex(idx, ox, oy, oz));
}
BlockDefinition BlockDefinition::UnitModel(IndexType iNumX, IndexType iNumY, IndexType iNumZ)
{
BlockDefinition def(iNumX, iNumY, iNumZ, 0, 0, 0, 1, 1, 1);
return def;
}
const double PI = 3.141592653589793238462643383280;
const double TAU = 6.283185307179586476925286766559;
////////////////////////////////////////////////////////////////////////////////
AzmSlopePair::AzmSlopePair()
{
}
AzmSlopePair::AzmSlopePair(double iazm, double islope)
: Azimuth(iazm), Slope(islope)
{
}
AzmSlopePair::~AzmSlopePair()
{
}
std::ostream& mvd::mineflow::operator<<(std::ostream& os, const AzmSlopePair& a)
{
os << "{" << a.Azimuth << " (" << ToDegrees(a.Azimuth) << "_deg), " << a.Slope << " (" << ToDegrees(a.Slope) << "_deg)}";
return os;
}
bool AzmSlopePair::operator<(const AzmSlopePair& other) const
{
if (Azimuth == other.Azimuth) {
return Slope < other.Slope;
}
return Azimuth < other.Azimuth;
}
bool AzmSlopePair::operator<(double otherAzimuth) const
{
return Azimuth < otherAzimuth;
}
////////////////////////////////////////////////////////////////////////////////
typedef std::vector<AzmSlopePair>::const_iterator pairIter;
typedef std::pair<pairIter, pairIter> pairIterPair;
static pairIterPair GetLeftRight(const std::vector<AzmSlopePair>& pairs,
double azimuth)
{
while (azimuth >= TAU) azimuth -= TAU;
while (azimuth < 0) azimuth += TAU;
auto right = std::lower_bound(pairs.begin(), pairs.end(), azimuth);
if (right == pairs.end()) right = pairs.begin();
auto left = (right == pairs.begin()) ? std::prev(pairs.end()) : std::prev(right);
return std::make_pair(left, right);
}
static double GetXval(const pairIter& left, const pairIter& right, double azimuth)
{
double to_left, to_right;
if (left->Azimuth > azimuth) {
to_left = TAU - left->Azimuth + azimuth;
} else {
to_left = azimuth - left->Azimuth;
}
if (right->Azimuth < azimuth) {
to_right = TAU - azimuth + right->Azimuth;
} else {
to_right = right->Azimuth - azimuth;
}
return to_left / (to_left + to_right);
}
////////////////////////////////////////////////////////////////////////////////
SlopeDefinition::SlopeDefinition(std::initializer_list<std::initializer_list<double>> list)
{
for (auto & rec : list) {
double azm = *rec.begin();
double slope = *std::next(rec.begin());
while (azm >= TAU) azm -= TAU;
while (azm < 0) azm += TAU;
AzmSlopePair as;
as.Azimuth = azm;
as.Slope = slope;
m_Pairs.push_back(as);
}
std::sort(m_Pairs.begin(), m_Pairs.end());
}
SlopeDefinition::SlopeDefinition(const std::vector<AzmSlopePair>& pairs)
: m_Pairs(pairs)
{
std::sort(m_Pairs.begin(), m_Pairs.end());
}
std::ostream& mvd::mineflow::operator<<(std::ostream& os, const SlopeDefinition& def)
{
for (auto & pair : def.Pairs()) {
os << pair << std::endl;
}
return os;
}
SlopeDefinition SlopeDefinition::Constant(double slope)
{
SlopeDefinition def({{0, slope}});
return def;
}
double SlopeDefinition::operator()(double azimuth) const
{
return Get(azimuth);
}
double SlopeDefinition::Get(double azimuth) const
{
if (m_Pairs.empty()) {
return 0.0;
}
if (m_Pairs.size() == 1) {
return m_Pairs[0].Slope;
}
auto lr = GetLeftRight(m_Pairs, azimuth);
double xval = GetXval(lr.first, lr.second, azimuth);
double slope = lr.first->Slope + (lr.second->Slope - lr.first->Slope) * xval;
return slope;
}
bool SlopeDefinition::Within(const Vector3D& vec) const
{
return Within(vec.x, vec.y, vec.z);
}
bool SlopeDefinition::Within(double dx, double dy, double dz) const
{
if (dx == 0 && dy == 0) {
return true;
}
double dt = std::sqrt(dx * dx + dy * dy);
double theta = std::atan(std::abs(dz) / dt);
double azm = PI / 2 - std::atan2(dy, dx);
return theta >= Get(azm);
}
double SlopeDefinition::MinSlope() const
{
if (m_Pairs.empty()) {
return 0.0;
}
double minslope = m_Pairs[0].Slope;
for (auto & pair : m_Pairs) {
if (pair.Slope < minslope) {
minslope = pair.Slope;
}
}
return minslope;
}
uint64_t SlopeDefinition::NumPairs() const
{
return static_cast<uint64_t>(m_Pairs.size());
}
bool SlopeDefinition::Empty() const
{
return m_Pairs.empty();
}
const std::vector<AzmSlopePair>& SlopeDefinition::Pairs() const
{
return m_Pairs;
}
////////////////////////////////////////////////////////////////////////////////
SlopeDefinition mvd::mineflow::CubicInterpolate(const SlopeDefinition& def, int cnt)
{
if (def.NumPairs() < 4) {
throw std::runtime_error("must be at least 4 pairs for cubic");
}
auto y0it = std::prev(def.Pairs().end());
auto y1it = def.Pairs().begin();
auto y2it = std::next(y1it);
auto y3it = std::next(y2it);
std::vector<AzmSlopePair> outPairs;
outPairs.resize(cnt + 1);
int i = 0;
for (auto v : Linspace(0, TAU, cnt + 1)) {
if (v >= y2it->Azimuth && y2it->Azimuth != 0) {
y0it = y1it;
y1it = y2it;
y2it = y3it;
y3it = std::next(y3it);
if (y3it == def.Pairs().end()) y3it = def.Pairs().begin();
}
double mu = GetXval(y1it, y2it, v);
double y0 = y0it->Slope;
double y1 = y1it->Slope;
double y2 = y2it->Slope;
double y3 = y3it->Slope;
double mu2 = mu * mu;
double a0 = y3 - y2 - y0 + y1;
double a1 = y0 - y1 - a0;
double a2 = y2 - y0;
double a3 = y1;
outPairs[i].Azimuth = v;
double yn = a0 * mu * mu2 + a1 * mu2 + a2 * mu + a3;
outPairs[i].Slope = yn;
i++;
}
outPairs.pop_back();
return SlopeDefinition(outPairs);
}
SlopeDefinition mvd::mineflow::CosineInterpolate(const SlopeDefinition& def, int cnt)
{
if (def.NumPairs() < 2) {
throw std::runtime_error("must be at least 2 pairs for cosine");
}
auto y1it = def.Pairs().begin();
auto y2it = std::next(y1it);
std::vector<AzmSlopePair> outPairs(cnt + 1);
int i = 0;
for (auto v : Linspace(0, TAU, cnt + 1)) {
if (v >= y2it->Azimuth && y2it->Azimuth != 0) {
y1it = y2it;
y2it = std::next(y2it);
if (y2it == def.Pairs().end()) y2it = def.Pairs().begin();
}
double mu = GetXval(y1it, y2it, v);
double y1 = y1it->Slope;
double y2 = y2it->Slope;
double mu2 = (1 - std::cos(mu * PI)) / 2.0;
outPairs[i].Azimuth = v;
double yn = y1 * (1 - mu2) + y2 * mu2;
outPairs[i].Slope = yn;
i++;
}
outPairs.pop_back();
return SlopeDefinition(outPairs);
}
////////////////////////////////////////////////////////////////////////////////
IBlockIndexInputIteratorSource::IBlockIndexInputIteratorSource()
{
}
IBlockIndexInputIteratorSource::~IBlockIndexInputIteratorSource()
{
}
////////////////////////////////////////////////////////////////////////////////
BlockIndexInputIteratorBase::BlockIndexInputIteratorBase(
IBlockIndexInputIteratorSource* source)
: m_Source(source)
{
}
BlockIndexInputIteratorBase::~BlockIndexInputIteratorBase()
{
if (m_Source) {
delete m_Source;
}
}
BlockIndexInputIteratorBase::iterator BlockIndexInputIteratorBase::begin() const
{
return BlockIndexInputIteratorBase::iterator(m_Source);
}
BlockIndexInputIteratorBase::iterator BlockIndexInputIteratorBase::end() const
{
return BlockIndexInputIteratorBase::iterator();
}
////////////////////////////////////////////////////////////////////////////////
BlockIndexInputIteratorBase::iterator::iterator(IBlockIndexInputIteratorSource* source)
: m_Source(source)
{
Next();
}
BlockIndexInputIteratorBase::iterator::reference BlockIndexInputIteratorBase::iterator::operator*() const
{
return m_CurrentIndex;
}
BlockIndexInputIteratorBase::iterator& BlockIndexInputIteratorBase::iterator::operator++()
{
Next();
return *this;
}
BlockIndexInputIteratorBase::iterator& BlockIndexInputIteratorBase::iterator::operator++(int)
{
Next();
return *this;
}
bool BlockIndexInputIteratorBase::iterator::operator==(BlockIndexInputIteratorBase::iterator rhs) const
{
return m_Source == rhs.m_Source;
}
bool BlockIndexInputIteratorBase::iterator::operator!=(BlockIndexInputIteratorBase::iterator rhs) const
{
return m_Source != rhs.m_Source;
}
void BlockIndexInputIteratorBase::iterator::Next()
{
if (m_Source && m_Source->HasMore()) {
m_CurrentIndex = m_Source->Next();
} else {
m_Source = nullptr;
}
}
////////////////////////////////////////////////////////////////////////////////
IPrecedenceConstraintInputIteratorSource::IPrecedenceConstraintInputIteratorSource()
{
}
IPrecedenceConstraintInputIteratorSource::~IPrecedenceConstraintInputIteratorSource()
{
}
////////////////////////////////////////////////////////////////////////////////
PrecedenceConstraintInputIteratorBase::PrecedenceConstraintInputIteratorBase(
IPrecedenceConstraintInputIteratorSource* source)
: m_Source(source)
{
}
PrecedenceConstraintInputIteratorBase::~PrecedenceConstraintInputIteratorBase()
{
if (m_Source) {
delete m_Source;
}
}
PrecedenceConstraintInputIteratorBase::iterator PrecedenceConstraintInputIteratorBase::begin() const
{
return PrecedenceConstraintInputIteratorBase::iterator(m_Source);
}
PrecedenceConstraintInputIteratorBase::iterator PrecedenceConstraintInputIteratorBase::end() const
{
return PrecedenceConstraintInputIteratorBase::iterator();
}
////////////////////////////////////////////////////////////////////////////////
PrecedenceConstraintInputIteratorBase::iterator::iterator(
IPrecedenceConstraintInputIteratorSource* source)
: m_Source(source)
{
Next();
}
PrecedenceConstraintInputIteratorBase::iterator::reference PrecedenceConstraintInputIteratorBase::iterator::operator*() const
{
return m_CurrentPrecedenceConstraint;
}
PrecedenceConstraintInputIteratorBase::iterator& PrecedenceConstraintInputIteratorBase::iterator::operator++()
{
Next();
return *this;
}
PrecedenceConstraintInputIteratorBase::iterator& PrecedenceConstraintInputIteratorBase::iterator::operator++(int)
{
Next();
return *this;
}
bool PrecedenceConstraintInputIteratorBase::iterator::operator==(PrecedenceConstraintInputIteratorBase::iterator rhs) const
{
return m_Source == rhs.m_Source;
}
bool PrecedenceConstraintInputIteratorBase::iterator::operator!=(PrecedenceConstraintInputIteratorBase::iterator rhs) const
{
return m_Source != rhs.m_Source;
}
void PrecedenceConstraintInputIteratorBase::iterator::Next()
{
if (m_Source && m_Source->HasMore()) {
m_CurrentPrecedenceConstraint = m_Source->Next();
} else {
m_Source = nullptr;
}
}
////////////////////////////////////////////////////////////////////////////////
class SimplePrecedenceConstraintInputIteratorSource : public IPrecedenceConstraintInputIteratorSource
{
public:
SimplePrecedenceConstraintInputIteratorSource(IndexType numBlocks,
std::function<BlockIndexInputIteratorBase(IndexType)> antecedents)
: m_NumBlocks(numBlocks)
, m_CurrentBlockIndex(0)
, m_AntecedentsFunc(antecedents)
{
for (auto & v : m_AntecedentsFunc(m_CurrentBlockIndex)) {
m_Remaining.push(v);
}
PopulateRemaining();
}
~SimplePrecedenceConstraintInputIteratorSource() {}
PrecedenceConstraint Next()
{
PrecedenceConstraint c;
c.From = m_CurrentBlockIndex;
c.To = m_Remaining.front();
m_Remaining.pop();
PopulateRemaining();
return c;
}
bool HasMore() const
{
return !m_Remaining.empty();
}
void PopulateRemaining()
{
while (m_Remaining.empty() && m_CurrentBlockIndex < (m_NumBlocks - 1)) {
m_CurrentBlockIndex++;
for (auto & v : m_AntecedentsFunc(m_CurrentBlockIndex)) {
m_Remaining.push(v);
}
}
}
private:
IndexType m_NumBlocks;
IndexType m_CurrentBlockIndex;
std::function<BlockIndexInputIteratorBase(IndexType)> m_AntecedentsFunc;
std::queue<IndexType> m_Remaining;
};
////////////////////////////////////////////////////////////////////////////////
IPrecedenceConstraints::IPrecedenceConstraints()
{
}
IPrecedenceConstraints::~IPrecedenceConstraints()
{
}
BlockIndexInputIteratorBase IPrecedenceConstraints::Successors(IndexType toBlockIndex) const
{
throw std::runtime_error("not implemented");
}
IndexType IPrecedenceConstraints::NumAntecedents(IndexType fromBlockIndex) const
{
IndexType cnt = 0;
for (auto & v : Antecedents(fromBlockIndex)) {
cnt++;
}
return cnt;
}
IndexType IPrecedenceConstraints::ApproxNumAntecedents(IndexType fromBlockIndex) const
{
return 0;
}
void IPrecedenceConstraints::AntecedentsVector(IndexType fromBlockIndex, std::vector<IndexType>* vec) const
{
MVD_ASSERT(vec);
vec->clear();
for (auto & v : Antecedents(fromBlockIndex)) {
vec->push_back(v);
}
}
IndexType IPrecedenceConstraints::NumSuccessors(IndexType toBlockIndex) const
{
IndexType cnt = 0;
for (auto & v : Successors(toBlockIndex)) {
cnt++;
}
return cnt;
}
IndexType IPrecedenceConstraints::ApproxNumSuccessors(IndexType toBlockIndex) const
{
return 0;
}
void IPrecedenceConstraints::SuccessorsVector(IndexType toBlockIndex, std::vector<IndexType>* vec) const
{
MVD_ASSERT(vec);
vec->clear();
for (auto & v : Successors(toBlockIndex)) {
vec->push_back(v);
}
}
// Sometimes we want all the precedence constraints
IndexType IPrecedenceConstraints::NumPrecedenceConstraints() const
{
IndexType cnt = 0;
for (IndexType blockIndex = 0; blockIndex < NumBlocks(); blockIndex++) {
cnt += NumAntecedents(blockIndex);
}
return cnt;
}
IndexType IPrecedenceConstraints::ApproxNumPrecedenceConstraints() const
{
return 0;
}
PrecedenceConstraintInputIteratorBase IPrecedenceConstraints::PrecedenceConstraints() const
{
return PrecedenceConstraintInputIteratorBase(new
SimplePrecedenceConstraintInputIteratorSource(
NumBlocks(),
std::bind(&IPrecedenceConstraints::Antecedents, this, std::placeholders::_1)
));
}
void IPrecedenceConstraints::PrecedenceConstraintsVector(std::vector<PrecedenceConstraint>* vec) const
{
MVD_ASSERT(vec);
vec->clear();
for (auto & v : PrecedenceConstraints()) {
vec->push_back(v);
}
}
PrecedenceConstraintsReachableSearchBufferPtr IPrecedenceConstraints::GetNewSearchBuffer() const
{
return std::make_unique<PrecedenceConstraintsReachableSearchBuffer>(NumBlocks());
}
class ReachableBlockSource : public IBlockIndexInputIteratorSource
{
public:
ReachableBlockSource(
IndexType blockIndex,
std::function<BlockIndexInputIteratorBase(IndexType)> func,
PrecedenceConstraintsReachableSearchBuffer* buffer)
: m_Func(func)
, m_Buffer(buffer)
{
MVD_ASSERT(m_Buffer);
m_Buffer->NewSearch();
for (auto & v : m_Func(blockIndex)) {
m_Buffer->Queue(v);
}
}
~ReachableBlockSource() {}
IndexType Next() override final