PolyBoRi
|
00001 /* Copyright (c) 2005-2007 by The PolyBoRi Team */ 00002 00003 #include <polybori.h> 00004 #include <algorithm> 00005 #include <utility> 00006 #include <iostream> 00007 #include <boost/shared_ptr.hpp> 00008 #include "groebner_defs.h" 00009 #ifndef PBORI_GB_CACHE_H 00010 #define PBORI_GB_CACHE_H 00011 00012 00013 BEGIN_NAMESPACE_PBORIGB 00014 template<class idx_type1, class idx_type2> void set_up_translation_vectors(std::vector<idx_type1>& ring_2_0123, std::vector<idx_type2>& back_2_ring, const Exponent& used_variables){ 00015 BooleExponent::const_iterator it=used_variables.begin(); 00016 BooleExponent::const_iterator end=used_variables.end(); 00017 idx_type1 idx_0123=0; 00018 while(it!=end){ 00019 ring_2_0123[*it]=idx_0123; 00020 back_2_ring[idx_0123]=*it; 00021 idx_0123++; 00022 it++; 00023 } 00024 } 00025 00026 //template<class idx_type1, class idx_type2> void set_up_translation_vectors(std::vector<idx_type1>& ring_2_0123, std::vector<idx_type>& back_2_ring, const Exponent& used_variables); 00027 Polynomial translate_indices(const Polynomial& p, const std::vector<idx_type>& table); 00028 class CacheManager{ 00029 public: 00030 typedef Polynomial::poly_vec_map_type impl_type; 00031 00032 typedef std::vector<Polynomial> poly_vec_type; 00033 typedef boost::shared_ptr<poly_vec_type> res_type; 00034 typedef Polynomial::poly_vec_map_type::const_iterator impl_iterator_type; 00035 protected: 00036 impl_type impl; 00037 public: 00038 CacheManager(){ 00039 00040 } 00041 res_type lookup(const Polynomial& p, bool& succ){ 00042 int nv=p.ring().nVariables(); 00043 BooleExponent used_exp=p.usedVariablesExp(); 00044 std::vector<idx_type> back_2_ring(used_exp.size()); 00045 std::vector<idx_type> ring_2_0123(nv); 00046 00047 //PBORI_ASSERT(nv<255); 00048 set_up_translation_vectors(ring_2_0123,back_2_ring, used_exp); 00049 Polynomial p_t=translate_indices(p,ring_2_0123); 00050 impl_iterator_type it=impl.find(p_t); 00051 //cache_res_type res; 00052 if (it!=impl.end()){ 00053 succ=true; 00054 res_type res(new poly_vec_type(it->second)); 00055 00056 00057 for(poly_vec_type::size_type i=0;i<res->size();i++){ 00058 (*res)[i]=translate_indices((*res)[i],back_2_ring); 00059 } 00060 return res; 00061 } 00062 else{ 00063 succ=false; 00064 return res_type(); 00065 } 00066 } 00067 void insert(const Polynomial& key, const poly_vec_type& value){ 00068 int nv=key.ring().nVariables(); 00069 BooleExponent used_exp=key.usedVariablesExp(); 00070 std::vector<idx_type> back_2_ring(used_exp.size()); 00071 std::vector<idx_type> ring_2_0123(nv); 00072 set_up_translation_vectors(ring_2_0123,back_2_ring, used_exp); 00073 Polynomial key_t=translate_indices(key,ring_2_0123); 00074 poly_vec_type value_t(value); 00075 00076 for(poly_vec_type::size_type i=0;i<value_t.size();i++){ 00077 value_t[i]=translate_indices(value_t[i], ring_2_0123); 00078 } 00079 impl[key_t]=value_t; 00080 } 00081 00082 00083 }; 00084 END_NAMESPACE_PBORIGB 00085 #endif