Skip to content

Commit 82531b8

Browse files
committed
ascii ordered construction added
1 parent b53e3fd commit 82531b8

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

ROBDD.cpp

+13-6
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ bool BDD_node::operator==(const BDD_node &rhs) const
44
{
55
return this->var == rhs.var && this->low == rhs.low && this->high == rhs.high;
66
}
7-
ROBDD::ROBDD()
7+
ROBDD::ROBDD(bool order_by_ascii)
88
{
99
this->zero = new BDD_node({0, nullptr, nullptr});
1010
this->one = new BDD_node({1, nullptr, nullptr});
1111
this->node_table[*(this->zero)] = this->zero;
1212
this->node_table[*(this->one)] = this->one;
13+
this->order_by_ascii = order_by_ascii;
1314
}
1415

1516
BDD_node *ROBDD::get_one() const
@@ -29,7 +30,11 @@ unsigned int ROBDD::get_ID(std::string var)
2930
{
3031
// new var
3132
if (this->var_to_ID.find(var) == this->var_to_ID.end())
32-
this->var_to_ID[var] = avail_ID++;
33+
{
34+
this->var_to_ID[var] = avail_ID;
35+
this->ID_to_var[avail_ID] = var;
36+
avail_ID++;
37+
}
3338
return this->var_to_ID[var];
3439
}
3540

@@ -94,6 +99,10 @@ BDD_node *ROBDD::apply(binary_op op, BDD_node *left, BDD_node *right)
9499

95100
BDD_node *ROBDD::_apply(binary_op op, BDD_node *left, BDD_node *right)
96101
{
102+
auto less = [&](BDD_node *lhs, BDD_node *rhs)
103+
{
104+
return this->order_by_ascii ? this->ID_to_var[lhs->var] < this->ID_to_var[rhs->var] : lhs->var < rhs->var;
105+
};
97106
auto found = this->apply_table[op].find(std::pair<BDD_node *, BDD_node *>(left, right));
98107
BDD_node *res;
99108
if (found != this->apply_table[op].end())
@@ -106,13 +115,13 @@ BDD_node *ROBDD::_apply(binary_op op, BDD_node *left, BDD_node *right)
106115
auto r = this->_apply(op, left->high, right->high);
107116
res = this->make_node(left->var, l, r);
108117
}
109-
else if ((left->var < right->var and left->var > 1) or right->var <= 1) // unfold left BDD tree
118+
else if ((less(left, right) and left->var > 1) or right->var <= 1) // unfold left BDD tree
110119
{
111120
auto l = this->_apply(op, left->low, right);
112121
auto r = this->_apply(op, left->high, right);
113122
res = this->make_node(left->var, l, r);
114123
}
115-
else if ((left->var > right->var and right->var > 1) or left->var <= 1) // unfold right BDD tree
124+
else // unfold right BDD tree
116125
{
117126
auto l = this->_apply(op, left, right->low);
118127
auto r = this->_apply(op, left, right->high);
@@ -165,8 +174,6 @@ void ROBDD::output(std::ofstream &out)
165174
this->printed[this->zero] = 0;
166175
this->printed[this->one] = 1;
167176
this->uuid = 2;
168-
for (auto it : this->var_to_ID)
169-
this->ID_to_var[it.second] = it.first;
170177
out << "digraph ROBDD {\n"
171178
<< "fontname=\"Helvetica,Arial,sans-serif\"\n"
172179
<< "node [fontname=\"Helvetica,Arial,sans-serif\"]\n"

ROBDD.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,12 @@ struct ROBDD
4040
}
4141
};
4242
BDD_node *root;
43-
43+
bool order_by_ascii;
4444
// Construction
4545
// for make_node()
4646
std::unordered_map<BDD_node, BDD_node *, BDD_node_hash> node_table;
4747
std::unordered_map<std::string, unsigned int> var_to_ID;
48+
std::unordered_map<unsigned int, std::string> ID_to_var;
4849
// for var -> ID assignment
4950
unsigned int avail_ID = 2; // 0 & 1 is reserved
5051
// for apply()
@@ -54,7 +55,6 @@ struct ROBDD
5455

5556
// Q-A
5657
// for ROBDD printing
57-
std::unordered_map<unsigned int, std::string> ID_to_var;
5858
std::unordered_map<BDD_node *, size_t> printed;
5959
void _output(BDD_node *, std::ofstream &);
6060
// for node -> ID assignment, ID only works as unique tag for graphviz
@@ -65,7 +65,7 @@ struct ROBDD
6565
BDD_node *one, *zero;
6666

6767
public:
68-
ROBDD();
68+
ROBDD(bool order_by_ascii = false);
6969
// get id for var
7070
unsigned int get_ID(std::string var);
7171
// add or return existed node

main.cpp

+12-3
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,14 @@ bool frontend_err = false;
1111
int main(int argc, char **argv)
1212
{
1313
// for command line args parsing
14-
bool all_sat_flag = 0, any_sat_flag = 0, sat_count_flag = 0, timing_flag = 0;
14+
bool all_sat_flag = 0, any_sat_flag = 0, sat_count_flag = 0, timing_flag = 0, order_flag = 0;
1515
std::chrono::steady_clock::time_point begin, end;
1616
char *output_filename = nullptr;
1717
char flag;
1818
opterr = 0;
1919

2020
// ref: https://www.gnu.org/software/libc/manual/html_node/Example-of-Getopt.html
21-
while ((flag = getopt(argc, argv, "Sscto:")) != -1)
21+
while ((flag = getopt(argc, argv, "SscAato:")) != -1)
2222
{
2323
switch (flag)
2424
{
@@ -31,6 +31,12 @@ int main(int argc, char **argv)
3131
case 'c':
3232
sat_count_flag = 1;
3333
break;
34+
case 'a':
35+
order_flag = 0;
36+
break;
37+
case 'A':
38+
order_flag = 1;
39+
break;
3440
case 't':
3541
timing_flag = 1;
3642
break;
@@ -47,6 +53,9 @@ int main(int argc, char **argv)
4753
std::cerr << "Usage: ROBDD [-Ssct] [-o filename]" << std::endl;
4854
std::cerr << " -s\t\t- Check if the proposition is All-SAT" << std::endl;
4955
std::cerr << " -S\t\t- Check if the proposition is Any-SAT" << std::endl;
56+
std::cerr << " -A\t\t- Construct ROBDD under fixed ASCII order," << std::endl
57+
<< "\t\t smallest variable at top" << std::endl;
58+
std::cerr << " -a (default)\t- Construct ROBDD under parsing order" << std::endl;
5059
std::cerr << " -c\t\t- Return SAT count for the proposition" << std::endl;
5160
std::cerr << " -t\t\t- Return ROBDD construction time cost" << std::endl;
5261
std::cerr << " -o filename\t- Print ROBDD to filename.svg" << std::endl;
@@ -57,7 +66,7 @@ int main(int argc, char **argv)
5766
}
5867
if (timing_flag)
5968
begin = std::chrono::steady_clock::now();
60-
T = new ROBDD();
69+
T = new ROBDD(order_flag);
6170
yyparse();
6271
if (timing_flag)
6372
end = std::chrono::steady_clock::now();

0 commit comments

Comments
 (0)