$extrastylesheet
Olena  User documentation 2.1
An Image Processing Platform
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
value_array.hh
1 // Copyright (C) 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_VALUE_VALUE_ARRAY_HH
27 # define MLN_VALUE_VALUE_ARRAY_HH
28 
32 
33 # include <mln/value/set.hh>
34 
35 
36 namespace mln
37 {
38 
39  namespace value
40  {
41 
42 
44  template <typename T, typename V>
45  struct value_array
46  {
47  enum {
48  nvalues = mln_card(T)
49  };
50 
53  value_array();
54  value_array(const V& v);
58 
61  const V& operator()(const T& v) const;
62  V& operator()(const T& v);
64 
67  const V& operator[](unsigned i) const;
68  V& operator[](unsigned i);
70 
72  const mln::value::set<T>& vset() const;
73 
74  protected:
75 
77  V v_[nvalues];
78  };
79 
80 
81  template <typename T, typename V>
82  std::ostream& operator<<(std::ostream& ostr, const value_array<T, V>& a);
83 
84 
85 # ifndef MLN_INCLUDE_ONLY
86 
87  template <typename T, typename V>
88  inline
90  : s_ (mln::value::set<T>::the())
91  {
93  mlc_is(set<T>, U)::check();
94  }
95 
96  template <typename T, typename V>
97  inline
99  : s_(mln::value::set<T>::the())
100  {
102  mlc_is(set<T>, U)::check();
103 
104  memset(v_, v, nvalues * sizeof(V));
105  }
106 
107  template <typename T, typename V>
108  inline
109  value_array<T,V>::value_array(const value_array<T, V>& other)
110  : s_(other.s_)
111  {
112  memcpy(v_, other.v_, nvalues * sizeof(V));
113  }
114 
115  template <typename T, typename V>
116  inline
117  value_array<T,V>&
118  value_array<T,V>::operator=(const value_array<T, V>& other)
119  {
120  if (&other != this)
121  memcpy(v_, other.v_, nvalues * sizeof(V));
122  return *this;
123  }
124 
125  template <typename T, typename V>
126  inline
127  const V&
128  value_array<T,V>::operator()(const T& v) const
129  {
130  return v_[s_.index_of(v)];
131  }
132 
133  template <typename T, typename V>
134  inline
135  V&
137  {
138  return v_[s_.index_of(v)];
139  }
140 
141  template <typename T, typename V>
142  inline
143  const mln::value::set<T>&
144  value_array<T,V>::vset() const
145  {
146  return s_;
147  }
148 
149  template <typename T, typename V>
150  inline
151  const V&
152  value_array<T,V>::operator[](unsigned i) const
153  {
154  mln_precondition(i < nvalues);
155  return v_[i];
156  }
157 
158  template <typename T, typename V>
159  inline
160  V&
161  value_array<T,V>::operator[](unsigned i)
162  {
163  mln_precondition(i < nvalues);
164  return v_[i];
165  }
166 
167  template <typename T, typename V>
168  inline
169  std::ostream& operator<<(std::ostream& ostr, const value_array<T,V>& a)
170  {
171  mln_viter(mln::value::set<T>) v(a.vset());
172  for_all(v)
173  ostr << v << ':' << h(v) << ' ';
174  return ostr;
175  }
176 
177 # endif // ! MLN_INCLUDE_ONLY
178 
179  } // end of namespace mln::value
180 
181 } // end of namespace mln
182 
183 
184 #endif // ! MLN_VALUE_VALUE_ARRAY_HH