Introduction
Downloads
Documentation
Tutorials
Pixie Lite
Forum

Home

Array< TYPE > Class Template Reference
[Containers]

Generic dynamic array. More...

List of all members.


Public Member Functions

 Array (int initialCapacity=64)
 Array (const Array< TYPE > &arrayToCopy)
const Array< TYPE > & operator= (const Array< TYPE > &arrayToCopy)
 ~Array ()
TYPE & Add (const TYPE &item)
void InsertBefore (const TYPE &item, int index)
void InsertBefore (const TYPE &item, const ArrayIterator< TYPE > &insertBefore)
void InsertAfter (const TYPE &item, int index)
void InsertAfter (const TYPE &item, const ArrayIterator< TYPE > &insertAfter)
void RemoveLast ()
void Remove (int index)
void Remove (const ArrayIterator< TYPE > &iterator)
int GetItemCount () const
TYPE & Get (int index) const
TYPE & Get (const ArrayIterator< TYPE > &iterator) const
void Set (int index, const TYPE &item)
void Set (const ArrayIterator< TYPE > &iterator, const TYPE &item)
int GetCapacity () const
void SetCapacity (int capacity)
void Clear (bool releaseMemory=false)
bool ItemExists (const TYPE &item) const
TYPE * GetPointer () const

Detailed Description

template<class TYPE>
class Array< TYPE >

Generic dynamic array.

Author:
Mattias Gustavsson
This class store items in a dynamic array, which can grow in size as needed. Every time the array is full, it is resized to twice the size. There is also an iterator available, which supports a generic iterator interface of MoveFirst, MoveNext etc. so the dynamic array can also be used like a linked list. Note though, that removing/inserting items in the middle of the array will cause all items after that item to be moved one slot, which could potentially be a problem if you have a huge array and often need to do those operations. For most typical cases though, it's not an issue, as the memory fragmentation and cache-misses you're likely to get with linked lists typically make them much slower than using an array with reshuffling on insertion/removal. Note that memory used for storing items in the array is not allocated until the first time an item is added to the array - if it is important to have the memory allocated when the array is created, just do Add/RemoveLast to fix it.

Definition at line 29 of file Array.h.


Constructor & Destructor Documentation

template<class TYPE >
Array< TYPE >::Array ( int  initialCapacity = 64  ) 

Constructor

Parameters:
initialCapacity Maximum number of items the array can initially store. Will be doubled every time the available space is used up.

Definition at line 8 of file Array.inl.

template<class TYPE>
Array< TYPE >::Array ( const Array< TYPE > &  arrayToCopy  ) 

Copy Constructor

Parameters:
arrayToCopy Array to copy all items from

Definition at line 20 of file Array.inl.

template<class TYPE >
Array< TYPE >::~Array (  ) 

Destructor

Definition at line 87 of file Array.inl.


Member Function Documentation

template<class TYPE>
const Array< TYPE > & Array< TYPE >::operator= ( const Array< TYPE > &  arrayToCopy  ) 

Assignment operator

Returns:
The new array
Parameters:
arrayToCopy Array to copy all items from

Definition at line 51 of file Array.inl.

template<class TYPE>
TYPE & Array< TYPE >::Add ( const TYPE &  item  ) 

Adds an item to the end of the array, and resizes the array if necessary. This is the fastest way to add an item to the array, and if the capacity of he array is large enough to hold the additional item, so that no resizing has to be done, very little work is done.

Parameters:
item Item to insert at the end of the array

Definition at line 101 of file Array.inl.

template<class TYPE>
void Array< TYPE >::InsertBefore ( const TYPE &  item,
int  index 
)

Inserts an item into the array. The new item will be inserted before the item with the specified index, and all items after it will be moved one slot. The array will be resized if necessary. If the specified index is not within valid range, the item will not be inserted, and in debug builds an assertion is triggered.

Parameters:
item Item to insert
index Index before which the new item will be inserted.

Definition at line 150 of file Array.inl.

template<class TYPE>
void Array< TYPE >::InsertBefore ( const TYPE &  item,
const ArrayIterator< TYPE > &  insertBefore 
)

Inserts an item into the array. The new item will be inserted before the item indicated by the iterator, and all items after it will be moved one slot. The array will be resized if necessary. If the specified position is not within valid range, the item will not be inserted, and in debug builds an assertion is triggered.

Parameters:
item Item to insert
insertBefore Iterator indicating the item before which to insert the new one

Definition at line 179 of file Array.inl.

template<class TYPE>
void Array< TYPE >::InsertAfter ( const TYPE &  item,
int  index 
)

Inserts an item into the array. The new item will be inserted after the item with the specified index, and all items after it will be moved one slot. The array will be resized if necessary. If the specified index is not within valid range, the item will not be inserted, and in debug builds an assertion is triggered.

Parameters:
item Item to insert
index Index after which the new item will be inserted.

Definition at line 195 of file Array.inl.

template<class TYPE>
void Array< TYPE >::InsertAfter ( const TYPE &  item,
const ArrayIterator< TYPE > &  insertAfter 
)

Inserts an item into the array. The new item will be inserted after the item indicted by the iterator, and all items after it will be moved one slot. The array will be resized if necessary. If the specified position is not within valid range, the item will not be inserted, and in debug builds an assertion is triggered.

Parameters:
item Item to insert
insertAfter Iterator indicating the item after which to insert the new one

Definition at line 227 of file Array.inl.

template<class TYPE >
void Array< TYPE >::RemoveLast (  ) 

Removes the last item from the array. This is a very fast way to remove an item from the array. If there are no items in the array, this method doesn't do anything (though in debug builds and assertion is triggered).

Definition at line 242 of file Array.inl.

template<class TYPE >
void Array< TYPE >::Remove ( int  index  ) 

Removes an item from the array, specified by index. Moves all the items that come after it one slot to fill the slot of the removed item. If the specified index is not within valid range, no item will be removed, and in debug builds an assertion is triggered.

Parameters:
index Item to remove

Definition at line 259 of file Array.inl.

template<class TYPE>
void Array< TYPE >::Remove ( const ArrayIterator< TYPE > &  iterator  ) 

Removes an item from the array, as indicated by the specified iterator. Moves all the items that come after it one slot to fill the slot of the removed item. If the position is not within valid range, no item will be removed, and in debug builds an assertion is triggered.

Parameters:
iterator Iterator indicating the item to remove

Definition at line 288 of file Array.inl.

template<class TYPE >
int Array< TYPE >::GetItemCount (  )  const

Gets the number of items in the array

Returns:
Number of items currently being stored in the array

Definition at line 303 of file Array.inl.

template<class TYPE >
TYPE & Array< TYPE >::Get ( int  index  )  const

Gets the item at the specified index. If the specified index is not within valid range, a default value is returned, and in debug builds an assertion is triggered.

Returns:
Item at specified index
Parameters:
index Index of item to get

Definition at line 312 of file Array.inl.

template<class TYPE>
TYPE & Array< TYPE >::Get ( const ArrayIterator< TYPE > &  iterator  )  const

Gets the item at the location indicated by the specified iterator. If the iterator is not within valid range, a default value is returned, and in debug builds an assertion is triggered.

Returns:
Item at location indicated by iterator
Parameters:
iterator Iterator indicating the item to get

Definition at line 328 of file Array.inl.

template<class TYPE>
void Array< TYPE >::Set ( int  index,
const TYPE &  item 
)

Sets item at specified index. If the index is larger than the current number of items, the number of items will be adjusted. If the index is larger than the capacity of the array, the array will be resized to be large enough to cope with the specified index. If index < 0, the item will not be set, and in debug builds an assertion will be triggered.

Parameters:
index Index of item to set
item Item to set at the specified index

Definition at line 345 of file Array.inl.

template<class TYPE>
void Array< TYPE >::Set ( const ArrayIterator< TYPE > &  iterator,
const TYPE &  item 
)

Sets item indicated by the specified iterator. If the index is larger than the current number of items, the number of items will be adjusted. If the index is larger than the capacity of the array, the array will be resized to be large enough to cope with the specified index. If index < 0, the item will not be set, and in debug builds an assertion will be triggered.

Parameters:
iterator Iterator indicating the location to set
item Item to set at the specified location

Definition at line 413 of file Array.inl.

template<class TYPE >
int Array< TYPE >::GetCapacity (  )  const

Gets the maximum number of items that can be stored in the array

Returns:
The current capacity of the array

Definition at line 427 of file Array.inl.

template<class TYPE >
void Array< TYPE >::SetCapacity ( int  capacity  ) 

Sets the maximum number of items that can be stored in the array. If the new capacity is <= 0, the capacity will remain unchanged, and in debug builds an assertion will be triggered. If the new capacity is less than the current number of items, the array will be truncated to the new capacity.

Definition at line 436 of file Array.inl.

template<class TYPE >
void Array< TYPE >::Clear ( bool  releaseMemory = false  ) 

Removes all items from the array

Parameters:
releaseMemory If set to true, all memory used by the array will be released, and the capacity is reset to the initial value

Definition at line 481 of file Array.inl.

template<class TYPE>
bool Array< TYPE >::ItemExists ( const TYPE &  item  )  const

Loops through the array and checks for the specified item. If it is found, it returns true.

Returns:
True if the item exists, false it it doesn't
Parameters:
item Item to look for

Definition at line 506 of file Array.inl.

template<class TYPE >
TYPE * Array< TYPE >::GetPointer (  )  const

Gets a pointer to the array data. This should be used carefully, and only when performance is really, really crucial. In most cases, the Get/Set methods are sufficient, and worth using as they are safe and won't crash.

Returns:
A pointer to the array data

Definition at line 524 of file Array.inl.



Pixie University and the Pixie Game Engine is created and managed by Mattias Gustavsson.
Reproduction/republishing of any material on this site without permission is strictly prohibited.