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 
00026 #ifndef MLN_VALUE_VALUE_ARRAY_HH
00027 # define MLN_VALUE_VALUE_ARRAY_HH
00028 
00032 
00033 # include <mln/value/set.hh>
00034 
00035 
00036 namespace mln
00037 {
00038 
00039   namespace value
00040   {
00041 
00042 
00044     template <typename T, typename V>
00045     struct value_array
00046     {
00047       enum {
00048         nvalues = mln_card(T)
00049       };
00050 
00053       value_array();
00054       value_array(const V& v);
00055       value_array(const value_array<T, V>& other);
00056       value_array& operator=(const value_array<T, V>& other);
00058 
00061       const V& operator()(const T& v) const;
00062       V& operator()(const T& v);
00064 
00067       const V& operator[](unsigned i) const;
00068       V& operator[](unsigned i);
00070 
00072       const mln::value::set<T>& vset() const;
00073 
00074     protected:
00075 
00076       const mln::value::set<T>& s_;
00077       V v_[nvalues];
00078     };
00079 
00080 
00081     template <typename T, typename V>
00082     std::ostream& operator<<(std::ostream& ostr, const value_array<T, V>& a);
00083 
00084 
00085 # ifndef MLN_INCLUDE_ONLY
00086 
00087     template <typename T, typename V>
00088     inline
00089     value_array<T,V>::value_array()
00090       : s_ (mln::value::set<T>::the())
00091     {
00092       typedef value::internal::iterable_set< T, set<T> > U;
00093       mlc_is(set<T>, U)::check();
00094     }
00095 
00096     template <typename T, typename V>
00097     inline
00098     value_array<T,V>::value_array(const V& v)
00099       : s_(mln::value::set<T>::the())
00100     {
00101       typedef value::internal::iterable_set< T, set<T> > U;
00102       mlc_is(set<T>, U)::check();
00103 
00104       memset(v_, v, nvalues * sizeof(V));
00105     }
00106 
00107     template <typename T, typename V>
00108     inline
00109     value_array<T,V>::value_array(const value_array<T, V>& other)
00110       : s_(other.s_)
00111     {
00112       memcpy(v_, other.v_, nvalues * sizeof(V));
00113     }
00114 
00115     template <typename T, typename V>
00116     inline
00117     value_array<T,V>&
00118     value_array<T,V>::operator=(const value_array<T, V>& other)
00119     {
00120       if (&other != this)
00121         memcpy(v_, other.v_, nvalues * sizeof(V));
00122       return *this;
00123     }
00124 
00125     template <typename T, typename V>
00126     inline
00127     const V&
00128     value_array<T,V>::operator()(const T& v) const
00129     {
00130       return v_[s_.index_of(v)];
00131     }
00132 
00133     template <typename T, typename V>
00134     inline
00135     V&
00136     value_array<T,V>::operator()(const T& v)
00137     {
00138       return v_[s_.index_of(v)];
00139     }
00140 
00141     template <typename T, typename V>
00142     inline
00143     const mln::value::set<T>&
00144     value_array<T,V>::vset() const
00145     {
00146       return s_;
00147     }
00148 
00149     template <typename T, typename V>
00150     inline
00151     const V&
00152     value_array<T,V>::operator[](unsigned i) const
00153     {
00154       mln_precondition(i < nvalues);
00155       return v_[i];
00156     }
00157 
00158     template <typename T, typename V>
00159     inline
00160     V&
00161     value_array<T,V>::operator[](unsigned i)
00162     {
00163       mln_precondition(i < nvalues);
00164       return v_[i];
00165     }
00166 
00167     template <typename T, typename V>
00168     inline
00169     std::ostream& operator<<(std::ostream& ostr, const value_array<T,V>& a)
00170     {
00171       mln_viter(mln::value::set<T>) v(a.vset());
00172       for_all(v)
00173         ostr << v << ':' << h(v) << ' ';
00174       return ostr;
00175     }
00176 
00177 # endif // ! MLN_INCLUDE_ONLY
00178 
00179   } 
00180 
00181 } 
00182 
00183 
00184 #endif // ! MLN_VALUE_VALUE_ARRAY_HH