00001
00002
00003
00004
00005
00006
00007
00008
00009
00010 #ifdef _MSC_VER
00011 #pragma warning( disable : 4786 )
00012 #endif
00013
00014 #include <cassert>
00015 #include "BFIGAssignment.h"
00016
00017
00018 #ifdef _MSC_VER
00019 #include "ListManipulator.h"
00020 using Mers::Generators::BFIG::ListManipulator;
00021 template ListManipulator<BFIGAssignment const *>;
00022 #else
00023 typedef bool (*assignment_comp_func)(BFIGAssignment const *, BFIGAssignment const *);
00024 #endif
00025
00026 #include "BFIGConflict.h"
00027
00028 using namespace std;
00029
00030 static bool
00031 assignment_cost_less(BFIGAssignment const * a1, BFIGAssignment const * a2)
00032 {
00033 return ( a1->getCost() < a2->getCost() );
00034 }
00035
00036 static bool
00037 assignment_less(BFIGAssignment const * a1, BFIGAssignment const * a2)
00038 {
00039 return ( (*a1) < (*a2) );
00040 }
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054 BFIGConflict::BFIGConflict(list<BFIGAssignment const *> inconsistentAssignments_pList)
00055 : primeImplicants()
00056 {
00057 inconsistentAssignments = inconsistentAssignments_pList;
00058
00059 #ifdef _WIN32
00060 #ifndef __CYGWIN__
00061 {
00062 ListManipulator<BFIGAssignment const *> lm;
00063 lm.sort(inconsistentAssignments, assignment_less);
00064 }
00065 #else
00066 inconsistentAssignments.sort<assignment_comp_func>(assignment_less);
00067 #endif
00068 #else
00069 inconsistentAssignments.sort<assignment_comp_func>(assignment_less);
00070 #endif
00071
00072 createPrimeImplicants();
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085
00086 list<BFIGAssignment const *>
00087 BFIGConflict::getInconsistentAssignments() const
00088 {
00089 return inconsistentAssignments;
00090 }
00091
00092 list<BFIGAssignment const *>
00093 BFIGConflict::getPrimeImplicants() const
00094 {
00095 return primeImplicants;
00096 }
00097
00098
00099 int
00100 BFIGConflict::getLength() const
00101 {
00102 return inconsistentAssignments.size();
00103 }
00104
00105 bool
00106 BFIGConflict::isEmpty() const
00107 {
00108 return (getLength() == 0);
00109 }
00110
00111
00112 void
00113 BFIGConflict::createPrimeImplicants()
00114
00115
00116
00117
00118
00119
00120 {
00121 list<BFIGAssignment const *>::const_iterator inconsistentAssignmentsIter;
00122 list<BFIGAssignment const *> tempAssignment_pList;
00123 list<BFIGAssignment const *>::iterator tempAssignmentIter;
00124
00125 for (inconsistentAssignmentsIter = inconsistentAssignments.begin();
00126 inconsistentAssignmentsIter != inconsistentAssignments.end();
00127 inconsistentAssignmentsIter++)
00128 {
00129 tempAssignment_pList = (*inconsistentAssignmentsIter)->alternateAssignments();
00130
00131 for (tempAssignmentIter = tempAssignment_pList.begin();
00132 tempAssignmentIter != tempAssignment_pList.end();
00133 tempAssignmentIter++)
00134 {
00135 primeImplicants.push_back(*tempAssignmentIter);
00136 }
00137 }
00138
00139 #ifdef _WIN32
00140 #ifndef __CYGWIN__
00141 ListManipulator<BFIGAssignment const *> lm;
00142 lm.sort(primeImplicants, assignment_cost_less);
00143 #else
00144 primeImplicants.sort<assignment_comp_func>(assignment_cost_less);
00145 #endif
00146 #else
00147 primeImplicants.sort<assignment_comp_func>(assignment_cost_less);
00148 #endif
00149 }
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210 bool
00211 BFIGConflict::supportedBy(Implicant const * implicant) const
00212 {
00213 list<BFIGAssignment const *> assignments = implicant->getAssignments();
00214 list<BFIGAssignment const *>::const_iterator assignmentsIter;
00215 for (assignmentsIter = assignments.begin(); assignmentsIter != assignments.end();
00216 assignmentsIter++)
00217 {
00218 list<BFIGAssignment const *>::const_iterator conflictAssignmentsIter;
00219 for (conflictAssignmentsIter = inconsistentAssignments.begin();
00220 conflictAssignmentsIter != inconsistentAssignments.end();
00221 conflictAssignmentsIter++)
00222 {
00223 if (! (*assignmentsIter)->compatible(*conflictAssignmentsIter) )
00224 {
00225 return true;
00226 }
00227 }
00228 }
00229 return false;
00230 }
00231
00232
00233 bool
00234 BFIGConflict::supportedBy(BFIGAssignment const * a) const
00235 {
00236 list<BFIGAssignment const *>::const_iterator conflictAssignmentsIter;
00237 for (conflictAssignmentsIter = inconsistentAssignments.begin();
00238 conflictAssignmentsIter != inconsistentAssignments.end();
00239 conflictAssignmentsIter++)
00240 {
00241 if (! a->compatible(*conflictAssignmentsIter) )
00242 {
00243 return true;
00244 }
00245 }
00246 return false;
00247 }
00248
00249
00250 bool
00251 BFIGConflict::operator==(BFIGConflict const & c) const
00252 {
00253
00254 if (getLength() != c.getLength())
00255 return false;
00256
00257
00258 list<BFIGAssignment const *>::const_iterator c1Iter;
00259 list<BFIGAssignment const *>::const_iterator c2Iter;
00260 for (c1Iter = inconsistentAssignments.begin(), c2Iter = c.inconsistentAssignments.begin();
00261 c1Iter != inconsistentAssignments.end(); c1Iter++, c2Iter++)
00262 {
00263 if (!( (*c1Iter) == (*c2Iter) ))
00264 return false;
00265 }
00266
00267 return true;
00268 }
00269
00270
00271 bool
00272 BFIGConflict::operator<(BFIGConflict const & c) const
00273 {
00274 if (getLength() < c.getLength())
00275 return true;
00276 if (getLength() > c.getLength())
00277 return false;
00278
00279 list<BFIGAssignment const *>::const_iterator c1Iter;
00280 list<BFIGAssignment const *>::const_iterator c2Iter;
00281 for (c1Iter = inconsistentAssignments.begin(), c2Iter = c.inconsistentAssignments.begin();
00282 c1Iter != inconsistentAssignments.end(); c1Iter++, c2Iter++)
00283 {
00284 if ((*c1Iter) == (*c2Iter))
00285 continue;
00286 if ((*(*c1Iter)) < (*(*c2Iter)))
00287 return true;
00288 return false;
00289 }
00290
00291 return false;
00292 }
00293
00294
00295 String
00296 BFIGConflict::toString() const
00297 {
00298 list<BFIGAssignment const *>::const_iterator conflictIter;
00299 list<BFIGAssignment const *> conflictInconsistent = inconsistentAssignments;
00300
00301 String res = "";
00302 res += " ( " ;
00303 for (conflictIter = conflictInconsistent.begin();
00304 conflictIter != conflictInconsistent.end(); conflictIter++)
00305 {
00306 res += (*conflictIter)->toString();
00307 }
00308 res += " )" ;
00309
00310 return res;
00311 }
00312
00313 ostream&
00314 operator<<(ostream& os, BFIGConflict const & c)
00315 {
00316 list<BFIGAssignment const *>::const_iterator conflictIter;
00317 list<BFIGAssignment const *> conflictInconsistent = c.inconsistentAssignments;
00318
00319 os << "(" ;
00320 for (conflictIter = conflictInconsistent.begin();
00321 conflictIter != conflictInconsistent.end(); conflictIter++)
00322 {
00323 os << *(*conflictIter) << " ";
00324 }
00325 os << ")" ;
00326 return os;
00327 }
00328
00329
00330
00331
00332
00333