NeoPZ
vectors.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 _vectors_h
21 #define _vectors_h
22 
23 #include <cstdio>
24 
25 #include <memory>
26 #include <iostream>
27 #include <iomanip>
28 #include <stddef.h>
29 
30 
31 #include <utils/error.h>
32 #include <utils/tinyvec.h>
33 
34 #define RESTRICT
35 
36 
37 
38 // Copy ala Blitz
39 template <class T> class MEM_CPY {
40  public:
41  static void copy(T* RESTRICT dest, const T* src, const int N)
42  {
43  // Unwind the inner loop, four elements at a time
44  int Nmod4 = N & 0x03;
45 
46  int i=0;
47  for (; i< Nmod4; ++i)
48  dest[i] = src[i];
49 
50  for (; i<N; i+=4)
51  {
52  // Common subexpression elimination: avoid doing i+1, i+2, i+3
53  // multiple times (compilers *won't* do this cse automatically)
54  int i1 = i+1;
55  int i2 = i+2;
56  int i3 = i+3;
57 
58  // Reading all the results first avoids aliasing
59  // ambiguities, and provides more flexibility in
60  // register allocation & instruction scheduling
61  T tmp0,tmp1, tmp2, tmp3;
62  tmp0 = src[i];
63  tmp1 = src[i1];
64  tmp2 = src[i2];
65  tmp3 = src[i3];
66 
67  dest[i] = tmp0;
68  dest[i1] = tmp1;
69  dest[i2] = tmp2;
70  dest[i3] = tmp3;
71  }
72  }
73 };
74 
75 
76 //-------------------------------------------------------------------
77 // Tableau 1D
78 //-------------------------------------------------------------------
79 //interface
80 
81 template < class T > class Vector {
82 
83 public:
84  typedef T value_type;
85  typedef value_type* pointer;
86  typedef const value_type* const_pointer;
87  typedef value_type* iterator;
88  typedef const value_type* const_iterator;
89  typedef value_type& reference;
90  typedef const value_type& const_reference;
91  typedef size_t size_type;
92  typedef ptrdiff_t difference_type;
93 
94 // Constructors
95  inline Vector();
96  inline Vector(int csize);
97  Vector(int csize, const T& val);
98  inline Vector(const Vector< T >& a);
99 
100 // destructor
101  ~Vector();
102 
103 // Operators
104  inline T& operator [] (int );
105  inline const T& operator [] (int ) const;
106  inline T& operator () ( int );
107  inline const T& operator () ( int ) const;
108  Vector< T >& operator=(const Vector< T >& a);
109  Vector< T >& operator=(const T & val);
110 
112  {
113  int sz = size();
114  if ( sz != a.size() ) error("operator-=(const Vector< T >& a), size error");
115  for (int i=0; i<sz; ++i) (*this)[i] += a[i];
116  return *this;
117  }
118 
120  {
121  int sz = size();
122  if ( sz != a.size() ) error("operator-=(const Vector< T >& a), size error");
123  for (int i=0; i<sz; ++i) (*this)[i] -= a[i];
124  return *this;
125  }
126 
127 
128 // Member functions
129  inline int empty() const { return (!size());}
130  inline void reserve(int );
131  inline void resize(int );
132  inline int size() const { return capacity();}
133  inline int length() const { return capacity();}
134  inline int capacity() const;
135  inline T* begin() const;
136  inline T* end() const { return ptr_to_data + num_elts;}
137  inline void destroy();
138  inline int no(const T * ptr) const { return (ptr - begin());}
139 
140 private:
141 
142  void copy(const Vector< T >& a);
143 
144 private:
145 
146  int num_elts;
148 };
149 
150 template < class T> Vector<T> operator+(const Vector<T> & x, const Vector<T> & y)
151 {
152  int sz = x.size();
153  if ( sz!=y.size() ) error("operator+(const Vector<T> & x, const Vector<T> & y), size error");
154 
155  Vector<T> z(sz);
156 
157  for (int i=0; i<sz; ++i)
158  z[i] = x[i] + y[i];
159 
160  return z;
161 }
162 
163 template < class T> Vector<T> operator-(const Vector<T> & x, const Vector<T> & y)
164 {
165  int sz = x.size();
166  if ( sz!=y.size() ) error("operator+(const Vector<T> & x, const Vector<T> & y), size error");
167 
168  Vector<T> z(sz);
169 
170  for (int i=0; i<sz; ++i)
171  z[i] = x[i] - y[i];
172 
173  return z;
174 }
175 
176 template < class T> Vector<T> operator*(const T& a, const Vector<T> & x)
177 {
178  int sz = x.size();
179  Vector<T> y(sz);
180 
181  for (int i=0; i<sz; ++i)
182  y[i] = a*x[i];
183 
184  return y;
185 }
186 
187 template <class T> inline std::ostream& operator << (std::ostream& os, const Vector<T>& x)
188 {
189  os.setf(std::ios::fixed,std::ios::floatfield);
190  os.width(12);
191  int sz = x.size();
192  os << sz << std::endl;
193 
194 
195  for (int i=0; i< sz; ++i) {
196  os.width(12);
197  os << x[i] << std::endl;
198  }
199 
200  os << "\n";
201 
202  return os;
203 }
204 
205 
206 
207 
208 
209 template< class T > inline Vector< T >::Vector() : num_elts(0), ptr_to_data((T*)0)
210 // default constructor
211 {
212 #ifdef DEBUG
213  std::cerr << "Vector<>::Vector() |default constructor|";
214 #endif
215 
216 #ifdef DEBUG
217  fprintf(stderr," adr = %x \n", ptr_to_data);
218 #endif
219 }
220 
221 
222 template< class T > inline Vector< T >::Vector(int csize) : num_elts(0), ptr_to_data((T*)0)
223 // size constructor
224 {
225 #ifdef DEBUG
226  std::cerr << "Vector<>::Vector(int ) |size constructor |";
227 #endif
228 
229  if (csize > 0 ){
230  num_elts = csize;
231  ptr_to_data = new T[num_elts];
232 
233  if ( ptr_to_data == 0 ) error("Vector<>::Vector(), Memoire insuffisante");
234  }
235 
236 #ifdef DEBUG
237  fprintf(stderr," adr = %x \n", ptr_to_data);
238 #endif
239 }
240 
241 
242 template< class T > inline Vector< T >::Vector(const Vector< T >& a) : num_elts(a.num_elts), ptr_to_data((T*)0)
243 // copy constructor
244 {
245 #ifdef DEBUG
246  std::cerr << "Vector<>::Vector(const Vector< T >& ) |copy constructor |";
247 #endif
248 
249  if ( num_elts != 0 ) {
250  ptr_to_data = new T[num_elts];
251  copy(a);
252  }
253 
254 #ifdef DEBUG
255  fprintf(stderr," adr = %x \n", ptr_to_data);
256 #endif
257 }
258 
259 
260 
261 template< class T > inline void Vector< T >::destroy()
262 {
263 #ifdef DEBUG
264  fprintf(stderr,"Vector<>::destroy() | | adr = %x \n", ptr_to_data);
265 #endif
266  if (ptr_to_data != 0)
267  delete [] ptr_to_data;
268  else {
269  }
270 
271  num_elts = 0; ptr_to_data = (T*)0;
272 }
273 
274 
275 
276 template< class T > inline T& Vector< T >::operator [] (int i)
277 {
278 #ifdef DEBUG
279  // cerr << "T& Vector< T >::operator [] (int i)" << endl;
280 #endif
281 
282 #ifdef CHECK_SIZE
283  if ( ptr_to_data == 0 ) error("Vector<>::operator[], empty array");
284 
285  if ( !( (i >= 0) && (i < num_elts) ) ) error("Vector<>::operator[], index out of bound");
286 #endif
287 
288  return *(ptr_to_data + i);
289 }
290 
291 
292 template< class T > inline const T& Vector< T >::operator [] (int i) const
293 {
294 #ifdef DEBUG
295 // cerr << "const T& Vector< T >::operator [] (int i) const" << endl;
296 #endif
297 
298 #ifdef CHECK_SIZE
299  if ( ptr_to_data == 0 ) error("Vector<>::operator[], empty array");
300 
301  if ( !( (i >= 0) && (i < num_elts) ) ) error("Vector<>::operator[], index out of bound");
302 #endif
303 
304  return *(ptr_to_data + i);
305 }
306 
307 template< class T > inline T& Vector< T >::operator () (int i)
308 {
309  return Vector<T>::operator[](i);
310 }
311 
312 template< class T > inline const T& Vector< T >::operator () (int i) const
313 {
314  return Vector<T>::operator[](i);
315 }
316 
317 
318 
319 template< class T > inline void Vector< T >::reserve(int ssize)
320 {
321 #ifdef CHECK_SIZE
322  if ( ptr_to_data != 0 ) error("Vector<>::reserve(), array already allocted");
323 
324  if ( ssize <0 ) error("Vector<>::reserve(), negative size");
325 #endif
326 
327  if (ssize != 0){
328  num_elts = ssize;
329  ptr_to_data = new T[num_elts];
330 
331  if ( ptr_to_data == 0 ) error("Vector<>::reserve(), not enough memory");
332  }
333 #ifdef DEBUG
334  fprintf(stderr,"Vector<>::reserve() | | adr = %x \n", ptr_to_data);
335 #endif
336 }
337 
338 
339 template< class T > inline void Vector< T >::resize(int ssize)
340 {
341 #ifdef CHECK_SIZE
342  if ( ssize <0 ) error("Vector<>::reserve(), negative size");
343 #endif
344 
345  if ( ssize != 0){
346  if ( num_elts != 0) destroy();
347 
348  num_elts = ssize;
349  ptr_to_data = new T[num_elts];
350 
351  if ( ptr_to_data == 0 ) error("Vector<>::reserve(), not enough memory");
352 
353  }
354  else
355  if ( num_elts != 0) destroy();
356 
357 #ifdef DEBUG
358  fprintf(stderr,"Vector<>::reserve() | | adr = %x \n", ptr_to_data);
359 #endif
360 }
361 
362 
363 template< class T > inline int Vector< T >::capacity() const
364 {
365  return num_elts;
366 }
367 
368 
369 
370 template< class T > inline T* Vector< T >::begin() const
371 {
372  return ptr_to_data;
373 }
374 
375 
376 template< class T > Vector< T >::Vector(int csize, const T& val) : num_elts(0), ptr_to_data((T*)0)
377 {
378 #ifdef DEBUG
379  std::cerr << "Vector<>::Vector(int, const T& )|size constructor |";
380 #endif
381 
382  if (csize > 0 ){
383  num_elts = csize;
384  ptr_to_data = new T[num_elts];
385 
386  if ( ptr_to_data == 0 ) error("Vector<>::Vector(), Memoire insuffisante");
387  }
388 
389  T* p = ptr_to_data + num_elts;
390  while ( p > ptr_to_data )
391  *--p = val;//pb si = n'est pas surcharge pour le type T
392 
393 #ifdef DEBUG
394  fprintf(stderr," adr = %x \n", ptr_to_data);
395 #endif
396 }
397 
398 
399 template< class T > void Vector< T >::copy(const Vector< T >& a)
400 {
402 }
403 
404 
405 template< class T > Vector< T >::~Vector()// destructeur
406 {
407 #ifdef DEBUG
408  std::cerr << "Vector<>::~Vector() |destructor |" << std::endl;;
409 #endif
410 
411  destroy();
412 
413 }
414 
415 
416 //le return dans le if empeche de mettre la fonction en ligne
417 template< class T > Vector< T >& Vector< T >::operator = (const Vector< T >& a)
418 {
419  if ( this != &a) {
420  // Cas ou le pointeur est non alloue
421  if ( num_elts == 0 ) {
422  if ( a.num_elts == 0 ) return (*this);
423  else reserve(a.num_elts);
424  }
425  // Cas ou le pointeur est deja alloue
426  if ( num_elts != a.num_elts ) {
427  //cerr << "size = " << num_elts << ", a.size = " << a.num_elts << endl;
428  //cerr << "ptr_to_data = " << (void*)ptr_to_data << ", a.ptr_to_data = " << (void*)a.ptr_to_data << endl;
429  //error("Vector::operator=, tailles incompatibles");
430  destroy();//on desalloue
431  reserve(a.num_elts);
432  }
433 
434  copy(a);
435  }
436 
437  return *this;
438 }
439 
440 
441 template< class T > Vector< T >& Vector< T >::operator = (const T& val)
442 {
443  if (num_elts != 0)
444  for (int i=0; i<num_elts; ++i)
445  ptr_to_data[i] = val;
446  else error("Vector< T >& Vector< T >::operator = (const T& val), unallocated vector");
447 
448  return *this;
449 }
450 
451 
452 
453 
454 #endif
455 
456 
457 
458 
459 
460 
461 
int no(const T *ptr) const
Definition: vectors.h:138
size_t size_type
Definition: vectors.h:91
T value_type
Definition: vectors.h:84
T * end() const
Definition: vectors.h:136
Vector< T > operator-(const Vector< T > &x, const Vector< T > &y)
Definition: vectors.h:163
int capacity() const
Definition: vectors.h:363
#define RESTRICT
Definition: vectors.h:34
int length() const
Definition: vectors.h:133
int num_elts
Definition: vectors.h:146
const value_type & const_reference
Definition: vectors.h:90
int size() const
Definition: vectors.h:132
REAL val(STATE &number)
Returns value of the variable.
Definition: pzartdiff.h:23
Vector< T > operator+(const Vector< T > &x, const Vector< T > &y)
Definition: vectors.h:150
const value_type * const_iterator
Definition: vectors.h:88
Vector< T > operator*(const T &a, const Vector< T > &x)
Definition: vectors.h:176
int empty() const
Definition: vectors.h:129
void error(char *string)
Definition: testShape.cc:7
value_type & reference
Definition: vectors.h:89
void reserve(int)
Definition: vectors.h:319
T * begin() const
Definition: vectors.h:370
void copy(const Vector< T > &a)
Definition: vectors.h:399
T & operator[](int)
Definition: vectors.h:276
void resize(int)
Definition: vectors.h:339
const value_type * const_pointer
Definition: vectors.h:86
Vector< T > & operator+=(const Vector< T > &a)
Definition: vectors.h:111
value_type * pointer
Definition: vectors.h:85
Vector< T > & operator=(const Vector< T > &a)
Definition: vectors.h:417
static void copy(T *RESTRICT dest, const T *src, const int N)
Definition: vectors.h:41
value_type * iterator
Definition: vectors.h:87
void destroy()
Definition: vectors.h:261
Vector< T > & operator-=(const Vector< T > &a)
Definition: vectors.h:119
Definition: vectors.h:81
Vector()
Definition: vectors.h:209
ptrdiff_t difference_type
Definition: vectors.h:92
~Vector()
Definition: vectors.h:405
T *RESTRICT ptr_to_data
Definition: vectors.h:147
T & operator()(int)
Definition: vectors.h:307