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 TERM_H_JUN_26_2005
00067 #define TERM_H_JUN_26_2005
00068
00069 #include <climits>
00070 #include <stdlib.h>
00071 #include <ostream>
00072 using namespace std;
00073 #include "array.h"
00074
00075 class Function;
00076 class Domain;
00077
00078
00079
00080 class Term
00081 {
00082 public:
00083 enum { NONE = 0, CONSTANT = 1, VARIABLE = 2, FUNCTION = 3 };
00084
00085 public:
00086 Term() : id_(-1), function_(NULL), intArrRep_(NULL), dirty_(true),
00087 parent_(NULL), parentIsPred_(true) {}
00088
00089 Term(void* const & parent, const bool& parentIsPred)
00090 : id_(-1), function_(NULL), intArrRep_(NULL), dirty_(true),
00091 parent_(parent), parentIsPred_(parentIsPred) {}
00092
00093 Term(const int& id)
00094 : id_(id), function_(NULL), intArrRep_(NULL), dirty_(true), parent_(NULL),
00095 parentIsPred_(true) {}
00096
00097 Term(const int& id, void* const & parent, const bool& parentIsPred)
00098 : id_(id), function_(NULL), intArrRep_(NULL), dirty_(true),
00099 parent_(parent), parentIsPred_(parentIsPred) {}
00100
00101 Term(Function* f) : id_(-1), function_(f), intArrRep_(NULL), dirty_(true),
00102 parent_(NULL), parentIsPred_(true) {}
00103
00104 Term(Function* f, void* const & parent, const bool& parentIsPred)
00105 : id_(-1), function_(f), intArrRep_(NULL), dirty_(true),
00106 parent_(parent), parentIsPred_(parentIsPred) {}
00107
00108 Term(const Term& t);
00109
00110 Term(const Term& t, void* const & parent, const bool& parentIsPred);
00111
00112 ~Term();
00113
00114
00115
00116 double sizeMB() const
00117 { return (fixedSizeB_ + intArrRep_->size()*sizeof(int))/1000000.0; }
00118
00119
00120 static void computeFixedSizeB()
00121 { fixedSizeB_ = sizeof(Term) + sizeof(Array<int>); }
00122
00123
00124
00125 void compress() { if (intArrRep_) intArrRep_->compress(); }
00126
00127
00128 void setDirty();
00129 bool isDirty() const { return dirty_; }
00130
00131
00132 int getType() const
00133 {
00134 if (id_ >= 0) return CONSTANT;
00135 if (function_ != NULL) return FUNCTION;
00136 return VARIABLE;
00137 }
00138
00139
00140 void setParent(void* const parent, const bool& parentIsPred)
00141 { parent_ = parent; parentIsPred_ = parentIsPred; setDirty(); }
00142
00143
00144 void* getParent(bool& parentIsPred) const
00145 { parentIsPred = parentIsPred_; return parent_; }
00146
00147
00148 void setId(const int& id)
00149 {
00150 id_ = id;
00151 setDirty();
00152 }
00153
00154 int getId() const { return id_; }
00155
00156
00157 void revertToBeFunction() { assert(function_); id_ = -1; }
00158
00159 void setFunction(Function* const & f) { function_ = f; setDirty(); }
00160 Function* getFunction() const { return function_; }
00161
00162 bool isConstant() const { return id_ >= 0; }
00163 bool isGrounded() const { return id_ >= 0; }
00164
00165
00166 void appendIntArrRep(Array<int>& rep)
00167 {
00168 if (dirty_) computeAndStoreIntArrRep();
00169 rep.append(*intArrRep_);
00170 }
00171
00172
00173 bool same(Term* const & t)
00174 {
00175 if (this==t) return true;
00176 const Array<int>* tArr = t->getIntArrRep();
00177 const Array<int>* myArr = getIntArrRep();
00178 if (myArr->size() != tArr->size()) return false;
00179 const int* tItems = t->getIntArrRep()->getItems();
00180 const int* myItems = getIntArrRep()->getItems();
00181 return (memcmp(myItems, tItems, myArr->size()*sizeof(int))==0);
00182 }
00183
00184
00185 void printAsInt(ostream& out) const;
00186 void printWithStrVar(ostream& out, const Domain* const & domain) const;
00187 void print(ostream& out, const Domain* const & domain) const;
00188
00189
00190 private:
00191 void copy(const Term& t);
00192 bool noDirtyFunction();
00193
00194 const Array<int>* getIntArrRep()
00195 { if (dirty_) computeAndStoreIntArrRep(); return intArrRep_; }
00196
00197 void computeAndStoreIntArrRep();
00198
00199 private:
00200
00201
00202
00203
00204
00205
00206 int id_;
00207 Function* function_;
00208 Array<int>* intArrRep_;
00209 bool dirty_;
00210
00211 void* parent_;
00212 bool parentIsPred_;
00213
00214 static double fixedSizeB_;
00215 };
00216
00217 #endif