rheolef  6.3
vec.h
Go to the documentation of this file.
1 #ifndef _RHEO_VEC_H
2 #define _RHEO_VEC_H
3 
4 # include "rheolef/array.h"
5 # include "boost/numeric/ublas/storage.hpp"
6 
7 namespace rheolef {
8 
9 using boost::numeric::ublas::basic_range;
10 using boost::numeric::ublas::range;
11 template <class Expr> struct vec_expr;
12 template <class T, class M> class vec_range;
13 template <class T, class M> class vec_range_const;
14 
15 template <class T, class M> class vec_concat_value;
16 
43 template <class T, class M = rheo_default_memory_model>
44 class vec : public array<T, M> {
45 public:
46 
47 
48  typedef array<T, M> base;
49  typedef typename base::size_type size_type;
50  typedef std::ptrdiff_t difference_type;
51 #ifdef TODO
52  typedef typename base::difference_type difference_type;
53 #endif // TODO
54  typedef basic_range<size_type, difference_type> range_type;
55  typedef typename base::reference reference;
56  typedef typename base::const_reference const_reference;
57  typedef typename base::iterator iterator;
58  typedef typename base::const_iterator const_iterator;
59 
60 // allocator/deallocator:
61 
62  vec (const distributor& ownership,
63  const T& init_val = std::numeric_limits<T>::max());
64 
65  vec(size_type dis_size = 0,
66  const T& init_val = std::numeric_limits<T>::max());
67 
68  void resize (
69  const distributor& ownership,
70  const T& init_val = std::numeric_limits<T>::max());
71 
72  void resize (
73  size_type size = 0,
74  const T& init_val = std::numeric_limits<T>::max());
75 
76 
79 
80  T max_abs () const;
81 
82 
83  vec(const vec_range<T,M>& vr);
84  vec(const vec_range_const<T,M>& vr);
87 
90 
91 
92  vec<T,M>& operator= (const int& expr);
93  vec<T,M>& operator= (const T& expr);
94 
95 
96  template<typename Expr>
97  vec (const vec_expr<Expr>& expr);
98 
99  template<typename Expr>
100  vec<T,M>& operator= (const vec_expr<Expr>& expr);
101 
102 
103 #ifdef _RHEOLEF_HAVE_STD_INITIALIZER_LIST
104  vec (const std::initializer_list<vec_concat_value<T,M> >& init_list);
105  vec<T,M>& operator= (const std::initializer_list<vec_concat_value<T,M> >& init_list);
106 #endif // _RHEOLEF_HAVE_STD_INITIALIZER_LIST
107 };
108 @endcode
109 
110 template <class T, class M>
111 inline
113  const distributor& ownership,
114  const T& init_val)
115  : array<T,M>(ownership,init_val)
116 {
117 }
118 template <class T, class M>
119 inline
121  size_type dis_size,
122  const T& init_val)
123  : array<T,M>(dis_size,init_val)
124 {
125 }
126 template <class T, class M>
127 inline
128 void
130  const distributor& ownership,
131  const T& init_val)
132 {
133  base::resize (ownership, init_val);
134 }
135 template <class T, class M>
136 inline
137 void
139  size_type dis_size,
140  const T& init_val)
141 {
142  base::resize (dis_size, init_val);
143 }
144 template <class T, class M>
145 inline
146 vec<T,M>&
147 vec<T,M>::operator= (const int& expr)
148 {
149  std::fill (array<T,M>::begin(), array<T,M>::end(), expr);
150  return *this;
151 }
152 template <class T, class M>
153 inline
154 vec<T,M>&
155 vec<T,M>::operator= (const T& expr)
156 {
157  std::fill (array<T,M>::begin(), array<T,M>::end(), expr);
158  return *this;
159 }
160 template <class T, class M>
161 inline
162 vec<T,M>&
164 {
165  distributor ownership (distributor::decide, vr._u.comm(), vr._r.size());
166  resize (ownership);
167  std::copy (vr.begin(), vr.end(), base::begin());
168  return *this;
169 }
170 template <class T, class M>
171 inline
172 vec<T,M>&
174 {
175  operator= (vec_range_const<T,M>(vr));
176  return *this;
177 }
178 template <class T, class M>
179 inline
181  : array<T,M>()
182 {
183  operator= (vr);
184 }
185 template <class T, class M>
186 inline
188  : array<T,M>()
189 {
190  operator= (vr);
191 }
192 template <class T, class M>
193 inline
196 {
197  return base::operator[] (i);
198 }
199 template <class T, class M>
200 inline
201 typename vec<T,M>::reference
203 {
204  return base::operator[] (i);
205 }
206 template <class T, class M>
207 inline
210 {
211  return vec_range<T,M> (*this, r);
212 }
213 template <class T, class M>
214 inline
217 {
218  return vec_range_const<T,M> (*this, r);
219 }
220 template <class T, class M>
221 inline
222 T
224 {
225  T val = 0;
226  for (const_iterator iter = base::begin(), last = base::end(); iter != last; iter++) {
227  val = std::max(val, abs(*iter));
228  }
229 #ifdef _RHEOLEF_HAVE_MPI
230  val = mpi::all_reduce (base::comm(), val, mpi::maximum<T>());
231 #endif // _RHEOLEF_HAVE_MPI
232  return val;
233 }
234 template <class T>
235 inline
236 idiststream&
237 operator >> (idiststream& ips, vec<T,sequential>& x)
238 {
239  return x.get_values(ips);
240 }
241 template <class T, class M>
242 inline
243 odiststream&
244 operator << (odiststream& ods, const vec<T,M>& x)
245 {
247  if (format [iorheo::matlab] || format [iorheo::sparse_matlab]) {
248  return x.data().put_matlab (ods);
249  }
250  return x.put_values(ods);
251 }
252 #ifdef _RHEOLEF_HAVE_MPI
253 template <class T>
254 inline
255 idiststream&
256 operator >> (idiststream& ips, vec<T,distributed>& x)
257 {
258  return x.get_values(ips);
259 }
260 #ifdef TO_CLEAN
261 template <class T>
262 inline
263 odiststream&
264 operator << (odiststream& ods, const vec<T,distributed>& x)
265 {
267  if (format [iorheo::matlab] || format [iorheo::sparse_matlab]) {
268  warning_macro ("put_matlab");
269  return x.put_matlab (ods);
270  }
271  warning_macro ("put_default");
272  return x.put_values(ods);
273 }
274 #endif // TO_CLEAN
275 #endif // _RHEOLEF_HAVE_MPI
276 
277 } // namespace rheolef
278 #endif // _RHEO_VEC_H
279