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 _EVALUATOR_H_
00029 #define _EVALUATOR_H_
00030
00031 #include <vector>
00032 #include <dag.h>
00033 #include <g_algo.h>
00034 #include <stdint.h>
00035 #include <coconut_config.h>
00036
00037 using namespace vgtl;
00038
00039 namespace coco {
00040
00045 static unsigned int _sbits[256] = {
00046 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
00047 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00048 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00049 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00050 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00051 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00052 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00053 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00054
00055 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
00056 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00057 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00058 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00059 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
00060 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00061 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
00062 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
00063 };
00064
00066
00073 class variable_indicator
00074 {
00075 private:
00077 std::vector<uint32_t> v;
00078 public:
00080 variable_indicator() : v() {}
00081
00083 variable_indicator(int num_of_vars) : v((num_of_vars+31)/32, 0) {}
00084
00087 variable_indicator(const std::vector<int>& __v, int num_of_vars)
00088 : v((num_of_vars+31)/32, 0)
00089 {
00090 for(std::vector<int>::const_iterator b = __v.begin(); b != __v.end(); ++b)
00091 v[*b/32] |= (1 << (*b % 32));
00092 }
00093
00095 variable_indicator(const variable_indicator& __x) : v(__x.v) {}
00096
00098 virtual ~variable_indicator() {}
00099
00102 void reserve(int num_of_vars)
00103 {
00104 unsigned int nv;
00105 v.reserve(nv = (num_of_vars+31)/32);
00106 if(nv > v.size()) v.insert(v.end(), nv-v.size(), 0);
00107 }
00108
00112 void set_all(const variable_indicator& _vi)
00113 {
00114 std::vector<uint32_t>::iterator b;
00115 std::vector<uint32_t>::const_iterator d;
00116
00117 for(b = v.begin(), d = _vi.v.begin(); b != v.end() && d != _vi.v.end();
00118 ++b, ++d)
00119 {
00120 *b |= *d;
00121 }
00122 }
00123
00126 void set(int __i)
00127 {
00128 v[__i/32] |= (1 << (__i % 32));
00129 }
00130
00132 void set(std::vector<int> __v)
00133 {
00134 for(std::vector<int>::const_iterator b = __v.begin(); b != __v.end(); ++b)
00135 v[*b/32] |= (1 << (*b % 32));
00136 }
00137
00140 void set(int start_idx, int end_idx)
00141 {
00142 uint32_t i = start_idx/32;
00143 uint32_t j = start_idx%32;
00144
00145 while(end_idx > start_idx)
00146 {
00147 uint32_t k = end_idx - start_idx;
00148 if(k+j > 32) k = 32-j;
00149 if(k == 32)
00150 v[i] = 0xffffffff;
00151 else
00152 v[i] |= ((1 << k) - 1) << j;
00153 ++i;
00154 j=0;
00155 start_idx = 32*i;
00156 }
00157 }
00158
00160 void clear()
00161 {
00162 for(std::vector<uint32_t>::iterator b = v.begin(); b != v.end(); ++b)
00163 *b = 0;
00164 }
00165
00168 void unset(int __i)
00169 {
00170 v[__i/32] &= ~(1 << (__i % 32));
00171 }
00172
00175 void unset(std::vector<int> __v)
00176 {
00177 for(std::vector<int>::const_iterator b = __v.begin(); b != __v.end(); ++b)
00178 v[*b/32] &= ~(1 << (*b % 32));
00179 }
00180
00183 void unset(int start_idx, int end_idx)
00184 {
00185 uint32_t i = start_idx/32;
00186 uint32_t j = start_idx%32;
00187
00188 while(end_idx > start_idx)
00189 {
00190 uint32_t k = end_idx - start_idx;
00191 if(k+j > 32) k = 32-j;
00192 if(k == 32)
00193 v[i] = 0;
00194 else
00195 v[i] &= ~(((1 << k) - 1) << j);
00196 ++i;
00197 j=0;
00198 start_idx = 32*i;
00199 }
00200 }
00201
00204 unsigned int sum(int start_idx, int end_idx) const
00205 {
00206 uint32_t i = start_idx/32;
00207 uint32_t j = start_idx%32;
00208 uint32_t help;
00209 unsigned int _r = 0;
00210
00211 while(end_idx > start_idx)
00212 {
00213 uint32_t k = end_idx - start_idx;
00214 if(k+j > 32) k = 32-j;
00215 if(k == 32)
00216 {
00217 help = v[i];
00218 }
00219 else
00220 help = v[i] & (((1 << k) - 1) << j);
00221 for(int ___j = 4; ___j != 0; --___j)
00222 {
00223 _r += _sbits[help & 255];
00224 help >>= 8;
00225 }
00226 ++i;
00227 j=0;
00228 start_idx = 32*i;
00229 }
00230 return _r;
00231 }
00232
00234 bool test(int __i) const
00235 {
00236 return v[__i/32] & (1 << (__i % 32));
00237 }
00238
00242 bool match(const variable_indicator& __v) const
00243 {
00244 std::vector<uint32_t>::const_iterator b1, b2;
00245 uint32_t r = 0;
00246
00247 for(b1 = v.begin(), b2 = __v.v.begin();
00248 b1 != v.end() && b2 != __v.v.end();
00249 ++b1, ++b2)
00250 {
00251 r |= *b1 & *b2;
00252 }
00253 return !r;
00254 }
00255
00257 variable_indicator& operator=(const variable_indicator& __v)
00258 {
00259 v = __v.v;
00260 return *this;
00261 }
00262
00264 bool operator==(const variable_indicator& __v)
00265 {
00266 return v == __v.v;
00267 }
00268
00271 std::vector<int> encode()
00272 {
00273 std::vector<int> __c;
00274
00275 __c.reserve(v.size());
00276 __c.insert(__c.end(), v.begin(), v.end());
00277 return __c;
00278 }
00279
00282 void decode(const std::vector<int>& __e)
00283 {
00284 if(!v.empty())
00285 v.erase(v.begin(), v.end());
00286 v.reserve(__e.size());
00287 v.insert(v.end(), __e.begin(), __e.end());
00288 }
00289 };
00290
00292
00297 template <class _Tp, class _NData, class _Result, class _Walker>
00298 class _evaluator_base
00299 {
00300 private:
00301 typedef _evaluator_base<_Tp,_NData,_Result,_Walker> _Self;
00302
00303 public:
00305 typedef _Tp data_type;
00307 typedef _NData node_data_type;
00308
00310 typedef _Result return_value;
00312 typedef _Walker const_walker;
00313
00314 protected:
00316 _Tp eval_data;
00317
00318 public:
00320 _evaluator_base() : eval_data() {}
00322 _evaluator_base(const _Tp& __x) : eval_data(__x) {}
00324 _evaluator_base(const _Self& __x) : eval_data(__x.eval_data) {}
00325
00327 virtual ~_evaluator_base() {}
00328
00332 virtual return_value vvalue() { return return_value(); }
00336 virtual return_value value() { return return_value(); }
00337
00348 virtual int vcollect(const return_value& __cresult) { return 0; }
00359 virtual int collect(const node_data_type& __data,
00360 const return_value& __cresult) { return 0; }
00361
00364 virtual void postorder(const node_data_type& __data) {}
00368 virtual const_walker short_cut_to(const node_data_type& __data) PURE_VIRTUAL
00369 };
00370
00372
00378 template <class _Tp, class _NData, class _Result, class _Walker>
00379 class evaluator_base : public _evaluator_base<_Tp,_NData,_Result,_Walker>
00380 {
00381 private:
00383 typedef _evaluator_base<_Tp,_NData,_Result,_Walker> _Base;
00384 public:
00386 typedef typename _Base::node_data_type node_data_type;
00388 typedef typename _Base::return_value return_value;
00390 typedef typename _Base::const_walker const_walker;
00391
00401 virtual int preorder(const node_data_type& __data) { return 1; }
00405 virtual const_walker short_cut_to(const node_data_type& __data)
00406 { return const_walker(); }
00407 };
00408
00410
00416 template <class _Tp, class _NData, class _Result, class _Walker>
00417 class cached_evaluator_base : public _evaluator_base<_Tp, _NData, _Result,_Walker>
00418 {
00419 private:
00421 typedef _evaluator_base<_Tp, _NData, _Result,_Walker> _Base;
00422 typedef cached_evaluator_base<_Tp,_NData,_Result,_Walker> _Self;
00423
00424 protected:
00427 const variable_indicator* v_ind;
00428
00429 public:
00431 typedef typename _Base::node_data_type node_data_type;
00433 typedef typename _Base::return_value return_value;
00435 typedef typename _Base::const_walker const_walker;
00436
00446 virtual int preorder(const node_data_type& __data) { return 1; }
00450 virtual const_walker short_cut_to(const node_data_type& __data)
00451 { return const_walker(); }
00452
00453 public:
00455 cached_evaluator_base() : _Base(), v_ind() {}
00458 cached_evaluator_base(const _Tp& __x, const variable_indicator& __v) :
00459 _Base(__x), v_ind(&__v) {}
00461 cached_evaluator_base(const _Self& __x) : _Base(__x), v_ind(__x.v_ind) {}
00462
00464 virtual ~cached_evaluator_base() {}
00465 };
00466
00468
00474 template <class _Tp, class _NData, class _Result, class _Walker>
00475 class forward_evaluator_base : public evaluator_base<_Tp, _NData, _Result,_Walker>
00476 {
00477 private:
00479 typedef evaluator_base<_Tp, _NData, _Result,_Walker> _Base;
00480
00481 public:
00482
00483
00485 typedef typename _Base::node_data_type node_data_type;
00487 typedef typename _Base::return_value return_value;
00489 typedef typename _Base::const_walker const_walker;
00490
00491 public:
00495 int preorder(const node_data_type& __data)
00496 { return initialize(__data); }
00500 void postorder(const node_data_type& __data)
00501 { calculate(__data); cleanup(__data); }
00506 int collect(const node_data_type& __data, const return_value& __rval)
00507 { return update(__data, __rval); }
00512 int vcollect(const return_value& __rval)
00513 { return update(__rval); }
00518 return_value value() { return calculate_value(false); }
00523 return_value vvalue() { return calculate_value(true); }
00527 void vinit() { initialize(); }
00528
00529 public:
00532 virtual void initialize() {}
00543 virtual int initialize(const node_data_type& __data) { return 0; }
00547 virtual void calculate(const node_data_type& __data) {}
00551 virtual void cleanup(const node_data_type& __data) {}
00561 virtual int update(const return_value& __rval) { return 0; }
00572 virtual int update(const node_data_type& __data, const return_value& __rval)
00573 { return 0; }
00577 virtual return_value calculate_value(bool eval_all) { return return_value(); }
00578 };
00579
00581
00587 template <class _Tp, class _NData, class _Result, class _Walker>
00588 class backward_evaluator_base : public evaluator_base<_Tp, _NData, _Result,_Walker>
00589 {
00590 private:
00592 typedef evaluator_base<_Tp, _NData, _Result,_Walker> _Base;
00593 typedef backward_evaluator_base<_Tp, _NData, _Result,_Walker> _Self;
00594
00595 public:
00597 typedef typename _Base::node_data_type node_data_type;
00599 typedef typename _Base::return_value return_value;
00601 typedef typename _Base::const_walker const_walker;
00602
00603
00604
00606 backward_evaluator_base() : _Base() {}
00608 backward_evaluator_base(const _Tp& __x) : _Base(__x) {}
00610 backward_evaluator_base(const _Self& __x) : _Base(__x) {}
00612 virtual ~backward_evaluator_base() {}
00613
00614 public:
00618 int preorder(const node_data_type& __data) { initialize(__data);
00619 return calculate(__data); }
00623 void postorder(const node_data_type& __data) { cleanup(__data); }
00628 int collect(const node_data_type& __data, const return_value& __rval)
00629 { return update(__data, __rval); }
00634 int vcollect(const return_value& __rval)
00635 { return update(__rval); }
00640 return_value value() { return calculate_value(false); }
00645 return_value vvalue() { return calculate_value(true); }
00649 void vinit() { initialize(); }
00650
00651 public:
00654 virtual void initialize() {}
00658 virtual void initialize(const node_data_type& __data) {}
00669 virtual int calculate(const node_data_type& __data) { return 0; }
00673 virtual void cleanup(const node_data_type& __data) {}
00684 virtual int update(const return_value& __rval) { return 0; }
00695 virtual int update(const node_data_type& __data, const return_value& __rval)
00696 { return 0; }
00700 virtual return_value calculate_value(bool eval_all) { return return_value(); }
00701 };
00702
00704
00710 template <class _Tp, class _NData, class _Result, class _Walker>
00711 class cached_forward_evaluator_base : public cached_evaluator_base<_Tp, _NData, _Result,_Walker>
00712 {
00713 private:
00715 typedef cached_evaluator_base<_Tp, _NData, _Result,_Walker> _Base;
00716
00717 public:
00719 typedef typename _Base::node_data_type node_data_type;
00721 typedef typename _Base::return_value return_value;
00723 typedef typename _Base::const_walker const_walker;
00724
00725
00726
00727 public:
00732 int preorder(const node_data_type& __data)
00733 { if(!is_cached(__data))
00734 return initialize(__data);
00735 else
00736 {
00737 retrieve_from_cache(__data);
00738 return 0;
00739 }
00740 }
00744 void postorder(const node_data_type& __data)
00745 { calculate(__data); cleanup(__data); }
00750 int collect(const node_data_type& __data, const return_value& __rval)
00751 { return update(__data, __rval); }
00756 int vcollect(const return_value& __rval)
00757 { return update(__rval); }
00762 return_value value() { return calculate_value(false); }
00767 return_value vvalue() { return calculate_value(true); }
00771 void vinit() { initialize(); }
00772
00773 public:
00776 virtual bool is_cached(const node_data_type& __data)
00777 { return this->v_ind->match(__data.var_indicator()); }
00778
00781 virtual void initialize() {}
00792 virtual int initialize(const node_data_type& __data) { return 1; }
00796 virtual void calculate(const node_data_type& __data) {}
00800 virtual void retrieve_from_cache(const node_data_type& __data) {}
00804 virtual void cleanup(const node_data_type& __data) {}
00814 virtual int update(const node_data_type& __data, const return_value& __rval)
00815 { return 0; }
00826 virtual int update(const return_value& __rval) { return 0; }
00830 virtual return_value calculate_value(bool eval_all) {return return_value(); }
00831 };
00832
00834
00840 template <class _Tp, class _NData, class _Result, class _Walker>
00841 class cached_backward_evaluator_base : public cached_evaluator_base<_Tp, _NData, _Result,_Walker>
00842 {
00843 private:
00845 typedef cached_evaluator_base<_Tp, _NData, _Result,_Walker> _Base;
00846
00847 public:
00849 typedef typename _Base::node_data_type node_data_type;
00851 typedef typename _Base::return_value return_value;
00853 typedef typename _Base::const_walker const_walker;
00854
00855
00856
00857 public:
00862 int preorder(const node_data_type& __data)
00863 { if(!is_cached(__data))
00864 return calculate(__data);
00865 else
00866 {
00867 retrieve_from_cache(__data);
00868 return 0;
00869 }
00870 }
00874 void postorder(const node_data_type& __data) { cleanup(__data); }
00879 int collect(const node_data_type& __data, const return_value& __rval)
00880 { return update(__data, __rval); }
00885 int vcollect(const return_value& __rval)
00886 { return update(__rval); }
00891 return_value value() { return calculate_value(false); }
00896 return_value vvalue() { return calculate_value(true); }
00900 void vinit() { initialize(); }
00901
00902 public:
00905 virtual bool is_cached(const node_data_type& __data)
00906 { return (*this->v_ind).match(__data.var_indicator()); }
00907
00910 virtual void initialize() {}
00921 virtual int calculate(const node_data_type& __data) {return 1;}
00925 virtual void cleanup(const node_data_type& __data) {}
00929 virtual void retrieve_from_cache(const node_data_type& __data) {}
00940 virtual int update(const return_value& __rval) { return 0; }
00951 virtual int update(const node_data_type& __data, const return_value& __rval)
00952 { return 0; }
00956 virtual return_value calculate_value(bool eval_all) {return return_value();}
00957 };
00958
00960
00988 template <class _Walker, class _Visitor>
00989 typename _Visitor::return_value recursive_short_cut_walk(_Walker __w,
00990 _Visitor __f)
00991 {
00992 _Walker __x;
00993 typename _Walker::children_iterator __it, __e;
00994
00995 if(__w.is_ground())
00996 {
00997 int j = 0;
00998 __e = __w.child_end();
00999 __f.vinit();
01000 for(__it = __w.child_begin(); __it != __e; ++__it)
01001 {
01002 if(!j)
01003 {
01004 j = __f.vcollect(_recursive_short_cut_walk(__w>>__it, __f));
01005 if(j < 0)
01006 break;
01007 }
01008 else
01009 j--;
01010 }
01011 return __f.vvalue();
01012 }
01013 else if(__w.is_sky())
01014 return __f.vvalue();
01015 else
01016 {
01017 int i = __f.preorder(*__w);
01018 if(i < 0)
01019 {
01020 __x = __f.short_cut_to(*__w);
01021 if(__x.is_sky())
01022 return __f.vvalue();
01023 }
01024 else
01025 __x = __w;
01026 if(i != 0)
01027 {
01028 int j = i-1;
01029 __e = __x.child_end();
01030 for(__it = __x.child_begin(); __it != __e; ++__it)
01031 {
01032 if(!j)
01033 {
01034 j = __f.collect(*__w, _recursive_short_cut_walk(__x>>__it, __f));
01035 if(j<0)
01036 break;
01037 }
01038 else
01039 j--;
01040 }
01041 }
01042
01043 __f.postorder(*__w);
01044 return __f.value();
01045 }
01046 }
01047
01049
01078 template <class _Walker, class _Visitor>
01079 typename _Visitor::return_value _recursive_short_cut_walk(_Walker __w,
01080 _Visitor __f)
01081 {
01082 _Walker __x;
01083 typename _Walker::children_iterator __it, __e;
01084
01085 if(__w.is_sky())
01086 return __f.vvalue();
01087 else
01088 {
01089 int i = __f.preorder(*__w);
01090 if(i < 0)
01091 {
01092 __x = __f.short_cut_to(*__w);
01093 if(__x.is_sky())
01094 return __f.vvalue();
01095 }
01096 else
01097 __x = __w;
01098 if(i != 0)
01099 {
01100 int j = i-1;
01101 __e = __x.child_end();
01102 for(__it = __x.child_begin(); __it != __e; ++__it)
01103 {
01104 if(!j)
01105 {
01106 j = __f.collect(*__w, _recursive_short_cut_walk(__x>>__it, __f));
01107 if(j<0)
01108 break;
01109 }
01110 else
01111 j--;
01112 }
01113 }
01114
01115 __f.postorder(*__w);
01116 return __f.value();
01117 }
01118 }
01119
01121
01126 template <class _Visitor, class _Walker>
01127 typename _Visitor::return_value evaluate(_Visitor __v, _Walker __start)
01128 {
01129 return recursive_short_cut_walk(__start, __v);
01130 }
01131
01132 }
01133
01134 #endif