AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size > Class Template Reference

#include <DynamicArray.h>

Inherits AVRCpp::Collection::BaseArray< DataType, SizeType, DataType * >.

List of all members.

Public Member Functions

 DynamicArray ()
 Constructor.
 ~DynamicArray ()
 Destructor.
bool Resize (const SizeType size, const DataType &values=DataType())
 Resize array to certain length.
bool Add (const DataType &value)
 Add item to the end of array.
bool Insert (const DataType &value, const SizeType pos)
 Insert item to the pos.
bool Remove (const SizeType pos)
 Remove item at pos.
bool Clear ()
 Clear array.
SizeType GetCapacity (void)
 Return array capacity.
bool IsFull (void)
 Return whether the array is in full capacity or not.
SizeType GetSize (void)
 Return array size.
bool IsEmpty (void)
 Return whether the array is empty or not.
DataType & At (SizeTypepos)
 Return reference of item at specified position NB! For speed purposes does not check constraints!

Protected Member Functions

bool Realloc (SizeType size)
 Try resize the array.

Protected Attributes

SizeType allocated_size
DataType * data
SizeType current_size


Detailed Description

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
class AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >

Definition at line 63 of file DynamicArray.h.


Constructor & Destructor Documentation

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::DynamicArray (  )  [inline]

Constructor.

Definition at line 96 of file DynamicArray.h.

00097                 {
00098                         BaseArray<DataType, SizeType, DataType*>();
00099 
00100                         this->data                              = (DataType *) malloc(reserve_size);
00101                         this->allocated_size    = 0;
00102                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::~DynamicArray (  )  [inline]

Destructor.

Definition at line 107 of file DynamicArray.h.

00108                 {
00109                         free(this->data);
00110                 }


Member Function Documentation

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::Realloc ( SizeType  size  )  [inline, protected]

Try resize the array.

Definition at line 71 of file DynamicArray.h.

00072                 {
00073                         // Don't allow smaller allocation than reserve size
00074                         if (size < reserve_size) size = reserve_size;
00075 
00076                         // If current allocation equals new size then skip
00077                         if (this->allocated_size == size) return true;
00078 
00079                         // Try reallocate
00080                         DataType *newData = (DataType *) realloc(this->data, sizeof(DataType) * size);
00081                         if (newData)
00082                         {
00083                                 this->data = newData;
00084                                 this->allocated_size = size;
00085                                 return true;
00086                         }
00087                         
00088                         return false;
00089                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::Resize ( const SizeType  size,
const DataType &  values = DataType() 
) [inline]

Resize array to certain length.

Reimplemented from AVRCpp::Collection::BaseArray< DataType, SizeType, DataType * >.

Definition at line 115 of file DynamicArray.h.

00116                 {
00117                         // If new size is larger than maximum capacity then abort
00118                         if (size >= array_capacity) return false;
00119 
00120                         if (!this->Realloc(size)) return false;
00121 
00122                         return BaseArray<DataType, SizeType, DataType*>::Resize(size, values);
00123                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::Add ( const DataType &  value  )  [inline]

Add item to the end of array.

Reimplemented from AVRCpp::Collection::BaseArray< DataType, SizeType, DataType * >.

Definition at line 128 of file DynamicArray.h.

00129                 {
00130                         // If new size is larger than maximum capacity then abort
00131                         if (this->current_size >= array_capacity) return false;
00132 
00133                         // If new size is larger than allocated size then increase memory size by one block
00134                         if (this->current_size >= this->allocated_size)
00135                         {
00136                                 if (!this->Realloc(this->current_size + block_size)) return false;
00137                         }
00138 
00139                         return BaseArray<DataType, SizeType, DataType*>::Add(value);
00140                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::Insert ( const DataType &  value,
const SizeType  pos 
) [inline]

Insert item to the pos.

Reimplemented from AVRCpp::Collection::BaseArray< DataType, SizeType, DataType * >.

Definition at line 145 of file DynamicArray.h.

00146                 {
00147                         // If new size is larger than maximum then abort
00148                         if (this->current_size >= array_capacity) return false;
00149 
00150                         // Check constraints
00151                         if (pos > this->current_size) return false;
00152 
00153                         // If new size is larger than allocated size then increase memory size by one block
00154                         if (this->current_size >= this->allocated_size)
00155                         {
00156                                 if (!this->Realloc(this->current_size + block_size)) return false;
00157                         }
00158 
00159                         return BaseArray<DataType, SizeType, DataType*>::Insert(value, pos);
00160                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::Remove ( const SizeType  pos  )  [inline]

Remove item at pos.

Reimplemented from AVRCpp::Collection::BaseArray< DataType, SizeType, DataType * >.

Definition at line 165 of file DynamicArray.h.

00166                 {
00167                         if (!BaseArray<DataType, SizeType, DataType*>::Remove(pos)) return false;
00168                         
00169                         // If current size is one block size smaller than allocated size then decrease memory size
00170                         if (this->current_size <= this->allocated_size - block_size)
00171                         {
00172                                 // No matter the realloc succeeds or not, fictios size will still be smaller
00173                                 this->Realloc(this->allocated_size - block_size);
00174                         }
00175 
00176                         return true;
00177                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::Clear (  )  [inline]

Clear array.

Reimplemented from AVRCpp::Collection::BaseArray< DataType, SizeType, DataType * >.

Definition at line 182 of file DynamicArray.h.

00183                 {
00184                         if (!BaseArray<DataType, SizeType, DataType*>::Clear()) return false;
00185                         
00186                         // No matter the realloc succeeds or not, fictios size will still be 0
00187                         this->Realloc(0);
00188 
00189                         return true;
00190                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
SizeType AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::GetCapacity ( void   )  [inline]

Return array capacity.

Definition at line 195 of file DynamicArray.h.

00196                 {
00197                         return array_capacity;
00198                 }

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
bool AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::IsFull ( void   )  [inline]

Return whether the array is in full capacity or not.

Definition at line 203 of file DynamicArray.h.

00204                 {
00205                         return (this->current_size == array_capacity);
00206                 }

SizeType AVRCpp::Collection::BaseArray< DataType , SizeType , DataType * >::GetSize ( void   )  [inline, inherited]

Return array size.

Definition at line 145 of file BaseArray.h.

00146                 {
00147                         return this->current_size;
00148                 }

bool AVRCpp::Collection::BaseArray< DataType , SizeType , DataType * >::IsEmpty ( void   )  [inline, inherited]

Return whether the array is empty or not.

Definition at line 153 of file BaseArray.h.

00154                 {
00155                         return (this->current_size == 0);
00156                 }

DataType & AVRCpp::Collection::BaseArray< DataType , SizeType , DataType * >::At ( SizeType   pos  )  [inline, inherited]

Return reference of item at specified position NB! For speed purposes does not check constraints!

Definition at line 172 of file BaseArray.h.

00173                 {
00174                         return this->data[pos];
00175                 }


Member Data Documentation

template<typename DataType, typename SizeType, SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1, SizeType reserve_size = 8, SizeType block_size = 8>
SizeType AVRCpp::Collection::DynamicArray< DataType, SizeType, array_capacity, reserve_size, block_size >::allocated_size [protected]

Definition at line 66 of file DynamicArray.h.

DataType * AVRCpp::Collection::BaseArray< DataType , SizeType , DataType * >::data [protected, inherited]

Definition at line 55 of file BaseArray.h.

SizeType AVRCpp::Collection::BaseArray< DataType , SizeType , DataType * >::current_size [protected, inherited]

Definition at line 56 of file BaseArray.h.


The documentation for this class was generated from the following file:
Generated on Sat Sep 15 23:41:47 2007 for AVR C++ Lib (common) by  doxygen 1.5.2
SourceForge.net Logo MTÜ TTÜ Robotiklubi