• Main Page
  • Related Pages
  • Modules
  • Namespaces
  • Classes
  • Files
  • File List

lut_vec.hh

00001 // Copyright (C) 2007, 2009, 2010 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       mln_invariant(0); // FIXME
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   } // end of namespace mln::value
00222 
00223 
00224 } // end of namespace mln
00225 
00226 
00227 # include <mln/value/viter.hh>
00228 
00229 
00230 #endif // ! MLN_VALUE_LUT_VEC_HH

Generated on Tue Oct 4 2011 15:24:03 for Milena (Olena) by  doxygen 1.7.1