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 GROUNDCLAUSE_H_JUN_26_2005
00067 #define GROUNDCLAUSE_H_JUN_26_2005
00068
00069 #include <cfloat>
00070 #include <ext/hash_set>
00071 using namespace __gnu_cxx;
00072 #include "hasharray.h"
00073 #include "array.h"
00074 #include "hash.h"
00075 #include <map>
00076
00077 using namespace std;
00078
00079
00080 typedef map<int,int> IntPair;
00081 typedef IntPair::iterator IntPairItr;
00083
00084
00085 const double HARD_GROUNDCLAUSE_WT = DBL_MAX;
00086
00087
00088 class Domain;
00089 class Clause;
00090 class GroundPredicate;
00091 class HashGroundPredicate;
00092 class EqualGroundPredicate;
00093
00094
00095 typedef HashArray<GroundPredicate*, HashGroundPredicate, EqualGroundPredicate>
00096 GroundPredicateHashArray;
00097
00101 class GroundClause
00102 {
00103 public:
00104
00105 GroundClause(const Clause* const & c,
00106 GroundPredicateHashArray* const & gndPredHashArray);
00107
00108 ~GroundClause()
00109 {
00110 if (gndPredIndexes_) delete gndPredIndexes_;
00111 if (parentWtPtrs_) delete parentWtPtrs_;
00112 if (foClauseFrequencies_) delete foClauseFrequencies_;
00113 }
00114
00115 void deleteParentWtPtrs()
00116 {
00117 if (parentWtPtrs_)
00118 {
00119 for (int i = 0; i < parentWtPtrs_->size(); i++)
00120 delete (*parentWtPtrs_)[i];
00121 delete parentWtPtrs_;
00122 }
00123 parentWtPtrs_ = NULL;
00124 }
00125
00126 void deleteFoClauseFrequencies()
00127 {
00128 if (foClauseFrequencies_) delete foClauseFrequencies_;
00129 foClauseFrequencies_ = NULL;
00130 }
00131
00132 void addWt(const double& wt)
00133 { if (wt_ == HARD_GROUNDCLAUSE_WT) return; wt_ += wt; }
00134
00135 void setWt(const double& wt)
00136 { if (wt_ == HARD_GROUNDCLAUSE_WT) return; wt_ = wt; }
00137
00138 double getWt() const { return wt_; }
00139
00140 void setWtToHardWt() { wt_ = HARD_GROUNDCLAUSE_WT; }
00141 bool isHardClause() const { return (wt_ == HARD_GROUNDCLAUSE_WT); }
00142
00143 int getNumGroundPredicates() const { return gndPredIndexes_->size(); }
00144
00145 const GroundPredicate* getGroundPredicate(const int& i,
00146 GroundPredicateHashArray* const & gndPredHashArray) const
00147 {
00148 return (*gndPredHashArray)[abs((*gndPredIndexes_)[i]) - 1];
00149 }
00150
00157 void appendToGndPreds(GroundPredicateHashArray* const & gndPredHashArray);
00158
00159 bool getGroundPredicateSense(const int& i) const
00160 { return ((*gndPredIndexes_)[i] > 0); }
00161
00162 void setGroundPredicateIndex(const int& i, const int& gndPredIdx)
00163 { (*gndPredIndexes_)[i] = gndPredIdx; }
00164
00165 int getGroundPredicateIndex(const int& i) const
00166 { return (*gndPredIndexes_)[i]; }
00167
00168 const Array<int>* getGndPredIndexes() const
00169 {
00170 return gndPredIndexes_;
00171 }
00172
00173 void appendParentWtPtr(const double* const & wtPtr)
00174 {
00175 if (parentWtPtrs_ == NULL)
00176 parentWtPtrs_ = new Array<const double*>;
00177 parentWtPtrs_->append(wtPtr);
00178 }
00179
00180 void setWtToSumOfParentWts()
00181 {
00182 assert(parentWtPtrs_);
00183 wt_ = 0;
00184 for (int i = 0; i < parentWtPtrs_->size(); i++)
00185 wt_ += *((*parentWtPtrs_)[i]);
00186 }
00187
00188 IntPair *getClauseFrequencies()
00189 {
00190 return foClauseFrequencies_;
00191 }
00192
00193 int getClauseFrequency(int clauseno)
00194 {
00195 if (!foClauseFrequencies_) return 0;
00196 IntPairItr itr = foClauseFrequencies_->find(clauseno);
00197 if (itr == foClauseFrequencies_->end())
00198 return 0;
00199 else
00200 return itr->second;
00201 }
00202
00203 void incrementClauseFrequency(int clauseno, int increment)
00204 {
00205 if (!foClauseFrequencies_)
00206 foClauseFrequencies_ = new IntPair;
00207 IntPairItr itr = foClauseFrequencies_->find(clauseno);
00208 if (itr == foClauseFrequencies_->end())
00209 foClauseFrequencies_->insert(IntPair::value_type(clauseno, increment));
00210 else
00211 itr->second += increment;
00212 }
00213
00220 void removeGndPred(const int& gndPred)
00221 {
00222 for (int i = 0; i < gndPredIndexes_->size(); i++)
00223 {
00224 if (gndPred == (*gndPredIndexes_)[i])
00225 {
00226 gndPredIndexes_->removeItem(i);
00227 gndPredIndexes_->compress();
00228 rehash();
00229 break;
00230 }
00231 }
00232 }
00233
00241 void changeGndPredIndex(const int& oldIdx, const int& newIdx)
00242 {
00243 for (int i = 0; i < gndPredIndexes_->size(); i++)
00244 {
00245 if (oldIdx == (*gndPredIndexes_)[i])
00246 {
00247 (*gndPredIndexes_)[i] = newIdx;
00248 rehash();
00249 break;
00250 }
00251 }
00252 }
00253
00254 size_t hashCode() { return hashCode_; }
00255
00256 bool same(const GroundClause* const & gc)
00257 {
00258 if (this == gc) return true;
00259 if (gndPredIndexes_->size() != gc->getGndPredIndexes()->size())
00260 {
00261 return false;
00262 }
00263 return (memcmp(gndPredIndexes_->getItems(),
00264 gc->getGndPredIndexes()->getItems(),
00265 (gndPredIndexes_->size())*sizeof(int)) == 0);
00266 }
00267
00268 void printWithoutWt(ostream& out) const;
00269 void print(ostream& out) const;
00270
00271 ostream& print(ostream& out, const Domain* const& domain,
00272 const bool& withWt, const bool& asInt,
00273 const bool& withStrVar,
00274 const GroundPredicateHashArray* const & predHashArray) const;
00275
00276 ostream& printWithoutWt(ostream& out, const Domain* const & domain,
00277 const GroundPredicateHashArray* const & predHashArray) const;
00278
00279 ostream&
00280 printWithoutWtWithStrVar(ostream& out, const Domain* const & domain,
00281 const GroundPredicateHashArray* const & predHashArray) const;
00282
00283 ostream& printWithWtAndStrVar(ostream& out, const Domain* const& domain,
00284 const GroundPredicateHashArray* const & predHashArray) const;
00285
00286 ostream& print(ostream& out, const Domain* const& domain,
00287 const GroundPredicateHashArray* const & predHashArray) const;
00288
00289 ostream& printWithoutWtWithStrVarAndPeriod(ostream& out,
00290 const Domain* const& domain,
00291 const GroundPredicateHashArray* const & predHashArray) const;
00292
00293 double sizeKB();
00294
00295 private:
00296
00300 void rehash()
00301 {
00302 Array<unsigned int>* intArrRep = new Array<unsigned int>;
00303
00304
00305 for (int i = 0; i < gndPredIndexes_->size(); i++)
00306 {
00307
00308 if ((*gndPredIndexes_)[i] > 0)
00309 intArrRep->append(1);
00310 else
00311 intArrRep->append((unsigned int)0);
00312 intArrRep->append(abs((*gndPredIndexes_)[i]));
00313 }
00314
00315 hashCode_ = Hash::hash(*intArrRep);
00316 delete intArrRep;
00317 }
00318
00319 private:
00320
00321 size_t hashCode_;
00322 Array<int>* gndPredIndexes_;
00323
00324
00325
00326 double wt_;
00327
00328
00329 IntPair* foClauseFrequencies_;
00330
00331
00332
00333
00334 Array<const double*>* parentWtPtrs_;
00335
00336 };
00337
00338
00340
00341
00342 class HashGroundClause
00343 {
00344 public:
00345 size_t operator()(GroundClause* const & gc) const { return gc->hashCode(); }
00346 };
00347
00348
00349 class EqualGroundClause
00350 {
00351 public:
00352 bool operator()(GroundClause* const & c1, GroundClause* const & c2) const
00353 { return c1->same(c2); }
00354 };
00355
00356
00358
00359 typedef HashArray<GroundClause*, HashGroundClause, EqualGroundClause>
00360 GroundClauseHashArray;
00361
00362 typedef hash_set<GroundClause*, HashGroundClause, EqualGroundClause>
00363 GroundClauseSet;
00364
00365
00366 #endif