avr/cpp/collection/DynamicArray.h

Go to the documentation of this file.
00001 /**********************************************************************************************************************\
00002 
00003         C++ library for Atmel AVR microcontrollers
00004         Copyright (C) 2007 Lauri Kirikal, Mikk Leini, MT� TT� Robotiklubi
00005 
00006         This program is free software; you can redistribute it and/or
00007         modify it under the terms of the GNU General Public License
00008         as published by the Free Software Foundation; either version 2
00009         of the License, or (at your option) any later version.
00010 
00011         This program is distributed in the hope that it will be useful,
00012         but WITHOUT ANY WARRANTY; without even the implied warranty of
00013         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014         GNU General Public License for more details.
00015 
00016         You should have received a copy of the GNU General Public License
00017         along with this program; if not, write to the Free Software
00018         Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00019 
00020         See http://creativecommons.org/licenses/GPL/2.0/
00021 
00022         MT� TT� Robotiklubi  http://www.robotiklubi.ee robotiklubi@gmail.com
00023         Lauri Kirikal        laurikirikal@gmail.com
00024         Mikk Leini           mikk.leini@gmail.com
00025 
00026 \**********************************************************************************************************************/
00027 
00028 #ifndef __AVR_CPP_DYNAMIC_ARRAY_H__
00029 #define __AVR_CPP_DYNAMIC_ARRAY_H__
00030 
00031 #include <stdlib.h>
00032 #include "BaseArray.h"
00033 
00034 
00035 /**********************************************************************************************************************\
00036 
00037         AVRCppLib collection objects
00038 
00039 \**********************************************************************************************************************/
00040 
00041 namespace AVRCpp
00042 {
00043         namespace Collection
00044         {
00045 
00046 
00047 /**********************************************************************************************************************\
00048 
00049         General dynamic array class
00050         
00051         Holds a list of custom typed items. Supports virtually unlimited amount of item adding, inserting and removing.
00052         Possible to define maximum size limit, reserved and block size.
00053 
00054 \**********************************************************************************************************************/
00055 
00056 template <
00057                 typename DataType,
00058                 typename SizeType,
00059                 SizeType array_capacity = (1 << (sizeof(SizeType) << 3)) - 1,
00060                 SizeType reserve_size = 8,
00061                 SizeType block_size = 8>
00062 
00063 class DynamicArray : public BaseArray<DataType, SizeType, DataType*>
00064 {
00065         protected:
00066                 SizeType allocated_size;
00067 
00071                 bool Realloc(SizeType size)
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                 }
00090 
00091         public:
00092 
00096                 DynamicArray()
00097                 {
00098                         BaseArray<DataType, SizeType, DataType*>();
00099 
00100                         this->data                              = (DataType *) malloc(reserve_size);
00101                         this->allocated_size    = 0;
00102                 }
00103 
00107                 ~DynamicArray()
00108                 {
00109                         free(this->data);
00110                 }
00111 
00115                 bool Resize(const SizeType size, const DataType &values = DataType())
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                 }
00124 
00128                 bool Add(const DataType &value)
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                 }
00141 
00145                 bool Insert(const DataType &value, const SizeType pos)
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                 }
00161 
00165                 bool Remove(const SizeType pos)
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                 }
00178 
00182                 inline bool Clear()
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                 }
00191                 
00195                 inline SizeType GetCapacity(void)
00196                 {
00197                         return array_capacity;
00198                 }
00199 
00203                 inline bool IsFull(void)
00204                 {
00205                         return (this->current_size == array_capacity);
00206                 }
00207                 
00208 }; // class DynamicArray
00209 
00210 
00211 /**********************************************************************************************************************/
00212 
00213         } // namespace Collection
00214 
00215 } // namespace AVRCpp
00216 
00217 
00218 /**********************************************************************************************************************/
00219 
00220 #endif // ifndef __AVR_CPP_DYNAMIC_ARRAY_H__

Generated on Sat Sep 15 23:41:46 2007 for AVR C++ Lib (common) by  doxygen 1.5.2
SourceForge.net Logo MTÜ TTÜ Robotiklubi