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

lut_vec.hh

00001 // Copyright (C) 2007, 2009 EPITA Research and Development Laboratory (LRDE)
00002 //
00003 // This file is part of Olena.
00004 //
00005 // Olena is free software: you can redistribute it and/or modify it under
00006 // the terms of the GNU General Public License as published by the Free
00007 // Software Foundation, version 2 of the License.
00008 //
00009 // Olena is distributed in the hope that it will be useful,
00010 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00011 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012 // General Public License for more details.
00013 //
00014 // You should have received a copy of the GNU General Public License
00015 // along with Olena.  If not, see <http://www.gnu.org/licenses/>.
00016 //
00017 // As a special exception, you may use this file as part of a free
00018 // software project without restriction.  Specifically, if other files
00019 // instantiate templates or use macros or inline functions from this
00020 // file, or you compile this file and link it with other files to produce
00021 // an executable, this file does not by itself cause the resulting
00022 // executable to be covered by the GNU General Public License.  This
00023 // exception does not however invalidate any other reasons why the
00024 // executable file might be covered by the GNU General Public License.
00025 
00026 #ifndef MLN_VALUE_LUT_VEC_HH
00027 # define MLN_VALUE_LUT_VEC_HH
00028 
00034 
00035 # include <vector>
00036 
00037 # include <mln/core/concept/value_set.hh>
00038 # include <mln/core/concept/function.hh>
00039 # include <mln/trait/value_.hh>
00040 
00041 
00042 namespace mln
00043 {
00044 
00046   namespace fun {
00047     namespace i2v {
00048       template <typename T> class array;
00049     } // end of namespace mln::fun::i2v
00050   } // end of namespace mln::fun
00051   namespace util{
00052     template <typename T> class array;
00053   } // end of namespace mln::util
00054 
00055   namespace value
00056   {
00057 
00058     // Fwd decls.
00059     template <typename S> struct fwd_viter_;
00060     template <typename S> struct bkd_viter_;
00061 
00062 
00064 
00069     template <typename S, typename T>
00070     struct lut_vec : public Value_Set< lut_vec<S,T> >
00071     {
00073       typedef T value;
00074 
00076       typedef fwd_viter_< lut_vec<S,T> > fwd_viter;
00077 
00079       typedef bkd_viter_< lut_vec<S,T> > bkd_viter;
00080 
00082       T operator[](unsigned i) const;
00083 
00085       unsigned nvalues() const;
00086 
00087       // Apply the look-up-table.  FIXME: Doc!
00088       T operator()(const mln_value(S)& val) const;
00089 
00091       bool has(const value& v) const;
00092 
00094       unsigned index_of(const value& v) const;
00095 
00099       template <typename F>
00100       lut_vec(const S& vset, const Function_v2v<F>& f);
00101 
00103       template <typename V>
00104       lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f);
00105 
00107       template <typename V>
00108       lut_vec(const S& vset, const Function_v2v< util::array<V> >& f);
00110 
00111     protected:
00112 
00113       const S& vset_;
00114       std::vector<T> vec_;
00115       unsigned n_;
00116     };
00117 
00118 
00119     template <typename S, typename T>
00120     std::ostream&
00121     operator<<(std::ostream& ostr, const lut_vec<S,T>& lut);
00122 
00123 
00124 # ifndef MLN_INCLUDE_ONLY
00125 
00126     template <typename S, typename T>
00127     inline
00128     bool
00129     lut_vec<S,T>::has(const T&) const
00130     {
00131       mln_invariant(0); // FIXME
00132       return false;
00133     }
00134 
00135     template <typename S, typename T>
00136     inline
00137     unsigned
00138     lut_vec<S,T>::index_of(const T& v) const
00139     {
00140       mln_invariant(0); // FIXME
00141       return 0;
00142     }
00143 
00144     template <typename S, typename T>
00145     template <typename F>
00146     inline
00147     lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v<F>& f)
00148       : vset_(vset)
00149     {
00150       const F& f_ = exact(f);
00151       n_ = vset.nvalues();
00152       vec_.reserve(n_);
00153       for (unsigned i = 0; i < n_; ++i)
00154         vec_.push_back(f_(vset[i]));
00155     }
00156 
00157     template <typename S, typename T>
00158     template <typename V>
00159     inline
00160     lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f)
00161       : vset_(vset)
00162     {
00163       const fun::i2v::array<V>& f_ = exact(f);
00164       n_ = f_.size();
00165       vec_ = f_.std_vector();
00166     }
00167 
00168     template <typename S, typename T>
00169     template <typename V>
00170     inline
00171     lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< util::array<V> >& f)
00172       : vset_(vset)
00173     {
00174       const util::array<V>& f_ = exact(f);
00175       n_ = f_.size();
00176       vec_ = f_.std_vector();
00177     }
00178 
00179 
00180     template <typename S, typename T>
00181     inline
00182     T
00183     lut_vec<S,T>::operator()(const mln_value(S)& val) const
00184     {
00185       mln_precondition(vset_.index_of(val) < n_);
00186       return vec_[vset_.index_of(val)];
00187     }
00188 
00189     template <typename S, typename T>
00190     inline
00191     T
00192     lut_vec<S,T>::operator[](unsigned i) const
00193     {
00194       mln_precondition(i < nvalues());
00195       return vec_[i];
00196     }
00197 
00198     template <typename S, typename T>
00199     inline
00200     unsigned
00201     lut_vec<S,T>::nvalues() const
00202     {
00203       return vec_.size();
00204     }
00205 
00206     template <typename S, typename T>
00207     inline
00208     std::ostream&
00209     operator<<(std::ostream& ostr, const lut_vec<S,T>& lut)
00210     {
00211       ostr << "[ ";
00212       for (unsigned i = 0; i < lut.nvalues(); ++i)
00213         ostr << i << ':' << lut[i] << ' ';
00214       ostr << ']';
00215       return ostr;
00216     }
00217 
00218 # endif // ! MLN_INCLUDE_ONLY
00219 
00220   } // end of namespace mln::value
00221 
00222 
00223 } // end of namespace mln
00224 
00225 
00226 # include <mln/value/viter.hh>
00227 
00228 
00229 #endif // ! MLN_VALUE_LUT_VEC_HH

Generated on Thu Sep 8 2011 18:32:05 for Milena (Olena) by  doxygen 1.7.1