-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmddstate.hpp
More file actions
1604 lines (1563 loc) · 65.9 KB
/
mddstate.hpp
File metadata and controls
1604 lines (1563 loc) · 65.9 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
/*
* mini-cp is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License v3
* as published by the Free Software Foundation.
*
* mini-cp is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY.
* See the GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with mini-cp. If not, see http://www.gnu.org/licenses/lgpl-3.0.en.html
*
* Copyright (c) 2018. by Laurent Michel, Pierre Schaus, Pascal Van Hentenryck
*/
#ifndef mddstate_hpp
#define mddstate_hpp
#include "handle.hpp"
#include "intvar.hpp"
#include "utilities.hpp"
#include "trailVec.hpp"
#include <set>
#include <cstring>
#include <map>
#include <unordered_map>
#include <bitset>
#include <utility>
#if defined(__x86_64__)
#include <xmmintrin.h>
#endif
#include <limits>
#include "hashtable.hpp"
#define XXH_INLINE_ALL
#include "xxhash.h"
/**
* @brief Representation for a set of integers.
*
* The class treats singletons slightly differently (for speed).
*/
class MDDIntSet {
short _mxs,_sz;
bool _isSingle;
union {
int* _buf;
int _single;
};
public:
/**
* Copy Constructor with allocator
* @param p the allocator to use
* @param s the set to copy
*/
template <class Allocator>
MDDIntSet(Allocator p,const MDDIntSet& s) {
if (s._isSingle) {
_single = s._single;
_mxs = _sz = 1;
_isSingle = true;
} else {
_mxs = s._mxs;
_sz = s._sz;
_isSingle = false;
_buf = new (p) int[_mxs];
memcpy(_buf,s._buf,sizeof(int)*_mxs);
}
}
/**
* Default constructor. Creates an empty set.
*/
MDDIntSet() { _buf = 0;_mxs=_sz=0;_isSingle=false;}
/**
* Singleton constructor. It creates a singleton (can't add afterwards)
* @param v the value in the set
*/
MDDIntSet(int v) : _mxs(1),_sz(1),_isSingle(true) {
_single = v;
}
/**
* List constructor. It creates a set from the provided list of values and grabs memory from the given buffer
* @param buf a pointer to a buffer large enough to accommodate all the values in `val`
* @param vals the list of values to populate the set.
*
*/
MDDIntSet(char* buf,int mxs,std::initializer_list<int> vals)
: _mxs(mxs),_sz(0),_isSingle(false) {
_buf = reinterpret_cast<int*>(buf);
for(int v : vals)
_buf[_sz++] = v;
}
/**
* Basic constructor. It creates an empty set capable to hold up to `mxs` values.
* @param buf a pointer to a buffer large enough to accommodate `mxs` integers (32-bit wide each)
* @param mxs the maximum size of the array
*/
MDDIntSet(char* buf,int mxs) : _mxs(mxs),_sz(0),_isSingle(false) {
_buf = reinterpret_cast<int*>(buf);
}
/**
* Empties the set of all values.
*/
void clear() noexcept { _sz = 0;_isSingle=false;}
/**
* Adds a value to the set (not checking for duplicates)
* @param v the value to add.
* Notes: the storage must be sufficient to add an element. There is no duplicate check.
*/
constexpr void add(int v) noexcept {
assert(_sz < _mxs);
_buf[_sz++] = v;
}
/**
* Adds a value to the set (if the value is already present, nothing is done)
* @param v the value to add
*/
void insert(int v) noexcept {
if (contains(v)) return;
if(_sz >= _mxs) {
std::cerr << "Oops... overflowing MDDIntSet\n";
exit(1);
}
_buf[_sz++] = v;
}
/**
* Checks membership
* @param v the value to check
* @return true if and only if \f$v \in this\f$
*/
constexpr const bool contains(int v) const noexcept {
if (_isSingle)
return _single == v;
else {
for(short i=0;i < _sz;i++)
if (_buf[i]==v) return true;
return false;
}
}
/**
* Checks whether a member of this set is *NOT* in S (is therefore outside of S)
* @param S the set to check against
* @return \f$ \exists v \in this : v \notin S\f$
*/
const bool memberOutside(const ValueSet& S) const noexcept {
if (_isSingle) return !S.member(_single);
else {
for(short k=0;k <_sz;k++)
if (!S.member(_buf[k])) return true;
return false;
}
}
/**
* Checks whether *all* members of this set are in S (all inside of S)
* @param S the set to check against
* @return \f$ this \subseteq S\f$
*/
const bool allInside(const ValueSet& S) const noexcept {
if (_isSingle) return S.member(_single);
else {
for(short k=0;k <_sz;k++)
if (!S.member(_buf[k])) return false;
return true;
}
}
/**
* Checks whether there is at least one member of this set in S (one inside of S)
* @param S the set to check against
* @return \f$ this \cap S \neq \emptyset\f$
*/
const bool memberInside(const ValueSet& S) const noexcept {
if (_isSingle) return S.member(_single);
else {
for(short k=0;k <_sz;k++)
if (S.member(_buf[k])) return true;
return false;
}
}
/**
* Compute and returns the smallest element in the set
* @return the smallest element
*/
const int min() const noexcept {
if (_isSingle) return _single;
else {
int min = std::numeric_limits<int>::max();
for(short i=0;i < _sz;++i)
min = std::min(min,_buf[i]);
return min;
}
}
/**
* Compute and returns the largest element in the set
* @return the largest element
*/
const int max() const noexcept {
if (_isSingle) return _single;
else {
int max = std::numeric_limits<int>::min();
for(short i=0;i < _sz;++i)
max = std::max(max,_buf[i]);
return max;
}
}
/**
* Equality test.
* @param a a set of integers
* @param b a set of integers
* @return true if and only if both sets are equal.
*/
friend bool operator==(const MDDIntSet& a,const MDDIntSet& b) {
if (a._isSingle == b._isSingle) {
if (a._isSingle) return a._single == b._single;
else {
if (a._sz == b._sz) {
for(auto i : a)
if (!b.contains(i)) return false;
return true;
} else return false;
}
} else return false;
}
/**
* Computes a 32-bit hash value for the set.
* @param a hash
* Note : This is a very naive hash. (not that is matters much)
*/
int hash() const noexcept {
if (_isSingle) return _single;
else {
int ttl=0;
for(int k=0;k < _sz;++k)
ttl += _buf[k];
return ttl * _sz;
}
}
/**
* Returns the number of elements in the set
* @return number of values in the set
*/
constexpr const short size() const { return _sz;}
constexpr const bool isEmpty() const { return _sz == 0;}
constexpr const bool isSingleton() const { return _isSingle || _sz == 1;}
constexpr const int singleton() const {
if (_isSingle)
return _single;
else {
assert(_sz == 1);
return _buf[0];
}
}
/**
* @brief C++ STL style iterator.
*
* This returns an open range `[begin()..end() )` thas is usable with C++ for loops.
* One obtains iterator instances with the convenience function begin/end provided below
*/
class iterator {
union {
int* _data;
int _val;
};
short _num;
bool _single;
iterator(int* d,long num = 0) : _data(d),_num(num),_single(false) {}
iterator(int v,long num = 0) : _val(v),_num(num),_single(true) {}
public:
using iterator_category = std::forward_iterator_tag;
using value_type = int;
using difference_type = int;
using pointer = int*;
using reference = int&;
iterator& operator++() { _num = _num + 1; return *this;}
iterator operator++(int) { iterator retval = *this; ++(*this); return retval;}
iterator& operator--() { _num = _num - 1; return *this;}
iterator operator--(int) { iterator retval = *this; --(*this); return retval;}
bool operator==(iterator other) const {return _num == other._num;}
bool operator!=(iterator other) const {return !(*this == other);}
int operator*() const { return _single ? _val : _data[_num];}
friend class MDDIntSet;
};
/**
* Iterator pointing to the first element of the set
* @return the start iterator
*/
iterator begin() const { return _isSingle ? iterator(_single,0) : iterator(_buf,0);}
/**
* Iterator pointing just past the last element of the set
* @return the end iterator
*/
iterator end() const { return _isSingle ? iterator(_single,_sz) : iterator(_buf,_sz);}
iterator cbegin() const noexcept { return begin();}
iterator cend() const noexcept { return end();}
/**
* Convenience operator to print a set
* @param os the stream to print to
* @param s the set to print
* @return the stream after printing (for chaining sake)
*/
friend std::ostream& operator<<(std::ostream& os,const MDDIntSet& s) {
os << '{';
for(int v : s)
os << v << ',';
return os << '\b' << '}';
}
};
void printSet(const MDDIntSet& s);
enum RelaxWith { External, MinFun,MaxFun};
struct MDDPack;
class MDDState;
class MDDNode;
typedef std::function<bool(const MDDPack&)> NodeFun;
typedef std::function<bool(const MDDPack&,const MDDPack&,const var<int>::Ptr&,int)> ArcFun;
typedef std::function<void(const MDDPack&)> FixFun;
typedef std::function<void(MDDState&,const MDDPack&)> UpdateFun;
typedef std::function<void(MDDState&,const MDDPack&,const var<int>::Ptr&,const MDDIntSet&)> lambdaTrans;
typedef std::function<void(MDDState&,const MDDState&,const MDDState&)> lambdaRelax;
typedef std::function<double(const MDDState&,const MDDState&)> lambdaSim;
typedef std::function<double(const MDDNode&)> SplitFun;
typedef std::function<double(const MDDState&, void*, int)> CandidateFun;
typedef std::function<int(const MDDState&,const MDDState&)> EquivalenceValueFun;
//typedef std::function<int*(TVec<MDDNode*>*)> ValueScoreFun;
typedef std::function<int(TVec<MDDNode*>*)> BestValueFun;
class MDDStateSpec;
/**
* @brief Memory zone descriptor
*
* Instances of this class are used to describe consecutive "blocks" of memory within MDDState instances.
* Internal use only.
*/
class Zone {
unsigned short _startOfs;
unsigned short _length;
std::set<int> _props;
public:
Zone(unsigned short so,unsigned short eo,const std::set<int>& ps) : _startOfs(so),_length(eo-so),_props(ps) {}
friend std::ostream& operator<<(std::ostream& os,const Zone& z) {
os << "zone(" << z._startOfs << "-->" << (int)z._startOfs + z._length << ")";return os;
}
void copy(char* dst,char* src) const noexcept { memcpy(dst+_startOfs,src+_startOfs,_length);}
};
/**
* @brief MDD Constraint Descriptor class.
*
* Each MDD LTS added to a propagator is associated to one such descriptor.
* Its purpose is to track everything to model this LTS in the propagator.
*/
class MDDCstrDesc {
Factory::Veci _vars;
ValueSet _vset;
const char* _name;
std::vector<int> _propertiesDown;
std::vector<int> _propertiesUp;
std::vector<int> _propertiesCombined;
std::vector<int> _downTId; // transition ids
std::vector<int> _upTId; // up transition ids
std::vector<int> _downRId; // relaxation ids
std::vector<int> _upRId; // relaxation ids
std::vector<int> _uid; // update ids
std::vector<int> _sid; // similarity ids
public:
typedef handle_ptr<MDDCstrDesc> Ptr;
template <class Vec> MDDCstrDesc(const Vec& vars, const char* name)
: _vars(vars.size(),Factory::alloci(vars[0]->getStore())),
_vset(vars),
_name(name)
{
for(typename Vec::size_type i=0;i < vars.size();i++)
_vars[i] = vars[i];
}
MDDCstrDesc(const MDDCstrDesc&);
void addDownProperty(int p) {_propertiesDown.push_back(p);}
void addUpProperty(int p) {_propertiesUp.push_back(p);}
void addCombinedProperty(int p) {_propertiesCombined.push_back(p);}
bool ownsDownProperty(int p) const;
bool ownsUpProperty(int p) const;
bool ownsCombinedProperty(int p) const;
const std::vector<int>& downTransitions() const { return _downTId;}
const std::vector<int>& upTransitions() const { return _upTId;}
const std::vector<int>& downRelaxations() const { return _downRId;}
const std::vector<int>& upRelaxations() const { return _upRId;}
const std::vector<int>& similarities() const { return _sid;}
void registerDown(int t) { _downTId.emplace_back(t);}
void registerUp(int t) { _upTId.emplace_back(t);}
void registerDownRelaxation(int t) { _downRId.emplace_back(t);}
void registerUpRelaxation(int t) { _upRId.emplace_back(t);}
void registerUpdate(int t) { _uid.emplace_back(t);}
void registerSimilarity(int t) { _sid.emplace_back(t);}
bool inScope(const var<int>::Ptr& x) const noexcept { return _vset.member(x->getId());}
const Factory::Veci& vars() const { return _vars;}
std::vector<int>& propertiesDown() { return _propertiesDown;}
std::vector<int>& propertiesUp() { return _propertiesUp;}
std::vector<int>& propertiesCombined() { return _propertiesCombined;}
auto downBegin() { return _propertiesDown.begin();}
auto downEnd() { return _propertiesDown.end();}
auto upBegin() { return _propertiesUp.begin();}
auto upEnd() { return _propertiesUp.end();}
auto combinedBegin() { return _propertiesCombined.begin();}
auto combinedEnd() { return _propertiesCombined.end();}
void print (std::ostream& os) const { os << _name << "(" << _vars << ")\n";}
};
/**
* @brief A Bit Sequence _value_.
*
* This class is used to represent values in MDDState that hold a 0-based sequence of bits (of a given length)
* @see MDDState, MDDProperty, MDDPBitSequence
*/
class MDDBSValue {
unsigned long long* _buf;
const short _nbw;
public:
MDDBSValue(char* buf,short nbw)
: _buf((unsigned long long*)(buf)),_nbw(nbw) {}
MDDBSValue(const MDDBSValue& v)
: _buf(v._buf),_nbw(v._nbw) {}
MDDBSValue(MDDBSValue&& v) : _buf(v._buf),_nbw(v._nbw) { v._buf = nullptr;}
short nbWords() const noexcept { return _nbw;}
MDDBSValue& operator=(const MDDBSValue& v) noexcept {
for(int i=0;i <_nbw;++i)
_buf[i] = v._buf[i];
assert(_nbw == v._nbw);
return *this;
}
constexpr bool getBit(const int ofs) const noexcept {
const int wIdx = ofs >> 6;
const int bOfs = ofs & ((1<<6) - 1);
return (_buf[wIdx] >> bOfs) & 0x1;
}
void clear(const int ofs) noexcept {
const int wIdx = ofs >> 6;
const int bOfs = ofs & ((1<<6) - 1);
const unsigned long long bmask = 0x1ull << bOfs;
_buf[wIdx] &= ~bmask;
}
void set(const int ofs) noexcept {
const int wIdx = ofs >> 6;
const int bOfs = ofs & ((1<<6)-1);
const unsigned long long bmask = 0x1ull << bOfs;
_buf[wIdx] |= bmask;
}
constexpr int cardinality() const noexcept {
int nbb = 0;
for(int i = (int)_nbw-1;i >= 0;--i)
nbb += __builtin_popcountll(_buf[i]);
return nbb;
}
__attribute__((always_inline)) inline MDDBSValue& setBinOR(const MDDBSValue& a,const MDDBSValue& b) noexcept {
switch(_nbw) {
case 1: _buf[0] = a._buf[0] | b._buf[0];return *this;
#if defined(__x86_64__)
case 2: {
__m128i p0 = *(__m128i*) a._buf;
__m128i p1 = *(__m128i*) b._buf;
*(__m128i*)_buf = _mm_or_si128(p0,p1);
} return *this;
#endif
default:
for(int i=0;i < _nbw;i++)
_buf[i] = a._buf[i] | b._buf[i];
return *this;
}
}
MDDBSValue& setBinAND(const MDDBSValue& a,const MDDBSValue& b) noexcept {
for(int i=0;i < _nbw;i++)
_buf[i] = a._buf[i] & b._buf[i];
return *this;
}
MDDBSValue& setBinXOR(const MDDBSValue& a,const MDDBSValue& b) noexcept {
for(int i=0;i < _nbw;i++)
_buf[i] = a._buf[i] ^ b._buf[i];
return *this;
}
MDDBSValue& NOT() noexcept {
for(int i=0;i <_nbw;i++)
_buf[i] = ~_buf[i];
return *this;
}
class iterator {
unsigned long long* _t;
const short _nbw;
short _cwi; // current word index
unsigned long long _cw; // current word
iterator(unsigned long long* t,short nbw,short at)
: _t(t),_nbw(nbw),_cwi(at),_cw((at < nbw) ? t[at] : 0) {
while (_cw == 0 && ++_cwi < _nbw)
_cw = _t[_cwi];
}
iterator(unsigned long long* t,short nbw) : _t(t),_nbw(nbw),_cwi(nbw),_cw(0) {} // end constructor
public:
using iterator_category = std::forward_iterator_tag;
using value_type = short;
using difference_type = short;
using pointer = short*;
using reference = short&;
iterator& operator++() noexcept {
unsigned long long test = _cw & -_cw; // only leaves LSB at 1
_cw ^= test; // clear LSB
while (_cw == 0 && ++_cwi < _nbw) // all bits at zero-> done with this word.
_cw = _t[_cwi];
return *this;
}
iterator operator++(int) { iterator retval = *this; ++(*this); return retval;}
bool operator==(iterator other) const {return _cwi == other._cwi && _cw == other._cw;}
bool operator!=(iterator other) const {return !(*this == other);}
short operator*() const { return (_cwi<<6) + __builtin_ctzl(_cw);}
friend class MDDBSValue;
};
iterator begin() const { return iterator(_buf,_nbw,0);}
iterator end() const { return iterator(_buf,_nbw);}
friend bool operator==(const MDDBSValue& a,const MDDBSValue& b) {
bool eq = a._nbw == b._nbw;
for(short i = 0 ;eq && i < a._nbw;++i)
eq = a._buf[i] == b._buf[i];
return eq;
}
friend bool operator!=(const MDDBSValue& a,const MDDBSValue& b) {
bool eq = a._nbw == b._nbw;
for(short i = 0 ;eq && i < a._nbw;++i)
eq = a._buf[i] == b._buf[i];
return !eq;
}
};
/**
* @brief A sliding window _value_.
*
* This class is used to represent values in MDDState
* @see MDDState, MDDProperty, MDDPSWindow<T>
*/
template <class ET>
class MDDSWin {
ET* _buf;
int _nb;
public:
MDDSWin(ET* buf,int nb) : _buf(buf),_nb(nb) {}
MDDSWin(MDDSWin<ET>&& v) : _buf(v._buf),_nb(v._nb) {}
MDDSWin(const MDDSWin<short>& v) : _buf(v._buf),_nb(v._nb) {}
int nbWords() const noexcept { return _nb;}
MDDSWin<ET>& operator=(const MDDSWin<ET>& v) noexcept {
for(int i=0;i < _nb;++i) _buf[i] = v._buf[i];
return *this;
}
ET get(int ofs) const noexcept { return _buf[ofs];}
ET last() const noexcept { return _buf[_nb-1];}
ET first() const noexcept { return _buf[0];}
void set(int ofs,ET v) noexcept { _buf[ofs] = v;}
void setFirst(ET v) noexcept { _buf[0] = v;}
MDDSWin<ET>& assignSlideBy(const MDDSWin<ET>& v,const int shift=0) {
for(int i=0;i < _nb - shift;++i) _buf[i+shift] = v._buf[i];
return *this;
}
friend bool operator==(const MDDSWin<ET>& a,const MDDSWin<ET>& b) noexcept {
bool eq = a._nb == b._nb;
for(int i=0;eq && i < a._nb;++i)
eq = a._buf[i] == b._buf[i];
return eq;
}
friend bool operator!=(const MDDSWin<ET>& a,const MDDSWin<ET>& b) noexcept {
bool eq = a._nb == b._nb;
for(int i=0;eq && i < a._nb;++i)
eq = a._buf[i] == b._buf[i];
return !eq;
}
};
/**
* Indicate the direction for a state.
*/
enum Direction { None=0,Down=1,Up=2,Bi=3};
/**
* @brief Abstract class representing a property to be held in an MDDState
*
* It is subclassed for each type of property one could store in an MDD.
* The abstract class only handles very generic aspect such as storage size and layout along with remembering
* how to relax values of this property.
* @see MDDPInt, MDDPBit, MDDPByte, MDDPBitSequence, MDDPSWin<T>
*/
class MDDProperty {
protected:
int _id;
unsigned int _ofs; // offset in bytes within block
unsigned short _bsz; // size in bytes.
enum Direction _dir;
enum RelaxWith _rw;
std::set<int> _sp1; // if property is down or combined, sp1 is down antecedent. if property is up, sp1 is up
std::set<int> _sp2; // if property is down or up, sp2 is combined antecedent. if property is combined, sp2 is up
int _tid;
int _rid;
virtual size_t storageSize() const = 0; // given in _bits_
virtual size_t setOffset(size_t bitOffset) = 0;
public:
typedef handle_ptr<MDDProperty> Ptr;
MDDProperty(const MDDProperty& p)
: _id(p._id),_ofs(p._ofs),_bsz(p._bsz),_dir(p._dir),_rw(p._rw),_tid(p._tid),_rid(p._rid) {}
MDDProperty(MDDProperty&& p)
: _id(p._id),_ofs(p._ofs),_bsz(p._bsz),_dir(p._dir),_rw(p._rw),_tid(p._tid),_rid(p._rid) {}
MDDProperty(int id,unsigned int ofs,unsigned short bsz,enum RelaxWith rw = External)
: _id(id),_ofs(ofs),_bsz(bsz),_dir(Down),_rw(rw) { _tid = -1;_rid = -1;}
MDDProperty& operator=(const MDDProperty& p) {
_id = p._id;_ofs = p._ofs; _bsz = p._bsz;_dir = p._dir;_rw = p._rw;
_tid = p._tid;
_rid = p._rid;
return *this;
}
const int getId() const { return _id;}
enum RelaxWith relaxFun() const noexcept { return _rw;}
unsigned int startOfs() const noexcept { return _ofs;}
unsigned int endOfs() const noexcept { return _ofs + _bsz;}
size_t size() const noexcept { return storageSize() >> 3;}
void setDirection(enum Direction d) { _dir = d;}
void setAntecedents(const std::set<int>& sp,const std::set<int>& sp2) { _sp1 = sp; _sp2 = sp2; }
void setTransition(int tid) noexcept { _tid = tid;}
void setRelax(int rid) noexcept { _rid = rid;}
int getTransition() const noexcept { return _tid;}
int getRelax() const noexcept { return _rid;}
const std::set<int>& antecedents() const { return _sp1;}
const std::set<int>& antecedentsSecondary() const { return _sp2;}
bool isUp() const noexcept { return (_dir & Up) == Up;}
bool isDown() const noexcept { return (_dir & Down) == Down;}
virtual void init(char* buf) const noexcept {}
virtual void minWith(char* buf,char* other) const noexcept {}
virtual void maxWith(char* buf,char* other) const noexcept {}
virtual bool diff(char* buf,char* other) const noexcept { return false;}
int getByte(char* buf) const noexcept { return buf[_ofs];}
MDDBSValue getBS(char* buf) const noexcept { return MDDBSValue(buf + _ofs,_bsz >> 3);}
template <class ET>
MDDSWin<ET> getSW(char* buf) const noexcept { return MDDSWin<ET>(reinterpret_cast<short*>(buf + _ofs),_bsz / sizeof(ET));}
void setInt(char* buf,int v) const noexcept { *reinterpret_cast<int*>(buf+_ofs) = v;}
void setByte(char* buf,char v) const noexcept { buf[_ofs] = v;}
MDDBSValue setBS(char* buf,const MDDBSValue& v) const noexcept {
MDDBSValue dest(buf + _ofs,_bsz >> 3);
dest = v;
return dest;
}
virtual void setProp(char* buf,char* from) const noexcept {
switch(_bsz) {
case 1: buf[_ofs] = from[_ofs];break;
case 2: *reinterpret_cast<short*>(buf+_ofs) = *reinterpret_cast<short*>(from+_ofs);break;
case 4: *reinterpret_cast<int*>(buf+_ofs) = *reinterpret_cast<int*>(from+_ofs);break;
case 8: *reinterpret_cast<long long*>(buf+_ofs) = *reinterpret_cast<long long*>(from+_ofs);break;
default: memcpy(buf + _ofs,from + _ofs,_bsz);break;
}
}
virtual void print(std::ostream& os) const = 0;
virtual void stream(char* buf,std::ostream& os) const {}
friend class MDDStateSpec;
};
/**
* @brief MDD State 32-bit Signed Integer Property
*
* This is a concrete proprety that refers to a specific integer property within an MDD State.
* Since an MDD State is a block of bytes, a property _value_ is held at a specific offset within
* this block of bytes. The `_ofs` attribute inherited from MDDProperty holds the offset.
* The class provides a few methods to read/write to this relative location within an MDDState.
* Those are use *internally* only and the user-level API is more abstract (but uses those).
* @see MDDPropValue<MDDPInt>
*/
class MDDPInt :public MDDProperty {
int _init;
int _max;
size_t storageSize() const override { return 32;}
size_t setOffset(size_t bitOffset) override {
size_t boW = bitOffset & 0x1F;
if (boW != 0)
bitOffset = (bitOffset | 0x1F) + 1;
_ofs = bitOffset >> 3;
return bitOffset + storageSize();
}
public:
typedef handle_ptr<MDDPInt> Ptr;
MDDPInt(int id,unsigned int ofs,int init,int max,enum RelaxWith rw)
: MDDProperty(id,ofs,4,rw),_init(init),_max(max) {}
void init(char* buf) const noexcept override { *reinterpret_cast<int*>(buf+_ofs) = _init;}
int getInt(char* buf) const noexcept { return *reinterpret_cast<int*>(buf+_ofs);}
void setInt(char* buf,int v) const noexcept { *reinterpret_cast<int*>(buf+_ofs) = v;}
void stream(char* buf,std::ostream& os) const override { os << *reinterpret_cast<int*>(buf+_ofs);}
void minWith(char* buf,char* other) const noexcept override {
int* o = reinterpret_cast<int*>(buf+_ofs);
int* i = reinterpret_cast<int*>(other+_ofs);
*o = std::min(*o,*i);
}
void maxWith(char* buf,char* other) const noexcept override {
int* o = reinterpret_cast<int*>(buf+_ofs);
int* i = reinterpret_cast<int*>(other+_ofs);
*o = std::max(*o,*i);
}
bool diff(char* buf,char* other) const noexcept override {
int o = *reinterpret_cast<int*>(buf+_ofs);
int i = *reinterpret_cast<int*>(other+_ofs);
return o != i;
}
void print(std::ostream& os) const override {
os << "PInt(" << _id << ',' << _ofs << ',' << _init << ',' << _max << ')';
}
friend class MDDStateSpec;
};
/**
* @brief MDD State 1-bit Boolean Property
*
* This is a concrete proprety that refers to a specific bit property within an MDD State.
* Since an MDD State is a block of bytes, a property _value_ is held at a specific offset within
* this block of bytes. The `_ofs` attribute inherited from MDDProperty holds the offset.
* The class provides a few methods to read/write to this relative location within an MDDState.
* Those are use *internally* only and the user-level API is more abstract (but uses those).
* @see MDDPropValue<MDDPBit>
*/
class MDDPBit :public MDDProperty {
bool _init;
char _bitmask;
size_t storageSize() const override { return 1;}
size_t setOffset(size_t bitOffset) override {
size_t boW = bitOffset & 0x7;
_bitmask = 0x1 << boW;
_ofs = bitOffset >> 3;
return bitOffset + storageSize();
}
public:
typedef handle_ptr<MDDPBit> Ptr;
MDDPBit(int id,unsigned int ofs,int init,enum RelaxWith rw)
: MDDProperty(id,ofs,1,rw),_init(init) {}
void init(char* buf) const noexcept override {
if (_init)
buf[_ofs] |= _bitmask;
else
buf[_ofs] &= ~_bitmask;
}
int getInt(char* buf) const noexcept { return (buf[_ofs] & _bitmask) == _bitmask;}
int getBit(char* buf) const noexcept { return (buf[_ofs] & _bitmask) == _bitmask;}
void setBit(char* buf,int v) const noexcept {
if (v)
buf[_ofs] |= _bitmask;
else
buf[_ofs] &= ~_bitmask;
}
void stream(char* buf,std::ostream& os) const override { bool v = buf[_ofs] & _bitmask;os << (int)v;}
void minWith(char* buf,char* other) const noexcept override {
if (!(buf[_ofs] & other[_ofs] & _bitmask))
buf[_ofs] &= ~_bitmask;
}
void maxWith(char* buf,char* other) const noexcept override {
buf[_ofs] |= (other[_ofs] & _bitmask);
}
bool diff(char* buf,char* other) const noexcept override {
return (buf[_ofs] & _bitmask) != (other[_ofs] & _bitmask);
}
void setProp(char* buf,char* from) const noexcept override {
if ((from[_ofs] & _bitmask) == _bitmask)
buf[_ofs] |= _bitmask;
else
buf[_ofs] &= ~_bitmask;
}
void print(std::ostream& os) const override {
os << "PBit(" << _id << ',' << _ofs << ',' << (int)_bitmask << ',' << (int)_init << ')';
}
friend class MDDStateSpec;
};
/**
* @brief MDD State 8-bit signed integer Property
*
* This is a concrete proprety that refers to a specific bit property within an MDD State.
* Since an MDD State is a block of bytes, a property _value_ is held at a specific offset within
* this block of bytes. The `_ofs` attribute inherited from MDDProperty holds the offset.
* The class provides a few methods to read/write to this relative location within an MDDState.
* Those are use *internally* only and the user-level API is more abstract (but uses those).
* @see MDDPropValue<MDDPByte>
*/
class MDDPByte :public MDDProperty {
char _init;
char _max;
size_t storageSize() const override { return 8;}
size_t setOffset(size_t bitOffset) override {
size_t boW = bitOffset & 0x7;
if (boW != 0)
bitOffset = (bitOffset | 0x7) + 1;
_ofs = bitOffset >> 3;
return bitOffset + storageSize();
}
public:
typedef handle_ptr<MDDPByte> Ptr;
MDDPByte(int id,unsigned int ofs,char init,char max,enum RelaxWith rw)
: MDDProperty(id,ofs,1,rw),_init(init),_max(max) {}
void init(char* buf) const noexcept override { buf[_ofs] = _init;}
int getByte(char* buf) const noexcept { return buf[_ofs];}
void setByte(char* buf,int v) const noexcept { buf[_ofs] = (char)v;}
void stream(char* buf,std::ostream& os) const override { int v = buf[_ofs];os << v;}
void minWith(char* buf,char* other) const noexcept override {
buf[_ofs] = std::min(buf[_ofs],other[_ofs]);
}
void maxWith(char* buf,char* other) const noexcept override {
buf[_ofs] = std::max(buf[_ofs],other[_ofs]);
}
bool diff(char* buf,char* other) const noexcept override {
return buf[_ofs] != other[_ofs];
}
void print(std::ostream& os) const override {
os << "PByte(" << _id << ',' << _ofs << ',' << (int)_init << ',' << (int)_max << ')';
}
friend class MDDStateSpec;
};
/**
* @brief MDD State bit sequence Property
*
* This is a concrete proprety that refers to a specific bit sequence property within an MDD State.
* Since an MDD State is a block of bytes, a property _value_ is held at a specific offset within
* this block of bytes. The `_ofs` attribute inherited from MDDProperty holds the offset.
* The class provides a few methods to read/write to this relative location within an MDDState.
* Those are use *internally* only and the user-level API is more abstract (but uses those).
* @see MDDPropValue<MDDPBitSequence>
*/
class MDDPBitSequence : public MDDProperty {
const int _nbBits;
unsigned char _init;
size_t storageSize() const override {
int up;
if (_nbBits % 64) {
up = ((_nbBits / 64) + 1) * 64;
} else up = _nbBits;
return up;
}
size_t setOffset(size_t bitOffset) override {
size_t boW = bitOffset & 0x1F;
if (boW != 0)
bitOffset = (bitOffset | 0x1F) + 1;
_ofs = bitOffset >> 3;
_ofs = ((_ofs & 0xF) != 0) ? (_ofs | 0xF)+1 : _ofs; // 16-byte align
return (_ofs << 3) + storageSize();
}
public:
typedef handle_ptr<MDDPBitSequence> Ptr;
MDDPBitSequence(int id,unsigned int ofs,int nbbits,unsigned char init,enum RelaxWith rw) // init = 0 | 1
: MDDProperty(id,ofs,8 * ((nbbits % 64) ? nbbits/64 + 1 : nbbits/64),rw),_nbBits(nbbits),_init(init)
{}
void init(char* buf) const noexcept override {
unsigned long long* ptr = reinterpret_cast<unsigned long long*>(buf + _ofs);
unsigned short nbw = _bsz >> 3;
for(unsigned short i=0u;i < nbw;++i) ptr[i] = 0x0ull;
unsigned long long bmask = (_init) ? ~0x0ull : 0x0ull;
short nbWords = _bsz >> 3;
for(int i=0;i < nbWords - 1;i++)
ptr[i] = bmask;
if (_init) {
int nbr = _nbBits % 64;
unsigned long long lm = (1ull << (nbr+1)) - 1;
ptr[nbWords-1] = lm;
}
}
bool diff(char* buf,char* other) const noexcept override {
return getBS(buf) != getBS(other);
}
void minWith(char* buf,char* other) const noexcept override {
auto a = getBS(buf);
auto b = getBS(other);
a.setBinOR(a,b);
}
void stream(char* buf,std::ostream& os) const override {
unsigned long long* words = reinterpret_cast<unsigned long long*>(buf + _ofs);
os << '[';
unsigned nbb = _nbBits;
int val = 0;
short nbWords = _bsz >> 3;
for(int i=0;i < nbWords;i++) {
unsigned long long w = words[i];
const unsigned biw = nbb >= 64 ? 64 : nbb;
nbb -= 64;
unsigned long long mask = 1ull;
unsigned bOfs = 0;
while (bOfs != biw) {
bool hasValue = ((w & mask)==mask);
if (hasValue) os << val << ',';
val++;
bOfs++;
mask <<=1;
}
}
os << ']';
}
void print(std::ostream& os) const override {
os << "PBS(" << _id << ',' << _ofs << ',' << _nbBits << ',' << (int)_init << ')';
}
friend class MDDStateSpec;
};
/**
* @brief MDD State Sliding Window Properties
*
* This is a concrete proprety that refers to a specific sliding window property within an MDD State.
* Since an MDD State is a block of bytes, a property _value_ is held at a specific offset within
* this block of bytes. The `_ofs` attribute inherited from MDDProperty holds the offset.
* The class provides a few methods to read/write to this relative location within an MDDState.
* Those are use *internally* only and the user-level API is more abstract (but uses those).
* @see MDDPropValue<MDDPSWindow<short>>
*/
template <class ET = unsigned char>
class MDDPSWindow : public MDDProperty {
ET _eltInit;
ET _fstInit;
const int _len; // number of elements in window (0,...._len-1)
size_t storageSize() const override {
return _len * sizeof(ET) * 8; // number of element * size of element in bytes * number of bits per byte.
}
size_t setOffset(size_t bitOffset) override {
constexpr int bitAlign = std::alignment_of<ET>::value * sizeof(char); // get byte, then bit alignment for ET
if (bitOffset & (bitAlign - 1)) // if not properly aligned for ET's requirements.
bitOffset = (bitOffset | (bitAlign - 1)) + 1; // realign by setting all alignment bits to 1 and adding 1.
_ofs = bitOffset >> 3; // _ofs is the start location with alignment respected
return bitOffset + storageSize();
}
public:
typedef handle_ptr<MDDPSWindow<ET>> Ptr;
MDDPSWindow(int id,unsigned int ofs,int len,ET eInit,ET fInit,enum RelaxWith rw)
: MDDProperty(id,ofs,len * sizeof(ET),rw),_eltInit(eInit),_fstInit(fInit),_len(len) {}
void init(char* buf) const noexcept override {
ET* ptr = reinterpret_cast<ET*>(buf + _ofs);
for(int i=0;i < _len;++i)
ptr[i] = _eltInit;
ptr[0] = _fstInit;
}
void minWith(char* buf,char* other) const noexcept override {
ET* a = reinterpret_cast<ET*>(buf + _ofs);
ET* b = reinterpret_cast<ET*>(other + _ofs);
for(int i=0;i < _len;++i)
a[i] = std::min(a[i],b[i]);
}
void maxWith(char* buf,char* other) const noexcept override {
ET* a = reinterpret_cast<ET*>(buf + _ofs);
ET* b = reinterpret_cast<ET*>(other + _ofs);
for(int i=0;i < _len;++i)
a[i] = std::max(a[i],b[i]);
}
bool diff(char* buf,char* other) const noexcept override {
ET* a = reinterpret_cast<ET*>(buf + _ofs);
ET* b = reinterpret_cast<ET*>(other + _ofs);
for(int i=0;i < _len;++i)
if (a[i] != b[i]) return true;
return false;
}
void stream(char* buf,std::ostream& os) const override {
os << '<';
ET* ptr = reinterpret_cast<ET*>(buf + _ofs);
for(int i=0;i < _len;++i) {
os << (int)(ptr[i]);
if (i < _len - 1)
os << ',';
}
os << '>';
}
void print(std::ostream& os) const override {
os << "PW(" << _id << ',' << _ofs << ',' << _len << ')';
}
friend class MDDStateSpec;
};
/**
* @brief This is the specification of an MDDState
*
* A state is <Down[,Up][,Combined]> (Both up and combined are optional, all three are MDDState instances)
* The class holds all the necessary attribute to compute all the properties via
* transitions and relaxations for any part of the state. It is intimately related
* to the MDDState and the properties they hold.
* @see MDDState, MDDProperty
*/
class MDDStateSpec {
protected:
MDDProperty::Ptr* _attrsDown;
MDDProperty::Ptr* _attrsUp;
MDDProperty::Ptr* _attrsCombined;
MDDPropSet* _omapDown;
MDDPropSet* _omapUp;
MDDPropSet* _omapDownToCombined;
MDDPropSet* _omapUpToCombined;
MDDPropSet* _omapCombinedToDown;
MDDPropSet* _omapCombinedToUp;
size_t _mxpDown;
size_t _mxpUp;
size_t _mxpCombined;
size_t _nbpDown;
size_t _nbpUp;
size_t _nbpCombined;
size_t _lszDown;
size_t _lszUp;
size_t _lszCombined;
enum RelaxWith _relax;
size_t _width;
void addDownProperty(MDDProperty::Ptr p) noexcept;
void addUpProperty(MDDProperty::Ptr p) noexcept;
void addCombinedProperty(MDDProperty::Ptr p) noexcept;
public:
MDDStateSpec();
void setWidth(size_t w) { _width = w;}