$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
histo/array.hh
1 // Copyright (C) 2007, 2008, 2009, 2012 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_HISTO_ARRAY_HH
28 # define MLN_HISTO_ARRAY_HH
29 
33 
34 # include <vector>
35 # include <algorithm>
36 
37 # include <mln/value/set.hh>
38 
39 // For conversion
40 # include <mln/convert/from_to.hh>
41 # include <mln/make/box1d.hh>
42 
43 
44 namespace mln
45 {
46 
47  // Forward declaration
48  template <typename T> struct image1d;
49 
50  namespace histo
51  {
52 
53 
55  template <typename T>
56  struct array
57  {
58  typedef T value;
59 
60  array();
61 
62  array(const array& other);
63  array& operator=(const array& other);
64 
65  void clear();
66 
67  unsigned operator()(const T& v) const;
68  unsigned& operator()(const T& v);
69 
70  const std::vector<unsigned>& vect() const;
71  const mln::value::set<T>& vset() const;
72  unsigned operator[](unsigned i) const;
73  unsigned& operator[](unsigned i);
74 
75  unsigned nvalues() const;
76 
77  protected:
78 
80  std::vector<unsigned> h_;
81  };
82 
83 
84  template <typename T>
85  std::ostream& operator<<(std::ostream& ostr, const array<T>& h);
86 
88  template <typename V, typename T>
89  void from_to_(const array<V>& from, image1d<T>& to);
90 
91 # ifndef MLN_INCLUDE_ONLY
92 
93  template <typename T>
94  inline
96  : s_(mln::value::set<T>::the()),
97  h_(s_.nvalues(), 0)
98  {
99  clear();
100  }
101 
102  template <typename T>
103  inline
104  array<T>::array(const array& other)
105  : s_(other.s_),
106  h_(other.h_)
107  {
108  }
109 
110  template <typename T>
111  inline
112  array<T>&
113  array<T>::operator=(const array& other)
114  {
115  if (&other == this)
116  return *this;
117  h_ = other.h_;
118  return *this;
119  }
120 
121  template <typename T>
122  inline
123  void
124  array<T>::clear()
125  {
126  std::fill(h_.begin(), h_.end(), 0);
127  }
128 
129  template <typename T>
130  inline
131  unsigned
132  array<T>::operator()(const T& v) const
133  {
134  return h_[s_.index_of(v)];
135  }
136 
137  template <typename T>
138  inline
139  unsigned&
140  array<T>::operator()(const T& v)
141  {
142  return h_[s_.index_of(v)];
143  }
144 
145  template <typename T>
146  inline
147  const mln::value::set<T>&
148  array<T>::vset() const
149  {
150  return s_;
151  }
152 
153  template <typename T>
154  inline
155  unsigned
156  array<T>::operator[](unsigned i) const
157  {
158  mln_precondition(i < s_.nvalues());
159  return h_[i];
160  }
161 
162  template <typename T>
163  inline
164  unsigned&
165  array<T>::operator[](unsigned i)
166  {
167  mln_precondition(i < s_.nvalues());
168  return h_[i];
169  }
170 
171  template <typename T>
172  inline
173  const std::vector<unsigned>&
174  array<T>::vect() const
175  {
176  return h_;
177  }
178 
179  template <typename T>
180  inline
181  unsigned array<T>::nvalues() const
182  {
183  return h_.size();
184  }
185 
186  template <typename T>
187  inline
188  std::ostream& operator<<(std::ostream& ostr, const array<T>& h)
189  {
190  mln_viter(mln::value::set<T>) v(h.vset());
191  for_all(v)
192  if (h(v) != 0)
193  ostr << v << ':' << h(v) << ' ';
194  return ostr;
195  }
196 
197 
198  // Conversions
199 
200  template <typename V, typename T>
201  inline
202  void
203  from_to_(const array<V>& from, image1d<T>& to)
204  {
205  // FIXME: The code should looks like:
206 
207 // box1d b(point1d(mln_min(V)), point1d(mln_max(V)));
208 // ima.init_(b, 0);
209 // for_all(v)
210 // from_to(h(v), ima.at_( index_of(v) ));
211  to.init_(make::box1d(from.nvalues()));
212  for (unsigned i = 0; i < from.nvalues(); ++i)
213  convert::from_to(from[i], to(point1d(i)));
214  }
215 
216 # endif // ! MLN_INCLUDE_ONLY
217 
218  } // end of namespace mln::histo
219 
220 } // end of namespace mln
221 
222 
223 #endif // ! MLN_HISTO_ARRAY_HH