blitz  Version 0.9
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
tinyvec.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  * blitz/tinyvec.h Declaration of the TinyVector<T, N> class
4  *
5  * $Id: tinyvec.h,v 1.9 2005/10/06 23:23:40 julianc Exp $
6  *
7  * Copyright (C) 1997-2001 Todd Veldhuizen <tveldhui@oonumerics.org>
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * Suggestions: blitz-dev@oonumerics.org
20  * Bugs: blitz-bugs@oonumerics.org
21  *
22  * For more information, please see the Blitz++ Home Page:
23  * http://oonumerics.org/blitz/
24  *
25  ***************************************************************************/
26 
27 #ifndef BZ_TINYVEC_H
28 #define BZ_TINYVEC_H
29 
30 #include <blitz/blitz.h>
31 #include <blitz/range.h>
32 #include <blitz/listinit.h>
33 #include <blitz/tiny.h>
34 
35 BZ_NAMESPACE(blitz)
36 
37 /*****************************************************************************
38  * Forward declarations
39  */
40 
41 template<typename P_numtype, int N_length, int N_stride BZ_TEMPLATE_DEFAULT(1) >
42 class TinyVectorIter;
43 
44 template<typename P_numtype, int N_length, int N_stride BZ_TEMPLATE_DEFAULT(1) >
46 
47 template<typename P_numtype>
48 class Vector;
49 
50 template<typename P_expr>
51 class _bz_VecExpr;
52 
53 template<typename P_distribution>
54 class Random;
55 
56 template<typename P_numtype>
57 class VectorPick;
58 
59 template<typename T_numtype1, typename T_numtype2, int N_rows, int N_columns,
60  int N_vecStride>
61 class _bz_matrixVectorProduct;
62 
63 
64 
65 /*****************************************************************************
66  * Declaration of class TinyVector
67  */
68 
69 template<typename P_numtype, int N_length>
70 class TinyVector {
71 public:
72 
74  // Public Types
76 
77  typedef P_numtype T_numtype;
81  typedef T_numtype* iterator;
82  typedef const T_numtype* const_iterator;
83  enum { numElements = N_length };
84 
85  TinyVector() { }
87 
88  inline TinyVector(const TinyVector<T_numtype,N_length>& x);
89 
90  template <typename T_numtype2>
91  inline TinyVector(const TinyVector<T_numtype2,N_length>& x);
92 
93  inline TinyVector(const T_numtype initValue);
94 
95  inline TinyVector(const T_numtype x[]) {
96  memcpy(data_,x,N_length*sizeof(T_numtype));
97  }
98 
99  TinyVector(T_numtype x0, T_numtype x1)
100  {
101  data_[0] = x0;
102  data_[1] = x1;
103  }
104 
105  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2)
106  {
107  data_[0] = x0;
108  data_[1] = x1;
109  data_[2] = x2;
110  }
111 
112  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
113  T_numtype x3)
114  {
115  data_[0] = x0;
116  data_[1] = x1;
117  data_[2] = x2;
118  data_[3] = x3;
119  }
120 
121  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
122  T_numtype x3, T_numtype x4)
123  {
124  data_[0] = x0;
125  data_[1] = x1;
126  data_[2] = x2;
127  data_[3] = x3;
128  data_[4] = x4;
129  }
130 
131  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
132  T_numtype x3, T_numtype x4, T_numtype x5)
133  {
134  data_[0] = x0;
135  data_[1] = x1;
136  data_[2] = x2;
137  data_[3] = x3;
138  data_[4] = x4;
139  data_[5] = x5;
140  }
141 
142  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
143  T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6)
144  {
145  data_[0] = x0;
146  data_[1] = x1;
147  data_[2] = x2;
148  data_[3] = x3;
149  data_[4] = x4;
150  data_[5] = x5;
151  data_[6] = x6;
152  }
153 
154  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
155  T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
156  T_numtype x7)
157  {
158  data_[0] = x0;
159  data_[1] = x1;
160  data_[2] = x2;
161  data_[3] = x3;
162  data_[4] = x4;
163  data_[5] = x5;
164  data_[6] = x6;
165  data_[7] = x7;
166  }
167 
168  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
169  T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
170  T_numtype x7, T_numtype x8)
171  {
172  data_[0] = x0;
173  data_[1] = x1;
174  data_[2] = x2;
175  data_[3] = x3;
176  data_[4] = x4;
177  data_[5] = x5;
178  data_[6] = x6;
179  data_[7] = x7;
180  data_[8] = x8;
181  }
182 
183  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
184  T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
185  T_numtype x7, T_numtype x8, T_numtype x9)
186  {
187  data_[0] = x0;
188  data_[1] = x1;
189  data_[2] = x2;
190  data_[3] = x3;
191  data_[4] = x4;
192  data_[5] = x5;
193  data_[6] = x6;
194  data_[7] = x7;
195  data_[8] = x8;
196  data_[9] = x9;
197  }
198 
199  TinyVector(T_numtype x0, T_numtype x1, T_numtype x2,
200  T_numtype x3, T_numtype x4, T_numtype x5, T_numtype x6,
201  T_numtype x7, T_numtype x8, T_numtype x9, T_numtype x10)
202  {
203  data_[0] = x0;
204  data_[1] = x1;
205  data_[2] = x2;
206  data_[3] = x3;
207  data_[4] = x4;
208  data_[5] = x5;
209  data_[6] = x6;
210  data_[7] = x7;
211  data_[8] = x8;
212  data_[9] = x9;
213  data_[10] = x10;
214  }
215 
216  // Constructor added by Peter Nordlund
217  template<typename P_expr>
218  inline TinyVector(_bz_VecExpr<P_expr> expr);
219 
220  T_iterator beginFast() { return T_iterator(*this); }
221  T_constIterator beginFast() const { return T_constIterator(*this); }
222 
223  iterator begin() { return data_; }
224  const_iterator begin() const { return data_; }
225 
226  iterator end() { return data_ + N_length; }
227  const_iterator end() const { return data_ + N_length; }
228 
229  T_numtype * restrict data()
230  { return data_; }
231 
232  const T_numtype * restrict data() const
233  { return data_; }
234 
235  T_numtype * restrict dataFirst()
236  { return data_; }
237 
238  const T_numtype * restrict dataFirst() const
239  { return data_; }
240 
241  unsigned length() const
242  { return N_length; }
243 
245  // Library-internal member functions
246  // These are undocumented and may change or
247  // disappear in future releases.
249 
250  unsigned _bz_suggestLength() const
251  { return N_length; }
252 
253  bool _bz_hasFastAccess() const
254  { return true; }
255 
256  T_numtype& restrict _bz_fastAccess(unsigned i)
257  { return data_[i]; }
258 
259  T_numtype _bz_fastAccess(unsigned i) const
260  { return data_[i]; }
261 
262  template<typename P_expr, typename P_updater>
263  void _bz_assign(P_expr, P_updater);
264 
265  _bz_VecExpr<T_constIterator> _bz_asVecExpr() const
266  { return _bz_VecExpr<T_constIterator>(beginFast()); }
267 
269  // Subscripting operators
271 
272  bool lengthCheck(unsigned i) const
273  {
274  BZPRECHECK(i < N_length,
275  "TinyVector<" << BZ_DEBUG_TEMPLATE_AS_STRING_LITERAL(T_numtype)
276  << "," << N_length << "> index out of bounds: " << i);
277  return true;
278  }
279 
280  const T_numtype& operator()(unsigned i) const
281  {
282  BZPRECONDITION(lengthCheck(i));
283  return data_[i];
284  }
285 
286  T_numtype& restrict operator()(unsigned i)
287  {
288  BZPRECONDITION(lengthCheck(i));
289  return data_[i];
290  }
291 
292  const T_numtype& operator[](unsigned i) const
293  {
294  BZPRECONDITION(lengthCheck(i));
295  return data_[i];
296  }
297 
298  T_numtype& restrict operator[](unsigned i)
299  {
300  BZPRECONDITION(lengthCheck(i));
301  return data_[i];
302  }
303 
305  // Assignment operators
307 
308  // Scalar operand
310  {
312  }
313 
314  T_vector& initialize(const T_numtype);
315  T_vector& operator+=(const T_numtype);
316  T_vector& operator-=(const T_numtype);
317  T_vector& operator*=(const T_numtype);
318  T_vector& operator/=(const T_numtype);
319  T_vector& operator%=(const T_numtype);
320  T_vector& operator^=(const T_numtype);
321  T_vector& operator&=(const T_numtype);
322  T_vector& operator|=(const T_numtype);
323  T_vector& operator>>=(const int);
324  T_vector& operator<<=(const int);
325 
326  template<typename P_numtype2>
327  T_vector& operator=(const TinyVector<P_numtype2, N_length> &);
328  template<typename P_numtype2>
329  T_vector& operator+=(const TinyVector<P_numtype2, N_length> &);
330  template<typename P_numtype2>
331  T_vector& operator-=(const TinyVector<P_numtype2, N_length> &);
332  template<typename P_numtype2>
333  T_vector& operator*=(const TinyVector<P_numtype2, N_length> &);
334  template<typename P_numtype2>
335  T_vector& operator/=(const TinyVector<P_numtype2, N_length> &);
336  template<typename P_numtype2>
337  T_vector& operator%=(const TinyVector<P_numtype2, N_length> &);
338  template<typename P_numtype2>
339  T_vector& operator^=(const TinyVector<P_numtype2, N_length> &);
340  template<typename P_numtype2>
341  T_vector& operator&=(const TinyVector<P_numtype2, N_length> &);
342  template<typename P_numtype2>
343  T_vector& operator|=(const TinyVector<P_numtype2, N_length> &);
344  template<typename P_numtype2>
345  T_vector& operator>>=(const TinyVector<P_numtype2, N_length> &);
346  template<typename P_numtype2>
347  T_vector& operator<<=(const TinyVector<P_numtype2, N_length> &);
348 
349  template<typename P_numtype2> T_vector& operator=(const Vector<P_numtype2> &);
350  template<typename P_numtype2> T_vector& operator+=(const Vector<P_numtype2> &);
351  template<typename P_numtype2> T_vector& operator-=(const Vector<P_numtype2> &);
352  template<typename P_numtype2> T_vector& operator*=(const Vector<P_numtype2> &);
353  template<typename P_numtype2> T_vector& operator/=(const Vector<P_numtype2> &);
354  template<typename P_numtype2> T_vector& operator%=(const Vector<P_numtype2> &);
355  template<typename P_numtype2> T_vector& operator^=(const Vector<P_numtype2> &);
356  template<typename P_numtype2> T_vector& operator&=(const Vector<P_numtype2> &);
357  template<typename P_numtype2> T_vector& operator|=(const Vector<P_numtype2> &);
358  template<typename P_numtype2> T_vector& operator>>=(const Vector<P_numtype2> &);
359  template<typename P_numtype2> T_vector& operator<<=(const Vector<P_numtype2> &);
360 
361  // Vector expression operand
362  template<typename P_expr> T_vector& operator=(_bz_VecExpr<P_expr>);
363  template<typename P_expr> T_vector& operator+=(_bz_VecExpr<P_expr>);
364  template<typename P_expr> T_vector& operator-=(_bz_VecExpr<P_expr>);
365  template<typename P_expr> T_vector& operator*=(_bz_VecExpr<P_expr>);
366  template<typename P_expr> T_vector& operator/=(_bz_VecExpr<P_expr>);
367  template<typename P_expr> T_vector& operator%=(_bz_VecExpr<P_expr>);
368  template<typename P_expr> T_vector& operator^=(_bz_VecExpr<P_expr>);
369  template<typename P_expr> T_vector& operator&=(_bz_VecExpr<P_expr>);
370  template<typename P_expr> T_vector& operator|=(_bz_VecExpr<P_expr>);
371  template<typename P_expr> T_vector& operator>>=(_bz_VecExpr<P_expr>);
372  template<typename P_expr> T_vector& operator<<=(_bz_VecExpr<P_expr>);
373 
374  // VectorPick operand
375  template<typename P_numtype2>
376  T_vector& operator=(const VectorPick<P_numtype2> &);
377  template<typename P_numtype2>
378  T_vector& operator+=(const VectorPick<P_numtype2> &);
379  template<typename P_numtype2>
380  T_vector& operator-=(const VectorPick<P_numtype2> &);
381  template<typename P_numtype2>
382  T_vector& operator*=(const VectorPick<P_numtype2> &);
383  template<typename P_numtype2>
384  T_vector& operator/=(const VectorPick<P_numtype2> &);
385  template<typename P_numtype2>
386  T_vector& operator%=(const VectorPick<P_numtype2> &);
387  template<typename P_numtype2>
388  T_vector& operator^=(const VectorPick<P_numtype2> &);
389  template<typename P_numtype2>
390  T_vector& operator&=(const VectorPick<P_numtype2> &);
391  template<typename P_numtype2>
392  T_vector& operator|=(const VectorPick<P_numtype2> &);
393  template<typename P_numtype2>
394  T_vector& operator>>=(const VectorPick<P_numtype2> &);
395  template<typename P_numtype2>
396  T_vector& operator<<=(const VectorPick<P_numtype2> &);
397 
398  // Range operand
399  T_vector& operator=(const Range&);
400  T_vector& operator+=(const Range&);
401  T_vector& operator-=(const Range&);
402  T_vector& operator*=(const Range&);
403  T_vector& operator/=(const Range&);
404  T_vector& operator%=(const Range&);
405  T_vector& operator^=(const Range&);
406  T_vector& operator&=(const Range&);
407  T_vector& operator|=(const Range&);
408  T_vector& operator>>=(const Range&);
409  T_vector& operator<<=(const Range&);
410 
411  T_numtype* restrict getInitializationIterator()
412  { return dataFirst(); }
413 
414 private:
415  T_numtype data_[N_length];
416 };
417 
418 
419 // Specialization for N = 0: KCC is giving some
420 // peculiar errors, perhaps this will fix.
421 
422 template<typename T>
423 class TinyVector<T,0> {
424 };
425 
427 
428 #include <blitz/tinyveciter.h> // Iterators
429 #include <blitz/tvecglobs.h> // Global functions
430 #include <blitz/vector.h> // Expression templates
431 #include <blitz/tinyvec.cc> // Member functions
432 #include <blitz/tinyvecio.cc> // I/O functions
433 
434 #endif // BZ_TINYVEC_H
435