SPHinXsys  alpha version
array.h
Go to the documentation of this file.
1 
7 #ifndef ARRAY_SIMBODY_H
8 #define ARRAY_SIMBODY_H
9 
10 
11 
12 #include "base_data_package.h"
13 
14 #include <iostream>
15 #include <string>
16 #include <cstdio>
17 #include <stdexcept>
18 
19 static const int Array_CAPMIN = 1;
20 
21 namespace SPH {
30  template<class T> class Array
31  {
32  protected:
34  int size_;
36  int capacity_;
43  T *array_;
44  public:
56  explicit Array(const T &aDefaultValue=T(),int aSize=0,int aCapacity=Array_CAPMIN)
57  {
58  setNull();
59  defaultValue_ = aDefaultValue;
60  int newCapacity;
61  int min = aSize + 1;
62  if(min < aCapacity) min = aCapacity;
63  computeNewCapacity(min,newCapacity);
64  ensureCapacity(newCapacity);
65  size_ = aSize;
66  if(size_<0) size_=0;
67  }
73  Array(const Array<T> &aArray)
74  {
75  setNull();
76  *this = aArray;
77  }
84  virtual ~Array()
85  {
86  if(array_!=nullptr) { delete[] array_; array_ = nullptr; }
87  }
88  private:
92  void setNull()
93  {
94  size_ = 0;
95  capacityIncrement_ = -1;
96  capacity_ = 0;
97  array_ = nullptr;
98  }
99  //=============================================================================
100  // OPERATORS
101  //=============================================================================
102  public:
106  bool arrayEquals(const Array<T> &aArray) const
107  {
108  return *this == aArray;
109  }
128  T& operator[](int aIndex) const
129  {
130  return(array_[aIndex]);
131  }
140  Array<T>& operator=(const Array<T> &aArray)
141  {
142  size_ = aArray.size_;
143  capacity_ = aArray.capacity_;
145  defaultValue_ = aArray.defaultValue_;
146 
147  // ARRAY
148  if(array_!=nullptr) delete[] array_;
149  array_ = new T[capacity_];
150  for(int i = 0;i < capacity_; i++) array_[i] = aArray.array_[i];
151 
152  return(*this);
153  }
164  bool operator==(const Array<T> &aArray) const
165  {
166  if(size_ != aArray.size_) return(false);
167 
168  int i;
169  for(i=0; i<size_; i++)
170  {
171  if( !(array_[i]==aArray.array_[i]) ) return(false);
172  }
173 
174  return(true);
175  }
186  friend std::ostream& operator<<(std::ostream &aOut,const Array<T> &aArray)
187  {
188  int i;
189  for(i=0; i<aArray.getSize(); i++) {
190  aOut << " ";
191  aOut << aArray[i];
192  }
193 
194  return(aOut);
195  }
196 
197  friend std::istream& operator>>(std::istream& in, Array<T>& out)
198  {
199  return in;
200  }
222  bool computeNewCapacity(int aMinCapacity,int &rNewCapacity)
223  {
224  rNewCapacity = capacity_;
225  if(rNewCapacity < Array_CAPMIN) rNewCapacity = Array_CAPMIN;
226 
227  if(capacityIncrement_ == 0) {
228  std::cout << "Array.computeNewCapacity: WARN- capacity is set";
229  std::cout << " not to increase (i.e., capacityIncrement_==0).\n";
230  return(false);
231  }
232 
233  while(rNewCapacity < aMinCapacity) {
234  if(capacityIncrement_ < 0) {
235  rNewCapacity = 2 * rNewCapacity;
236  } else {
237  rNewCapacity = rNewCapacity + capacityIncrement_;
238  }
239  }
240 
241  return(true);
242  }
250  bool ensureCapacity(int aCapacity)
251  {
252  if(aCapacity < Array_CAPMIN) aCapacity = Array_CAPMIN;
253  if(capacity_ >= aCapacity) return(true);
254 
255  int i;
256  T *newArray = new T[aCapacity];
257  if(newArray == nullptr)
258  {
259  std::cout << "Array.ensureCapacity: ERR- failed to increase capacity.\n";
260  return(false);
261  }
262 
263  if(array_ != nullptr) {
264  for(i =0; i < size_; i++) newArray[i] = array_[i];
265  for(i = size_; i < aCapacity; i++) newArray[i] = defaultValue_;
266  delete []array_;
267  array_ = nullptr;
268  } else {
269  for(i = 0; i < aCapacity; i++) newArray[i] = defaultValue_;
270  }
271 
272  capacity_ = aCapacity;
273  array_ = newArray;
274 
275  return(true);
276  }
277 
285  void trim()
286  {
287  int newCapacity = size_ + 1;
288  if(newCapacity >= capacity_) return;
289  if(newCapacity < Array_CAPMIN) newCapacity = Array_CAPMIN;
290 
291  int i;
292  T *newArray = new T[newCapacity];
293  if(newArray==nullptr) {
294  std::cout << "Array.trim: ERR- unable to allocate temporary array.\n";
295  return;
296  }
297 
298  for(i = 0; i < size_; i++) newArray[i] = array_[i];
299 
300  delete[] array_;
301 
302  array_ = newArray;
303 
304  capacity_ = newCapacity;
305  }
309  int getCapacity() const
310  {
311 
312  return(capacity_);
313  }
322  void setCapacityIncrement(int aIncrement)
323  {
324  capacityIncrement_ = aIncrement;
325  }
326 
331  {
332  return(capacityIncrement_);
333  }
348  bool setSize(int aSize)
349  {
350  if(aSize == size_) return(true);
351  if(aSize < 0) aSize = 0;
352  bool success = true;
353  if(aSize < size_)
354  {
355  int i;
356  for(i = (size_ - 1);i >= aSize; i--) array_[i] = defaultValue_;
357  size_ = aSize;
358  } else if(aSize <= capacity_) {
359  size_ = aSize;
360  } else {
361  int newCapacity;
362  success = computeNewCapacity(aSize+1, newCapacity);
363  if(!success) return(false);
364  success = ensureCapacity(newCapacity);
365  if(success) size_ = aSize;
366  }
367 
368  return(success);
369  }
375  int getSize() const
376  {
377  return(size_);
378  }
380  int size() const {return getSize();}
381 
389  int append(const T &aValue)
390  {
391  if((size_ + 1) >= capacity_) {
392  int newCapacity;
393  bool success;
394  success = computeNewCapacity(size_ + 1, newCapacity);
395  if(!success) return(size_);
396  success = ensureCapacity(newCapacity);
397  if(!success) return(size_);
398  }
399 
400  array_[size_] = aValue;
401  size_++;
402 
403  return(size_);
404  }
412  int append(const Array<T> &aArray)
413  {
414  int i,n = aArray.getSize();
415  for(i = 0; i < n; i++) {
416  append(aArray[i]);
417  }
418 
419  return(size_);
420  }
421 
430  int append(int aSize,const T *aArray)
431  {
432  if(aSize < 0) return(size_);
433  if(aArray == nullptr) return(size_);
434 
435  int i;
436  for(i = 0;i < aSize; i++) {
437  append(aArray[i]);
438  }
439 
440  return(size_);
441  }
457  int insert(int aIndex,const T &aValue)
458  {
459  if(aIndex<0) {
460  std::cout << "Array.insert: ERR- aIndex was less than 0.\n";
461  return(size_);
462  }
463 
464  if(aIndex >= size_) {
465  setSize(aIndex+1);
466  array_[aIndex] = aValue;
467  return(size_);
468  }
469 
470  if((size_ + 1) >= capacity_) {
471  int newCapacity;
472  bool success;
473  success = computeNewCapacity(size_ + 1,newCapacity);
474  if(!success) return(size_);
475  success = ensureCapacity(newCapacity);
476  if(!success) return(size_);
477  }
478 
479  int i;
480  for(i = size_; i > aIndex; i--) {
481  array_[i] = array_[i-1];
482  }
483 
484  array_[aIndex] = aValue;
485  size_++;
486 
487  return(size_);
488  }
501  int remove(int aIndex)
502  {
503  if(aIndex < 0) {
504  std::cout << "Array.remove: ERR- aIndex was less than 0.\n";
505  return(size_);
506  }
507  if(aIndex >= size_) {
508  std::cout << "Array.remove: ERR- aIndex was greater than or equal the ";
509  std::cout << "size of the array.\n";
510  return(size_);
511  }
512 
513  int i;
514  size_--;
515  for(i = aIndex; i < size_; i++) {
516  array_[i] = array_[i+1];
517  }
519 
520  return(size_);
521  }
531  void set(int aIndex,const T &aValue)
532  {
533  if(aIndex < 0) return;
534 
535  bool success = false;
536  if((aIndex+2) >= capacity_) {
537  int newCapacity;
538  success = computeNewCapacity(aIndex+2, newCapacity);
539  if(!success) return;
540  success = ensureCapacity(newCapacity);
541  if(!success) return;
542  }
543  array_[aIndex] = aValue;
544  if(aIndex >= size_) size_ = aIndex + 1;
545  }
551  T* get()
552  {
553  return(array_);
554  }
561  const T* get() const
562  {
563  return(array_);
564  }
579  const T& get(int aIndex) const
580  {
581  if((aIndex < 0) || (aIndex >= size_)) {
582  std::stringstream msg;
583  msg << "Array index out of bounds. " << ".";
584  throw (msg.str(),__FILE__,__LINE__);
585  }
586  return(array_[aIndex]);
587  }
594  const T& getLast() const
595  {
596  if(size_ <= 0) {
597  std::stringstream msg;
598  msg << "Array is empty. " << ".";
599  throw (msg.str(),__FILE__,__LINE__);
600  }
601  return(array_[size_ - 1]);
602  }
609  T& updLast() const
610  {
611  if(size_ <= 0) {
612  std::stringstream msg;
613  msg << "Array is empty. " << ".";
614  throw (msg.str(),__FILE__,__LINE__);
615  }
616  return(array_[size_ - 1]);
617  }
626  int findIndex(const T &aValue) const
627  {
628  for(int i = 0; i < size_; i++) if(array_[i] == aValue) return i;
629  return -1;
630  }
631 
640  int rfindIndex(const T &aValue) const
641  {
642  for(int i=size_ - 1; i >= 0; i--) if(array_[i]==aValue) return i;
643  return -1;
644  }
645  };
647 }
648 #endif //ARRAY_SIMBODY_H
int capacityIncrement_
Definition: array.h:39
bool arrayEquals(const Array< T > &aArray) const
A non-operator version of operator ==.
Definition: array.h:106
Definition: array.h:30
int append(const T &aValue)
Append a value onto the array.
Definition: array.h:389
int getCapacityIncrement() const
Get the amount by which the capacity is increased.
Definition: array.h:330
bool setSize(int aSize)
Definition: array.h:348
T * array_
Definition: array.h:43
int rfindIndex(const T &aValue) const
Definition: array.h:640
const T & getLast() const
Definition: array.h:594
int append(int aSize, const T *aArray)
Append an array of values.
Definition: array.h:430
Array(const T &aDefaultValue=T(), int aSize=0, int aCapacity=Array_CAPMIN)
Default constructor.
Definition: array.h:56
void setCapacityIncrement(int aIncrement)
Set the amount by which the capacity is increased when the capacity of of the array in exceeded...
Definition: array.h:322
int size_
Definition: array.h:34
Array(const Array< T > &aArray)
Copy constructor.
Definition: array.h:73
int getSize() const
Get the size of the array.
Definition: array.h:375
virtual ~Array()
Destructor.
Definition: array.h:84
T & updLast() const
Definition: array.h:609
T defaultValue_
Definition: array.h:41
void trim()
Definition: array.h:285
bool computeNewCapacity(int aMinCapacity, int &rNewCapacity)
Compute a new capacity that is at least as large as a specified minimum capacity; this method does no...
Definition: array.h:222
int findIndex(const T &aValue) const
Definition: array.h:626
int insert(int aIndex, const T &aValue)
Insert a value into the array at a specified index.
Definition: array.h:457
int size() const
Definition: array.h:380
bool ensureCapacity(int aCapacity)
Ensure that the capacity of this array is at least the specified amount. Note that the newly allocate...
Definition: array.h:250
int getCapacity() const
Definition: array.h:309
int capacity_
Definition: array.h:36
T & operator[](int aIndex) const
Get the array element at a specified index. This overloaded operator can be used both to set and get ...
Definition: array.h:128
int append(const Array< T > &aArray)
Append an array of values.
Definition: array.h:412
bool operator==(const Array< T > &aArray) const
Determine if two arrays are equal.
Definition: array.h:164
Definition: solid_body_supplementary.cpp:9
Array< T > & operator=(const Array< T > &aArray)
Assign this array to a specified array. This operator makes a complete copy of the specified array; a...
Definition: array.h:140