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
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066 #ifndef _SUPERPRED_H_DEC_2007
00067 #define _SUPERPRED_H_DEC_2007
00068
00069 #include "superclause.h"
00070
00071
00072
00073
00074
00075
00076 typedef hash_map<int, int, HashInt, EqualInt> IntToIntMap;
00077
00078
00079
00080 class ClauseCounter
00081 {
00082 public:
00083 ClauseCounter()
00084 {
00085 idToIndex_ = new IntToIntMap();
00086 clauseIds_ = new Array<int>;
00087 clauseCntsArr_ = new Array<Array<double>*>;
00088 dirty_ = true;
00089 }
00090
00091 ~ClauseCounter()
00092 {
00093 delete idToIndex_;
00094 delete clauseIds_;
00095 for (int i = 0; i < clauseCntsArr_->size(); i++)
00096 {
00097 delete (*clauseCntsArr_)[i];
00098 }
00099 delete clauseCntsArr_;
00100 }
00101
00102 int getNumClauses() { return clauseIds_->size();}
00103
00104 const Array<int>* getClauseIds() const { return clauseIds_;}
00105
00106 const Array<double>* getClauseCounts(int index) const
00107 {
00108 return (*clauseCntsArr_)[index];
00109 }
00110
00111 int getClauseId(int index) { return (*clauseIds_)[index];}
00112
00113 void incrementCount(int clauseId, int predId, int clausePredCnt, double cnt)
00114 {
00115 int index;
00116 int id = clauseId;
00117 Array<double> *clauseCnts;
00118 IntToIntMap::iterator itr;
00119 itr = idToIndex_->find(id);
00120
00121 if (itr == idToIndex_->end())
00122 {
00123 index = clauseIds_->size();
00124 (*idToIndex_)[id] = index;
00125 clauseIds_->append(id);
00126
00127 clauseCnts = new Array<double>();
00128 clauseCnts->growToSize(clausePredCnt, 0);
00129 clauseCntsArr_->append(clauseCnts);
00130 }
00131 else
00132 {
00133 index = (*idToIndex_)[id];
00134 }
00135
00136
00137 clauseCnts = (*clauseCntsArr_)[index];
00138 (*clauseCnts)[predId] += cnt;
00139 dirty_ = true;
00140 }
00141
00142 size_t getHashCode()
00143 {
00144 if (!dirty_)
00145 return hashCode_;
00146 IntToIntMap::iterator itr;
00147 Array<double> * clauseCnts;
00148 int code = 1;
00149 for (int index = 0; index < clauseIds_->size(); index++)
00150 {
00151 code = 31*code + (*clauseIds_)[index];
00152 clauseCnts = (*clauseCntsArr_)[index];
00153 for (int predId = 0; predId < clauseCnts->size(); predId++)
00154 {
00155 code = 31*code + (int)(*clauseCnts)[predId];
00156 }
00157 }
00158 dirty_ = false;
00159 hashCode_ = (size_t)code;
00160 return hashCode_;
00161 }
00162
00163 ostream & print(ostream & out)
00164 {
00165 for (int index = 0; index < clauseIds_->size(); index++)
00166 {
00167 out<<(*clauseIds_)[index]<<" : ";
00168 printArray(*(*clauseCntsArr_)[index], out);
00169 out<<" ** ";
00170 }
00171 return out;
00172 }
00173
00174 private:
00175 IntToIntMap *idToIndex_;
00176
00177
00178
00179
00180
00181
00182 Array<int> * clauseIds_;
00183 Array<Array<double>*> * clauseCntsArr_;
00184 bool dirty_;
00185 size_t hashCode_;
00186 };
00187
00188 class PredicateConstantsInfo
00189 {
00190 public:
00191 PredicateConstantsInfo(ClauseCounter * const & cc, int superPredId)
00192 {
00193 cc_ = cc;
00194 superPredId_ = superPredId;
00195 }
00196
00197 ~PredicateConstantsInfo()
00198 {
00199 delete cc_;
00200 }
00201
00202 int getSuperPredId() { return superPredId_;}
00203 ClauseCounter * getClauseCounter() {return cc_;}
00204
00205 private:
00206 int superPredId_;
00207 ClauseCounter *cc_;
00208 };
00209
00210
00211 class SuperPred;
00212 typedef hash_map<Array<int>*, SuperPred*, HashIntArray, EqualIntArray>
00213 IntArrayToSuperPredMap;
00214
00215
00216 extern void createSuperPreds(
00217 Array<Array<SuperClause*>*>* const & superClausesArr,
00218 Domain * const & domain);
00219
00220 class SuperPred
00221 {
00222 public:
00223 SuperPred(int & predId, ClauseCounter* const & clauseCounter,
00224 int parentSuperPredId)
00225 {
00226 constantTuples_ = new Array<Array<int>*>;
00227 predId_ = predId;
00228
00229
00230
00231 Array<SuperPred *> * superPreds = (*(SuperPred::superPredsArr_))[predId];
00232 superPredId_ = superPreds->size();
00233 superPreds->append(this);
00234 clauseCounter_ = clauseCounter;
00235 parentSuperPredId_ = parentSuperPredId;
00236 }
00237
00238
00239 ~SuperPred()
00240 {
00241 delete constantTuples_;
00242
00243
00244 delete clauseCounter_;
00245 }
00246
00247 int getPredId() {return predId_;}
00248
00249 Array<int> * getConstantTuple(int tindex) {return (*constantTuples_)[tindex];}
00250
00251 int getSuperPredId() {return superPredId_;}
00252
00253 int getParentSuperPredId() {return parentSuperPredId_;}
00254
00255 int getNumTuples() { return constantTuples_->size();}
00256
00257 const ClauseCounter * getClauseCounter() { return clauseCounter_;}
00258
00259 void addConstantTuple(Array<int> * constants, int predId)
00260 {
00261 constantTuples_->append(constants);
00262 IntArrayToSuperPredMap * constantsToSuperPred;
00263 constantsToSuperPred = (*(SuperPred::constantsToSuperPredArr_))[predId];
00264 (*constantsToSuperPred)[constants] = this;
00265 }
00266
00267
00268 static int getSuperPredCount(int predId)
00269 {
00270 return (*superPredsArr_)[predId]->size();
00271 }
00272
00273
00274 static Array<SuperPred*>* getSuperPreds(int predId)
00275 {
00276 return (*superPredsArr_)[predId];
00277 }
00278
00279 static void clear(int predCnt)
00280 {
00281
00282 if (isFirstAccessForStaticVars_)
00283 {
00284 superPredsArr_ = new Array<Array<SuperPred *>*>();
00285 constantsToSuperPredArr_ = new Array<IntArrayToSuperPredMap *>();
00286 for (int predId = 0; predId < predCnt; predId++)
00287 {
00288 constantsToSuperPredArr_->append(new IntArrayToSuperPredMap());
00289 superPredsArr_->append(new Array<SuperPred *>());
00290 }
00291 isFirstAccessForStaticVars_ = false;
00292 }
00293 else
00294 {
00295
00296 Array<SuperPred *> *superPreds;
00297 IntArrayToSuperPredMap * constantsToSuperPred;
00298 IntArrayToSuperPredMap::iterator iaToSpItr;
00299 for (int predId = 0; predId < predCnt; predId++)
00300 {
00301
00302 superPreds = (*superPredsArr_)[predId];
00303 for (int i = 0; i < superPreds->size(); i++)
00304 {
00305 delete (*superPreds)[i];
00306 }
00307
00308
00309 Array<Array<int> *> keysArr;
00310 constantsToSuperPred = (*constantsToSuperPredArr_)[predId];
00311 keysArr.clear();
00312 for (iaToSpItr = constantsToSuperPred->begin();
00313 iaToSpItr != constantsToSuperPred->end();
00314 iaToSpItr++)
00315 {
00316 keysArr.append(iaToSpItr->first);
00317 }
00318 for (int i = 0; i < keysArr.size(); i++)
00319 {
00320 delete keysArr[i];
00321 }
00322
00323
00324 superPreds->clear();
00325 constantsToSuperPred->clear();
00326 }
00327 }
00328 }
00329
00330 static int getSuperPredId(Array<int> * constants, int & predId)
00331 {
00332 IntArrayToSuperPredMap * constantsToSuperPred;
00333 IntArrayToSuperPredMap::iterator iaToSpItr;
00334 if (isFirstAccessForStaticVars_)
00335 return -1;
00336 assert(constantsToSuperPredArr_);
00337 constantsToSuperPred = (*constantsToSuperPredArr_)[predId];
00338 iaToSpItr = constantsToSuperPred->find(constants);
00339 SuperPred *superPred = iaToSpItr->second;
00340 return superPred->getSuperPredId();
00341 }
00342
00343 private:
00344 int predId_;
00345 int superPredId_;
00346 int parentSuperPredId_;
00347 Array<Array<int> *> *constantTuples_;
00348 ClauseCounter *clauseCounter_;
00349
00350 static bool isFirstAccessForStaticVars_;
00351 static Array<IntArrayToSuperPredMap*>* constantsToSuperPredArr_;
00352 static Array<Array<SuperPred *>*>* superPredsArr_;
00353 };
00354
00355
00356 class HashClauseCounter
00357 {
00358 public:
00359 size_t operator()(ClauseCounter *cc) const
00360 {
00361 return cc->getHashCode();
00362 }
00363 };
00364
00365
00366 class EqualClauseCounter
00367 {
00368 public:
00369 bool operator()(ClauseCounter* const & cc1, ClauseCounter* const & cc2) const
00370 {
00371 bool same;
00372 const int *items1, *items2;
00373 const Array<double> *cnts1, *cnts2;
00374
00375 int size1, size2;
00376
00377 size1 = cc1->getNumClauses();
00378 size2 = cc2->getNumClauses();
00379
00380 if (size1 != size2) return false;
00381
00382
00383 items1 = (cc1->getClauseIds())->getItems();
00384 items2 = (cc2->getClauseIds())->getItems();
00385
00386 same = memcmp(items1, items2, size1*sizeof(int))==0;
00387 if (!same)
00388 return same;
00389
00390 double epsilon = 1e-6;
00391
00392
00393 for (int index = 0; index < size1; index++)
00394 {
00395 cnts1 = cc1->getClauseCounts(index);
00396 cnts2 = cc2->getClauseCounts(index);
00397 for (int i = 0;i < cnts1->size(); i++)
00398 {
00399 if(((*cnts1)[i] + epsilon < (*cnts2)[i]) ||
00400 ((*cnts1)[i] - epsilon > (*cnts2)[i]))
00401 return false;
00402 }
00403 }
00404 return true;
00405 }
00406 };
00407
00408 typedef hash_map<Array<int>*, PredicateConstantsInfo*, HashIntArray,
00409 EqualIntArray> IntArrayToPredicateConstantsInfoMap;
00410 typedef hash_map<ClauseCounter*, SuperPred*, HashClauseCounter,
00411 EqualClauseCounter> ClauseCounterToSuperPredMap;
00412
00413 #endif