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 
00027 #ifndef MLN_VALUE_LUT_VEC_HH
00028 # define MLN_VALUE_LUT_VEC_HH
00029 
00035 
00036 # include <vector>
00037 
00038 # include <mln/core/concept/value_set.hh>
00039 # include <mln/core/concept/function.hh>
00040 # include <mln/trait/value_.hh>
00041 
00042 
00043 namespace mln
00044 {
00045 
00047   namespace fun {
00048     namespace i2v {
00049       template <typename T> class array;
00050     } 
00051   } 
00052   namespace util{
00053     template <typename T> class array;
00054   } 
00055 
00056   namespace value
00057   {
00058 
00059     
00060     template <typename S> struct fwd_viter_;
00061     template <typename S> struct bkd_viter_;
00062 
00063 
00065 
00070     template <typename S, typename T>
00071     struct lut_vec : public Value_Set< lut_vec<S,T> >
00072     {
00074       typedef T value;
00075 
00077       typedef fwd_viter_< lut_vec<S,T> > fwd_viter;
00078 
00080       typedef bkd_viter_< lut_vec<S,T> > bkd_viter;
00081 
00083       T operator[](unsigned i) const;
00084 
00086       unsigned nvalues() const;
00087 
00088       
00089       T operator()(const mln_value(S)& val) const;
00090 
00092       bool has(const value& v) const;
00093 
00095       unsigned index_of(const value& v) const;
00096 
00100       template <typename F>
00101       lut_vec(const S& vset, const Function_v2v<F>& f);
00102 
00104       template <typename V>
00105       lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f);
00106 
00108       template <typename V>
00109       lut_vec(const S& vset, const Function_v2v< util::array<V> >& f);
00111 
00112     protected:
00113 
00114       const S& vset_;
00115       std::vector<T> vec_;
00116       unsigned n_;
00117     };
00118 
00119 
00120     template <typename S, typename T>
00121     std::ostream&
00122     operator<<(std::ostream& ostr, const lut_vec<S,T>& lut);
00123 
00124 
00125 # ifndef MLN_INCLUDE_ONLY
00126 
00127     template <typename S, typename T>
00128     inline
00129     bool
00130     lut_vec<S,T>::has(const T&) const
00131     {
00132       mln_invariant(0); 
00133       return false;
00134     }
00135 
00136     template <typename S, typename T>
00137     inline
00138     unsigned
00139     lut_vec<S,T>::index_of(const T& ) const
00140     {
00141       mln_invariant(0); 
00142       return 0;
00143     }
00144 
00145     template <typename S, typename T>
00146     template <typename F>
00147     inline
00148     lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v<F>& f)
00149       : vset_(vset)
00150     {
00151       const F& f_ = exact(f);
00152       n_ = vset.nvalues();
00153       vec_.reserve(n_);
00154       for (unsigned i = 0; i < n_; ++i)
00155         vec_.push_back(f_(vset[i]));
00156     }
00157 
00158     template <typename S, typename T>
00159     template <typename V>
00160     inline
00161     lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f)
00162       : vset_(vset)
00163     {
00164       const fun::i2v::array<V>& f_ = exact(f);
00165       n_ = f_.size();
00166       vec_ = f_.std_vector();
00167     }
00168 
00169     template <typename S, typename T>
00170     template <typename V>
00171     inline
00172     lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< util::array<V> >& f)
00173       : vset_(vset)
00174     {
00175       const util::array<V>& f_ = exact(f);
00176       n_ = f_.size();
00177       vec_ = f_.std_vector();
00178     }
00179 
00180 
00181     template <typename S, typename T>
00182     inline
00183     T
00184     lut_vec<S,T>::operator()(const mln_value(S)& val) const
00185     {
00186       mln_precondition(vset_.index_of(val) < n_);
00187       return vec_[vset_.index_of(val)];
00188     }
00189 
00190     template <typename S, typename T>
00191     inline
00192     T
00193     lut_vec<S,T>::operator[](unsigned i) const
00194     {
00195       mln_precondition(i < nvalues());
00196       return vec_[i];
00197     }
00198 
00199     template <typename S, typename T>
00200     inline
00201     unsigned
00202     lut_vec<S,T>::nvalues() const
00203     {
00204       return vec_.size();
00205     }
00206 
00207     template <typename S, typename T>
00208     inline
00209     std::ostream&
00210     operator<<(std::ostream& ostr, const lut_vec<S,T>& lut)
00211     {
00212       ostr << "[ ";
00213       for (unsigned i = 0; i < lut.nvalues(); ++i)
00214         ostr << i << ':' << lut[i] << ' ';
00215       ostr << ']';
00216       return ostr;
00217     }
00218 
00219 # endif // ! MLN_INCLUDE_ONLY
00220 
00221   } 
00222 
00223 
00224 } 
00225 
00226 
00227 # include <mln/value/viter.hh>
00228 
00229 
00230 #endif // ! MLN_VALUE_LUT_VEC_HH