Array< T > Class Template Reference

#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 generic, 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().

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

Constructor & Destructor Documentation

Array ( )

Initializes an empty array.

22 : mpArray(NULL),
23  mReservedLen(0),
24  mUsedLen(0),
26 {
27  if(mGrowLen < 1) {
28  // Growth length needs to be at least 1.
29  mGrowLen = 1;
30  }
31 }
#define NULL
Definition: autoptr.h:18
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:337
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
The default growth length. See setGrowLength().
Definition: Array.h:327
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().
34 : mpArray(NULL),
35  mReservedLen(0),
36  mUsedLen(0),
38 {
39  if(mGrowLen < 1) {
40  // Growth length needs to be at least 1.
41  mGrowLen = 1;
42  }
43 
44  // Re-size the array if a non-zero length was specified.
45  if(usedLength > 0) {
46  mpArray = ArrayAllocate(usedLength);
47  if (mpArray == NULL) {
49  }
50  else {
51 
52  // Initialize the new elements
53  ArrayConstruct(mpArray, usedLength, defaultVal);
54 
55  mReservedLen = usedLength;
56  mUsedLen = usedLength;
57  }
58  }
59 }
static void ArrayConstruct(T *arrayBegin, size_t len, const T &defaultVal)
Constructs an array of elements.
Definition: Array.inline.h:34
#define NULL
Definition: autoptr.h:18
size_t growLength() const
Returns the growth length of the array.
Definition: Array.inline.h:176
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:337
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:339
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:21
Array ( const Array< T > &  src)

Copy constructor.

Copies the contents of another array.

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

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

88 {
89  if (mpArray != NULL) {
92  }
93 }
#define NULL
Definition: autoptr.h:18
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:46

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'.
105 {
106  if (this != &src) {
107  // Re-allocate the buffer if necessary
108  if (mReservedLen < src.mUsedLen) {
109  // Destroy the existing list
110  if (mpArray != NULL) {
113  }
114  // Allocate a new buffer
115  mReservedLen = src.mUsedLen;
117  if (mpArray == NULL) { // ...so this only happens if failure.
119  mReservedLen = 0;
120  mUsedLen = 0;
121  return *this;
122  }
123  // Copy the list
124  mUsedLen = src.mUsedLen;
125  ArrayCopyConstruct(mpArray, src.mpArray, mUsedLen);
126  }
127  else if(mUsedLen < src.mUsedLen) {
128  // The entire destination list is to be overwritten
129  ArrayCopy(mpArray, src.mpArray, mUsedLen);
130  // Remaining elements need to be added to the list
131  ArrayCopyConstruct(mpArray + mUsedLen, src.mpArray + mUsedLen, src.mUsedLen - mUsedLen);
132  mUsedLen = src.mUsedLen;
133  }
134  else if(mUsedLen > src.mUsedLen) {
135  // Copy the entire source list.
136  ArrayCopy(mpArray, src.mpArray, src.mUsedLen);
137  // Truncate unused elements in the destination list.
138  ArrayDestruct(mpArray + src.mUsedLen, mUsedLen - src.mUsedLen);
139  mUsedLen = src.mUsedLen;
140  }
141  else {
142  // Lists are of identical size; simply copy the entire contents
143  ArrayCopy(mpArray, src.mpArray, mUsedLen);
144  }
145  }
146  return *this;
147 }
#define NULL
Definition: autoptr.h:18
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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:65
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:127
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:339
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:46
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:21
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.
157 {
158  if (mUsedLen == cpr.mUsedLen)
159  {
160  for (size_t i = 0; i < mUsedLen; i++)
161  if (mpArray[i] != cpr.mpArray[i])
162  return false;
163  return true;
164  }
165  return false;
166 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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.
198 {
199  if (!isValidIndex(i))
200  {
201  DbgAssert(false);
202  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::operator[]"));
203  }
204  return mpArray[i];
205 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:52
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
const T & operator[] ( size_t  i) const
inline
208 {
209  if (!isValidIndex(i))
210  {
211  DbgAssert(false);
212  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::operator[]"));
213  }
214  return mpArray[i];
215 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:52
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
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.
228 {
229  if (!isValidIndex(i))
230  {
231  DbgAssert(false);
232  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::at()"));
233  }
234  return mpArray[i];
235 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:52
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
T & at ( size_t  index)
inline
218 {
219  if (!isValidIndex(i))
220  {
221  DbgAssert(false);
222  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::at()"));
223  }
224  return mpArray[i];
225 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:52
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
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.
238 {
239  if (!isValidIndex(i))
240  {
241  DbgAssert(false);
242  throw MaxSDK::Util::OutOfRangeException(_M("Argument index out of bounds, passed into a MaxSDK::Array::setAt()"));
243  }
244  mpArray[i] = value;
245  return *this;
246 }
Thrown when an out of bounds index is detected.
Definition: MaxExceptions.h:52
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
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'.
172 {
173  for (size_t i = 0; i < mUsedLen; i++) {
174  mpArray[i] = value;
175  }
176  return *this;
177 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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.
249 {
250  DbgAssert(!this->isEmpty());
251  DbgAssert(mpArray != nullptr);
252  if (this->isEmpty() || (nullptr == mpArray))
253  {
254  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::first() on an empty array"));
255  }
256  return mpArray[0];
257 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:161
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:68
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
const T & first ( ) const
inline
260 {
261  DbgAssert(!this->isEmpty());
262  DbgAssert(mpArray != nullptr);
263  if (this->isEmpty() || (nullptr == mpArray))
264  {
265  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::first() on an empty array"));
266  }
267  return mpArray[0];
268 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:161
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:68
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
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.
271 {
272  DbgAssert(!this->isEmpty());
273  DbgAssert(mpArray != nullptr);
274  if (this->isEmpty() || (nullptr == mpArray))
275  {
276  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::last() on an empty array"));
277  }
278  return mpArray[mUsedLen-1];
279 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:161
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:68
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
const T & last ( ) const
inline
282 {
283  DbgAssert(!this->isEmpty());
284  DbgAssert(mpArray != nullptr);
285  if (this->isEmpty() || (nullptr == mpArray))
286  {
287  throw MaxSDK::Util::RunTimeException(_M("Attempting to call MaxSDK::Array::last() on an empty array"));
288  }
289  return mpArray[mUsedLen-1];
290 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:161
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
General multi-purpose exception for runtime errors.
Definition: MaxExceptions.h:68
#define _M(x)
Used to wrap string literals.
Definition: strbasic.h:67
#define DbgAssert(expr)
Definition: assert1.h:72
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.
293 {
294  insertAt(mUsedLen, value);
295  return mUsedLen-1;
296 }
Array< T > & insertAt(size_t index, const T &value)
Inserts a single value, at a given location, into this array.
Definition: Array.imp.h:209
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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'.
299 {
300  return insertAt(mUsedLen, values, count);
301 }
Array< T > & insertAt(size_t index, const T &value)
Inserts a single value, at a given location, into this array.
Definition: Array.imp.h:209
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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'.
186 {
187  size_t otherLen = otherArray.length();
188  if (otherLen == 0) {
189  return *this;
190  }
191  size_t newLen = mUsedLen + otherLen;
192  if (newLen > mReservedLen) {
193  setLengthReserved(newLen);
194  }
195 
196  ArrayCopyConstruct(mpArray + mUsedLen, otherArray.mpArray, otherLen);
197 
198  mUsedLen = newLen;
199  return *this;
200 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
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:437
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:127
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'.
210 {
211  DbgAssert(index >= 0 && index <= mUsedLen);
212 
213  if (mUsedLen >= mReservedLen) {
214  size_t growth = (mUsedLen * sizeof(T)) < kArrayGrowthThreshold ?
215  (mUsedLen / 2) : kArrayGrowthThreshold / sizeof(T);
216  setLengthReserved(mUsedLen + __max(growth, mGrowLen));
217  }
218 
219  if (index != mUsedLen) {
220 
221  // Initialize the new member of the array
223 
224  // Copy the remainder of the list that needs to be shifted
225  for(size_t i = mUsedLen - 1; i > index; --i) {
226  mpArray[i] = mpArray[i-1];
227  }
228 
229  // Now copy the new element into the array
230  mpArray[index] = value;
231  }
232  else {
233  // Add the new value to the end of the list
234  ArrayCopyConstruct(mpArray + mUsedLen, &value, 1);
235  }
236 
237  mUsedLen++;
238  return *this;
239 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:337
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
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:437
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:127
#define DbgAssert(expr)
Definition: assert1.h:72
The maximum extra space (in bytes) that may be reserved when the buffer is enlarged.
Definition: Array.h:325
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'.
242 {
243  DbgAssert(index >= 0 && index <= mUsedLen);
244 
245  if(index <= mUsedLen) {
246 
247  size_t lastInsertIndex = index + count - 1;
248 
249  // Increase the allocated memory if necessary
250  size_t newUsedLen = mUsedLen + count;
251  if(newUsedLen > mReservedLen) {
252 
253  // Allocate a new buffer
254  T* newArray = ArrayAllocate(newUsedLen);
255  if(newArray == NULL) {
256  // Can't insert the new element since the allocation failed.
258  return *this;
259  }
260 
261  // Copy existing elements located to the left of the insertion range
262  ArrayCopyConstruct(newArray, mpArray, index);
263 
264  // Copy the inserted elements
265  ArrayCopyConstruct(newArray + index, values, count);
266 
267  // Copy existing elements located to the right of the insertion range
268  if(index < mUsedLen) {
269  ArrayCopyConstruct(newArray + index + count, mpArray + index, mUsedLen - index);
270  }
271 
272  // Destroy the old array
275 
276  mpArray = newArray;
277  mUsedLen = newUsedLen;
278  mReservedLen = newUsedLen;
279  }
280  else {
281  if(index < mUsedLen) {
282  // Shift elements that get moved beyond the current limit of the array
283  ArrayCopyConstruct(mpArray + mUsedLen, mpArray + mUsedLen - count, count);
284 
285  // Shift elements that stay inside the current limits of the array
286  if((index + count) < mUsedLen) {
287  ArrayCopyOverlap(mpArray + index + count, mpArray + index, mUsedLen - index - count);
288  }
289 
290  // Copy new elements that get inserted within the current size of the array
291  if(lastInsertIndex < mUsedLen) {
292  ArrayCopy(mpArray + index, values, count);
293  }
294  else {
295  ArrayCopy(mpArray + index, values, mUsedLen - index);
296  }
297  }
298 
299  // Copy new elements that get inserted beyond the current size of the array
300  if(lastInsertIndex >= mUsedLen) {
301  size_t numElementsInserted = (mUsedLen - index);
302  DbgAssert(numElementsInserted < count);
303  ArrayCopyConstruct(mpArray + mUsedLen, values + numElementsInserted, count - numElementsInserted);
304  }
305 
306  mUsedLen += count;
307  }
308  }
309 
310  return *this;
311 }
#define NULL
Definition: autoptr.h:18
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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:65
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:127
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:339
#define DbgAssert(expr)
Definition: assert1.h:72
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:46
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:86
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:21
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'.
317 {
318  DbgAssert(isValidIndex(index));
319 
320  if(index < mUsedLen) {
321  // Shift array elements to the left if needed.
322  //
323  if (index < mUsedLen - 1) {
324  for(size_t i = index; i < mUsedLen - 1; ++i) {
325  mpArray[i] = mpArray[i+1];
326  }
327  }
328 
329  // Destroy the last element of the array
330  ArrayDestruct(mpArray + mUsedLen - 1, 1);
331 
332  mUsedLen--;
333  }
334 
335  return *this;
336 }
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
#define DbgAssert(expr)
Definition: assert1.h:72
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.
535 {
536  const size_t i = this->findFrom(value, start);
537  if (i == -1)
538  return false;
539  this->removeAt(i);
540  return true;
541 }
Array< T > & removeAt(size_t index)
Removes a single element from the array.
Definition: Array.imp.h:316
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:388
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.
304 {
305  DbgAssert(!isEmpty());
306  return removeAt(0);
307 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:161
Array< T > & removeAt(size_t index)
Removes a single element from the array.
Definition: Array.imp.h:316
#define DbgAssert(expr)
Definition: assert1.h:72
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.
310 {
311  DbgAssert(!isEmpty());
312  return removeAt(mUsedLen - 1);
313 }
bool isEmpty() const
Returns true if the number of used elements in the array is 0; returns false otherwise.
Definition: Array.inline.h:161
Array< T > & removeAt(size_t index)
Removes a single element from the array.
Definition: Array.imp.h:316
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
#define DbgAssert(expr)
Definition: assert1.h:72
Array< T > & removeAll ( )
inline

Removes all the elements from the array.

Returns
A reference to 'this'.
316 {
317  if(mUsedLen > 0) {
319  mUsedLen = 0;
320  }
321  return *this;
322 }
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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.
342 {
343  DbgAssert(isValidIndex(startIndex));
344  DbgAssert(startIndex <= endIndex);
345 
346  if(startIndex < mUsedLen) {
347 
348  if(endIndex >= mUsedLen) {
349  endIndex = mUsedLen - 1;
350  }
351 
352  size_t numToRemove = endIndex - startIndex + 1;
353 
354  // Shift all elements that reside on the right of the sub-array to be removed
355  for(size_t i = endIndex + 1; i < mUsedLen; ++i) {
356  mpArray[i - numToRemove] = mpArray[i];
357  }
358 
359  // Truncate the array
360  ArrayDestruct(mpArray + mUsedLen - numToRemove, numToRemove);
361 
362  mUsedLen -= numToRemove;
363  }
364 
365  return *this;
366 }
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
#define DbgAssert(expr)
Definition: assert1.h:72
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.
152 {
153  return this->findFrom(value, start) != -1;
154 }
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:388
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.
375 {
376  const size_t nFoundAt = this->findFrom(value, start);
377  if (nFoundAt == -1)
378  return false;
379  index = nFoundAt;
380  return true;
381 }
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:388
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).
384 {
385  return this->findFrom(value, 0); // search from the beginning
386 }
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:388
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).
389 {
390  for (size_t i = start; i < this->mUsedLen; i++) {
391  if (mpArray[i] == value)
392  return i;
393  }
394  return (size_t)-1;
395 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
size_t length ( ) const
inline

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

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

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

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

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

167 {
168  return mUsedLen;
169 }
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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'.
405 {
406  DbgAssert(n >= 0);
407  if (n > mReservedLen) {
408 
409  size_t growth = (mReservedLen * sizeof(T)) < kArrayGrowthThreshold ?
410  (mReservedLen / 2) : kArrayGrowthThreshold / sizeof(T);
411 
412  size_t minSize = mReservedLen + __max(growth, mGrowLen);
413  if ( n > minSize)
414  minSize = n;
415  setLengthReserved(minSize);
416  }
417 
418  if(n > mUsedLen) {
419  // Initialize the new elements
420  ArrayConstruct(mpArray + mUsedLen, n - mUsedLen, defaultVal);
421  }
422  else {
423  // Destroy the elements to be removed
424  ArrayDestruct(mpArray + n, mUsedLen - n);
425  }
426 
427  mUsedLen = n;
428  return *this;
429 }
static void ArrayConstruct(T *arrayBegin, size_t len, const T &defaultVal)
Constructs an array of elements.
Definition: Array.inline.h:34
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:337
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
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:437
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
#define DbgAssert(expr)
Definition: assert1.h:72
The maximum extra space (in bytes) that may be reserved when the buffer is enlarged.
Definition: Array.h:325
size_t lengthReserved ( ) const
inline

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

172 {
173  return mReservedLen;
174 }
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
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'.
438 {
439  DbgAssert(n >= 0);
440 
441  if(n != mReservedLen) {
442 
443  if(n == 0) {
444  if(mReservedLen > 0) {
447  mpArray = NULL;
448  mUsedLen = 0;
449  mReservedLen = 0;
450  }
451  }
452  else if(mReservedLen == 0) {
453  mpArray = ArrayAllocate(n);
454  if(mpArray == NULL) {
455  // Failure to allocate memory; can't increase the reserved length.
457  return *this;
458  }
459  mReservedLen = n;
460  }
461  else {
462  T* oldArray = mpArray;
463  size_t oldUsedLen = mUsedLen;
464 
465  // Allocate the new array
466  mpArray = ArrayAllocate(n);
467  if(mpArray == NULL) {
468  // Failure to allocate memory; can't change the reserved length.
470  mpArray = oldArray;
471  return *this;
472  }
473 
474  // Copy the old array to the new one.
475  if(n < mUsedLen) {
476  // The old members don't all fit in the new array
477  ArrayCopyConstruct(mpArray, oldArray, n);
478  mUsedLen = n;
479  }
480  else {
481  ArrayCopyConstruct(mpArray, oldArray, mUsedLen);
482  }
483  mReservedLen = n;
484 
485  // Destroy the old array
486  ArrayDestruct(oldArray, oldUsedLen);
487  ArrayDeAllocate(oldArray);
488  }
489  }
490 
491  return *this;
492 }
#define NULL
Definition: autoptr.h:18
static void ArrayDestruct(T *arrayBegin, size_t len)
Destructs an array of elements.
Definition: Array.inline.h:53
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mReservedLen
The reserved length (in number of elements, not bytes).
Definition: Array.h:333
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
static void ArrayCopyConstruct(T *pCopy, const T *pSource, size_t nCount)
Copies and array of elements to a non-constructed.
Definition: Array.inline.h:127
static void handleOutOfMemory()
Utility function, called when the array fails to allocate memory.
Definition: Array.inline.h:339
#define DbgAssert(expr)
Definition: assert1.h:72
static void ArrayDeAllocate(T *arrayBegin)
De-allocates an array of elements without destructing them.
Definition: Array.inline.h:46
static T * ArrayAllocate(size_t len)
Allocates an array of elements without constructing them.
Definition: Array.inline.h:21
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
495 {
496  setLengthReserved(capacity);
497 }
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:437
size_t growLength ( ) const
inline

Returns the growth length of the array.

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

177 {
178  return mGrowLen;
179 }
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:337
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.

325 {
326  DbgAssert(glen > 0);
327  if(glen > 0) {
328  mGrowLen = glen;
329  }
330  else {
331  DbgAssert(false);
332  // Growth length needs to be at least 1.
333  mGrowLen = 1;
334  }
335  return *this;
336 }
size_t mGrowLen
The growth length. See setGrowLength().
Definition: Array.h:337
#define DbgAssert(expr)
Definition: assert1.h:72
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'.
504 {
505  size_t halfUsedLen = mUsedLen/2;
506  for (size_t i = 0; i < halfUsedLen; i++) {
507  T tmp = mpArray[i];
508  mpArray[i] = mpArray[mUsedLen - 1 - i];
509  mpArray[mUsedLen - 1 - i] = tmp;
510  }
511  return *this;
512 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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.
517 {
518  DbgAssert(isValidIndex(i1));
519  DbgAssert(isValidIndex(i2));
520 
521  if (i1 == i2) return *this;
522 
523  T tmp = mpArray[i1];
524  mpArray[i1] = mpArray[i2];
525  mpArray[i2] = tmp;
526  return *this;
527 }
bool isValidIndex(size_t) const
Returns whether the given array index is valid for this array.
Definition: Array.inline.h:191
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
#define DbgAssert(expr)
Definition: assert1.h:72
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
545  {
546 
547  if(mUsedLen > 1) {
548  // Use the standard C function of the type doesn't have a copy operator
549  // (meaning that memcpy() is safe)
550  /* From MSDN: The compiler's support for type traits allows library writers to
551  determine various characteristics of a type at compile time.
552  All type traits return false if the specified conditions are not met.
553  Returns true if the CLR or native type has a copy assignment operator. */
554  if(!__has_assign(T)) {
555  qsort(mpArray, mUsedLen, sizeof(T), cmp);
556  }
557  else {
558  quickSortRecursive(mpArray, 0, mUsedLen - 1, cmp);
559  }
560  }
561 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
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:596
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.
182 {
183  return mpArray;
184 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
T * asArrayPtr ( )
inline
187 {
188  return mpArray;
189 }
T * mpArray
Pointer to the storage buffer.
Definition: Array.h:331
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.
192 {
193  // We should prohibit index's that are the maximum size_t value
194  return (i != (size_t)-1) && i < mUsedLen;
195 }
size_t mUsedLen
The used length (in number of elements, not bytes).
Definition: Array.h:335
size_t quickSortPartition ( T *  data,
size_t  first,
size_t  last,
CompareFnc  cmp 
)
staticprotected

The partition portion of the QuickSort algorithm.

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

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

597 {
598  if (first < last)
599  {
600  size_t pivot_position = quickSortPartition(data, first, last, cmp);
601 
602  // Protect against overflow. Normally the "if (first < last)" test would
603  // guard against this, but size_t is unsigned, meaning "right - 1" can result
604  // in a test of -1 > 0 when right is 0, which is an invalid unsigned inequality.
605  if (pivot_position > 0)
606  {
607  quickSortRecursive(data, first, pivot_position - 1, cmp);
608  }
609  quickSortRecursive(data, pivot_position + 1, last, cmp);
610  }
611 }
T & first()
Accesses the first element in the array.
Definition: Array.inline.h:248
T & last()
Accesses the last element in the array.
Definition: Array.inline.h:270
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:564
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:596
void handleOutOfMemory ( )
inlinestaticprotected

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

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

Allocates an array of elements without constructing them.

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

Constructs an array of elements.

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

De-allocates an array of elements without destructing them.

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

Destructs an array of elements.

54 {
55  if(!__has_trivial_destructor(T))
56  {
57  for(size_t i = 0; i < len; ++i)
58  {
59  arrayBegin[i].~T();
60  }
61  }
62 }
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.

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

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

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

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().