Fix tabbing
[satune.git] / src / AST / iterator.cc
1 #include "iterator.h"
2 #include "boolean.h"
3 #include "element.h"
4 #include "csolver.h"
5
6 BooleanIterator::BooleanIterator(CSolver *_solver) :
7         solverit(_solver->getConstraints()) {
8         updateNext();
9 }
10
11 BooleanIterator::~BooleanIterator() {
12         delete solverit;
13 }
14
15 bool BooleanIterator::hasNext() {
16         return boolean.getSize() != 0;
17 }
18
19 void BooleanIterator::updateNext() {
20         if (boolean.getSize() != 0) {
21                 boolean.pop();
22                 index.pop();
23         }
24
25         while (true) {
26                 if (boolean.getSize() == 0) {
27                         if (solverit->hasNext()) {
28                                 Boolean *b = solverit->next().getBoolean();
29                                 if (discovered.add(b)) {
30                                         boolean.push(b);
31                                         index.push(0);
32                                 } else
33                                         continue;
34                         } else
35                                 return;
36                 }
37                 Boolean *topboolean = boolean.last();
38                 uint topindex = index.last();
39                 switch (topboolean->type) {
40                 case ORDERCONST:
41                 case BOOLEANVAR:
42                 case PREDICATEOP:
43                 case BOOLCONST:
44                         return;
45                 case LOGICOP: {
46                         BooleanLogic *logicop = (BooleanLogic *) topboolean;
47                         uint size = logicop->inputs.getSize();
48                         if (topindex == size)
49                                 return;
50                         index.pop();
51                         index.push(topindex + 1);
52                         Boolean *newchild = logicop->inputs.get(topindex).getBoolean();
53                         if (discovered.add(newchild)) {
54                                 boolean.push(newchild);
55                                 index.push(0);
56                         }
57                         break;
58                 }
59                 default:
60                         ASSERT(0);
61                 }
62         }
63 }
64
65 Boolean *BooleanIterator::next() {
66         Boolean *b = boolean.last();
67         updateNext();
68         return b;
69 }
70
71 ElementIterator::ElementIterator(CSolver *_solver) :
72         bit(_solver),
73         base(NULL),
74         baseindex(0) {
75         updateNext();
76 }
77
78 ElementIterator::~ElementIterator() {
79 }
80
81 bool ElementIterator::hasNext() {
82         return element.getSize() != 0;
83 }
84
85 void ElementIterator::updateNext() {
86         if (element.getSize() != 0) {
87                 element.pop();
88                 index.pop();
89         }
90
91         while (true) {
92                 if (element.getSize() == 0) {
93                         if (base != NULL) {
94                                 if (baseindex == base->inputs.getSize()) {
95                                         base = NULL;
96                                         continue;
97                                 } else {
98                                         Element *e = base->inputs.get(baseindex);
99                                         baseindex++;
100                                         if (discovered.add(e)) {
101                                                 element.push(e);
102                                                 index.push(0);
103                                         } else
104                                                 continue;
105                                 }
106                         } else {
107                                 if (bit.hasNext()) {
108                                         Boolean *b = bit.next();
109                                         if (b->type == PREDICATEOP) {
110                                                 base = (BooleanPredicate *)b;
111                                                 baseindex = 0;
112                                         }
113                                         continue;
114                                 } else
115                                         return;
116                         }
117                 }
118                 Element *topelement = element.last();
119                 uint topindex = index.last();
120                 switch (topelement->type) {
121                 case ELEMSET:
122                 case ELEMCONST:
123                         return;
124                 case ELEMFUNCRETURN: {
125                         ElementFunction *func = (ElementFunction *) topelement;
126                         uint size = func->inputs.getSize();
127                         if (topindex == size)
128                                 return;
129                         index.pop();
130                         index.push(topindex + 1);
131                         Element *newchild = func->inputs.get(topindex);
132                         if (discovered.add(newchild)) {
133                                 element.push(newchild);
134                                 index.push(0);
135                         }
136                         break;
137                 }
138                 default:
139                         ASSERT(0);
140                 }
141         }
142 }
143
144 Element *ElementIterator::next() {
145         Element *e = element.last();
146         updateNext();
147         return e;
148 }