#include <BasicArray.h>
Collaboration diagram for BasicArray< T >:
Public Member Functions | |
BasicArray (unsigned int initialCapacity=0) | |
Create an empty array. | |
~BasicArray () | |
Destruct the array and all the data in it. | |
bool | isEmpty () const |
unsigned int | getSize () const |
void | setSize (const unsigned int size) |
setSize() will increase the array capacity if the new size is larger than the current capacity. | |
unsigned int | getCapacity () const |
Will always be >= to getSize(). | |
T * | getBuffer () |
Modifying or deallocating this buffer may adversely effect the contents of the array. | |
T * | adopt () |
Release the internal buffer. | |
void | reset (unsigned int initialCapacity=0) |
Set size to zero and deallocate memory. | |
void | clear () |
Set size to zero. | |
void | increase (const unsigned int minCapacity, const unsigned int multiple=2) |
Increase array capacity if minCapacity is greater than the current capacity. | |
void | shrink () |
Calling this function assures that the array capacity is equal to its size by releasing any extra allocated memory. | |
unsigned int | put (const T &x) |
Add the array element to the end of the array. | |
void | put (const unsigned int i, const T &x) |
Add an array element. | |
unsigned int | put (const T *x, const int count) |
Add an array of elements to the end of the array. | |
void | put (const unsigned int i, const T *x, const int count) |
Add an array of elements to the array. | |
void * | alloc (const unsigned int count) |
Allocate space for. | |
void * | alloc (const unsigned int i, const unsigned int count) |
Allocate space for. | |
T & | get (const unsigned int i) const |
If the index is out of range a BasicException will be thrown. | |
T & | operator[] (const unsigned int i) const |
Allows you to access a BasicArray like a normal array. | |
Private Attributes | |
unsigned int | capacity |
unsigned int | size |
T * | data |
WARNING class constructors and destructors are not called. Allocated memory is initially set to zero. This works well with arrays of pointers or basic data types, but will not work for classes such as std::string which requires the constructor be called.
|
Create an empty array.
|
|
Destruct the array and all the data in it.
|
|
Release the internal buffer. It is then the callers responsibility to deallocate the returned pointer if not NULL.
|
|
Allocate space for.
|
|
Allocate space for.
|
|
Set size to zero. Does not effect capacity. Memory is not initialized to zero! |
|
If the index is out of range a BasicException will be thrown. Array indexing starts from zero.
|
|
Modifying or deallocating this buffer may adversely effect the contents of the array.
|
|
Will always be >= to getSize().
|
|
|
|
Increase array capacity if minCapacity is greater than the current capacity. The capacity will be increased to the larger of minCapacity or multiple times the current capacity. The default value for multiple is 2.0. The advantage of this liberal memory allocation comes from the unfortunate side effect that when a memory block is reallocated it may be copied to a new location. Doubling the capacity reduces the amortized time complexity of adding n elements one at a time from O(n^2) to O(n*log(n)). The disadvantage is that up to twice as much memory may be used. If you know the maximum size of the array ahead of time you can set the capacity to that size in the constructor. You may also pass a multiple of zero causing increase to allocate no more than minCapacity. If memory allocation fails when capacity is greater than minCapacity then allocation is retried with capacity equal to minCapacity. If allocation fails again an std::bad_alloc() exception will be thrown.
|
|
|
|
Allows you to access a BasicArray like a normal array. See get(). |
|
Add an array of elements to the array. Elements from i to count - 1 will be overwritten. If i + count is greater than the current size the array will automatically be increased.
|
|
Add an array of elements to the end of the array.
|
|
Add an array element. If there is all ready an element at this index it will be overwritten. If the index is beyond the current size of the array the array size will be automatically increased.
|
|
Add the array element to the end of the array. The size of the array will be increased by one. See the notes on the increase() function.
|
|
Set size to zero and deallocate memory.
|
|
setSize() will increase the array capacity if the new size is larger than the current capacity.
|
|
Calling this function assures that the array capacity is equal to its size by releasing any extra allocated memory. shrink() should normally only be used after the last put() call and before a call to adopt(). However, when the difference between capacity and size is small the recovered memory will often not be usable because of memory fragmentation. As a rule of thumb do not bother with shrink() when array sizes are small or when you have only a few array instances. It is generally more efficient to set the array capacity in the constructor.
|
|
|
|
|
|
|