Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
lut_vec.hh
1 // Copyright (C) 2007, 2009, 2010, 2011 EPITA Research and Development
2 // Laboratory (LRDE)
3 //
4 // This file is part of Olena.
5 //
6 // Olena is free software: you can redistribute it and/or modify it under
7 // the terms of the GNU General Public License as published by the Free
8 // Software Foundation, version 2 of the License.
9 //
10 // Olena is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
17 //
18 // As a special exception, you may use this file as part of a free
19 // software project without restriction. Specifically, if other files
20 // instantiate templates or use macros or inline functions from this
21 // file, or you compile this file and link it with other files to produce
22 // an executable, this file does not by itself cause the resulting
23 // executable to be covered by the GNU General Public License. This
24 // exception does not however invalidate any other reasons why the
25 // executable file might be covered by the GNU General Public License.
26 
27 #ifndef MLN_VALUE_LUT_VEC_HH
28 # define MLN_VALUE_LUT_VEC_HH
29 
35 
36 # include <vector>
37 
38 # include <mln/core/concept/value_set.hh>
39 # include <mln/core/concept/function.hh>
40 # include <mln/trait/value_.hh>
41 
42 
43 namespace mln
44 {
45 
47  namespace fun {
48  namespace i2v {
49  template <typename T> class array;
50  } // end of namespace mln::fun::i2v
51  } // end of namespace mln::fun
52  namespace util{
53  template <typename T> class array;
54  } // end of namespace mln::util
55 
56  namespace value
57  {
58 
59  // Fwd decls.
60  template <typename S> struct fwd_viter_;
61  template <typename S> struct bkd_viter_;
62 
63 
65 
70  template <typename S, typename T>
71  struct lut_vec : public Value_Set< lut_vec<S,T> >
72  {
74  typedef T value;
75 
77  typedef fwd_viter_< lut_vec<S,T> > fwd_viter;
78 
80  typedef bkd_viter_< lut_vec<S,T> > bkd_viter;
81 
83  T operator[](unsigned i) const;
84 
86  unsigned nvalues() const;
87 
88  // Apply the look-up-table. FIXME: Doc!
89  T operator()(const mln_value(S)& val) const;
90 
92  bool has(const value& v) const;
93 
95  unsigned index_of(const value& v) const;
96 
100  template <typename F>
101  lut_vec(const S& vset, const Function_v2v<F>& f);
102 
104  template <typename V>
105  lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f);
106 
108  template <typename V>
109  lut_vec(const S& vset, const Function_v2v< util::array<V> >& f);
111 
112  protected:
113 
114  const S& vset_;
115  std::vector<T> vec_;
116  unsigned n_;
117  };
118 
119 
120  template <typename S, typename T>
121  std::ostream&
122  operator<<(std::ostream& ostr, const lut_vec<S,T>& lut);
123 
124 
125 # ifndef MLN_INCLUDE_ONLY
126 
127  template <typename S, typename T>
128  inline
129  bool
130  lut_vec<S,T>::has(const T&) const
131  {
132  mln_invariant(0); // FIXME
133  return false;
134  }
135 
136  template <typename S, typename T>
137  inline
138  unsigned
139  lut_vec<S,T>::index_of(const T& v) const
140  {
141  (void) v;
142  mln_invariant(0); // FIXME
143  return 0;
144  }
145 
146  template <typename S, typename T>
147  template <typename F>
148  inline
149  lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v<F>& f)
150  : vset_(vset)
151  {
152  const F& f_ = exact(f);
153  n_ = vset.nvalues();
154  vec_.reserve(n_);
155  for (unsigned i = 0; i < n_; ++i)
156  vec_.push_back(f_(vset[i]));
157  }
158 
159  template <typename S, typename T>
160  template <typename V>
161  inline
162  lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f)
163  : vset_(vset)
164  {
165  const fun::i2v::array<V>& f_ = exact(f);
166  n_ = f_.size();
167  vec_ = f_.std_vector();
168  }
169 
170  template <typename S, typename T>
171  template <typename V>
172  inline
174  : vset_(vset)
175  {
176  const util::array<V>& f_ = exact(f);
177  n_ = f_.size();
178  vec_ = f_.std_vector();
179  }
180 
181 
182  template <typename S, typename T>
183  inline
184  T
185  lut_vec<S,T>::operator()(const mln_value(S)& val) const
186  {
187  mln_precondition(vset_.index_of(val) < n_);
188  return vec_[vset_.index_of(val)];
189  }
190 
191  template <typename S, typename T>
192  inline
193  T
194  lut_vec<S,T>::operator[](unsigned i) const
195  {
196  mln_precondition(i < nvalues());
197  return vec_[i];
198  }
199 
200  template <typename S, typename T>
201  inline
202  unsigned
204  {
205  return vec_.size();
206  }
207 
208  template <typename S, typename T>
209  inline
210  std::ostream&
211  operator<<(std::ostream& ostr, const lut_vec<S,T>& lut)
212  {
213  ostr << "[ ";
214  for (unsigned i = 0; i < lut.nvalues(); ++i)
215  ostr << i << ':' << lut[i] << ' ';
216  ostr << ']';
217  return ostr;
218  }
219 
220 # endif // ! MLN_INCLUDE_ONLY
221 
222  } // end of namespace mln::value
223 
224 
225 } // end of namespace mln
226 
227 
228 # include <mln/value/viter.hh>
229 
230 
231 #endif // ! MLN_VALUE_LUT_VEC_HH