$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends 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 
78 
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 
99 
101  template <typename F>
102  lut_vec(const S& vset, const Function_v2v<F>& f);
103 
105  template <typename V>
106  lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f);
107 
109  template <typename V>
110  lut_vec(const S& vset, const Function_v2v< util::array<V> >& f);
111 
113 
114  protected:
115 
116  const S& vset_;
117  std::vector<T> vec_;
118  unsigned n_;
119  };
120 
121 
122  template <typename S, typename T>
123  std::ostream&
124  operator<<(std::ostream& ostr, const lut_vec<S,T>& lut);
125 
126 
127 # ifndef MLN_INCLUDE_ONLY
128 
129  template <typename S, typename T>
130  inline
131  bool
132  lut_vec<S,T>::has(const T&) const
133  {
134  mln_invariant(0); // FIXME
135  return false;
136  }
137 
138  template <typename S, typename T>
139  inline
140  unsigned
141  lut_vec<S,T>::index_of(const T& v) const
142  {
143  (void) v;
144  mln_invariant(0); // FIXME
145  return 0;
146  }
147 
148  template <typename S, typename T>
149  template <typename F>
150  inline
151  lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v<F>& f)
152  : vset_(vset)
153  {
154  const F& f_ = exact(f);
155  n_ = vset.nvalues();
156  vec_.reserve(n_);
157  for (unsigned i = 0; i < n_; ++i)
158  vec_.push_back(f_(vset[i]));
159  }
160 
161  template <typename S, typename T>
162  template <typename V>
163  inline
164  lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< fun::i2v::array<V> >& f)
165  : vset_(vset)
166  {
167  const fun::i2v::array<V>& f_ = exact(f);
168  n_ = f_.size();
169  vec_ = f_.std_vector();
170  }
171 
172  template <typename S, typename T>
173  template <typename V>
174  inline
175  lut_vec<S,T>::lut_vec(const S& vset, const Function_v2v< util::array<V> >& f)
176  : vset_(vset)
177  {
178  const util::array<V>& f_ = exact(f);
179  n_ = f_.size();
180  vec_ = f_.std_vector();
181  }
182 
183 
184  template <typename S, typename T>
185  inline
186  T
187  lut_vec<S,T>::operator()(const mln_value(S)& val) const
188  {
189  mln_precondition(vset_.index_of(val) < n_);
190  return vec_[vset_.index_of(val)];
191  }
192 
193  template <typename S, typename T>
194  inline
195  T
196  lut_vec<S,T>::operator[](unsigned i) const
197  {
198  mln_precondition(i < nvalues());
199  return vec_[i];
200  }
201 
202  template <typename S, typename T>
203  inline
204  unsigned
205  lut_vec<S,T>::nvalues() const
206  {
207  return vec_.size();
208  }
209 
210  template <typename S, typename T>
211  inline
212  std::ostream&
213  operator<<(std::ostream& ostr, const lut_vec<S,T>& lut)
214  {
215  ostr << "[ ";
216  for (unsigned i = 0; i < lut.nvalues(); ++i)
217  ostr << i << ':' << lut[i] << ' ';
218  ostr << ']';
219  return ostr;
220  }
221 
222 # endif // ! MLN_INCLUDE_ONLY
223 
224  } // end of namespace mln::value
225 
226 
227 } // end of namespace mln
228 
229 
230 # include <mln/value/viter.hh>
231 
232 
233 #endif // ! MLN_VALUE_LUT_VEC_HH