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
00028 #ifndef _DAG_DELTA_H_
00029 #define _DAG_DELTA_H_
00030
00031 #include <api_delta.h>
00032 #include <counted_ptr.h>
00033
00034 namespace coco {
00035
00037
00042 class dag_undelta : public undelta_base
00043 {
00044 private:
00045 typedef expression_walker walker;
00046
00047 public:
00050 gptr<model>* old_model;
00051
00054 counted_ptr<model> rm_dag;
00057 std::vector<model::enhanced_edge> rm_e;
00058
00060 std::vector<walker> added_nodes;
00062 std::vector<walker> added_constraints;
00064 std::vector<walker> added_ghosts;
00066 std::vector<walker> added_vars;
00067
00071 std::map<unsigned int, interval> bounds_chgd;
00072
00075 bool is_full_undelta;
00076
00077 private:
00081 class __check_walkers
00082 {
00083 private:
00084 const std::vector<walker>& nr;
00085
00086 public:
00087 __check_walkers(const std::vector<walker>& __nr) : nr(__nr) {}
00088
00089 bool operator()(const walker& _w) const
00090 {
00091 std::vector<walker>::const_iterator __x(nr.begin()), __e(nr.end());
00092 for(; __x != __e; ++__x)
00093 if(_w == *__x)
00094 return false;
00095 return true;
00096 }
00097 };
00098
00099 public:
00101 dag_undelta(bool full=false) : undelta_base(),
00102 old_model(NULL),
00103 rm_dag(), rm_e(),
00104 added_nodes(), added_constraints(),
00105 added_ghosts(), added_vars(),
00106 bounds_chgd(), is_full_undelta(full)
00107 {}
00108
00111 dag_undelta(gptr<model>* __nc) : undelta_base(),
00112 old_model(__nc),
00113 rm_dag(), rm_e(),
00114 added_nodes(), added_constraints(),
00115 added_ghosts(), added_vars(),
00116 bounds_chgd(), is_full_undelta(true)
00117 {}
00118
00120 dag_undelta(const dag_undelta& _du) : undelta_base(_du),
00121 old_model(_du.old_model),
00122 rm_dag(_du.rm_dag), rm_e(_du.rm_e),
00123 added_nodes(_du.added_nodes),
00124 added_constraints(_du.added_constraints),
00125 added_ghosts(_du.added_ghosts),
00126 added_vars(_du.added_vars),
00127 bounds_chgd(_du.bounds_chgd),
00128 is_full_undelta(_du.is_full_undelta)
00129 {
00130 #if DEBUG_DELTA
00131 std::cerr << "Called dag_undelta copy constructor" << std::endl;
00132 #endif
00133 }
00134
00136 ~dag_undelta()
00137 {
00138 #if DEBUG_DELTA
00139 std::cerr << "Called dag_undelta destructor" << std::endl;
00140 #endif
00141 if(old_model) delete old_model;
00142 }
00143
00145 dag_undelta* new_copy() const { return new dag_undelta(*this); }
00147 void destroy_copy(undelta_base* __d) const { delete (dag_undelta*)__d; }
00148
00150 bool unapply(work_node& x, const delta_id& _did) const;
00151
00152 friend class dag_delta;
00153 };
00154
00156
00161 class dag_delta : public delta_base
00162 {
00163 private:
00164 typedef expression_walker walker;
00165
00166 public:
00170 counted_ptr<model> new_constraints;
00171
00176 std::vector<walker> rm_nodes;
00177
00181 bool is_full_delta;
00182
00185 dag_delta(const std::string& __a, bool full=false) :
00186 delta_base(__a), new_constraints(),
00187 rm_nodes(), is_full_delta(full)
00188 {}
00189
00193 dag_delta(const std::string& __a, model* __nc, bool full=false) :
00194 delta_base(__a),
00195 new_constraints(__nc),
00196 rm_nodes(), is_full_delta(full)
00197 { __nc->free_nodes_on_destroy(); }
00198
00200 dag_delta(const dag_delta& __d) : delta_base(__d),
00201 new_constraints(__d.new_constraints),
00202 rm_nodes(__d.rm_nodes),
00203 is_full_delta(__d.is_full_delta)
00204 {
00205 #if DEBUG_DELTA
00206 std::cerr << "Called dag_delta copy constructor" << std::endl;
00207 #endif
00208 }
00209
00211 ~dag_delta()
00212 {
00213 #if DEBUG_DELTA
00214 std::cerr << "Called dag_delta destructor"
00215 #if COUNTED_PTR_DEBUG
00216 << ": new_constraints ctr = " << new_constraints.get_count()
00217 #endif
00218 << std::endl;
00219 #endif
00220 }
00221
00223 dag_delta* new_copy() const { return new dag_delta(*this); }
00225 void destroy_copy(delta_base* __d) const { delete (dag_delta*)__d; }
00226
00228 void add_new(model* __m)
00229 { __m->free_nodes_on_destroy();
00230 new_constraints = __m;
00231 }
00233 void add_new(model& __m)
00234 { new_constraints = new model(__m);
00235 new_constraints->free_nodes_on_destroy();
00236 }
00237
00239 void remove(const walker& _nn) { rm_nodes.push_back(_nn); }
00242 void remove(const std::vector<walker>& _nn)
00243 { rm_nodes.insert(rm_nodes.end(), _nn.begin(), _nn.end()); }
00244
00247 void unkeep() { new_constraints->keep_nodes_on_destroy(); }
00248
00249 private:
00252 void help_apply_p(work_node& x, expression_walker& _x, model* nm) const;
00254 void help_apply_s(work_node& x, model* nm) const;
00257 void help_apply_c(work_node& x, expression_walker& _x, model* nm) const;
00259 void help_apply_g(work_node& x, model* nm) const;
00260
00263 struct __docompare_nodes
00264 {
00265 bool operator() (const expression_walker& _a, unsigned int _b) const
00266 {return _a->node_num < _b;}
00267 bool operator() (unsigned int _a, const expression_walker& _b) const
00268 {return _a < _b->node_num;}
00269 bool operator() (const expression_walker& _a, const expression_walker& _b) const
00270 {return _a->node_num < _b->node_num;}
00271 };
00272
00276 class __check_nodes
00277 {
00278 private:
00279 const std::vector<walker>& nr;
00280
00281 public:
00282 __check_nodes(const std::vector<walker>& __nr) : nr(__nr) {}
00283
00284 bool operator()(const walker& _w) const
00285 {
00286 std::vector<walker>::const_iterator
00287 __x(lower_bound(nr.begin(), nr.end(),
00288 _w->node_num, __docompare_nodes()));
00289 if(__x != nr.end() && (*__x)->node_num == _w->node_num)
00290 return false;
00291 else
00292 return true;
00293 }
00294 };
00295
00296 public:
00299 bool apply(work_node& x, undelta_base*& _u, const delta_id& _did) const;
00300
00302 bool operator==(const delta_base& _c) const
00303 { return _c.get_action() == get_action() &&
00304 this->operator==(*(dag_delta*)&_c); }
00305 bool operator!=(const delta_base& _c) const
00306 { return _c.get_action() != get_action() ||
00307 this->operator!=(*(dag_delta*)&_c); }
00308
00310 bool operator==(const dag_delta& _c) const
00311 { return this == &_c; }
00312 bool operator!=(const dag_delta& _c) const
00313 { return this != &_c; }
00314
00315 friend class dag_undelta;
00316 };
00317
00318 }
00319
00320 #endif