Array< T > Class Template Reference

+ This reference page is linked to from the following overview topics:

#include <Array.h>

Class Description

template<class T>
class MaxSDK::Array< T >

A generic array container, with proper support for non-POD types.

This template class is a generaic, dynamic array container, similar to the STL class "vector". Whereas the classical 3ds Max SDK class Tab supports only POD types, this class supports non-POD types as well.

Note
POD stands for "plain old data", and basically denotes any data type which does not need to be constructed, destructed, and which can be copied with memcpy() rather than requiring a copy operator to be called.
+ Inheritance diagram for Array< T >:

Public Types

typedef const voidelem2
 

Public Member Functions

typedef int (__cdecl *CompareFnc)(const void *elem1
 Type of function to pass to sort(). More...
 
 Array ()
 Initializes an empty array. More...
 
 Array (size_t initUsedLength, const T &defaultVal=T(), size_t initGrowLength=kDefaultGrowthLength)
 Initializes an array with an initial size. More...
 
 Array (const Array< T > &src)
 Copy constructor. More...
 
 ~Array ()
 Destructor. Destroys every element stored in the array, and frees the allocated storage. More...
 
Array< T > & operator= (const Array< T > &src)
 Copy operator. More...
 
bool operator== (const Array< T > &op) const
 Equality operator. More...
 
Array< T > & setAt (size_t index, const T &value)
 Sets a copy of value at the given index. More...
 
Array< T > & setAll (const T &value)
 Sets all the elements of the array to the given value. More...
 
size_t append (const T &value)
 Appends a copy of value to the array. More...
 
Array< T > & append (const T *values, size_t count)
 Appends one or more element(s) to the array. More...
 
Array< T > & append (const Array< T > &array)
 Appends the contents of another array to this array. More...
 
Array< T > & insertAt (size_t index, const T &value)
 Inserts a single value, at a given location, into this array. More...
 
Array< T > & insertAt (size_t index, const T *values, size_t count)
 Inserts a one or more value(s), at a given location, into this array. More...
 
Array< T > & removeAt (size_t index)
 Removes a single element from the array. More...
 
bool remove (const T &value, size_t start=0)
 Searches for a value in the array and, if it is found, removes it from the array. More...
 
Array< T > & removeFirst ()
 Removes the first element of the array. More...
 
Array< T > & removeLast ()
 Removes the last element of the array. More...
 
Array< T > & removeAll ()
 Removes all the elements from the array. More...
 
Array< T > & removeSubArray (size_t startIndex, size_t endIndex)
 Removes a subset of the array. More...
 
bool contains (const T &value, size_t start=0) const
 Determines if a value is stored in the array. More...
 
bool find (const T &value, size_t &foundAt, size_t start=0) const
 Searches for a value in the array. More...
 
size_t find (const T &value) const
 Searches for a value in the array. More...
 
size_t findFrom (const T &value, size_t start) const
 Searches for a value in the array, starting at a given index. More...
 
size_t length () const
 Returns the number of used elements (as opposed to simply allocated/reserved) in the array. More...
 
bool isEmpty () const
 Returns true if the number of used elements in the array is 0; returns false otherwise. More...
 
size_t lengthUsed () const
 Returns the number of elements used (as opposed to simply allocated/reserved) in the array. More...
 
Array< T > & setLengthUsed (size_t length, const T &defaultVal=T())
 Sets the number of elements used (as opposed to simply allocated/reserved) in the array. More...
 
size_t lengthReserved () const
 Returns the number of elements allocated/reserved (as opposed to actually used) in the array. More...
 
Array< T > & setLengthReserved (size_t length)
 Sets the number of elements allocated/reserved (as opposed to actually used) in the array. More...
 
void reserve (size_t capacity)
 Alias for setLengthReserved. More...
 
size_t growLength () const
 Returns the growth length of the array. More...
 
Array< T > & setGrowLength (size_t)
 Sets the growth length of the array. More...
 
Array< T > & reverse ()
 Reverses the sequence of elements in the array. More...
 
Array< T > & swap (size_t i1, size_t i2)
 Swaps two elements in this array. More...
 
void sort (CompareFnc cmp)
 Sorts the elements of the array using a custom comparison function. More...
 
bool isValidIndex (size_t) const
 Returns whether the given array index is valid for this array. More...
 
T & operator[] (size_t i)
 Subscript operator. More...
 
const T & operator[] (size_t i) const
 
const T & at (size_t index) const
 Same as subscript operator. More...
 
T & at (size_t index)
 
T & first ()
 Accesses the first element in the array. More...
 
const T & first () const
 
T & last ()
 Accesses the last element in the array. More...
 
const T & last () const
 
const T * asArrayPtr () const
 Returns the array storage as a C-style array pointer. More...
 
T * asArrayPtr ()
 

Protected Types

enum  { kArrayGrowthThreshold = 0x10000, kDefaultGrowthLength = 8 }
 

Static Protected Member Functions

static size_t quickSortPartition (T *data, size_t first, size_t last, CompareFnc cmp)
 The partition portion of the QuickSort algorithm. More...
 
static void quickSortRecursive (T *data, size_t first, size_t last, CompareFnc cmp)
 Recursive QuickSort function used to sort the elements of the array. More...
 
static void handleOutOfMemory ()
 Utility function, called when the array fails to allocate memory. More...
 
static T * ArrayAllocate (size_t len)
 Allocates an array of elements without constructing them. More...
 
static void ArrayConstruct (T *arrayBegin, size_t len, const T &defaultVal)
 Constructs an array of elements. More...
 
static void ArrayDeAllocate (T *arrayBegin)
 De-allocates an array of elements without destructing them. More...
 
static void ArrayDestruct (T *arrayBegin, size_t len)
 Destructs an array of elements. More...
 
static void ArrayCopy (T *pCopy, const T *pSource, size_t nCount)
 Copies an array of elements to an already-constructed buffer. More...
 
static void ArrayCopyOverlap (T *pCopy, const T *pSource, size_t nCount)
 Copies an array of elements when the target and destination memory buffers may overlap. More...
 
static void ArrayCopyConstruct (T *pCopy, const T *pSource, size_t nCount)
 Copies and array of elements to a non-constructed. More...
 

Protected Attributes

T * mpArray
 Pointer to the storage buffer. More...
 
size_t mReservedLen
 The reserved length (in number of elements, not bytes). More...
 
size_t mUsedLen
 The used length (in number of elements, not bytes). More...
 
size_t mGrowLen
 The growth length. See setGrowLength(). More...
 

Additional Inherited Members

- Static Public Member Functions inherited from MaxHeapOperators
static UtilExport voidoperator new (size_t size)
 Standard new operator used to allocate objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e)
 Standard new operator used to allocate objects if there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate objects that takes the type of memory, filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate arrays of objects. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport void operator delete (void *ptr)
 Standard delete operator used to deallocate an object If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an object If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an object that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, void *placement_ptr)
 Placement new operator. More...
 
static UtilExport void operator delete (void *ptr, void *placement_ptr)
 Placement delete operator. More...
 

Member Typedef Documentation

typedef const void* elem2

Member Enumeration Documentation

anonymous enum
protected
Enumerator
kArrayGrowthThreshold 

The maximum extra space (in bytes) that may be reserved when the buffer is enlarged.

The array will usually enlarge the buffer by half its size whenever it runs out of space, unless that enlargement would exceed this value.

kDefaultGrowthLength 

The default growth length. See setGrowLength().

321  {
327  kArrayGrowthThreshold = 0x10000,
330  };
The default growth length. See setGrowLength().
Definition: Array.h:329
The maximum extra space (in bytes) that may be reserved when the buffer is enlarged.
Definition: Array.h:327

Constructor & Destructor Documentation

Array ( )

Initializes an empty array.

23 : mpArray(NULL),
24  mReservedLen(0),
25  mUsedLen(0),
27 {
28  if(mGrowLen < 1) {
29  // Growth length needs to be at least 1.
30  mGrowLen = 1;
31  }
32 }
#define NULL
Definition: autoptr.h:20
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
The default growth length. See setGrowLength().
Definition: Array.h:329
Array ( size_t  initUsedLength,
const T &  defaultVal = T(),
size_t  initGrowLength = kDefaultGrowthLength 
)

Initializes an array with an initial size.

Parameters
[in]initUsedLength- Number of elements initially allocated in the array.
[in]defaultVal- The default value for the elements initially allocated.
[in]initGrowLength- The initial growth length of the array. For more information on the growth length, see setGrowLength().
35 : mpArray(NULL),
36  mReservedLen(0),
37  mUsedLen(0),
39 {
40  if(mGrowLen < 1) {
41  // Growth length needs to be at least 1.
42  mGrowLen = 1;
43  }
44 
45  // Re-size the array if a non-zero length was specified.
46  if(usedLength > 0) {
47  mpArray = ArrayAllocate(usedLength);
48  if (mpArray == NULL) {
50  }
51  else {
52 
53  // Initialize the new elements
54  ArrayConstruct(mpArray, usedLength, defaultVal);
55 
56  mReservedLen = usedLength;
57  mUsedLen = usedLength;
58  }
59  }
60 }
static void ArrayConstruct(T *arrayBegin, size_t len, const T &defaultVal)
Constructs an array of elements.
Definition: Array.inline.h:36
#define NULL
Definition: autoptr.h:20
size_t growLength() const
Returns the growth length of the array.
Definition: Array.inline.h:178
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:341
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:23
Array ( const Array< T > &  src)

Copy constructor.

Copies the contents of another array.

Parameters
[in]src- Array from which the elements are copied.
70 : mpArray(NULL),
71  mReservedLen(src.mReservedLen),
72  mUsedLen(src.mUsedLen),
73  mGrowLen(src.mGrowLen)
74 {
75  if (mReservedLen > 0) {
77  if (mpArray == NULL) {
79  mReservedLen = 0;
80  mUsedLen = 0;
81  }
82  else {
83  ArrayCopyConstruct(mpArray, src.mpArray, mUsedLen);
84  }
85  }
86 }
#define NULL
Definition: autoptr.h:20
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:129
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:341
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:23
~Array ( )

Destructor. Destroys every element stored in the array, and frees the allocated storage.

89 {
90  if (mpArray != NULL) {
93  }
94 }
#define NULL
Definition: autoptr.h:20
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:48

Member Function Documentation

typedef int ( __cdecl *  CompareFnc) const

Type of function to pass to sort().

Must return:
  • < 0 if elem1 is smaller than elem2,
  • > 0 if elem 1 is greater,
  • or 0 if they're equal.
Array< T > & operator= ( const Array< T > &  src)

Copy operator.

Copies the contents of another array.

Parameters
[in]src- Array from which the elements are copied.
Returns
A reference to 'this'.
106 {
107  if (this != &src) {
108  // Re-allocate the buffer if necessary
109  if (mReservedLen < src.mUsedLen) {
110  // Destroy the existing list
111  if (mpArray != NULL) {
114  }
115  // Allocate a new buffer
116  mReservedLen = src.mUsedLen;
118  if (mpArray == NULL) { // ...so this only happens if failure.
120  mReservedLen = 0;
121  mUsedLen = 0;
122  return *this;
123  }
124  // Copy the list
125  mUsedLen = src.mUsedLen;
126  ArrayCopyConstruct(mpArray, src.mpArray, mUsedLen);
127  }
128  else if(mUsedLen < src.mUsedLen) {
129  // The entire destination list is to be overwritten
130  ArrayCopy(mpArray, src.mpArray, mUsedLen);
131  // Remaining elements need to be added to the list
132  ArrayCopyConstruct(mpArray + mUsedLen, src.mpArray + mUsedLen, src.mUsedLen - mUsedLen);
133  mUsedLen = src.mUsedLen;
134  }
135  else if(mUsedLen > src.mUsedLen) {
136  // Copy the entire source list.
137  ArrayCopy(mpArray, src.mpArray, src.mUsedLen);
138  // Truncate unused elements in the destination list.
139  ArrayDestruct(mpArray + src.mUsedLen, mUsedLen - src.mUsedLen);
140  mUsedLen = src.mUsedLen;
141  }
142  else {
143  // Lists are of identical size; simply copy the entire contents
144  ArrayCopy(mpArray, src.mpArray, mUsedLen);
145  }
146  }
147  return *this;
148 }
#define NULL
Definition: autoptr.h:20
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayCopy(T *pCopy, const T *pSource, size_t nCount)
Copies an array of elements to an already-constructed buffer.
Definition: Array.inline.h:67
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:129
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:341
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:48
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:23
bool operator== ( const Array< T > &  op) const

Equality operator.

Parameters
[in]op- Array to be compared to 'this'.
Returns
true if and only if both arrays contain the same number of elements and each of these elements if equal to the corresponding element from the other Array.
158 {
159  if (mUsedLen == cpr.mUsedLen)
160  {
161  for (size_t i = 0; i < mUsedLen; i++)
162  if (mpArray[i] != cpr.mpArray[i])
163  return false;
164  return true;
165  }
166  return false;
167 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
T & operator[] ( size_t  i)
inline

Subscript operator.

Parameters
[in]i- Index of array element to access. This index must be within the array bounds. If the index is out of bounds it will throw a MaxSDK::Util::MaxOutOfRangeException exception.
Returns
A reference to the array element at the specified index.
Remarks
Does implement bounds checking.
200 {
201  if (!isValidIndex(i))
202  {
203  DbgAssert(false);
204  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::operator[]"));
205  }
206  return mpArray[i];
207 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:54
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
const T & operator[] ( size_t  i) const
inline
210 {
211  if (!isValidIndex(i))
212  {
213  DbgAssert(false);
214  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::operator[]"));
215  }
216  return mpArray[i];
217 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:54
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
const T & at ( size_t  index) const
inline

Same as subscript operator.

Parameters
[in]index- Index of array element to access. This index must be within the array bounds.
Returns
A reference to the array element at the specified index.
Remarks
Does not implement bounds checking.
230 {
231  if (!isValidIndex(i))
232  {
233  DbgAssert(false);
234  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::at()"));
235  }
236  return mpArray[i];
237 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:54
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
T & at ( size_t  index)
inline
220 {
221  if (!isValidIndex(i))
222  {
223  DbgAssert(false);
224  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::at()"));
225  }
226  return mpArray[i];
227 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:54
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
Array< T > & setAt ( size_t  index,
const T &  value 
)
inline

Sets a copy of value at the given index.

Parameters
[in]index- The position in the array where a copy of value is placed. This index must be within the array bounds.
[in]value- a reference to the original object.
Returns
A reference to 'this'.
Remarks
Does not implement bounds checking.
240 {
241  if (!isValidIndex(i))
242  {
243  DbgAssert(false);
244  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::setAt()"));
245  }
246  mpArray[i] = value;
247  return *this;
248 }
FPValue value
Definition: functions.h:635
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:54
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
Array< T > & setAll ( const T &  value)

Sets all the elements of the array to the given value.

Parameters
[in]value- The value to which the elements of the array are set.
Returns
A reference to 'this'.
173 {
174  for (size_t i = 0; i < mUsedLen; i++) {
175  mpArray[i] = value;
176  }
177  return *this;
178 }
FPValue value
Definition: functions.h:635
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
T & first ( )
inline

Accesses the first element in the array.

Returns
A reference to the first element of the array.
Remarks
It is invalid to call this on an empty array.
251 {
252  DbgAssert(!this->isEmpty());
253  DbgAssert(mpArray != nullptr);
254  if (this->isEmpty() || (nullptr == mpArray))
255  {
256  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::first() on an empty array"));
257  }
258  return mpArray[0];
259 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:163
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:70
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
const T & first ( ) const
inline
262 {
263  DbgAssert(!this->isEmpty());
264  DbgAssert(mpArray != nullptr);
265  if (this->isEmpty() || (nullptr == mpArray))
266  {
267  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::first() on an empty array"));
268  }
269  return mpArray[0];
270 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:163
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:70
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
T & last ( )
inline

Accesses the last element in the array.

Returns
A reference to the last element of the array.
Remarks
It is invalid to call this on an empty array.
273 {
274  DbgAssert(!this->isEmpty());
275  DbgAssert(mpArray != nullptr);
276  if (this->isEmpty() || (nullptr == mpArray))
277  {
278  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::last() on an empty array"));
279  }
280  return mpArray[mUsedLen-1];
281 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:163
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:70
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
const T & last ( ) const
inline
284 {
285  DbgAssert(!this->isEmpty());
286  DbgAssert(mpArray != nullptr);
287  if (this->isEmpty() || (nullptr == mpArray))
288  {
289  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::last() on an empty array"));
290  }
291  return mpArray[mUsedLen-1];
292 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:163
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:70
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:122
#define DbgAssert(expr)
Definition: assert1.h:74
size_t append ( const T &  value)
inline

Appends a copy of value to the array.

Parameters
[in]value- A reference to the original value.
Returns
The number of elements in the array prior to the append operation.
295 {
297  return mUsedLen-1;
298 }
FPValue value
Definition: functions.h:635
Array< T > & insertAt(size_t index, const T &value)
Inserts a single value, at a given location, into this array.
Definition: Array.imp.h:210
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
Array< T > & append ( const T *  values,
size_t  count 
)

Appends one or more element(s) to the array.

Parameters
[in]values- A pointer to a C-style array of elements, from which the appended elements will be copied.
[in]count- The number of elements to be appended.
Returns
A reference to 'this'.
301 {
302  return insertAt(mUsedLen, values, count);
303 }
Array< T > & insertAt(size_t index, const T &value)
Inserts a single value, at a given location, into this array.
Definition: Array.imp.h:210
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
Array< T > & append ( const Array< T > &  array)

Appends the contents of another array to this array.

Parameters
[in]array- The array from which elements are to be appended.
Returns
A reference to 'this'.
187 {
188  size_t otherLen = otherArray.length();
189  if (otherLen == 0) {
190  return *this;
191  }
192  size_t newLen = mUsedLen + otherLen;
193  if (newLen > mReservedLen) {
194  setLengthReserved(newLen);
195  }
196 
197  ArrayCopyConstruct(mpArray + mUsedLen, otherArray.mpArray, otherLen);
198 
199  mUsedLen = newLen;
200  return *this;
201 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
Array< T > & setLengthReserved(size_t length)
Sets the number of elements allocated/reserved (as opposed to actually used) in the array...
Definition: Array.imp.h:438
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:129
Array< T > & insertAt ( size_t  index,
const T &  value 
)

Inserts a single value, at a given location, into this array.

Parameters
[in]index- The index at which the element is to be inserted. This index must be smaller or equal to the used length of the array.
[in]value- The value to be inserted.
Returns
A reference to 'this'.
211 {
212  DbgAssert(index >= 0 && index <= mUsedLen);
213 
214  if (mUsedLen >= mReservedLen) {
215  size_t growth = (mUsedLen * sizeof(T)) < kArrayGrowthThreshold ?
216  (mUsedLen / 2) : kArrayGrowthThreshold / sizeof(T);
217  setLengthReserved(mUsedLen + __max(growth, mGrowLen));
218  }
219 
220  if (index != mUsedLen) {
221 
222  // Initialize the new member of the array
224 
225  // Copy the remainder of the list that needs to be shifted
226  for(size_t i = mUsedLen - 1; i > index; --i) {
227  mpArray[i] = mpArray[i-1];
228  }
229 
230  // Now copy the new element into the array
231  mpArray[index] = value;
232  }
233  else {
234  // Add the new value to the end of the list
236  }
237 
238  mUsedLen++;
239  return *this;
240 }
FPValue value
Definition: functions.h:635
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
Array< T > & setLengthReserved(size_t length)
Sets the number of elements allocated/reserved (as opposed to actually used) in the array...
Definition: Array.imp.h:438
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:129
#define DbgAssert(expr)
Definition: assert1.h:74
The maximum extra space (in bytes) that may be reserved when the buffer is enlarged.
Definition: Array.h:327
Array< T > & insertAt ( size_t  index,
const T *  values,
size_t  count 
)

Inserts a one or more value(s), at a given location, into this array.

Parameters
[in]index- The index at which the element is to be inserted. This index must be smaller or equal to the used length of the array.
[in]values- A pointer to a C-style array of elements, from which the inserted elements will be copied.
[in]count- The number of elements to be inserted.
Returns
A reference to 'this'.
243 {
244  DbgAssert(index >= 0 && index <= mUsedLen);
245 
246  if(index <= mUsedLen) {
247 
248  size_t lastInsertIndex = index + count - 1;
249 
250  // Increase the allocated memory if necessary
251  size_t newUsedLen = mUsedLen + count;
252  if(newUsedLen > mReservedLen) {
253 
254  // Allocate a new buffer
255  T* newArray = ArrayAllocate(newUsedLen);
256  if(newArray == NULL) {
257  // Can't insert the new element since the allocation failed.
259  return *this;
260  }
261 
262  // Copy existing elements located to the left of the insertion range
263  ArrayCopyConstruct(newArray, mpArray, index);
264 
265  // Copy the inserted elements
266  ArrayCopyConstruct(newArray + index, values, count);
267 
268  // Copy existing elements located to the right of the insertion range
269  if(index < mUsedLen) {
270  ArrayCopyConstruct(newArray + index + count, mpArray + index, mUsedLen - index);
271  }
272 
273  // Destroy the old array
276 
277  mpArray = newArray;
278  mUsedLen = newUsedLen;
279  mReservedLen = newUsedLen;
280  }
281  else {
282  if(index < mUsedLen) {
283  // Shift elements that get moved beyond the current limit of the array
284  ArrayCopyConstruct(mpArray + mUsedLen, mpArray + mUsedLen - count, count);
285 
286  // Shift elements that stay inside the current limits of the array
287  if((index + count) < mUsedLen) {
288  ArrayCopyOverlap(mpArray + index + count, mpArray + index, mUsedLen - index - count);
289  }
290 
291  // Copy new elements that get inserted within the current size of the array
292  if(lastInsertIndex < mUsedLen) {
293  ArrayCopy(mpArray + index, values, count);
294  }
295  else {
296  ArrayCopy(mpArray + index, values, mUsedLen - index);
297  }
298  }
299 
300  // Copy new elements that get inserted beyond the current size of the array
301  if(lastInsertIndex >= mUsedLen) {
302  size_t numElementsInserted = (mUsedLen - index);
303  DbgAssert(numElementsInserted < count);
304  ArrayCopyConstruct(mpArray + mUsedLen, values + numElementsInserted, count - numElementsInserted);
305  }
306 
307  mUsedLen += count;
308  }
309  }
310 
311  return *this;
312 }
#define NULL
Definition: autoptr.h:20
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayCopy(T *pCopy, const T *pSource, size_t nCount)
Copies an array of elements to an already-constructed buffer.
Definition: Array.inline.h:67
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:129
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:341
#define DbgAssert(expr)
Definition: assert1.h:74
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:48
static void ArrayCopyOverlap(T *pCopy, const T *pSource, size_t nCount)
Copies an array of elements when the target and destination memory buffers may overlap.
Definition: Array.inline.h:88
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:23
Array< T > & removeAt ( size_t  index)

Removes a single element from the array.

Parameters
[in]index- The index of the element to be removed. This index must be valid (within bounds).
Returns
A reference to 'this'.
318 {
319  DbgAssert(isValidIndex(index));
320 
321  if(index < mUsedLen) {
322  // Shift array elements to the left if needed.
323  //
324  if (index < mUsedLen - 1) {
325  for(size_t i = index; i < mUsedLen - 1; ++i) {
326  mpArray[i] = mpArray[i+1];
327  }
328  }
329 
330  // Destroy the last element of the array
331  ArrayDestruct(mpArray + mUsedLen - 1, 1);
332 
333  mUsedLen--;
334  }
335 
336  return *this;
337 }
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
#define DbgAssert(expr)
Definition: assert1.h:74
bool remove ( const T &  value,
size_t  start = 0 
)

Searches for a value in the array and, if it is found, removes it from the array.

Parameters
[in]value- The value to search for.
[in]start- The index at which to start searching. Preceding elements are not searched.
Returns
true if a value was found & removed; false otherwise.
Remarks
If multiple copies of the same value are stored in the array, only the first instance will be removed.
536 {
537  const size_t i = this->findFrom(value, start);
538  if (i == -1)
539  return false;
540  this->removeAt(i);
541  return true;
542 }
FPValue value
Definition: functions.h:635
Array< T > & removeAt(size_t index)
Removes a single element from the array.
Definition: Array.imp.h:317
size_t findFrom(const T &value, size_t start) const
Searches for a value in the array, starting at a given index.
Definition: Array.imp.h:389
Array< T > & removeFirst ( )
inline

Removes the first element of the array.

Returns
A reference to 'this'.
Remarks
Must not be called on an empty array.
306 {
307  DbgAssert(!isEmpty());
308  return removeAt(0);
309 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:163
Array< T > & removeAt(size_t index)
Removes a single element from the array.
Definition: Array.imp.h:317
#define DbgAssert(expr)
Definition: assert1.h:74
Array< T > & removeLast ( )
inline

Removes the last element of the array.

Returns
A reference to 'this'.
Remarks
Must not be called on an empty array.
312 {
313  DbgAssert(!isEmpty());
314  return removeAt(mUsedLen - 1);
315 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:163
Array< T > & removeAt(size_t index)
Removes a single element from the array.
Definition: Array.imp.h:317
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
#define DbgAssert(expr)
Definition: assert1.h:74
Array< T > & removeAll ( )
inline

Removes all the elements from the array.

Returns
A reference to 'this'.
318 {
319  if(mUsedLen > 0) {
321  mUsedLen = 0;
322  }
323  return *this;
324 }
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
Array< T > & removeSubArray ( size_t  startIndex,
size_t  endIndex 
)

Removes a subset of the array.

Parameters
[in]startIndex- The index of the first element to be removed.
[in]endIndex- The index of the last element to be removed.
Returns
A reference to 'this'.
Remarks
  • Both the start and end indices must be within bounds.
  • The end index must be greater or equal to the start index.
343 {
344  DbgAssert(isValidIndex(startIndex));
345  DbgAssert(startIndex <= endIndex);
346 
347  if(startIndex < mUsedLen) {
348 
349  if(endIndex >= mUsedLen) {
350  endIndex = mUsedLen - 1;
351  }
352 
353  size_t numToRemove = endIndex - startIndex + 1;
354 
355  // Shift all elements that reside on the right of the sub-array to be removed
356  for(size_t i = endIndex + 1; i < mUsedLen; ++i) {
357  mpArray[i - numToRemove] = mpArray[i];
358  }
359 
360  // Truncate the array
361  ArrayDestruct(mpArray + mUsedLen - numToRemove, numToRemove);
362 
363  mUsedLen -= numToRemove;
364  }
365 
366  return *this;
367 }
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
#define DbgAssert(expr)
Definition: assert1.h:74
bool contains ( const T &  value,
size_t  start = 0 
) const
inline

Determines if a value is stored in the array.

Parameters
[in]value- The value for which to search for.
[in]start- The index at which to start searching. Preceding elements are not searched.
Returns
true if the value was found in the array; false otherwise.
154 {
155  return this->findFrom(value, start) != -1;
156 }
FPValue value
Definition: functions.h:635
size_t findFrom(const T &value, size_t start) const
Searches for a value in the array, starting at a given index.
Definition: Array.imp.h:389
bool find ( const T &  value,
size_t foundAt,
size_t  start = 0 
) const

Searches for a value in the array.

Parameters
[in]value- The value to search for.
[out]foundAt- The index at which the value was found. Indeterminate if the value was not found.
[in]start- The index at which to start searching. Preceding elements are not searched.
Returns
true if the value was found in the array; false otherwise.
376 {
377  const size_t nFoundAt = this->findFrom(value, start);
378  if (nFoundAt == -1)
379  return false;
380  index = nFoundAt;
381  return true;
382 }
FPValue value
Definition: functions.h:635
size_t findFrom(const T &value, size_t start) const
Searches for a value in the array, starting at a given index.
Definition: Array.imp.h:389
size_t find ( const T &  value) const

Searches for a value in the array.

Parameters
[in]value- The value to search for.
Returns
The index at which the value was found, or -1 if the value was not found. (Since this returns an unsigned value, -1 is converted to the the largest positive value).
385 {
386  return this->findFrom(value, 0); // search from the beginning
387 }
FPValue value
Definition: functions.h:635
size_t findFrom(const T &value, size_t start) const
Searches for a value in the array, starting at a given index.
Definition: Array.imp.h:389
size_t findFrom ( const T &  value,
size_t  start 
) const

Searches for a value in the array, starting at a given index.

Parameters
[in]value- The value to search for.
[in]start- The index at which to start searching.
Returns
The index at which the value was found, or -1 if the value was not found. (Since this returns an unsigned value, -1 is converted to the the largest positive value).
390 {
391  for (size_t i = start; i < this->mUsedLen; i++) {
392  if (mpArray[i] == value)
393  return i;
394  }
395  return (size_t)-1;
396 }
FPValue value
Definition: functions.h:635
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
size_t length ( ) const
inline

Returns the number of used elements (as opposed to simply allocated/reserved) in the array.

159 {
160  return mUsedLen;
161 }
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
bool isEmpty ( ) const
inline

Returns true if the number of used elements in the array is 0; returns false otherwise.

164 {
165  return mUsedLen == 0;
166 }
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
size_t lengthUsed ( ) const
inline

Returns the number of elements used (as opposed to simply allocated/reserved) in the array.

169 {
170  return mUsedLen;
171 }
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
Array< T > & setLengthUsed ( size_t  length,
const T &  defaultVal = T() 
)

Sets the number of elements used (as opposed to simply allocated/reserved) in the array.

Parameters
[in]length- The new "used length" of the array.
[in]defaultVal- The default value for new elements, used only if the length of the array is increased.
Returns
A reference to 'this'.
406 {
407  DbgAssert(n >= 0);
408  if (n > mReservedLen) {
409 
410  size_t growth = (mReservedLen * sizeof(T)) < kArrayGrowthThreshold ?
411  (mReservedLen / 2) : kArrayGrowthThreshold / sizeof(T);
412 
413  size_t minSize = mReservedLen + __max(growth, mGrowLen);
414  if ( n > minSize)
415  minSize = n;
416  setLengthReserved(minSize);
417  }
418 
419  if(n > mUsedLen) {
420  // Initialize the new elements
421  ArrayConstruct(mpArray + mUsedLen, n - mUsedLen, defaultVal);
422  }
423  else {
424  // Destroy the elements to be removed
425  ArrayDestruct(mpArray + n, mUsedLen - n);
426  }
427 
428  mUsedLen = n;
429  return *this;
430 }
static void ArrayConstruct(T *arrayBegin, size_t len, const T &defaultVal)
Constructs an array of elements.
Definition: Array.inline.h:36
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
Array< T > & setLengthReserved(size_t length)
Sets the number of elements allocated/reserved (as opposed to actually used) in the array...
Definition: Array.imp.h:438
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
#define DbgAssert(expr)
Definition: assert1.h:74
The maximum extra space (in bytes) that may be reserved when the buffer is enlarged.
Definition: Array.h:327
size_t lengthReserved ( ) const
inline

Returns the number of elements allocated/reserved (as opposed to actually used) in the array.

174 {
175  return mReservedLen;
176 }
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
Array< T > & setLengthReserved ( size_t  length)

Sets the number of elements allocated/reserved (as opposed to actually used) in the array.

Parameters
[in]length- The new "reserved length" of the array.
Returns
A reference to 'this'.
439 {
440  DbgAssert(n >= 0);
441 
442  if(n != mReservedLen) {
443 
444  if(n == 0) {
445  if(mReservedLen > 0) {
448  mpArray = NULL;
449  mUsedLen = 0;
450  mReservedLen = 0;
451  }
452  }
453  else if(mReservedLen == 0) {
454  mpArray = ArrayAllocate(n);
455  if(mpArray == NULL) {
456  // Failure to allocate memory; can't increase the reserved length.
458  return *this;
459  }
460  mReservedLen = n;
461  }
462  else {
463  T* oldArray = mpArray;
464  size_t oldUsedLen = mUsedLen;
465 
466  // Allocate the new array
467  mpArray = ArrayAllocate(n);
468  if(mpArray == NULL) {
469  // Failure to allocate memory; can't change the reserved length.
471  mpArray = oldArray;
472  return *this;
473  }
474 
475  // Copy the old array to the new one.
476  if(n < mUsedLen) {
477  // The old members don't all fit in the new array
478  ArrayCopyConstruct(mpArray, oldArray, n);
479  mUsedLen = n;
480  }
481  else {
482  ArrayCopyConstruct(mpArray, oldArray, mUsedLen);
483  }
484  mReservedLen = n;
485 
486  // Destroy the old array
487  ArrayDestruct(oldArray, oldUsedLen);
488  ArrayDeAllocate(oldArray);
489  }
490  }
491 
492  return *this;
493 }
#define NULL
Definition: autoptr.h:20
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:55
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:335
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:129
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:341
#define DbgAssert(expr)
Definition: assert1.h:74
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:48
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:23
void reserve ( size_t  capacity)

Alias for setLengthReserved.

Sets the number of elements allocated/reserved (as opposed to actually used) in the array. Named to be similar to the STL containers.

Parameters
[in]capacity- The new "reserved length" or possible capacity of the array.
Returns
void
496 {
497  setLengthReserved(capacity);
498 }
Array< T > & setLengthReserved(size_t length)
Sets the number of elements allocated/reserved (as opposed to actually used) in the array...
Definition: Array.imp.h:438
size_t growLength ( ) const
inline

Returns the growth length of the array.

For more information on the growth length, see setGrowLength().

179 {
180  return mGrowLen;
181 }
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
Array< T > & setGrowLength ( size_t  glen)
inline

Sets the growth length of the array.

The growth length is the minimum number elements by which the reserved space is grown whenever the array runs out of reserved space.

327 {
328  DbgAssert(glen > 0);
329  if(glen > 0) {
330  mGrowLen = glen;
331  }
332  else {
333  DbgAssert(false);
334  // Growth length needs to be at least 1.
335  mGrowLen = 1;
336  }
337  return *this;
338 }
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:339
#define DbgAssert(expr)
Definition: assert1.h:74
Array< T > & reverse ( )

Reverses the sequence of elements in the array.

Reverses the sequence of elements in the array such that the last element becomes the first.

Returns
A reference to 'this'.
505 {
506  size_t halfUsedLen = mUsedLen/2;
507  for (size_t i = 0; i < halfUsedLen; i++) {
508  T tmp = mpArray[i];
509  mpArray[i] = mpArray[mUsedLen - 1 - i];
510  mpArray[mUsedLen - 1 - i] = tmp;
511  }
512  return *this;
513 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
Array< T > & swap ( size_t  i1,
size_t  i2 
)

Swaps two elements in this array.

Parameters
[in]i1- The index of the first element to swap. This index must be within bounds.
[in]i2- The index of the second element to swap. This index must be within bounds.
518 {
519  DbgAssert(isValidIndex(i1));
520  DbgAssert(isValidIndex(i2));
521 
522  if (i1 == i2) return *this;
523 
524  T tmp = mpArray[i1];
525  mpArray[i1] = mpArray[i2];
526  mpArray[i2] = tmp;
527  return *this;
528 }
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:193
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
#define DbgAssert(expr)
Definition: assert1.h:74
void sort ( CompareFnc  cmp)

Sorts the elements of the array using a custom comparison function.

The sort if performed with the QuickSort algorithm.

Parameters
[in]cmp- The comparison function used to order the elements.
See Also
CompareFnc
546  {
547 
548  if(mUsedLen > 1) {
549  // Use the standard C function of the type doesn't have a copy operator
550  // (meaning that memcpy() is safe)
551  /* From MSDN: The compiler's support for type traits allows library writers to
552  determine various characteristics of a type at compile time.
553  All type traits return false if the specified conditions are not met.
554  Returns true if the CLR or native type has a copy assignment operator. */
555  if(!__has_assign(T)) {
556  qsort(mpArray, mUsedLen, sizeof(T), cmp);
557  }
558  else {
559  quickSortRecursive(mpArray, 0, mUsedLen - 1, cmp);
560  }
561  }
562 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
static void quickSortRecursive(T *data, size_t first, size_t last, CompareFnc cmp)
Recursive QuickSort function used to sort the elements of the array.
Definition: Array.imp.h:597
const T * asArrayPtr ( ) const
inline

Returns the array storage as a C-style array pointer.

Remarks
Any modification to the contents of the array, through this pointer, may be dangerous.
184 {
185  return mpArray;
186 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
T * asArrayPtr ( )
inline
189 {
190  return mpArray;
191 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:333
bool isValidIndex ( size_t  i) const
inline

Returns whether the given array index is valid for this array.

Returns
true if the given index is within the bounds of this array; false otherwise.
194 {
195  // We should prohibit index's that are the maximum size_t value
196  return (i != (size_t)-1) && i < mUsedLen;
197 }
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:337
size_t quickSortPartition ( T *  data,
size_t  first,
size_t  last,
CompareFnc  cmp 
)
staticprotected

The partition portion of the QuickSort algorithm.

566 {
567  const T& pivot = data[last]; // use the last item as the pivot
568  size_t left = first; // sort from the first item
569  size_t right = last - 1; // sort to the item excluding the pivot
570 
571  do {
572  while ((left < last) && (cmp(&(data[left]), &pivot) <= 0))
573  {
574  ++left;
575  }
576  while ((right > first) && (cmp(&(data[right]), &pivot) >= 0))
577  {
578  --right;
579  }
580  if (left < right) {
581  T swapValue = data[left];
582  data[left] = data[right];
583  data[right] = swapValue;
584  }
585  } while (left < right);
586 
587  if (cmp(&data[left], &pivot) > 0)
588  {
589  T swapValue = data[left];
590  data[left] = data[last];
591  data[last] = swapValue;
592  }
593 
594  return left;
595 }
Value **volatile data
Definition: arrays.h:28
int left
Definition: rollouts.h:178
T & first()
Accesses the first element in the array.
Definition: Array.inline.h:250
T & last()
Accesses the last element in the array.
Definition: Array.inline.h:272
void quickSortRecursive ( T *  data,
size_t  first,
size_t  last,
CompareFnc  cmp 
)
staticprotected

Recursive QuickSort function used to sort the elements of the array.

598 {
599  if (first < last)
600  {
601  size_t pivot_position = quickSortPartition(data, first, last, cmp);
602 
603  // Protect against overflow. Normally the "if (first < last)" test would
604  // guard against this, but size_t is unsigned, meaning "right - 1" can result
605  // in a test of -1 > 0 when right is 0, which is an invalid unsigned inequality.
606  if (pivot_position > 0)
607  {
608  quickSortRecursive(data, first, pivot_position - 1, cmp);
609  }
610  quickSortRecursive(data, pivot_position + 1, last, cmp);
611  }
612 }
Value **volatile data
Definition: arrays.h:28
T & first()
Accesses the first element in the array.
Definition: Array.inline.h:250
T & last()
Accesses the last element in the array.
Definition: Array.inline.h:272
static size_t quickSortPartition(T *data, size_t first, size_t last, CompareFnc cmp)
The partition portion of the QuickSort algorithm.
Definition: Array.imp.h:565
static void quickSortRecursive(T *data, size_t first, size_t last, CompareFnc cmp)
Recursive QuickSort function used to sort the elements of the array.
Definition: Array.imp.h:597
void handleOutOfMemory ( )
inlinestaticprotected

Utility function, called when the array fails to allocate memory.

341  {
342 
343  DbgAssert(false);
345 }
UtilExport void UtilOutOfMemoryException()
#define DbgAssert(expr)
Definition: assert1.h:74
T * ArrayAllocate ( size_t  len)
inlinestaticprotected

Allocates an array of elements without constructing them.

24 {
25  DbgAssert(len < 0x40000000); // 1G sanity check
26  T* p = (T*) UtilAllocateMemory(len * sizeof(T));
27  return p;
28 }
UtilExport void * UtilAllocateMemory(size_t)
#define DbgAssert(expr)
Definition: assert1.h:74
void ArrayConstruct ( T *  arrayBegin,
size_t  len,
const T &  defaultVal 
)
inlinestaticprotected

Constructs an array of elements.

37 {
38  if(!__has_trivial_constructor(T))
39  {
40  for(size_t i = 0; i < len; ++i)
41  {
42  new(&(arrayBegin[i])) T(defaultVal);
43  }
44  }
45 }
void ArrayDeAllocate ( T *  arrayBegin)
inlinestaticprotected

De-allocates an array of elements without destructing them.

49 {
50  UtilDeallocateMemory(arrayBegin);
51 }
UtilExport void UtilDeallocateMemory(void *)
void ArrayDestruct ( T *  arrayBegin,
size_t  len 
)
inlinestaticprotected

Destructs an array of elements.

56 {
57  if(!__has_trivial_destructor(T))
58  {
59  for(size_t i = 0; i < len; ++i)
60  {
61  arrayBegin[i].~T();
62  }
63  }
64 }
void ArrayCopy ( T *  pCopy,
const T *  pSource,
size_t  nCount 
)
staticprotected

Copies an array of elements to an already-constructed buffer.

Will use the copy operator if needed.

68 {
69  // Auto-detect whether it's safe to use memcpy() or whether we need
70  // to call the copy operator. We're counting on the fact that this condition,
71  // being resolvable at compile-time, will be removed by the optimizer.
72  if(__has_assign(T)) {
73  // Type has an assignment operator; use it.
74  for(size_t i = 0; i < nCount; ++i)
75  {
76  pCopy[i] = (pSource[i]);
77  }
78  }
79  else {
80  // Type does not have an assignment operator; use memcpy() as it's usually faster.
81  if (nCount > 0)
82  {
83  memcpy(pCopy, pSource, nCount * sizeof(T));
84  }
85  }
86 }
void ArrayCopyOverlap ( T *  pCopy,
const T *  pSource,
size_t  nCount 
)
staticprotected

Copies an array of elements when the target and destination memory buffers may overlap.

89 {
90  // Auto-detect whether it's safe to use memcpy() or whether we need
91  // to call the copy operator. We're counting on the fact that this condition,
92  // being resolvable at compile-time, will be removed by the optimizer.
93  if(__has_assign(T)) {
94  // Type has an assignment operator; use it.
95  if (pCopy == pSource)
96  {
97  // nothing to do here, bail early
98  return;
99  }
100 
101  if (pCopy < pSource)
102  {
103  // forward iteration
104  for(size_t i = 0; i < nCount; i++ )
105  {
106  pCopy[i] = pSource[i];
107  }
108  }
109  else
110  {
111  // backward iteration
112  for(size_t i = nCount - 1; i != (size_t)-1; --i)
113  {
114  pCopy[i] = pSource[i];
115  }
116  }
117  }
118  else {
119  // Type does not have an assignment operator; use memcpy() as it's usually faster.
120  if (nCount > 0)
121  {
122  memmove(pCopy, pSource, nCount * sizeof(T));
123  }
124  }
125 }
MAXMEM_EXTERN_C UtilExport size_t(__cdecl *MAX_msize)(void *memblock)
void ArrayCopyConstruct ( T *  pCopy,
const T *  pSource,
size_t  nCount 
)
staticprotected

Copies and array of elements to a non-constructed.

Will use the copy constructor if needed.

130 {
131  // Auto-detect whether it's safe to use memcpy() or whether we need
132  // to call the copy operator. We're counting on the fact that this condition,
133  // being resolvable at compile-time, will be removed by the optimizer.
134  if(__has_copy(T)) {
135  // Type has an assignment operator; use it.
136  for(size_t i = 0; i < nCount; ++i)
137  {
138  new(&(pCopy[i])) T(pSource[i]); // using placement new.
139  }
140  }
141  else {
142  // Type does not have an assignment operator; use memcpy() as it's usually faster.
143  if (nCount > 0)
144  {
145  memcpy(pCopy, pSource, nCount * sizeof(T));
146  }
147  }
148 }

Member Data Documentation

T* mpArray
protected

Pointer to the storage buffer.

size_t mReservedLen
protected

The reserved length (in number of elements, not bytes).

size_t mUsedLen
protected

The used length (in number of elements, not bytes).

size_t mGrowLen
protected

The growth length. See setGrowLength().