7 #ifndef ARRAY_SIMBODY_H 8 #define ARRAY_SIMBODY_H 12 #include "base_data_package.h" 19 static const int Array_CAPMIN = 1;
56 explicit Array(
const T &aDefaultValue=T(),
int aSize=0,
int aCapacity=Array_CAPMIN)
62 if(min < aCapacity) min = aCapacity;
108 return *
this == aArray;
169 for(i=0; i<
size_; i++)
186 friend std::ostream& operator<<(std::ostream &aOut,const Array<T> &aArray)
189 for(i=0; i<aArray.getSize(); i++) {
197 friend std::istream& operator>>(std::istream& in,
Array<T>& out)
225 if(rNewCapacity < Array_CAPMIN) rNewCapacity = Array_CAPMIN;
228 std::cout <<
"Array.computeNewCapacity: WARN- capacity is set";
229 std::cout <<
" not to increase (i.e., capacityIncrement_==0).\n";
233 while(rNewCapacity < aMinCapacity) {
235 rNewCapacity = 2 * rNewCapacity;
252 if(aCapacity < Array_CAPMIN) aCapacity = Array_CAPMIN;
256 T *newArray =
new T[aCapacity];
257 if(newArray ==
nullptr)
259 std::cout <<
"Array.ensureCapacity: ERR- failed to increase capacity.\n";
287 int newCapacity =
size_ + 1;
289 if(newCapacity < Array_CAPMIN) newCapacity = Array_CAPMIN;
292 T *newArray =
new T[newCapacity];
293 if(newArray==
nullptr) {
294 std::cout <<
"Array.trim: ERR- unable to allocate temporary array.\n";
298 for(i = 0; i <
size_; i++) newArray[i] =
array_[i];
350 if(aSize ==
size_)
return(
true);
351 if(aSize < 0) aSize = 0;
363 if(!success)
return(
false);
365 if(success)
size_ = aSize;
395 if(!success)
return(
size_);
397 if(!success)
return(
size_);
415 for(i = 0; i < n; i++) {
432 if(aSize < 0)
return(
size_);
433 if(aArray ==
nullptr)
return(
size_);
436 for(i = 0;i < aSize; i++) {
460 std::cout <<
"Array.insert: ERR- aIndex was less than 0.\n";
464 if(aIndex >=
size_) {
474 if(!success)
return(
size_);
476 if(!success)
return(
size_);
480 for(i =
size_; i > aIndex; i--) {
501 int remove(
int aIndex)
504 std::cout <<
"Array.remove: ERR- aIndex was less than 0.\n";
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";
515 for(i = aIndex; i <
size_; i++) {
531 void set(
int aIndex,
const T &aValue)
533 if(aIndex < 0)
return;
535 bool success =
false;
579 const T&
get(
int aIndex)
const 581 if((aIndex < 0) || (aIndex >=
size_)) {
582 std::stringstream msg;
583 msg <<
"Array index out of bounds. " <<
".";
584 throw (msg.str(),__FILE__,__LINE__);
597 std::stringstream msg;
598 msg <<
"Array is empty. " <<
".";
599 throw (msg.str(),__FILE__,__LINE__);
612 std::stringstream msg;
613 msg <<
"Array is empty. " <<
".";
614 throw (msg.str(),__FILE__,__LINE__);
628 for(
int i = 0; i <
size_; i++)
if(
array_[i] == aValue)
return i;
642 for(
int i=
size_ - 1; i >= 0; i--)
if(
array_[i]==aValue)
return i;
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
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