Milena (Olena)  User documentation 2.0a Id
lut_vec.hh
00001 // Copyright (C) 2007, 2009, 2010, 2011 EPITA Research and Development
00002 // Laboratory (LRDE)
00003 //
00004 // This file is part of Olena.
00005 //
00006 // Olena is free software: you can redistribute it and/or modify it under
00007 // the terms of the GNU General Public License as published by the Free
00008 // Software Foundation, version 2 of the License.
00009 //
00010 // Olena is distributed in the hope that it will be useful,
00011 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013 // General Public License for more details.
00014 //
00015 // You should have received a copy of the GNU General Public License
00016 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00017 //
00018 // As a special exception, you may use this file as part of a free
00019 // software project without restriction.  Specifically, if other files
00020 // instantiate templates or use macros or inline functions from this
00021 // file, or you compile this file and link it with other files to produce
00022 // an executable, this file does not by itself cause the resulting
00023 // executable to be covered by the GNU General Public License.  This
00024 // exception does not however invalidate any other reasons why the
00025 // executable file might be covered by the GNU General Public License.
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     } // end of namespace mln::fun::i2v
00051   } // end of namespace mln::fun
00052   namespace util{
00053     template <typename T> class array;
00054   } // end of namespace mln::util
00055 
00056   namespace value
00057   {
00058 
00059     // Fwd decls.
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       // Apply the look-up-table.  FIXME: Doc!
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); // FIXME
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); // FIXME
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   } // end of namespace mln::value
00223 
00224 
00225 } // end of namespace mln
00226 
00227 
00228 # include <mln/value/viter.hh>
00229 
00230 
00231 #endif // ! MLN_VALUE_LUT_VEC_HH
 All Classes Namespaces Functions Variables Typedefs Enumerator