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& v) const
00140 {
00141 (void) v;
00142 mln_invariant(0);
00143 return 0;
00144 }
00145
00146 template <typename S, typename T>
00147 template <typename F>
00148 inline
00149 lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v<F>& f)
00150 : vset_(vset)
00151 {
00152 const F& f_ = exact(f);
00153 n_ = vset.nvalues();
00154 vec_.reserve(n_);
00155 for (unsigned i = 0; i < n_; ++i)
00156 vec_.push_back(f_(vset[i]));
00157 }
00158
00159 template <typename S, typename T>
00160 template <typename V>
00161 inline
00162 lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f)
00163 : vset_(vset)
00164 {
00165 const fun::i2v::array<V>& f_ = exact(f);
00166 n_ = f_.size();
00167 vec_ = f_.std_vector();
00168 }
00169
00170 template <typename S, typename T>
00171 template <typename V>
00172 inline
00173 lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< util::array<V> >& f)
00174 : vset_(vset)
00175 {
00176 const util::array<V>& f_ = exact(f);
00177 n_ = f_.size();
00178 vec_ = f_.std_vector();
00179 }
00180
00181
00182 template <typename S, typename T>
00183 inline
00184 T
00185 lut_vec<S,T>::operator()(const mln_value(S)& val) const
00186 {
00187 mln_precondition(vset_.index_of(val) < n_);
00188 return vec_[vset_.index_of(val)];
00189 }
00190
00191 template <typename S, typename T>
00192 inline
00193 T
00194 lut_vec<S,T>::operator[](unsigned i) const
00195 {
00196 mln_precondition(i < nvalues());
00197 return vec_[i];
00198 }
00199
00200 template <typename S, typename T>
00201 inline
00202 unsigned
00203 lut_vec<S,T>::nvalues() const
00204 {
00205 return vec_.size();
00206 }
00207
00208 template <typename S, typename T>
00209 inline
00210 std::ostream&
00211 operator<<(std::ostream& ostr, const lut_vec<S,T>& lut)
00212 {
00213 ostr << "[ ";
00214 for (unsigned i = 0; i < lut.nvalues(); ++i)
00215 ostr << i << ':' << lut[i] << ' ';
00216 ostr << ']';
00217 return ostr;
00218 }
00219
00220 # endif // ! MLN_INCLUDE_ONLY
00221
00222 }
00223
00224
00225 }
00226
00227
00228 # include <mln/value/viter.hh>
00229
00230
00231 #endif // ! MLN_VALUE_LUT_VEC_HH