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