Changeset 3903
 Timestamp:
 Sep 11, 2008, 7:48:19 PM (12 years ago)
 File:

 1 edited
Legend:
 Unmodified
 Added
 Removed

toimperative/trunk/opents/samples/trefal.hh
r3902 r3903 15 15 class GlobalExprs; 16 16 17 class TExpr : public ts::TVar<Expr> 18 { 19 public: 17 extern GlobalExprs global_exprs; 18 19 class TExpr : public ts::TVar<Expr> { 20 public: 20 21 TExpr () {}; 21 22 TExpr (const GExpr& _e); … … 27 28 }; 28 29 29 struct Term 30 { 30 struct Term { 31 31 enum Type { 32 32 CHAR, INT, EXPR, CONST … … 60 60 }; 61 61 62 struct GExpr 63 { 62 struct GExpr { 64 63 Term* terms; 65 64 size_t size; 66 65 67 GExpr () : terms(0), size(0) {}; 66 GExpr () : terms(0), size(0) {} 67 68 68 GExpr (TExpr& _e); 69 69 70 GExpr (Term & t) { 71 size = 1; 70 GExpr (int size) : terms(new Term[size]), size(0) {} 71 72 void init (int size) { 73 if (terms != 0) // must be unitialized 74 throw 0; 72 75 terms = new Term[size]; 73 *terms = t; 74 } 76 } 77 78 GExpr& operator= (const GExpr& e) { 79 if (terms != 0) // must be unitialized 80 throw 0; 81 terms = e.terms; 82 size = e.size; 83 return *this; 84 } 85 86 Term& operator[] (int i) const { 87 return terms[i]; 88 } 89 90 void add (const Term& t) { 91 terms[size++] = t; 92 } 93 94 void add (char c) { 95 Term t; 96 t.init_char(c); 97 add(t); 98 } 99 100 void add (int i) { 101 Term t; 102 t.init_int(i); 103 add(t); 104 } 105 106 void add (const GExpr& e); 107 108 void concat (const GExpr& x) { 109 for (size_t i = 0; i < x.size; i++) 110 terms[size++] = x.terms[i]; 111 } 112 113 void clear () { 114 delete[] terms; 115 } 116 }; 117 118 class GlobalExprs { 119 int size; 120 int free_idx; 121 122 public: 123 124 GExpr* exprs; 125 126 GlobalExprs () : size (200), free_idx(0), exprs(new GExpr[200]) {} 127 128 int add_global_expr (const GExpr& expr) { 129 if (free_idx == size) { 130 GExpr* old_exprs = exprs; 131 exprs = new GExpr[2*size]; 132 memcpy(exprs, old_exprs, size*sizeof(GExpr)); 133 delete old_exprs; 134 size *= 2; 135 } 136 exprs[free_idx] = expr; 137 return free_idx++; 138 } 139 140 void clear () { 141 for (int i = 0; i < size; i++) 142 exprs[i].clear(); 143 delete[] exprs; 144 } 145 }; 146 147 class Expr : private ts::TExtData { 148 bool is_const; 149 Term* terms; 150 151 void copyFrom (const Expr& e, size_t start, size_t length) { 152 Term* p = *this; 153 const Term* q = ((const Term*) e) + start; 154 if (terms) 155 for (unsigned i = 0; i < length; i++) 156 *(p++) = *(q++); 157 else 158 for (unsigned i = 0; i < length; i++) 159 new (p++) Term(*q++); 160 } 161 162 void clear () { 163 if (terms) { 164 if (!is_const) 165 delete[] terms; 166 terms = 0; 167 is_const = false; 168 } else { 169 Term* p = *this; 170 size_t length = get_len(); 171 for (unsigned i = 0; i < length; i++) 172 p++>~Term(); 173 } 174 } 175 176 public: 177 178 Expr () : is_const(false), terms(0) {}; 179 180 void init (size_t s) { 181 assert(!terms); 182 terms = new Term[s]; 183 extDataSize() = s * sizeof(Term); 184 }; 75 185 76 186 void init_str (const char* str, size_t len) { 77 size = len; 78 terms = new Term[len]; 187 init(len); 79 188 for (unsigned i = 0; i < len; i++) 80 189 terms[i].init_char(str[i]); 81 190 } 82 191 83 Term& operator[] (int i) { 84 return terms[i]; 85 } 86 87 GExpr & operator+ (const Term& t) { 88 size_t old_size = size; 89 Term* old_terms = terms; 90 91 size += 1; 92 terms = new Term[size]; 93 94 for (unsigned i = 0; i < old_size; i++) 95 *(terms++) = *(old_terms++); 96 *terms = t; 97 98 delete old_terms; 99 return *this; 100 } 101 102 GExpr & operator+ (const GExpr& x) { 103 size_t old_size = size; 104 Term* old_terms = terms; 105 106 size += x.size; 107 terms = new Term[size]; 108 109 for (unsigned i = 0; i < old_size; i++) 110 *(terms++) = *(old_terms++); 111 Term* new_terms = x.terms; 112 for (unsigned i = 0; i < x.size; i++) 113 *(terms++) = *(new_terms++); 114 115 delete old_terms; 116 return *this; 117 } 118 119 GExpr operator() () const; 120 121 void clear() { 122 delete[] terms; 123 } 124 }; 125 126 class GlobalExprs { 127 int size; 128 int free_idx; 129 130 public: 131 GExpr* exprs; 132 133 GlobalExprs () : size (8), free_idx(0), exprs(new GExpr[8]) {} 134 135 ~GlobalExprs () { 136 delete exprs; 192 void init_subexpr (const Expr& e, size_t start, size_t len) { 193 init(len); 194 copyFrom(e, start, len); 195 } 196 197 void init_paren (const TExpr& te) { 198 init(1); 199 terms[0].init_expr(te); 200 } 201 202 void init_concat (const Expr** exprs, size_t num) { 203 assert(!terms); 204 size_t len = 0; 205 for (unsigned i = 0; i < num; i++) 206 len += exprs[i]>get_len(); 207 terms = new Term[len]; 208 extDataSize() = len * sizeof(Term); 209 Term* p = terms; 210 for (unsigned i = 0; i < num; i++) { 211 const Term* q = *exprs[i]; 212 for (unsigned j = 0; j < exprs[i]>get_len(); j++) 213 *p++ = *q++; 137 214 } 138 139 int add_global_expr (const GExpr& expr) { 140 if (free_idx == size) { 141 GExpr* old_exprs = exprs; 142 exprs = new GExpr[2*size]; 143 memcpy(exprs, old_exprs, size*sizeof(GExpr)); 144 delete old_exprs; 145 size *= 2; 146 } 147 exprs[free_idx] = expr; 148 return free_idx++; 149 } 150 }; 151 152 extern GlobalExprs global_exprs; 153 154 GExpr GExpr::operator() () const 155 { 156 Term t; 157 t.init_const(global_exprs.add_global_expr(*this)); 158 return GExpr(t); 159 } 160 161 class Expr : private ts::TExtData 162 { 163 private: 164 165 bool is_const; 166 167 Term* terms; 168 169 void copyFrom (const Expr& e, size_t start, size_t length) { 170 Term* p = *this; 171 const Term* q = ((const Term*) e) + start; 172 if (terms) 173 for (unsigned i = 0; i < length; i++) 174 *(p++) = *(q++); 175 else 176 for (unsigned i = 0; i < length; i++) 177 new (p++) Term(*q++); 178 } 179 180 void clear () { 181 if (terms) { 182 if (!is_const) 183 delete[] terms; 184 terms = 0; 185 is_const = false; 186 } else { 187 Term* p = *this; 188 size_t length = get_len(); 189 for (unsigned i = 0; i < length; i++) 190 p++>~Term(); 191 } 192 } 193 194 public: 195 196 Expr () : is_const(false), terms(0) {}; 197 198 void init (size_t s) { 199 assert(!terms); 200 terms = new Term[s]; 201 extDataSize() = s * sizeof(Term); 202 }; 203 204 void init_str (const char* str, size_t len) { 205 init(len); 206 for (unsigned i = 0; i < len; i++) 207 terms[i].init_char(str[i]); 208 } 209 210 void init_subexpr (const Expr& e, size_t start, size_t len) { 211 init(len); 212 copyFrom(e, start, len); 213 } 214 215 void init_paren (const TExpr& te) { 216 init(1); 217 terms[0].init_expr(te); 218 } 219 220 void init_concat (const Expr** exprs, size_t num) { 221 assert(!terms); 222 size_t len = 0; 223 for (unsigned i = 0; i < num; i++) 224 len += exprs[i]>get_len(); 225 terms = new Term[len]; 226 extDataSize() = len * sizeof(Term); 227 Term* p = terms; 228 for (unsigned i = 0; i < num; i++) { 229 const Term* q = *exprs[i]; 230 for (unsigned j = 0; j < exprs[i]>get_len(); j++) 231 *p++ = *q++; 232 } 233 } 234 235 void init_const (int idx) { 236 terms = global_exprs.exprs[idx].terms; 237 extDataSize() = global_exprs.exprs[idx].size * sizeof(Term); 238 is_const = true; 239 } 240 241 void init_const (const GExpr& e) { 242 terms = e.terms; 243 extDataSize() = e.size * sizeof(Term); 244 is_const = true; 245 } 246 247 operator const Term* () const { return terms ? terms : (Term*)extData(); } 248 operator Term* () { return terms ? terms : (Term*)extData(); } 249 250 Expr (const Expr& e) : is_const(false), terms(0) { 215 } 216 217 void init_const (int idx) { 218 terms = global_exprs.exprs[idx].terms; 219 extDataSize() = global_exprs.exprs[idx].size * sizeof(Term); 220 is_const = true; 221 } 222 223 void init_const (const GExpr& e) { 224 terms = e.terms; 225 extDataSize() = e.size * sizeof(Term); 226 is_const = true; 227 } 228 229 operator const Term* () const { return terms ? terms : (Term*)extData(); } 230 operator Term* () { return terms ? terms : (Term*)extData(); } 231 232 Expr (const Expr& e) : is_const(false), terms(0) { 233 copyFrom(e, 0, e.get_len()); 234 } 235 236 Expr& operator= (const Expr& e) { 237 if (this != &e) { 238 clear(); 239 init(e.get_len()); 251 240 copyFrom(e, 0, e.get_len()); 252 241 } 253 254 Expr& operator= (const Expr& e) { 255 if (this != &e) { 256 clear(); 257 init(e.get_len()); 258 copyFrom(e, 0, e.get_len()); 259 } 260 return *this; 261 } 262 263 Expr subexpr (size_t start, size_t length) const { 264 Expr e; 265 e.init(length); 266 e.copyFrom(*this, start, length); 267 return e; 268 } 269 270 size_t get_len () const { 271 return extDataSize() / sizeof(Term); 272 } 273 274 Term& operator[] (int i) { 275 return ((Term *)*this)[i]; 276 } 277 278 bool symbol_at (size_t pos) { 279 return ((Term *)*this)[pos].type < Term::EXPR; 280 } 281 282 ~Expr () { 283 clear(); 284 } 242 return *this; 243 } 244 245 Expr subexpr (size_t start, size_t length) const { 246 Expr e; 247 e.init(length); 248 e.copyFrom(*this, start, length); 249 return e; 250 } 251 252 size_t get_len () const { 253 return extDataSize() / sizeof(Term); 254 } 255 256 Term& operator[] (int i) { 257 return ((Term *)*this)[i]; 258 } 259 260 bool symbol_at (size_t pos) { 261 return ((Term *)*this)[pos].type < Term::EXPR; 262 } 263 264 ~Expr () { 265 clear(); 266 } 285 267 }; 268 269 270 // ================================= operator<< ================================ 286 271 287 272 // (TExpr)e and t are intentionally not const! … … 311 296 return os << GExpr(e); 312 297 } 298 299 // ================================= operator>> ================================ 313 300 314 301 std::istream& operator>> (std::istream& is, GExpr& e); … … 336 323 337 324 std::istream& operator>> (std::istream& is, GExpr& e) { 338 int buffer_size = 20;325 int buffer_size = 30; 339 326 e.terms = new Term[buffer_size]; 340 327 char c; … … 353 340 } 354 341 355 inline GExpr::GExpr (TExpr& _e) : terms((Expr&)_e), size(((Expr&)_e).get_len()) {} 342 // ================================= operator== ================================ 356 343 357 344 inline bool operator== (const GExpr& _e1, const GExpr& _e2); … … 385 372 } 386 373 387 Expr operator+ (const Expr& e1, const Expr& e2) { 388 Expr e; 389 e.init(e1.get_len() + e2.get_len()); 390 Term* p = e; 391 const Term* q = e1; 392 for (unsigned i = 0; i < e1.get_len(); i++) 393 new (p++) Term(*q++); 394 q = e2; 395 for (unsigned i = 0; i < e2.get_len(); i++) 396 new (p++) Term(*q++); 397 return e; 398 } 374 // =================================== GExpr =================================== 375 376 inline GExpr::GExpr (TExpr& _e) : terms((Expr&)_e), size(((Expr&)_e).get_len()) {} 377 378 void GExpr::add (const GExpr& e) { 379 Term t; 380 t.init_const(global_exprs.add_global_expr(e)); 381 add(t); 382 } 383 384 // =================================== TExpr =================================== 399 385 400 386 inline TExpr::TExpr (const GExpr& _e) {
Note: See TracChangeset
for help on using the changeset viewer.