NeoPZ
pzvec.h
Go to the documentation of this file.
1 
6 #ifndef TPZVEC_H
7 #define TPZVEC_H
8 
9 #include "pzreal.h"
10 #include "pzerror.h"
12 #include <iostream>
13 #include <iomanip>
14 #include <cstdlib>
15 
16 #include <stdlib.h>
17 #include <string.h>
18 
20 inline std::ostream &operator<<(std::ostream &out, const std::pair<int,int> &element)
21 {
22  out << element.first << "|" << element.second;
23  return out;
24 }
25 
34 template< class T >
35 class TPZVec {
36 public:
38  TPZVec();
39 
45  TPZVec(const int64_t size);
46 
56  TPZVec(const int64_t size, const T& copy);
57 
63  TPZVec(const TPZVec<T> &copy);
64 
69  TPZVec(const std::initializer_list<T>& list);
70 
72  virtual ~TPZVec();
73 
74  void MigratePages() {
75  migrate_to_local((char*) fStore, fNElements * sizeof(T));
76  }
77  void ReallocForNuma() {
78  if (fNElements == 0 || fStore == NULL)
79  return;
80  T* newStore = new T[fNElements];
81  memcpy((void*)newStore,(void*)fStore,fNElements*sizeof(T));
82  delete [] fStore;
83  fStore = newStore;
84  }
85 
92  TPZVec<T> &operator=(const TPZVec<T> &copy);
93 
100  TPZVec<T> &operator=(const std::initializer_list<T> &list);
101 
107  TPZVec<T>& operator=(const T& a);
108 
114 #if defined(WIN32) && !defined(_WIN64)
115  T& operator[]( const int index ) const
116  {
117 #ifdef PZDEBUG
118  if( index < 0 || index >= fNElements )
119  {
120  PZError << __PRETTY_FUNCTION__ << " acessing element out of range.";
121  PZError << "|" << std::endl;
122  PZError << "+-> NElements = " << NElements() << std::endl;
123  PZError << "|" << std::endl;
124  PZError << "+-> Index = " << index << std::endl;
125  DebugStop();
126  exit( -1 );
127  }
128 #endif
129  return fStore[ index ];
130  }
131  T& operator[]( const unsigned int index ) const
132  {
133 #ifdef PZDEBUG
134  if( index >= (unsigned int)fNElements )
135  {
136  PZError << __PRETTY_FUNCTION__ << " acessing element out of range.";
137  PZError << "|" << std::endl;
138  PZError << "+-> NElements = " << NElements() << std::endl;
139  PZError << "|" << std::endl;
140  PZError << "+-> Index = " << index << std::endl;
141  DebugStop();
142  exit( -1 );
143  }
144 #endif
145  return fStore[ index ];
146  }
147 #endif //WIN32
148  T& operator[]( const int64_t index ) const
149  {
150 #ifdef PZDEBUG
151  if( index < 0 || index >= fNElements )
152  {
153  PZError << __PRETTY_FUNCTION__ << " acessing element out of range.";
154  PZError << "|" << std::endl;
155  PZError << "+-> NElements = " << NElements() << std::endl;
156  PZError << "|" << std::endl;
157  PZError << "+-> Index = " << index << std::endl;
158  DebugStop();
159  exit( -1 );
160  }
161 #endif
162  return fStore[ index ];
163  }
164 
166  template <class T2>
167  friend std::ostream& operator<<( std::ostream& Out, const TPZVec< T2 >& v );
168 
170  //operator T*() const { return fStore; }
171 
173  T *begin() const;
174 
176  T *end() const;
177 
184  void Fill(const T& copy, const int64_t from=0, const int64_t numelem=-1);
185 
190  inline int64_t NElements() const { return fNElements; }
191 
196  inline int64_t size() const { return fNElements; }
197 
204  virtual void Resize(const int64_t newsize,const T& object);
205 
212  virtual void Resize(const int64_t newsize);
213  virtual void resize(const int64_t newsize)
214  {
215  Resize(newsize);
216  }
217 
223  void Print(std::ostream &out = std::cout);
224 
226  virtual void clear();
227 
228 protected:
230  T* fStore;
231 
233  int64_t fNElements;
234 };
235 
236 template< class T >
237 inline TPZVec<T>::TPZVec() : fStore( 0 ), fNElements( 0 )
238 {
239  // NOTHING TO DO HERE!
240 }
241 
242 template< class T >
243 TPZVec<T>::TPZVec( const int64_t size ) : fStore( 0 )
244 {
245 #ifndef NODEBUG
246  if( size < 0 )
247  {
248  PZError << "TPZVec constructor. Bad parameter size " << size << " size is set to 0."
249  << std::endl;
250  PZError.flush();
251  fNElements = 0;
252  return;
253  }
254 #endif
255 
256  // If a positive value was requested, allocate it.
257  if( size > 0 )
258  {
259  fStore = new T[ size ];
260  }
261 
262  // Note that even 0 sized vectors are allowed.
263  fNElements = size;
264 }
265 
266 template< class T >
267 TPZVec<T>::TPZVec( const int64_t size, const T& copy ) : fStore( 0 )
268 {
269 #ifndef NODEBUG
270  if( size < 0 )
271  {
272  PZError << "TPZVec constructor. Bad parameter size, then size = 0."
273  << std::endl;
274  PZError.flush();
275  fNElements = 0;
276  return;
277  }
278 #endif
279 
280  if( size )
281  {
282  fStore = new T[size];
283  }
284 
285  fNElements = size;
286 
287  for( int64_t i = 0; i < size; i++ )
288  {
289  fStore[i] = copy;
290  }
291 }
292 
293 template< class T >
295  fStore = 0;
296 
297  if( copy.fNElements > 0 )
298  fStore = new T[copy.fNElements];
299  else
300  fStore = 0;
301 
302  for(int64_t i=0; i<copy.fNElements; i++)
303  fStore[i]=copy.fStore[i];
304 
305  fNElements = copy.fNElements;
306 }
307 
308 template< class T >
309 TPZVec<T>::TPZVec(const std::initializer_list<T>& list) {
310  fStore = NULL;
311 
312  if (list.size() > 0)
313  fStore = new T[list.size()];
314 
315  auto it = list.begin();
316  auto it_end = list.end();
317  T* aux = fStore;
318  for (; it != it_end; it++, aux++)
319  *aux = *it;
320 
321  fNElements = list.size();
322 }
323 
324 template<class T>
326  if( fStore )
327  {
328  delete [] fStore;
329  }
330 }
331 
332 template< class T >
334  if(this == &copy) return *this;
335 
336  Resize(copy.NElements());
337 
338  for(int64_t i=0; i<copy.fNElements; i++)
339  fStore[i]=copy.fStore[i];
340 
341  fNElements = copy.fNElements;
342 
343  return *this;
344 }
345 
346 template< class T >
347 TPZVec<T> &TPZVec<T>::operator=(const std::initializer_list<T> &list) {
348  Resize(list.size());
349 
350  auto it = list.begin();
351  auto it_end = list.end();
352  auto *aux = fStore;
353 
354  for (; it != it_end; it++, aux++)
355  *aux = *it;
356 
357  return *this;
358 }
359 
360 // OPENED QUESTION: what to do with 0 size vectors??? Cantao (2002.01.09)
361 template< class T >
363 {
364  T* end = fStore + fNElements;
365 
366  for( T* walk = fStore; walk < end; *walk++ = a );
367 
368  return *this;
369 }
370 
371 
372 template< class T >
373 void TPZVec<T>::Resize(const int64_t newsize,const T& object) {
374 #ifndef NODEBUG
375  if(newsize<0) {
376  PZError << "TPZVec::Resize. Bad parameter newsize." << std::endl;
377  PZError.flush();
378  return;
379  }
380 #endif
381  if(newsize == fNElements) return;
382  T* newstore;
383  if(newsize) newstore = new T[newsize];
384  else newstore = 0;
385  int64_t large = (fNElements < newsize) ? fNElements : newsize;
386  int64_t i;
387  for(i=0L; i<large; i++) {
388  newstore[i] = fStore[i];
389  }
390  for(;i<newsize;i++) { // then only to case : large=fNElement < newsize
391  newstore[i] = object;
392  }
393  if(fStore) {
394  delete[] fStore;
395  fStore = 0;
396  }
397  fStore = newstore;
398  fNElements = newsize;//cedric 20/11/99 e 29/04/00
399 }
400 
401 #include <limits>
402 
403 template< class T >
404 void TPZVec<T>::Resize(const int64_t newsize) {
405 #ifndef NODEBUG
406  // int64_t nlongsize = 2147483647;
407  if(newsize<0) {
408  PZError << "TPZVec::Resize. Bad parameter newsize: " << newsize << std::endl;
409  PZError.flush();
410  }
411 #ifdef WIN32
412  // Parece que o limite no windows é
413  int sz = sizeof(T);
414  int64_t nlongsize = 1704792168;
415  if((newsize+1) > (1./sz)*nlongsize) {
416  PZError << "TPZVec::Resize. Bad parameter newsize: " << newsize << std::endl;
417  std::cout << "TPZVec::Resize. Bad parameter newsize: " << newsize << std::endl;
418  PZError.flush();
419  // DebugStop();
420  // return;
421  }
422 #endif
423 #endif
424 
425  if(newsize == fNElements) return;
426  if (newsize == 0) {
427  fNElements = 0;
428  delete[] fStore;
429  fStore = 0;
430  return;
431  }
432  T *newstore = new T[newsize];
433  int64_t large = (fNElements < newsize) ? fNElements : newsize;
434  int64_t i;
435  for(i=0L; i<large; i++) {
436  newstore[i] = fStore[i];
437  }
438  if(fStore) delete[] fStore;
439  fStore = newstore;
440  fNElements = newsize;
441 }
442 
443 template<class T>
445 {
446  this->Resize(0);
447 }
448 
449 template<class T>
450 T *TPZVec<T>::begin() const {
451  return fStore;
452 }
453 
454 template<class T>
455 T *TPZVec<T>::end() const {
456  return fStore+fNElements;
457 }
458 
459 template< class T >
460 void TPZVec<T>::Fill(const T& copy, const int64_t from, const int64_t numelem){
461 #ifndef NODEBUG
462  if(numelem<0 && numelem != -1) {
463  PZError << "TPZVec::Fill" << std::endl
464  << "It's negative parameter numelem, then numelem = "
465  << fNElements << std::endl;
466 
467  PZError.flush();
468  }
469 #endif
470 
471  int64_t first = (from < 0) ? 0 : from;
472  int64_t nel = numelem;
473  first = (first > fNElements) ? fNElements : first;
474  if (nel < 0) nel = fNElements;
475  int64_t last = (from+nel > fNElements) ? fNElements : from+nel;
476 
477  for(int64_t i=first; i<last; i++)
478  fStore[i] = copy;
479 }
480 
481 template< class T >
482 inline void TPZVec<T>::Print(std::ostream &out)
483 {
484  out << std::endl << "Number of elements = " << fNElements;
485 }
486 
487 template <class T>
488 std::ostream& operator<<( std::ostream& Out, const TPZVec< T >& v )
489 {
490  std::streamsize width = Out.width();
491 
492  const char* sep = ( width == 0 ? ", " : "" );
493 
494  int64_t size = v.NElements();
495 
496  if(size) Out << std::setw(width) << v.fStore[0];
497 
498  for( int64_t ii = 1; ii < size; ii++ )
499  {
500  Out << std::setw( width ) << sep << v.fStore[ ii ];
501  }
502 
503  return Out;
504 }
505 
506 inline std::ostream& operator<<( std::ostream& Out, const TPZVec< std::pair<double,double> >& v )
507 {
508 
509  Out << "{";
510 
511  int64_t size = v.NElements();
512 
513  if(size > 0)
514  {
515  for( int64_t ii = 0; ii < size; ii++ )
516  {
517  Out << "{" << v[ii].first << ',' << v[ii].second << "}";
518  if( ii < size-1) Out << ",";
519  }
520  }
521 
522  Out << "}";
523  return Out;
524 }
525 
526 
527 #endif
void Print(std::ostream &out=std::cout)
Prints the structural information of the vector object to the output stream. This method will not p...
Definition: pzvec.h:482
void MigratePages()
Definition: pzvec.h:74
TPZVec()
Creates a vector with size 0.
Definition: pzvec.h:237
virtual void resize(const int64_t newsize)
Definition: pzvec.h:213
void ReallocForNuma()
Definition: pzvec.h:77
T & operator[](const int64_t index) const
Access operator, will perform bounds checking unless the variable NODEBUG is defined.
Definition: pzvec.h:148
Defines PZError.
Contains declaration of the TPZPageMigrationManager class which implements methods to migrate data in...
This class implements a simple vector storage scheme for a templated class T. Utility.
Definition: pzgeopoint.h:19
int64_t size() const
Returns the number of elements of the vector.
Definition: pzvec.h:196
virtual void Resize(const int64_t newsize, const T &object)
Resizes the vector object reallocating the necessary storage, copying the existing objects to the new...
Definition: pzvec.h:373
#define DebugStop()
Returns a message to user put a breakpoint in.
Definition: pzerror.h:20
virtual void clear()
Empty the vector, make its size zero.
Definition: pzvec.h:444
T * begin() const
Casting operator. Returns The fStore pointer.
Definition: pzvec.h:450
TPZVec< T > & operator=(const TPZVec< T > &copy)
will copy the vector into the current vector.
Definition: pzvec.h:333
T * fStore
Allocated storage for the vector object.
Definition: pzvec.h:230
int64_t fNElements
Number of elements of the vector object.
Definition: pzvec.h:233
void migrate_to_local(char *start, uint64_t sz_in_bytes)
virtual ~TPZVec()
destructor, will delete the storage allocated
Definition: pzvec.h:325
void Fill(const T &copy, const int64_t from=0, const int64_t numelem=-1)
Will fill the elements of the vector with a copy object.
Definition: pzvec.h:460
int64_t NElements() const
Returns the number of elements of the vector.
Definition: pzvec.h:190
Contains the declaration of TPZFlopCounter class and TPZCounter struct.
T * end() const
Returns a pointer to the last+1 element.
Definition: pzvec.h:455
#define PZError
Defines the output device to error messages and the DebugStop() function.
Definition: pzerror.h:15