1 //////////////////////////////////////////////////////////////////////////////
4 // ADLib, Prop and their related set of tools and documentation are in the
5 // public domain. The author(s) of this software reserve no copyrights on
6 // the source code and any code generated using the tools. You are encouraged
7 // to use ADLib and Prop to develop software, in both academic and commercial
8 // settings, and are free to incorporate any part of ADLib and Prop into
11 // Although you are under no obligation to do so, we strongly recommend that
12 // you give away all software developed using our tools.
14 // We also ask that credit be given to us when ADLib and/or Prop are used in
15 // your programs, and that this notice be preserved intact in all the source
18 // This software is still under development and we welcome any suggestions
19 // and help from the users.
23 //////////////////////////////////////////////////////////////////////////////
25 #ifndef binary_decision_diagrams_h
26 #define binary_decision_diagrams_h
28 //////////////////////////////////////////////////////////////////////////////
29 // Binary Decision Diagrams (BDD)\cite{bool-diag}
30 // for computing boolean functions. BDDs are maximally reduced
31 // binary dag structure used to represent boolean functions. A
32 // BDD is characterized as follows:
33 // (a) A finite linear ordered set of boolean variable names $x_i$ are
34 // used to label each non-leaf node such that given two nodes
35 // $a$ and $b$, if $a$ is a descendent of $b$ then, $var(a) > var(b)$.
36 // (b) A leaf node is labeled either 0 or 1.
37 // (c) No node points to the same dag on both its branches.
38 // (d) No two subdags are isomorphic. That is, all isomorphic
39 // subdags are merged.
41 // Class |BDDSet| represents a set of boolean functions, each one
42 // is of type |BDDSet::BDD|.
43 //////////////////////////////////////////////////////////////////////////////
46 #include <AD/generic/generic.h> // Generic definitions
48 //////////////////////////////////////////////////////////////////////////////
49 // Class BDDSet actually implements a collection of BDD's. All BDD's
50 // must be allocated from the same system and operations are assumed to
51 // be performed on BDD's in the same system.
53 // An BDDSet object manages the storage used during the BDDs computation.
54 // It performs internal garbage collection and compaction transparently
55 // from the client program.
56 //////////////////////////////////////////////////////////////////////////////
60 BDDSet(const BDDSet
&); // no copy constructor
61 void operator = (const BDDSet
&); // no assignment
65 ///////////////////////////////////////////////////////////////////////////
67 ///////////////////////////////////////////////////////////////////////////
68 enum Op
{ // Binary boolean operations
75 Xnor
, // exclusive nor
78 typedef int Var
; // Function variable names
79 typedef long BDD
; // a BDD
83 { Var var
; // name of variable
84 BDD branches
[2]; // the two branches
86 inline BDDNode(Var v
, BDD l
, BDD r
)
87 : var(v
) { branches
[0] = l
; branches
[1] = r
; }
93 ///////////////////////////////////////////////////////////////////////////
94 // Given an BDD, we'll use 0 to denote the fixed encoding for the zero
95 // function and 1 to denote the one function. This means that
96 // it is not possible to negate a function in constant time (but in
97 // linear time). However, with less sharing it is easier to
98 // perform garbage collection.
101 ///////////////////////////////////////////////////////////////////////////
103 ///////////////////////////////////////////////////////////////////////////
104 // Internal definitions.
105 ///////////////////////////////////////////////////////////////////////////
106 BDDNode
* bdds
; // array of bdds
107 BDDNode
* bdd_next
; // next available node
108 BDDNode
* bdd_limit
; // limit
109 BDDNode
* bdd_highwater
; // highwater
110 BDDNode
* bdd_last
; //
111 BDD
* bdd_roots
; // roots
112 int bdd_count
; // number of bdds used
113 int bdd_capacity
; // capacity of the bdds array.
114 class BDDTable
* table
; // internal hash table.
115 class BDDMemo
* memo
; // for memorization during computation.
119 ////////////////////////////////////////////////////////////////////////
120 // Constructors and destructor.
121 ////////////////////////////////////////////////////////////////////////
122 BDDSet(size_t default_nodes
= 1024);
125 ////////////////////////////////////////////////////////////////////////
127 ////////////////////////////////////////////////////////////////////////
128 virtual void clear();
130 ////////////////////////////////////////////////////////////////////////
132 ////////////////////////////////////////////////////////////////////////
133 inline int size () const { return bdd_count
; }
134 inline int capacity () const { return bdd_capacity
; }
135 inline Var
var_of (BDD f
) const { return bdds
[f
].var
; }
136 inline const BDD
* operator () (BDD f
) const { return bdds
[f
].branches
; }
138 ////////////////////////////////////////////////////////////////////////
140 ////////////////////////////////////////////////////////////////////////
141 inline BDD
operator [] (int i
) const { return bdd_roots
[i
]; }
142 void kill (BDD f
); // delete f from the set
144 ////////////////////////////////////////////////////////////////////////
146 ////////////////////////////////////////////////////////////////////////
147 virtual BDD
apply (Op op
, BDD f
, BDD g
);
148 virtual BDD
restrict (BDD f
, Var var
, BDD value
);
149 virtual BDD
negate (BDD f
);
151 ///////////////////////////////////////////////////////////////////////////
152 // Garbage collection
153 ///////////////////////////////////////////////////////////////////////////
154 virtual void collect(); // invoke garbage collection
157 ///////////////////////////////////////////////////////////////////////////
159 ///////////////////////////////////////////////////////////////////////////
160 BDD
hash_cons (Var v
, BDD f
, BDD g
); // create a node
161 BDD
do_apply (Op op
, BDD f
, BDD g
);
162 BDD
do_restrict (BDD f
, Var var
, BDD value
);
163 BDD
do_negate (BDD f
);
164 virtual void do_collect (); // garbage collection
165 void add_root (BDD f
);