Milena (Olena)  User documentation 2.0a Id
 All Classes Namespaces Functions Variables Typedefs Enumerator Groups Pages
histo/array.hh
1 // Copyright (C) 2007, 2008, 2009 EPITA Research and Development Laboratory (LRDE)
2 //
3 // This file is part of Olena.
4 //
5 // Olena is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free
7 // Software Foundation, version 2 of the License.
8 //
9 // Olena is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // General Public License for more details.
13 //
14 // You should have received a copy of the GNU General Public License
15 // along with Olena. If not, see <http://www.gnu.org/licenses/>.
16 //
17 // As a special exception, you may use this file as part of a free
18 // software project without restriction. Specifically, if other files
19 // instantiate templates or use macros or inline functions from this
20 // file, or you compile this file and link it with other files to produce
21 // an executable, this file does not by itself cause the resulting
22 // executable to be covered by the GNU General Public License. This
23 // exception does not however invalidate any other reasons why the
24 // executable file might be covered by the GNU General Public License.
25 
26 #ifndef MLN_HISTO_ARRAY_HH
27 # define MLN_HISTO_ARRAY_HH
28 
32 
33 # include <vector>
34 # include <algorithm>
35 
36 # include <mln/value/set.hh>
37 
38 
39 namespace mln
40 {
41 
42  namespace histo
43  {
44 
45 
47  template <typename T>
48  struct array
49  {
50  typedef T value;
51 
52  array();
53 
54  array(const array& other);
55  array& operator=(const array& other);
56 
57  void clear();
58 
59  unsigned operator()(const T& v) const;
60  unsigned& operator()(const T& v);
61 
62  const std::vector<unsigned>& vect() const;
63  const mln::value::set<T>& vset() const;
64  unsigned operator[](unsigned i) const;
65  unsigned& operator[](unsigned i);
66 
67  unsigned nvalues() const;
68 
69  protected:
70 
71  const mln::value::set<T>& s_;
72  std::vector<unsigned> h_;
73  };
74 
75 
76  template <typename T>
77  std::ostream& operator<<(std::ostream& ostr, const array<T>& h);
78 
79 
80 
81 
82 # ifndef MLN_INCLUDE_ONLY
83 
84  template <typename T>
85  inline
87  : s_(mln::value::set<T>::the()),
88  h_(s_.nvalues(), 0)
89  {
90  clear();
91  }
92 
93  template <typename T>
94  inline
95  array<T>::array(const array& other)
96  : s_(other.s_),
97  h_(other.h_)
98  {
99  }
100 
101  template <typename T>
102  inline
103  array<T>&
104  array<T>::operator=(const array& other)
105  {
106  if (&other == this)
107  return *this;
108  h_ = other.h_;
109  return *this;
110  }
111 
112  template <typename T>
113  inline
114  void
115  array<T>::clear()
116  {
117  std::fill(h_.begin(), h_.end(), 0);
118  }
119 
120  template <typename T>
121  inline
122  unsigned
123  array<T>::operator()(const T& v) const
124  {
125  return h_[s_.index_of(v)];
126  }
127 
128  template <typename T>
129  inline
130  unsigned&
131  array<T>::operator()(const T& v)
132  {
133  return h_[s_.index_of(v)];
134  }
135 
136  template <typename T>
137  inline
138  const mln::value::set<T>&
139  array<T>::vset() const
140  {
141  return s_;
142  }
143 
144  template <typename T>
145  inline
146  unsigned
147  array<T>::operator[](unsigned i) const
148  {
149  mln_precondition(i < s_.nvalues());
150  return h_[i];
151  }
152 
153  template <typename T>
154  inline
155  unsigned&
156  array<T>::operator[](unsigned i)
157  {
158  mln_precondition(i < s_.nvalues());
159  return h_[i];
160  }
161 
162  template <typename T>
163  inline
164  const std::vector<unsigned>&
165  array<T>::vect() const
166  {
167  return h_;
168  }
169 
170  template <typename T>
171  inline
172  unsigned array<T>::nvalues() const
173  {
174  return h_.size();
175  }
176 
177  template <typename T>
178  inline
179  std::ostream& operator<<(std::ostream& ostr, const array<T>& h)
180  {
181  mln_viter(mln::value::set<T>) v(h.vset());
182  for_all(v)
183  if (h(v) != 0)
184  ostr << v << ':' << h(v) << ' ';
185  return ostr;
186  }
187 
188 # endif // ! MLN_INCLUDE_ONLY
189 
190  } // end of namespace mln::histo
191 
192 } // end of namespace mln
193 
194 
195 #endif // ! MLN_HISTO_ARRAY_HH