00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030 #include "assumptions_value.h"
00031
00032
00033 using namespace std;
00034 using namespace CVCL;
00035
00036
00037 static bool TheoremEq(const Theorem& t1, const Theorem& t2)
00038 {
00039 DebugAssert(!t1.isNull() && !t2.isNull(),
00040 "AssumptionsValue() Null Theorem passed to constructor");
00041 return t1 == t2;
00042 }
00043
00044
00045 AssumptionsValue::AssumptionsValue(const std::vector<Theorem>& v)
00046 : d_refcount(0), d_const(false)
00047 {
00048 TRACE_MSG("assumptions", "AssumptionsValue {");
00049 d_vector.reserve(v.size());
00050
00051 const vector<Theorem>::const_iterator iend = v.end();
00052 vector<Theorem>::const_iterator i = v.begin();
00053
00054
00055
00056
00057
00058 for (;i != iend; ++i) {
00059 if (
00060 (i->isAssump() || !i->getAssumptions().empty())) {
00061 d_vector.push_back(*i);
00062
00063 }
00064 }
00065
00066 if (d_vector.size() <= 1) return;
00067 sort(d_vector.begin(), d_vector.end());
00068 vector<Theorem>::iterator newend =
00069 unique(d_vector.begin(), d_vector.end(), TheoremEq);
00070
00071 d_vector.resize(newend - d_vector.begin());
00072
00073 IF_DEBUG(
00074 if (CVCL::debugger.trace("assumptions")) {
00075 cout << *this << endl;
00076 }
00077 )
00078
00079 TRACE_MSG("assumptions", "}");
00080 }
00081
00082
00083 AssumptionsValue::AssumptionsValue(const Theorem& t1, const Theorem& t2)
00084 : d_refcount(0), d_const(false)
00085 {
00086 TRACE_MSG("assumptions", "AssumptionsValue2 {");
00087
00088
00089
00090
00091
00092
00093
00094 if (t1.isAssump() || !t1.getAssumptions().empty()) {
00095 if (t2.isAssump() || !t2.getAssumptions().empty()) {
00096 switch(compare(t1, t2)) {
00097 case -1:
00098 d_vector.push_back(t1);
00099 d_vector.push_back(t2);
00100 break;
00101 case 0:
00102 d_vector.push_back(t1);
00103 break;
00104 case 1:
00105 d_vector.push_back(t2);
00106 d_vector.push_back(t1);
00107 break;
00108 }
00109 } else d_vector.push_back(t1);
00110 } else if (t2.isAssump() || !t2.getAssumptions().empty()) {
00111 d_vector.push_back(t2);
00112 }
00113
00114 TRACE_MSG("assumptions", "}");
00115
00116 }
00117
00118
00119 void AssumptionsValue::mergeVectors(const vector<Theorem>& v1,
00120 const vector<Theorem>& v2,
00121 vector<Theorem>& v)
00122 {
00123 v.reserve(v1.size() + v2.size());
00124
00125 vector<Theorem>::const_iterator i = v1.begin();
00126 vector<Theorem>::const_iterator j = v2.begin();
00127 const vector<Theorem>::const_iterator v1end = v1.end();
00128 const vector<Theorem>::const_iterator v2end = v2.end();
00129
00130
00131 while (i != v1end && j != v2end) {
00132 switch(compare(*i, *j)) {
00133 case 0:
00134
00135 ++j;
00136 case -1:
00137 v.push_back(*i);
00138 ++i;
00139 break;
00140 default:
00141 v.push_back(*j);
00142 ++j;
00143 };
00144 }
00145
00146 for(; i != v1end; ++i) v.push_back(*i);
00147 for(; j != v2end; ++j) v.push_back(*j);
00148 }
00149
00150
00151 void AssumptionsValue::add(const Theorem& t)
00152 {
00153 TRACE_MSG("assumptions", "add");
00154 DebugAssert(!d_const,
00155 "AssumptionsValue::add() called on constant assumptions");
00156 DebugAssert(!t.isNull(),
00157 "AssumptionsValue::add() Null Assumption!");
00158
00159 if (!t.isAssump() && t.getAssumptions().empty()) return;
00160
00161 TRACE_MSG("assumptions", "AssumptionsValue::add(" + t.toString() + ")");
00162
00163
00164
00165
00166 vector<Theorem>::iterator iter = d_vector.begin();
00167 const vector<Theorem>::const_iterator vend = d_vector.end();
00168 for (; iter != vend; ++iter) {
00169 int c(compare(t, *iter));
00170 if(c == 0) return;
00171 if(c < 0) break;
00172 }
00173 d_vector.insert(iter, t);
00174 }
00175
00176
00177 void AssumptionsValue::add(const AssumptionsValue& a) {
00178 TRACE_MSG("assumptions", "add2");
00179 DebugAssert(!d_const,
00180 "AssumptionsValue::add() called on constant assumptions");
00181
00182 vector<Theorem>::const_iterator iend = a.d_vector.end();
00183 for (vector<Theorem>::const_iterator i = a.d_vector.begin();
00184 i != iend; ++i) {
00185 d_vector.push_back(*i);
00186 }
00187
00188 vector<Theorem> v;
00189 mergeVectors(d_vector, a.d_vector, v);
00190 d_vector.swap(v);
00191 }
00192
00193
00194 const Theorem& AssumptionsValue::find(const Expr& e) const {
00195 static Theorem null;
00196
00197 int lo = 0;
00198 int hi = d_vector.size() - 1;
00199 int loc;
00200 while (lo <= hi) {
00201 loc = (lo + hi) / 2;
00202
00203 switch (compare(d_vector[loc], e)) {
00204 case 0: return d_vector[loc];
00205 case -1:
00206 lo = loc + 1;
00207 break;
00208 default:
00209 hi = loc - 1;
00210 };
00211 }
00212
00213 return null;
00214 }
00215
00216
00217
00218 string AssumptionsValue::toString() const {
00219 ostringstream ss;
00220 ss << (*this);
00221 return ss.str();
00222 }
00223
00224
00225
00226
00227
00228
00229
00230 namespace CVCL {
00231
00232
00233 ostream &operator<<(ostream& os, const AssumptionsValue &assump) {
00234 vector<Theorem>::const_iterator i = assump.d_vector.begin();
00235 const vector<Theorem>::const_iterator iend = assump.d_vector.end();
00236 if(i != iend) { os << i->getExpr(); i++; }
00237 for(; i != iend; i++) os << ",\n " << i->getExpr();
00238 return os;
00239 }
00240
00241
00242 }