Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

semantics_delta.h

Go to the documentation of this file.
00001 // Semantics Delta implementation -*- C++ -*-
00002 
00003 // Copyright (C) 2001-2003 Hermann Schichl
00004 //
00005 // This file is part of the COCONUT API.  This library
00006 // is free software; you can redistribute it and/or modify it under the
00007 // terms of the Library GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // Library GNU General Public License for more details.
00015 
00016 // As a special exception, you may use this file as part of a free software
00017 // library without restriction.  Specifically, if other files instantiate
00018 // templates or use macros or inline functions from this file, or you compile
00019 // this file and link it with other files to produce an executable, this
00020 // file does not by itself cause the resulting executable to be covered by
00021 // the Library GNU General Public License.  This exception does not however
00022 // invalidate any other reasons why the executable file might be covered by
00023 // the Library GNU General Public License.
00024 
00027 #ifndef _SEMANTICS_DELTA_H_
00028 #define _SEMANTICS_DELTA_H_
00029 
00030 #include <api_delta.h>
00031 
00032 class semantics_undelta : public undelta_base
00033 {
00034 private:
00035   std::vector<unsigned int> indices;    // if all variables are being
00036                                         // updated this vector is empty
00037   std::vector<uint32_t> old_f_sem;      // original semantics are stored here
00038                                         // in order to restore the values
00039                                         // easily during backtracking
00040   // the coding is described in the semantics_delta class 
00041 public:
00042   semantics_undelta(const std::vector<unsigned int>& __i,
00043                 const std::vector<uint32_t>& __s) : undelta_base(),
00044                                                     indices(__i),
00045                                                     old_f_sem(__s) {}
00046   
00047   semantics_undelta(const std::vector<unsigned int>& __i) : undelta_base(),
00048                                                         indices(__i),
00049                                                         old_f_sem() {}
00050   
00051   semantics_undelta(const semantics_undelta& __d) : undelta_base(__d),
00052                                             indices(__d.indices),
00053                                             old_f_sem(__d.old_f_sem)
00054       {
00055 #if DEBUG_DELTA
00056         std::cerr << "Called semantics_undelta copy constructor" << std::endl;
00057 #endif
00058       }
00059 
00060   semantics_undelta* new_copy() const { return new semantics_undelta(*this); }
00061   void destroy_copy(semantics_undelta* __d) { delete __d; }
00062 
00063   bool unapply(work_node& _x) const;
00064 
00065   friend class semantics_delta;
00066 };
00067 
00068 class semantics_delta : public delta_base
00069 {
00070 private:
00071   std::vector<unsigned int> indices;         // if all variables are being
00072                                              // updated this vector is empty
00073   std::vector<uint32_t> new_f_sem;           // new semantics
00096 private:
00097   bool search(unsigned int _i, std::vector<unsigned int>::iterator& _xs);
00098   
00099   uint32_t ebool(uint32_t e, bool b, int idx) const
00100     // e contains a base 3 number. bi == 3^idx (for 0,1,2 which are all poss.)
00101     // e = a_2*3^2+a_1*3^1+a_0
00102         { uint32_t bi = idx == 0 ? 1 : (idx == 1 ? 3 : 9);
00103           uint32_t s = e % (bi*3);  // s = sum_{i=0:idx} a_i*3^i
00104           if(idx != 0) s -= e % bi; // s = a_idx*bi
00105           return e - s + (b ? 2*bi : bi); // e - s + (0,1,2)*3^idx is the new v.
00106         }
00107   uint32_t etristate(const tristate& t) const
00108         { return t == t_true ? 1 : (t == t_false ? 3 : 2); }
00109   uint32_t econvex_e(const convex_e& c) const
00110         { return (c == c_concave ? 3 : (uint32_t) c.i()) | (c.t() << 16); }
00111   uint32_t etype_annotation(const type_annotation& a) const
00112         { return (uint32_t) a; }
00113   uint32_t eactivity_descr(const activity_descr& a) const
00114         { return (uint32_t) a; }
00115   
00116   void dbool(uint32_t c, bool &b, int idx) const
00117     // c contains a base 3 number. bi == 3^idx (for 0,1,2 which are all poss.)
00118     // c = a_2*3^2+a_1*3^1+a_0
00119         { uint32_t bi = idx == 0 ? 1 : (idx == 1 ? 3 : 9);
00120           uint32_t s = c % (bi*3);  // s = sum_{i=0:idx} a_i*3^i
00121           if(idx != 0) s -= c % bi; // s = a_idx*bi
00122           if(s)                     // a_idx != 0
00123             b = (s != bi);         // a_idx == 1 => F, a_idx == 2 => T
00124         }
00125   void dtristate(uint32_t c, tristate& t) const
00126         { if(c == 3) t = t_false; else if(c != 0) t = (tristate) c; }
00127   void dconvex_e(uint32_t c, convex_e& ce) const
00128         { uint16_t t = c >> 16;
00129           ce = t; c = c&0xffff;
00130           if(c == 3) ce = c_concave; else if(c != 0) ce = (convex_info) c; }
00131   void dtype_annotation(uint32_t c, type_annotation& a) const
00132         { a = (type_annotation) c; }
00133   void dactivity_descr(uint32_t c, activity_descr& a) const
00134         { if(c) a = (activity_descr) c; }
00135   
00136 public:
00137   uint32_t encode_convex(uint32_t e, const convex_e& c) const
00138         { return (e & ~7) | econvex_e(c) | (1<<2); }
00139   uint32_t encode_activity(uint32_t e, const activity_descr& a) const
00140         { return (e & ~(7 << 3)) | (eactivity_descr(a)<<3); }
00141   uint32_t encode_is_at_any_bound(uint32_t e, bool b) const
00142         { return (e & ~(31 << 6)) | (ebool((e>>6)&31, b, 0)<<6); }
00143   uint32_t encode_integer(uint32_t e, bool b) const
00144         { return (e & ~(31 << 6)) | (ebool((e>>6)&31, b, 1)<<6); }
00145   uint32_t encode_hard(uint32_t e, bool b) const
00146         { return (e & ~(31 << 6)) | (ebool((e>>6)&31, b, 2)<<6); }
00147   uint32_t encode_separable(uint32_t e, const tristate& t) const
00148         { return (e & ~(3 << 11)) | (etristate(t)<<11); }
00149   uint32_t encode_type(uint32_t e, const type_annotation& a) const
00150         { return (e & ~(3 << 13)) | (etype_annotation(a)<<13) | (1<<15); }
00151   uint32_t encode(const semantics& s) const
00152         { uint32_t e(0);
00153           e = encode_convex(e, s.property_flags.c_info);
00154           e = encode_activity(e, s.property_flags.act);
00155           e = encode_separable(e, s.property_flags.separable);
00156           e = encode_is_at_any_bound(e, s.property_flags.is_at_any_bound);
00157           e = encode_integer(e, s.annotation_flags.integer);
00158           e = encode_type(e, s.annotation_flags.type);
00159           e = encode_hard(e, s.annotation_flags.hard);
00160           return e;
00161         }
00162 
00163   void decode_convex(uint32_t e, convex_e& c) const
00164         { if(e & (1<<2)) dconvex_e(e & 0xffff0003, c); }
00165   void decode_activity(uint32_t e, activity_descr& a) const
00166         { dactivity_descr((e>>3) & 7, a); }
00167   void decode_is_at_any_bound(uint32_t e, bool& b) const
00168         { dbool((e>>6) & 31, b, 0); }
00169   void decode_integer(uint32_t e, bool& b) const
00170         { dbool((e>>6) & 31, b, 1); }
00171   void decode_hard(uint32_t e, bool& b) const
00172         { dbool((e>>6) & 31, b, 2); }
00173   void decode_separable(uint32_t e, tristate& t) const
00174         { dtristate((e>>11) & 3, t); }
00175   void decode_type(uint32_t e, type_annotation& a) const
00176         { if(e & (1<<15)) dtype_annotation((e>>13) & 3, a); }
00177   void decode(uint32_t e, semantics& s) const
00178         {
00179           decode_convex(e, s.property_flags.c_info);
00180           decode_activity(e, s.property_flags.act);
00181           decode_separable(e, s.property_flags.separable);
00182           decode_is_at_any_bound(e, s.property_flags.is_at_any_bound);
00183           decode_integer(e, s.annotation_flags.integer);
00184           decode_type(e, s.annotation_flags.type);
00185           decode_hard(e, s.annotation_flags.hard);
00186         }
00187 
00188 public:
00189   semantics_delta(const std::vector<unsigned int>& __i,
00190                   const std::vector<uint32_t>& __b);
00191   
00192   semantics_delta(unsigned int __i, uint32_t __b)
00193                     : delta_base("change semantics"), indices(1,__i),
00194                       new_f_sem(1,__b) {}
00195  
00196   semantics_delta() : delta_base("change semantics"), indices(), new_f_sem() {} 
00197   semantics_delta(const semantics_delta& __d)
00198                     : delta_base(__d), indices(__d.indices),
00199                       new_f_sem(__d.new_f_sem)
00200     {
00201 #if DEBUG_DELTA
00202         std::cerr << "Called semantics_delta copy constructor" << std::endl;
00203 #endif
00204     }
00205 
00206   semantics_delta* new_copy() const { return new semantics_delta(*this); }
00207   void destroy_copy(semantics_delta* __d) { delete __d; }
00208 
00209   void set(const std::vector<unsigned int>& _i,
00210            const std::vector<semantics>& _s);
00211   void set(const std::vector<unsigned int>& _i,
00212            const std::vector<uint32_t>& _s);
00213   void set(unsigned int _i, const semantics& _s);
00214   void set_convex(const std::vector<unsigned int>& _i,
00215                   const std::vector<convex_e>& c);
00216   void set_convex(unsigned int _i, const convex_e& c);
00217   void set_activity(const std::vector<unsigned int>& _i,
00218                   const std::vector<activity_descr>& a);
00219   void set_activity(unsigned int _i, const activity_descr& a);
00220   void set_separable(const std::vector<unsigned int>& _i,
00221                      const std::vector<tristate>& t);
00222   void set_separable(unsigned int _i, const tristate& t);
00223   void set_is_at_any_bound(const std::vector<unsigned int>& _i,
00224                            const std::vector<bool>& b);
00225   void set_is_at_any_bound(unsigned int _i, bool b);
00226   void set_integer(const std::vector<unsigned int>& _i,
00227                    const std::vector<bool>& b);
00228   void set_integer(unsigned int _i, bool b);
00229   void set_hard(const std::vector<unsigned int>& _i,
00230                 const std::vector<bool>& b);
00231   void set_hard(unsigned int _i, bool b);
00232   void set_type(const std::vector<unsigned int>& _i,
00233                 const std::vector<type_annotation>& a);
00234   void set_type(unsigned int _i, const type_annotation& a);
00235 
00236   bool apply(work_node& _x, undelta_base*& _u) const;
00237 
00238   friend class semantics_undelta;
00239 };
00240 
00241 #endif /* _SEMANTICS_DELTA_H_ */

Generated on Tue Nov 4 01:57:58 2003 for COCONUT API by doxygen1.2.18