Changeset 3923
 Timestamp:
 Sep 17, 2008, 12:16:16 PM (12 years ago)
 Location:
 toimperative/trunk/opents/samples
 Files:

 2 edited
Legend:
 Unmodified
 Added
 Removed

toimperative/trunk/opents/samples/fab.tpp
r3922 r3923 145 145 tfun int init_ () { 146 146 _c_3 = GExpr(3).add("AEA", 3); 147 _c_1 = _c_3.subexpr(0, 1) .get_ref();147 _c_1 = _c_3.subexpr(0, 1); 148 148 _c_0 = GExpr(8).add("ABABA", 5).add(GExpr(5).add("CA", 2).add(GExpr(9).add("A", 1).add(_c_3).add("ADA", 3).add(GExpr(3).add("EA", 2).add(_c_1)).add("AAA", 3)).add("AC", 2)).add("BA", 2); 149 _c_2 = _c_0.subexpr(1, 1) .get_ref();149 _c_2 = _c_0.subexpr(1, 1); 150 150 return 0; 151 151 } 152 152 153 153 tfun int cleanup_ () { 154 _c_3.clear();155 _c_1.clear();156 _c_0.clear();157 _c_2.clear();158 154 global_exprs.clear(); 159 155 return 0; 
toimperative/trunk/opents/samples/trefal.hh
r3920 r3923 64 64 Term* terms; 65 65 size_t size; 66 bool need_delete; 67 68 GExpr () : terms(0), size(0), need_delete(false) {} 66 67 GExpr () : terms(0), size(0) {} 69 68 70 69 inline GExpr (TExpr& _e); 71 70 72 GExpr (int size) : terms(new Term[size]), size(0), need_delete(true) {}73 74 GExpr& operator= ( GExpr& e) {71 inline GExpr (int size); 72 73 GExpr& operator= (const GExpr& e) { 75 74 if (terms != 0) // must be uninitialized 76 75 throw 0; 77 76 terms = e.terms; 78 77 size = e.size; 79 need_delete = e.need_delete;80 e.need_delete = false;81 78 return *this; 82 79 } … … 123 120 return e; 124 121 } 125 126 GExpr& get_ref () { 127 return *this; 122 }; 123 124 template <class T> class ResizableArray { 125 size_t max_size; 126 T* array; 127 size_t size; 128 129 public: 130 131 ResizableArray (int init_max_size = 16) : max_size(init_max_size), array(new T[init_max_size]), size(0) {} 132 133 size_t add (T& elem) { 134 if (size == max_size) { 135 T* old_array = array; 136 array = new T[2*max_size]; 137 memcpy(array, old_array, max_size*sizeof(T)); 138 delete[] old_array; 139 max_size *= 2; 140 } 141 array[size] = elem; 142 return size++; 143 } 144 145 size_t get_size () { 146 return size; 147 } 148 149 T& operator[] (size_t i) { 150 return array[i]; 151 } 152 153 ~ResizableArray () { 154 delete[] array; 155 } 156 }; 157 158 class GlobalExprs { 159 ResizableArray<GExpr> exprs; 160 ResizableArray<Term*> memory; 161 162 public: 163 164 GlobalExprs () : exprs(), memory() {} 165 166 int add_global_expr (GExpr& expr) { 167 return exprs.add(expr); 168 } 169 170 Term* create_new_array (size_t size) { 171 Term* array = new Term[size]; 172 memory.add(array); 173 return array; 174 } 175 176 GExpr& operator[] (size_t i) { 177 return exprs[i]; 128 178 } 129 179 130 180 void clear () { 131 if (need_delete) 132 delete[] terms; 133 } 134 }; 135 136 class GlobalExprs { 137 int size; 138 int free_idx; 139 140 public: 141 142 GExpr* exprs; 143 144 GlobalExprs () : size (8), free_idx(0), exprs(new GExpr[8]) {} 145 146 int add_global_expr (GExpr& expr) { 147 if (free_idx == size) { 148 GExpr* old_exprs = exprs; 149 exprs = new GExpr[2*size]; 150 memcpy(exprs, old_exprs, size*sizeof(GExpr)); 151 delete[] old_exprs; 152 size *= 2; 153 } 154 exprs[free_idx] = expr; 155 return free_idx++; 156 } 157 158 void clear () { 159 for (int i = 0; i < size; i++) 160 exprs[i].clear(); 161 delete[] exprs; 181 for (size_t i = 0, size = exprs.get_size(); i < size; i++) 182 delete[] memory[i]; 162 183 } 163 184 }; … … 234 255 235 256 void init_const (int idx) { 236 terms = global_exprs .exprs[idx].terms;237 extDataSize() = global_exprs .exprs[idx].size * sizeof(Term);257 terms = global_exprs[idx].terms; 258 extDataSize() = global_exprs[idx].size * sizeof(Term); 238 259 is_const = true; 239 260 } … … 297 318 case Term::INT: os << t.data; break; 298 319 case Term::EXPR: os << GExpr(t.e); break; 299 case Term::CONST: os << global_exprs .exprs[t.data]; break;320 case Term::CONST: os << global_exprs[t.data]; break; 300 321 } 301 322 return os; … … 368 389 if (_t2.type == Term::EXPR) 369 390 return _t1.e == _t2.e; 370 return GExpr(_t1.e) == global_exprs .exprs[_t2.data];391 return GExpr(_t1.e) == global_exprs[_t2.data]; 371 392 } 372 393 if (_t2.type == Term::EXPR) 373 return global_exprs .exprs[_t1.data] == _t2.e;374 return _t1.data == _t2.data  global_exprs .exprs[_t1.data] == global_exprs.exprs[_t2.data];394 return global_exprs[_t1.data] == _t2.e; 395 return _t1.data == _t2.data  global_exprs[_t1.data] == global_exprs[_t2.data]; 375 396 } 376 397 … … 392 413 // =================================== GExpr =================================== 393 414 394 inline GExpr::GExpr (TExpr& _e) : terms((Expr&)_e), size(((Expr&)_e).get_len()), need_delete(false) {} 415 inline GExpr::GExpr (TExpr& _e) : terms((Expr&)_e), size(((Expr&)_e).get_len()) {} 416 inline GExpr::GExpr (int size) : terms(global_exprs.create_new_array(size)), size(0) {}; 395 417 396 418 GExpr& GExpr::add (GExpr& e) {
Note: See TracChangeset
for help on using the changeset viewer.