NeoPZ
tinyvec.h
Go to the documentation of this file.
1 // Emacs will be in -*- Mode: c++ -*-
2 //
3 // ***************** DO NOT REMOVE THIS BANNER *****************
4 //
5 // SUMMARY: Templatized Oriented Object Finte Element Method
6 // TOOFEM
7 // RELEASE: 0.1
8 // USAGE : You may copy freely these files and use it for
9 // teaching or research. These or part of these may
10 // not be sold or used for a commercial purpose with-
11 // out our consent : fax (33)1 44 27 72 00
12 //
13 // AUTHOR : Nicolas Di cesare
14 // ORG :
15 // E-MAIL : Nicolas.Dicesare@ann.jussieu.fr
16 //
17 // ORIG-DATE: September 98
18 // LAST-MOD : 15/09/98
19 // ************************************************************
20 #ifndef _tinyvec_h
21 #define _tinyvec_h
22 
23 
24 template < class T, int Num > class TinyVector;
25 
26 #include <utils/tinyveccpy.h>
27 
28 //------------------------------------------------------------------------------------------------//
29 
30 template < class T, int Num > class TinyVector {
31 
32 public:
33  typedef T value_type;
34 // Constructors
35  TinyVector() { copy( T() );}
36  TinyVector(const T& val) { copy( val );}
38 
39 // destructor
41 
42 // Operators
43  inline T& operator [] (int i)
44  { CheckSize(i); return ptr_to_data[i]; }
45 
46  inline const T& operator [] (int i) const
47  { CheckSize(i); return ptr_to_data[i]; }
48 
49  inline T& operator () ( int i)
50  { CheckSize(i); return ptr_to_data[i]; }
51 
52  inline const T& operator () ( int i) const
53  { CheckSize(i); return ptr_to_data[i]; }
54 
55 
56  TinyVector< T, Num >& operator=(const T & val) { copy(val); return *this;}
57  TinyVector< T, Num >& operator=(const TinyVector< T, Num >& a) { copy(a); return *this;}
58 
59 // Member functions
60  inline int size() const { return capacity();}
61  inline int capacity() const { return Num;}
62  inline T* begin() const { return ptr_to_data;}
63  inline int no(const T * ptr) const { return (ptr - begin());}
64 
67 
68 private:
69  void CheckSize(int i) const
70  {
71 #ifdef CHECK_SIZE
72  if ( !( (i >= 0) && (i < Num) ) ) error("TinyVector<>::CheckSize(int i), index out of bound");
73 #endif
74  }
75 
76 
77  void copy( const TinyVector< T, Num >& a )
78  { Copy(*this,a); }
79 
80  void copy( const T & val)
81  { Copy(*this,val); }
82 
83 private:
84  T ptr_to_data[Num];
85 };
86 
87 
88 
89 
90 template < class T, int Num > inline
92 {
94 
95  for (int i=0; i<Num; ++i)
96  tmp[i] *= val;
97 
98  return tmp;
99 }
100 
101 
102 template < class T, int Num > inline
104 {
105 
106  for (int i=0; i<Num; ++i)
107  ptr_to_data[i] += v.ptr_to_data[i];
108 
109  return *this;
110 }
111 
112 
113 template < class T, int Num > inline
115 {
116 
117  for (int i=0; i<Num; ++i)
118  ptr_to_data[i] -= v.ptr_to_data[i];
119 
120  return *this;
121 }
122 
123 template < class T, int Num > inline
124 std::ostream& operator << (std::ostream& os, const TinyVector< T, Num >& v)
125 {
126  os.setf(std::ios::fixed,std::ios::floatfield);
127 
128  for (int i=0; i<Num; ++i)
129  os << std::setw(12) << v[i];
130  os << std::endl;
131 
132  return os;
133 }
134 
135 
136 #endif
T ptr_to_data[Num]
Definition: tinyvec.h:84
void copy(const TinyVector< T, Num > &a)
Definition: tinyvec.h:77
T value_type
Definition: tinyvec.h:33
int capacity() const
Definition: tinyvec.h:61
TinyVector< T, Num > & operator=(const TinyVector< T, Num > &a)
Definition: tinyvec.h:57
T & operator()(int i)
Definition: tinyvec.h:49
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
AutoPointerMutexArrayInit tmp
int no(const T *ptr) const
Definition: tinyvec.h:63
TinyVector< T, Num > & operator+=(const TinyVector< T, Num > &v)
Definition: tinyvec.h:103
void CheckSize(int i) const
Definition: tinyvec.h:69
TinyVector(const T &val)
Definition: tinyvec.h:36
void error(char *string)
Definition: testShape.cc:7
~TinyVector()
Definition: tinyvec.h:40
T * begin() const
Definition: tinyvec.h:62
T & operator[](int i)
Definition: tinyvec.h:43
TinyVector(const TinyVector< T, Num > &a)
Definition: tinyvec.h:37
TinyVector()
Definition: tinyvec.h:35
TinyVector< T, Num > & operator=(const T &val)
Definition: tinyvec.h:56
void Copy(TinyVector< T, Num > &y, const TinyVector< T, Num > &x)
Definition: tinyveccpy.h:87
void copy(const T &val)
Definition: tinyvec.h:80
TinyVector< T, Num > operator*(const T &val, const TinyVector< T, Num > &v)
Definition: tinyvec.h:91
int size() const
Definition: tinyvec.h:60
TinyVector< T, Num > & operator-=(const TinyVector< T, Num > &v)
Definition: tinyvec.h:114