-
Notifications
You must be signed in to change notification settings - Fork 2
/
calObj.hh
2172 lines (1871 loc) · 87 KB
/
calObj.hh
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
/**CPPHeaderFile***************************************************************
FileName [calObj.hh]
PackageName [cal]
Synopsis [C++ Header CAL file for exported data structures and functions.]
Description []
SeeAlso []
Author [Steffan Sølvsten (soelvsten@cs.au.dk)]
Copyright [Copyright (c) 2023, Aarhus University]
All rights reserved.
Permission is hereby granted, without written agreement and without license
or royalty fees, to use, copy, modify, and distribute this software and its
documentation for any purpose, provided that the above copyright notice and
the following two paragraphs appear in all copies of this software.
IN NO EVENT SHALL AARHUS UNIVERSITY OR THE UNIVERSITY OF CALIFORNIA BE LIABLE
TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
DAMAGES ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
EITHER UNIVERSITY HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
BOTH UNIVERSITIES SPECIFICALLY DISCLAIMS ANY WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS,
AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO PROVIDE MAINTENANCE,
SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.]
Revision [$Id: calObj.h,v 3.0 2023/08/19 17:48:05 soelvsten Exp $]
******************************************************************************/
#ifndef _CALOBJ
#define _CALOBJ
extern "C" {
#include "cal.h"
#include "calInt.h"
}
#include <vector>
#include <sstream>
#include <string>
class BDD;
class Cal;
////////////////////////////////////////////////////////////////////////////////
/// \defgroup module__cpp C++ API
////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
/// \addtogroup module__cpp
///
/// \{
////////////////////////////////////////////////////////////////////////////////
/// \brief C++ wrapper for `Cal_Bdd`, managing reference count with *RAII*.
///
/// \details These BDDs are created from a `Cal` object which owns the unique
/// node table. Hence, the life-time of any `BDD` object should not outlast its
/// parent `Cal` object. Doing so leads to *undefined behaviour*.
///
/// \see Cal
////////////////////////////////////////////////////////////////////////////////
class BDD
{
friend Cal;
//////////////////////////////////////////////////////////////////////////////
// Types
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief Type of BDD identifiers, i.e. the variable name independent of the
/// current variable ordering.
//////////////////////////////////////////////////////////////////////////////
using Id_t = Cal_BddId_t;
//////////////////////////////////////////////////////////////////////////////
/// \brief Type of BDD indices, i.e. their placement in the current variable
/// ordering.
//////////////////////////////////////////////////////////////////////////////
using Index_t = Cal_BddIndex_t;
//////////////////////////////////////////////////////////////////////////////
// Members
private:
/// \brief C API reference to BDD Manager (needed to call the C API).
Cal_BddManager _bddManager;
/// \brief C API reference to BDD node to be managed.
Cal_Bdd _bdd;
//////////////////////////////////////////////////////////////////////////////
// Constructors
protected:
//////////////////////////////////////////////////////////////////////////////
/// \brief Wrap C API `Cal_Bdd` to be managed by this new `BDD` object.
///
/// \details This function does not increment the reference count of the given
/// `Cal_Bdd`, assuming the C API operation already has done so.
//////////////////////////////////////////////////////////////////////////////
BDD(Cal_BddManager bddManager, Cal_Bdd bdd)
: _bddManager(bddManager), _bdd(bdd)
{ }
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief The `NULL` BDD.
//////////////////////////////////////////////////////////////////////////////
BDD()
: _bddManager(NULL), _bdd(Cal_BddNull(NULL))
{ }
//////////////////////////////////////////////////////////////////////////////
/// \brief Copies ownership of another BDD.
//////////////////////////////////////////////////////////////////////////////
BDD(const BDD &other)
: _bddManager(other._bddManager), _bdd(other._bdd)
{
this->UnFree();
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Copy ownership of another BDD during assignment.
//////////////////////////////////////////////////////////////////////////////
BDD& operator= (const BDD &other)
{
this->Free();
this->_bdd = other._bdd;
this->_bddManager = other._bddManager;
this->UnFree();
return *this;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Move ownership of another BDD.
//////////////////////////////////////////////////////////////////////////////
BDD(BDD &&other)
: _bddManager(other._bddManager), _bdd(other._bdd)
{
other._bdd = Cal_BddNull(other._bddManager);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Move ownership of another BDD during assignment.
//////////////////////////////////////////////////////////////////////////////
BDD& operator= (BDD &&other)
{
this->Free();
this->_bdd = other._bdd;
this->_bddManager = other._bddManager;
other._bdd = Cal_BddNull(other._bddManager);
return *this;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Decrement reference count upon leaving scope.
//////////////////////////////////////////////////////////////////////////////
~BDD()
{
this->Free();
}
//////////////////////////////////////////////////////////////////////////////
// Predicates
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if the this BDD is constant one, `false `otherwise.
///
/// \see BDD::IsZero(), BDD::IsConst()
//////////////////////////////////////////////////////////////////////////////
bool IsOne() const
{ return Cal_BddIsBddOne(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if the this BDD is constant zero, `false `otherwise.
///
/// \see BDD::IsOne(), BDD::IsConst()
//////////////////////////////////////////////////////////////////////////////
bool IsZero() const
{ return Cal_BddIsBddZero(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if the this BDD is NULL, `false` otherwise.
//////////////////////////////////////////////////////////////////////////////
bool IsNull() const
{ return BDD::IsNull(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if the this BDD is either constant one or constant zero,
/// otherwise returns `false`.
///
/// \see BDD::IsOne(), BDD::IsZero()
//////////////////////////////////////////////////////////////////////////////
bool IsConst() const
{ return Cal_BddIsBddConst(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if the this BDD is a cube, `false` otherwise.
//////////////////////////////////////////////////////////////////////////////
bool IsCube() const
{ return Cal_BddIsCube(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if this BDD is equal to `other`, `false` otherwise.
//////////////////////////////////////////////////////////////////////////////
bool IsEqualTo(const BDD &other) const
{ return Cal_BddIsEqual(this->_bddManager, this->_bdd, other._bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if argument BDDs are equal, `false` otherwise.
///
/// \see BDD::IsEqualTo()
//////////////////////////////////////////////////////////////////////////////
bool operator== (const BDD &other) const
{ return this->IsEqualTo(other); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `false` if argument BDDs are equal, `true` otherwise.
///
/// \see BDD::IsEqualTo()
//////////////////////////////////////////////////////////////////////////////
bool operator!= (const BDD &other) const
{ return !(*this == other); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if this depends on `var`, `false` otherwise.
//////////////////////////////////////////////////////////////////////////////
bool DependsOn(const BDD &var) const
{ return Cal_BddDependsOn(this->_bddManager, this->_bdd, var._bdd); }
//////////////////////////////////////////////////////////////////////////////
// Node traversal and Information
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief BDD corresponding to the top variable of the this BDD.
///
/// \see Bdd::Id(), BDD::Index()
//////////////////////////////////////////////////////////////////////////////
BDD If() const
{ return BDD(this->_bddManager, Cal_BddIf(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Returns the id of this BDD's top variable.
///
/// \see BDD::Index()
//////////////////////////////////////////////////////////////////////////////
Id_t Id() const
{ return Cal_BddGetIfId(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Returns the index of this BDD's top variable.
///
/// \see BDD::Id()
//////////////////////////////////////////////////////////////////////////////
Index_t Index() const
{ return Cal_BddGetIfIndex(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
/// \brief The positive cofactor of this BDD with respect to its top
/// variable.
//////////////////////////////////////////////////////////////////////////////
BDD Then() const
{ return BDD(this->_bddManager, Cal_BddThen(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief The negative cofactor of this BDD with respect to its top variable.
//////////////////////////////////////////////////////////////////////////////
BDD Else() const
{ return BDD(this->_bddManager, Cal_BddElse(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Possible types a BDD can be.
///
/// \see BDD::Type()
//////////////////////////////////////////////////////////////////////////////
enum Type_t
{
/** The Zero terminal */
Zero = CAL_BDD_TYPE_ZERO,
/** The One terminal */
One = CAL_BDD_TYPE_ONE,
/** Constant (Non-Boolean) */
Constant = CAL_BDD_TYPE_CONSTANT,
/** Positive variable */
Posvar = CAL_BDD_TYPE_POSVAR,
/** Negative variable */
Negvar = CAL_BDD_TYPE_NEGVAR,
/** Invalid/Missing result due to Overflow */
Overflow = CAL_BDD_TYPE_OVERFLOW,
/** BDD type encompassing all other cases */
NonTerminal = CAL_BDD_TYPE_NONTERMINAL
};
//////////////////////////////////////////////////////////////////////////////
/// \brief The BDD type (0, 1, +var, -var, overflow, nonterminal).
//////////////////////////////////////////////////////////////////////////////
Type_t Type() const
{ return static_cast<Type_t>(Cal_BddType(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief This BDD's size.
///
/// \param negout If `false` then counting pretends the BDD does not have
/// negative-output pointers (complement edges).
//////////////////////////////////////////////////////////////////////////////
unsigned long Size(bool negout = true) const
{ return Cal_BddSize(this->_bddManager, this->_bdd, negout); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Fraction of valuations which make this BDD true.
///
/// \remark This fraction is independent of whatever set of variables `f` is
/// supposed to be a function of.
//////////////////////////////////////////////////////////////////////////////
double SatisfyingFraction() const
{ return Cal_BddSatisfyingFraction(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
// Operations
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief Duplicate of this BDD.
///
/// \see BDD::Not()
//////////////////////////////////////////////////////////////////////////////
BDD Identity() const
{ return BDD(this->_bddManager, Cal_BddIdentity(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Returns a BDD in positive form (regardless of this BDDs phase).
///
/// \see BDD::Not()
//////////////////////////////////////////////////////////////////////////////
BDD Regular() const
{
// Unlike other BDD operations of CAL, `Cal_BddGetRegular` does not
// increment the reference count of its output. Hence, we have to do so here
// to compensate for the `Free(...)` in `~BDD()`.
BDD res(this->_bddManager, Cal_BddGetRegular(this->_bddManager, this->_bdd));
BDD::UnFree(res._bddManager, res._bdd);
return res;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Complement of this BDD.
//////////////////////////////////////////////////////////////////////////////
BDD Not() const
{ return BDD(this->_bddManager, Cal_BddNot(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Not()
//////////////////////////////////////////////////////////////////////////////
BDD operator~ () const
{ return this->Not(); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Substitute a BDD variable by a function.
///
/// \param g Variable to be substituted.
///
/// \param h Function to substitute.
//////////////////////////////////////////////////////////////////////////////
BDD Compose(const BDD &g, const BDD &h) const
{ return BDD(this->_bddManager, Cal_BddCompose(this->_bddManager, this->_bdd, g._bdd, h._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Computes a BDD that implies conjunction of this and `g`.
///
/// \see BDD::Implies()
//////////////////////////////////////////////////////////////////////////////
BDD Intersects(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddIntersects(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Computes a BDD that implies conjunction of this and `g.Not()`.
///
/// \see BDD::Intersects()
//////////////////////////////////////////////////////////////////////////////
BDD Implies(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddImplies(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical If-Then-Else.
///
/// \details Returns the BDD for the logical operation `f ? g : h`, i.e.
/// `f&g | ~f&h`.
///
/// \see BDD::And(), BDD::Nand(), BDD::Or(), BDD::Nor(), BDD::Xor(),
/// BDD::Xnor()
//////////////////////////////////////////////////////////////////////////////
BDD ITE(const BDD &g, const BDD &h) const
{ return BDD(this->_bddManager, Cal_BddITE(this->_bddManager, this->_bdd, g._bdd, h._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical AND operation.
//////////////////////////////////////////////////////////////////////////////
BDD And(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddAnd(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::And()
//////////////////////////////////////////////////////////////////////////////
BDD operator& (const BDD &other) const
{ return this->And(other); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::And()
//////////////////////////////////////////////////////////////////////////////
BDD& operator&= (const BDD &other)
{ return (*this = (*this) & other); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical Negated AND operation.
//////////////////////////////////////////////////////////////////////////////
BDD Nand(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddNand(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical OR operation.
//////////////////////////////////////////////////////////////////////////////
BDD Or(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddOr(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Or()
//////////////////////////////////////////////////////////////////////////////
BDD operator| (const BDD &other) const
{ return this->Or(other); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Or()
//////////////////////////////////////////////////////////////////////////////
BDD& operator|= (const BDD &other)
{ return (*this = (*this) | other); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical Negated OR operation.
//////////////////////////////////////////////////////////////////////////////
BDD Nor(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddNor(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical XOR operation.
//////////////////////////////////////////////////////////////////////////////
BDD Xor(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddXor(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Xor()
//////////////////////////////////////////////////////////////////////////////
BDD operator^ (const BDD &other) const
{ return this->Xor(other); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Xor()
//////////////////////////////////////////////////////////////////////////////
BDD& operator^= (const BDD &other)
{ return (*this = (*this) ^ other); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical Negated XOR operation.
//////////////////////////////////////////////////////////////////////////////
BDD Xnor(const BDD &g) const
{ return BDD(this->_bddManager, Cal_BddXnor(this->_bddManager, this->_bdd, g._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Logical Difference operation.
//////////////////////////////////////////////////////////////////////////////
BDD Diff(const BDD &g) const
{ return *this & ~g; }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Diff()
//////////////////////////////////////////////////////////////////////////////
BDD operator- (const BDD &other) const
{ return this->Diff(other); }
//////////////////////////////////////////////////////////////////////////////
/// \see BDD::Diff()
//////////////////////////////////////////////////////////////////////////////
BDD& operator-= (const BDD &other)
{ return (*this = (*this) - other); }
//////////////////////////////////////////////////////////////////////////////
/// \brief A satisfying assignment of this BDD.
///
/// \details Returns a BDD which implies this, true for some valuation on
/// which f is true, and which has at most one node at each level.
//////////////////////////////////////////////////////////////////////////////
BDD Satisfy() const
{ return BDD(this->_bddManager, Cal_BddSatisfy(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Returns a special cube contained in this
///
/// \details The returned BDD which implies this, is true for some valuation
/// on which f is true, which has at most one node at each level, and which
/// has exactly one node corresponding to each variable which is associated
/// with something in the current variable association.
//////////////////////////////////////////////////////////////////////////////
BDD SatisfySupport() const
{ return BDD(this->_bddManager, Cal_BddSatisfySupport(this->_bddManager, this->_bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief The function obtained by swapping two variables.
///
/// \details Returns the BDD obtained by simultaneously substituting variable
/// `g` by variable `h` and variable `h` and variable `g` in this BDD.
///
/// \see BDD::Substitute()
//////////////////////////////////////////////////////////////////////////////
BDD SwapVars(const BDD &g, const BDD &h) const
{ return BDD(this->_bddManager, Cal_BddSwapVars(this->_bddManager, this->_bdd, g._bdd, h._bdd)); }
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// BDD Exists(...) const
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// BDD ForAll(...) const
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
// BDD RelProd(const BDD &g) const
//////////////////////////////////////////////////////////////////////////////
/// \brief Generalized cofactor of this with respect to `c`.
///
/// \details The constrain operator given by Coudert et al (ICCAD90) is used
/// to find the generalized cofactor.
///
/// \see BDD::Reduce()
//////////////////////////////////////////////////////////////////////////////
BDD Cofactor(const BDD &c) const
{ return BDD(this->_bddManager, Cal_BddCofactor(this->_bddManager, this->_bdd, c._bdd)); }
//////////////////////////////////////////////////////////////////////////////
/// \brief A function that agrees with this for all valuations which satisfy
/// `c`.
///
/// \details The result is usually smaller in terms of number of BDD nodes
/// than this. This operation is typically used in state space searches to
/// simplify the representation for the set of states wich will be expanded at
/// each step.
///
/// \see BDD::Cofactor()
//////////////////////////////////////////////////////////////////////////////
BDD Reduce(const BDD &c) const
{ return BDD(this->_bddManager, Cal_BddReduce(this->_bddManager, this->_bdd, c._bdd)); }
//////////////////////////////////////////////////////////////////////////////
// Debugging
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief Prints this BDD in the human readable form.
//////////////////////////////////////////////////////////////////////////////
void Print(FILE *fp = stdout) const
{
// TODO: Extend to use your own NamingFn and TerminalIdFn
// (and their environment).
Cal_BddPrintBdd(this->_bddManager, this->_bdd,
Cal_BddNamingFnNone, Cal_BddTerminalIdFnNone, NULL,
fp);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Prints the function implemented by this BDD.
//////////////////////////////////////////////////////////////////////////////
void FunctionPrint(std::string &name) const
{ Cal_BddFunctionPrint(this->_bddManager, this->_bdd, name.data()); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Obtain a vector with the number of nodes at each level in f.
///
/// \param negout If `false` then counting pretends the BDD does not have
/// negative-output pointers (complement edges).
//////////////////////////////////////////////////////////////////////////////
std::vector<long> Profile(bool negout = true) const
{
std::vector<long> results;
results.reserve(Cal_BddVars(this->_bddManager)+1);
Cal_BddProfile(this->_bddManager, this->_bdd, results.data(), negout);
return results;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Prints a BDD in the human readable form.
///
/// \param lineLength The maximum line length.
///
/// \param fp Pointer to the `FILE` to output to (default `stdout`).
//////////////////////////////////////////////////////////////////////////////
void PrintProfile(int lineLength = 79, FILE *fp = stdout) const
{
// TODO: Extend to use your own NamingFn (and its environment).
Cal_BddPrintProfile(this->_bddManager, this->_bdd,
Cal_BddNamingFnNone, NULL,
lineLength, fp);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief The number of subfunctions of this which may be obtained by
/// restricting variables with an index lower than *n*.
///
/// \details The nth entry of the function profile array is the number of
/// subfunctions of this which may be obtained by restricting the variables
/// whose index is less than n. An entry of zero indicates that this is
/// independent of the variable with the corresponding index.
//////////////////////////////////////////////////////////////////////////////
std::vector<long> FunctionProfile() const
{
std::vector<long> results;
results.reserve(Cal_BddVars(this->_bddManager)+1);
Cal_BddFunctionProfile(this->_bddManager, this->_bdd, results.data());
return results;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Similar to `BDD::PrintProfile()` but displays a function profile
/// for this.
///
/// \param lineLength The maximum line length.
///
/// \param fp Pointer to the `FILE` to output to (default `stdout`).
//////////////////////////////////////////////////////////////////////////////
void PrintFunctionProfile(int lineLength = 79, FILE *fp = stdout) const
{
// TODO: Extend to use your own NamingFn (and its environment).
Cal_BddPrintFunctionProfile(this->_bddManager, this->_bdd,
Cal_BddNamingFnNone, NULL,
lineLength, fp);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief The reference count of this BDD
///
/// \details The reference count is a value between 0 and 255. If a reference
/// count is 255 then incrementing or decrementing it has no effect. This is
/// to safe-guard constants and variables from being garbage collected.
//////////////////////////////////////////////////////////////////////////////
int RefCount() const
{
if (this->IsNull()) {
return 255; // <-- Simulate NULL has fixed MAX reference count.
}
CalBddNode_t *internal_node = CAL_BDD_POINTER(this->_bdd);
int res;
CalBddNodeGetRefCount(internal_node, res);
return res;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief String representation of this BDD node.
//////////////////////////////////////////////////////////////////////////////
std::string ToString() const
{
if (this->IsNull()) { return "NULL"; }
if (this->IsZero()) { return "(0)"; }
if (this->IsOne()) { return "(1)"; }
std::stringstream ss;
ss << "("
<< this->Id() << ", "
<< this->Then()._bdd << ", "
<< this->Else()._bdd
<< ")";
return ss.str();
}
//////////////////////////////////////////////////////////////////////////////
// Conversion back and from C null-terminated arrays.
protected:
/// \cond internal
//////////////////////////////////////////////////////////////////////////////
/// \brief Convert an iterator of `BDD`s (or `int`s) into a `std::vector` of
/// `Cal_Bdd`.
///
/// \detail By calling `.data()` on the result one exactly gets a list usable
/// in the C API.
//////////////////////////////////////////////////////////////////////////////
template<typename IT>
static std::vector<Cal_Bdd>
C_Bdd_vector(Cal_BddManager bddManager, IT begin, IT end)
{
// TODO: tidy up with template overloading
std::vector<Cal_Bdd> out;
out.reserve(std::distance(begin, end));
while (begin != end) {
const typename IT::value_type &x = *(begin++);
if constexpr (std::is_same_v<typename IT::value_type, BDD>) {
// TODO: assert same 'bddManager'...
BDD::UnFree(x._bddManager, x._bdd);
out.push_back(x._bdd);
} else if constexpr (std::is_same_v<typename IT::value_type, int>) {
out.push_back(Cal_BddManagerGetVarWithId(bddManager, x));
}
}
out.push_back(Cal_BddNull(bddManager));
return out;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Convert a null-terminated list of `Cal_Bdd`s from the C API to a
/// C++ vector of `BDD` classes.
//////////////////////////////////////////////////////////////////////////////
static std::vector<BDD>
From_C_Array(Cal_BddManager bddManager, Cal_Bdd * bddArray)
{
std::vector<BDD> res;
for (int i = 0; Cal_BddIsBddNull(bddManager, bddArray[i]) == 0; i++){
if (CalBddPreProcessing(bddManager, 1, bddArray[i]) == 0){
return std::vector<BDD>();
}
res.push_back(BDD(bddManager, bddArray[i]));
}
return res;
}
/// \endcond
//////////////////////////////////////////////////////////////////////////////
// Memory Management
private:
/// \cond internal
// TODO: move further down
//////////////////////////////////////////////////////////////////////////////
static inline bool
IsNull(Cal_BddManager bddManager, Cal_Bdd f)
{ return Cal_BddIsBddNull(bddManager, f); }
//////////////////////////////////////////////////////////////////////////////
static inline void
Free(Cal_BddManager bddManager, Cal_Bdd f)
{ if (!BDD::IsNull(bddManager, f)) Cal_BddFree(bddManager, f); }
//////////////////////////////////////////////////////////////////////////////
inline void
Free()
{ BDD::Free(this->_bddManager, this->_bdd); }
//////////////////////////////////////////////////////////////////////////////
template<typename IT>
static inline void
Free(IT begin, IT end)
{
static_assert(std::is_same_v<typename IT::value_type, BDD>,
"Must be called with iterator for BDD");
while (begin != end) (begin++)->Free();
}
//////////////////////////////////////////////////////////////////////////////
template<typename IT>
static inline void
Free(Cal_BddManager bddManager, IT begin, IT end)
{
static_assert(std::is_same_v<typename IT::value_type, Cal_Bdd>,
"Must be called with iterator for Cal_Bdd");
while (begin != end) BDD::Free(bddManager, *(begin++));
}
//////////////////////////////////////////////////////////////////////////////
static inline void
UnFree(Cal_BddManager bddManager, Cal_Bdd f)
{ if (!BDD::IsNull(bddManager, f)) Cal_BddUnFree(bddManager, f); }
//////////////////////////////////////////////////////////////////////////////
inline void
UnFree()
{ BDD::UnFree(this->_bddManager, this->_bdd); }
/// \endcond
};
////////////////////////////////////////////////////////////////////////////////
/// \brief Core Manager of everything BDDs, variables, and more.
///
/// \remark The `Cal` class is designed based on *non-sharing ownership*. You
/// can move the ownership to someone else, but you cannot create a copy of this
/// class to have multiple owners of the same BDD Manager. Please use C++
/// references instead.
////////////////////////////////////////////////////////////////////////////////
class Cal
{
friend BDD;
//////////////////////////////////////////////////////////////////////////////
// Types
//////////////////////////////////////////////////////////////////////////////
/// \brief Type of Cal's C++ BDD wrapping class.
//////////////////////////////////////////////////////////////////////////////
using Bdd_t = BDD;
//////////////////////////////////////////////////////////////////////////////
/// \brief Type of BDD identifiers, i.e. the variable name independent of the
/// current variable ordering.
//////////////////////////////////////////////////////////////////////////////
using Id_t = BDD::Id_t;
//////////////////////////////////////////////////////////////////////////////
/// \brief Type of BDD indices, i.e. their placement in the current variable
/// ordering.
//////////////////////////////////////////////////////////////////////////////
using Index_t = BDD::Index_t;
// ---------------------------------------------------------------------------
// Fields
// TODO -----------------------------------------------
// Multiple Cal objects for the same Cal_BddManager
// ----------------------------------------------- TODO
//
// Use a 'std::shared_ptr' for reference counting this 'Cal_BddManager'
// pointer. The 'Cal_BddManagerQuit' function is then the managed pointer's
// deleter.
//
// NOTE -------------------------------------------- NOTE
//
// If so, should all BDD objects also be part of this reference counting?
// Otherwise, if a BDD object survives for longer than the BDD manager, then
// it will result in Segmentation Faults. Arguably this is already an
// issue...
Cal_BddManager _bddManager;
//////////////////////////////////////////////////////////////////////////////
// Constructors
public:
//////////////////////////////////////////////////////////////////////////////
/// \brief Initialize a new BDD Manager.
//////////////////////////////////////////////////////////////////////////////
Cal()
: _bddManager(Cal_BddManagerInit())
{ }
//////////////////////////////////////////////////////////////////////////////
/// \brief Initialize a new BDD Manager with variables `[1, numVars]`.
//////////////////////////////////////////////////////////////////////////////
Cal(unsigned int numVars)
: Cal()
{
// Create variables
for (Id_t i = 0; i < numVars; ++i) {
this->CreateNewVarLast();
}
}
//////////////////////////////////////////////////////////////////////////////
// TODO: Copy constructor (requires reference counting)
Cal(const Cal &o) = delete;
//////////////////////////////////////////////////////////////////////////////
/// \brief Move ownership of C object.
//////////////////////////////////////////////////////////////////////////////
Cal(Cal &&o)
: _bddManager(o._bddManager)
{
o._bddManager = nullptr;
}
//////////////////////////////////////////////////////////////////////////////
/// \brief Clear memory of BDD Manager.
///
/// \details If this object owns a `Cal_BddManager` from the C API, then that
/// one is properly reset and freed.
//////////////////////////////////////////////////////////////////////////////
~Cal()
{
if (this->_bddManager)
Cal_BddManagerQuit(this->_bddManager);
}
//////////////////////////////////////////////////////////////////////////////
// Settings + Statistics
//////////////////////////////////////////////////////////////////////////////
/// \brief Sets appropriate fields of BDD Manager.
///
/// \param reorderingThreshold The number of nodes below which reordering will
/// NOT beinvoked.
///
/// \param maxForwardedNodes The maximum number of forwarded nodes which are
/// allowed (at that point the cleanup must be done)
///
/// \param repackAfterGCThreshold The fraction of the page utilized that
/// garbage collection has to achieve.
///
/// \param tableRepackThreshold The fraction of the page utilized below which
/// repacking has to be invoked.
//////////////////////////////////////////////////////////////////////////////
void SetParameters(long reorderingThreshold,
long maxForwardedNodes,
double repackAfterGCThreshold,
double tableRepackThreshold)
{
return Cal_BddManagerSetParameters(this->_bddManager,
reorderingThreshold,
maxForwardedNodes,
repackAfterGCThreshold,
tableRepackThreshold);
}
//////////////////////////////////////////////////////////////////////////////
/// \brief The number of BDD nodes.
///
/// \see Cal::TotalSize()
//////////////////////////////////////////////////////////////////////////////
unsigned long Nodes() const
{ return Cal_BddManagerGetNumNodes(this->_bddManager); }
//////////////////////////////////////////////////////////////////////////////
/// \brief The number of BDD variables.
//////////////////////////////////////////////////////////////////////////////
long Vars() const
{ return Cal_BddVars(this->_bddManager); }
//////////////////////////////////////////////////////////////////////////////
/// \brief `true` if the node limit has been exceeded, `false` otherwise.
///
/// \sideeffect The overflow flag is cleared.
///
/// \see Cal::NodeLimit()
//////////////////////////////////////////////////////////////////////////////
bool Overflow() const
{ return Cal_BddOverflow(this->_bddManager); }
//////////////////////////////////////////////////////////////////////////////
/// \brief The number of nodes in the Unique table.
///
/// \see Cal::Nodes()
//////////////////////////////////////////////////////////////////////////////
unsigned long TotalSize() const
{ return Cal_BddTotalSize(this->_bddManager); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Prints miscellaneous BDD statistics.
//////////////////////////////////////////////////////////////////////////////
void Stats(FILE* fp = stdout) const
{ Cal_BddStats(this->_bddManager, fp); }
// TODO: obtain error
//////////////////////////////////////////////////////////////////////////////
// Memory and Garbage Collection
//////////////////////////////////////////////////////////////////////////////
/// \brief Sets the node limit to `newLimit` and returns the previous limit.
///
/// \sideeffect Threshold for garbage collection may change.
///
/// \see Cal::SetGCLimit(), Cal::GC()
//////////////////////////////////////////////////////////////////////////////
long NodeLimit(long newLimit)
{ return Cal_BddNodeLimit(this->_bddManager, newLimit); }
//////////////////////////////////////////////////////////////////////////////
/// \brief Enable or Disable garbage collection.
//////////////////////////////////////////////////////////////////////////////
void SetGCMode(bool enableGC)
{ Cal_BddSetGCMode(this->_bddManager, enableGC); }