ac81e96ad8fe8b91b1f86a1b4628e932725aa64c
[satune.git] / src / AST / iterator.cc
1 #include "iterator.h"
2 #include "boolean.h"
3 #include "csolver.h"
4
5 BooleanIterator::BooleanIterator(CSolver * _solver) :
6         solverit(_solver->getConstraints()) {
7         updateNext();
8 }
9
10 BooleanIterator::~BooleanIterator() {
11         delete solverit;
12 }
13
14 bool BooleanIterator::hasNext() {
15         return boolean.getSize() != 0;
16 }
17
18 void BooleanIterator::updateNext() {
19         if (boolean.getSize() != 0) {
20                 boolean.pop();
21                 index.pop();
22         }
23         
24         while(true) {
25                 if (boolean.getSize() == 0) {
26                         if (solverit->hasNext()) {
27                                 Boolean *b=solverit->next().getBoolean();
28                                 if (discovered.add(b)) {
29                                         boolean.push(b);
30                                         index.push(0);
31                                 } else
32                                         continue;
33                         } else
34                                 return;
35                 }
36                 Boolean *topboolean=boolean.last();
37                 uint topindex=index.last();
38                 switch(topboolean->type) {
39                 case ORDERCONST:
40                 case BOOLEANVAR:
41                 case PREDICATEOP:
42                 case BOOLCONST:
43                         return;
44                 case LOGICOP: {
45                         BooleanLogic * logicop=(BooleanLogic*) topboolean;
46                         uint size=logicop->inputs.getSize();
47                         if (topindex == size)
48                                 return;
49                         index.pop();
50                         index.push(topindex+1);
51                         Boolean *newchild=logicop->inputs.get(topindex).getBoolean();
52                         if (discovered.add(newchild)) {
53                                 boolean.push(logicop->inputs.get(topindex).getBoolean());
54                                 index.push(0);
55                         }
56                         break;
57                 }
58                 default:
59                         ASSERT(0);
60                 }
61         }
62 }
63
64 Boolean * BooleanIterator::next() {
65         Boolean * b = boolean.last();
66         updateNext();
67         return b;
68 }