XFuDynamicArray.h Source File

Main Page Class Hierarchy Alphabetical List Compound List File List Compound Members File Members Related Pages

# XFuDynamicArray.h

Go to the documentation of this file.

    00001 /*! \file
    00002  * X-Forge Util <br>
    00003  * Copyright 2000-2003 Fathammer Ltd
    00004  *
    00005  * \brief Dynamic array template
    00006  *
    00007  * $Id: XFuDynamicArray.h,v 1.10 2003/08/12 13:33:52 lars Exp $
    00008  * $Date: 2003/08/12 13:33:52 $
    00009  * $Revision: 1.10 $
    00010  */
    00011
    00012 #ifndef XFUDYNAMICARRAY_H_INCLUDED
    00013 #define XFUDYNAMICARRAY_H_INCLUDED
    00014
    00015
    00016 //! Default size increment for the element pointer array.
    00017 #define XFUDYNAMICARRAY_DEFAULT_SIZE_INCREMENT 10
    00018
    00019
    00020 //! Dynamic array template.
    00021 /*!
    00022  * Simple usage example to store UINT32's:<br>
    00023  *
    00024  * \code
    00025  * // Create the array
    00026  * XFuDynamicArray<UINT32> *store = XFuDynamicArray<UINT32>::create();
    00027  * // put 3 values to the end of the array
    00028  * store->put(1);
    00029  * store->put(3);
    00030  * store->put(4);
    00031  * // Insert value in the second position of the array
    00032  * store->put(1, 2);
    00033  * // store now contains { 1, 2, 3, 4 }
    00034  * UINT32 value = store->get(3);
    00035  * // value is now 4
    00036  * delete store;
    00037  * \endcode
    00038  */
    00039 template<class T> class XFuDynamicArray
    00040 {
    00041 protected:
    00042     //! The number of elements in the array.
    00043     UINT32 mElementCount;
    00044     //! Array of elements stored by the dynamic array.
    00045     T *mElements;
    00046
    00047     //! Size increment.
    00048     UINT32 mSizeIncrement;
    00049
    00050     //! The size of the element array.
    00051     UINT32 mArraySize;
    00052
    00053     //! Resizes the element array.
    00054     void setArraySize(const UINT32 aSize);
    00055
    00056     //! Protected constructor.
    00057     XFuDynamicArray();
    00058
    00059 public:
    00060     //! Returns the size increment.
    00061     UINT32 getSizeIncrement() const;
    00062     //! Sets the size increment.
    00063     void setSizeIncrement(const UINT32 aSizeIncrement);
    00064     //! Returns the number of elements stored in the array.
    00065     UINT32 size() const;
    00066     //! Returns the size of the array.
    00067     UINT32 maxSize() const;
    00068     //! Checks whether the array has any elements.
    00069     INT isEmpty() const;
    00070
    00071     //! Returns an element at the specified position in the array.
    00072     T get(const UINT32 aPos) const;
    00073     //! Returns the last element from the array.
    00074     T get() const;
    00075     //! Adds an element to the end of the array.
    00076     void put(T aElement);
    00077     //! Inserts an element to the array at the specified position.
    00078     void put(const UINT32 aPos, T aElement);
    00079     //! Removes an element from the array and returns the removed element.
    00080     T removeIndex(const UINT32 aPos);
    00081     //! Removes an element from the array.
    00082     T remove(T aElement);
    00083     //! Removes the last element from the array and returns the removed element.
    00084     T remove();
    00085     //! Clears the array, but does not free its internal storage.
    00086     void clear();
    00087
    00088     //! Static constructor.
    00089     static XFuDynamicArray * create(const UINT32 aSize = XFUDYNAMICARRAY_DEFAULT_SIZE_INCREMENT);
    00090     //! Destructor.
    00091     ~XFuDynamicArray();
    00092 };
    00093
    00094
    00095 template<class T> void XFuDynamicArray<T>::setArraySize(const UINT32 aSize)
    00096 {
    00097     mElements = (T *)realloc(mElements, aSize * sizeof(T));
    00098     mArraySize = aSize;
    00099 }
    00100
    00101
    00102 template<class T> XFuDynamicArray<T>::XFuDynamicArray()
    00103 {
    00104     mElements = NULL;
    00105     mElementCount = 0;
    00106     mArraySize = 0;
    00107 }
    00108
    00109
    00110 template<class T> UINT32 XFuDynamicArray<T>::getSizeIncrement() const
    00111 {
    00112     return mSizeIncrement;
    00113 }
    00114
    00115
    00116 template<class T>
    00117 void XFuDynamicArray<T>::setSizeIncrement(const UINT32 aSizeIncrement)
    00118 {
    00119     mSizeIncrement = aSizeIncrement;
    00120 }
    00121
    00122
    00123 template<class T> UINT32 XFuDynamicArray<T>::size() const
    00124 {
    00125     return mElementCount;
    00126 }
    00127
    00128
    00129 template<class T> UINT32 XFuDynamicArray<T>::maxSize() const
    00130 {
    00131     return mArraySize;
    00132 }
    00133
    00134
    00135 template<class T> INT XFuDynamicArray<T>::isEmpty() const
    00136 {
    00137     if (mElementCount > 0)
    00138         return 0;
    00139     else
    00140         return 1;
    00141 }
    00142
    00143
    00144 template<class T> T XFuDynamicArray<T>::get(const UINT32 aPos) const
    00145 {
    00146     if (aPos >= mElementCount)
    00147         return T();
    00148     else
    00149         return mElements[aPos];
    00150 }
    00151
    00152
    00153 template<class T> T XFuDynamicArray<T>::get() const
    00154 {
    00155     if (mElementCount == 0)
    00156         return T();
    00157     else
    00158         return mElements[mElementCount - 1];
    00159 }
    00160
    00161
    00162 template<class T> void XFuDynamicArray<T>::put(T aElement)
    00163 {
    00164     put(mElementCount, aElement);
    00165 }
    00166
    00167
    00168 template<class T> void XFuDynamicArray<T>::put(const UINT32 aPos, T aElement)
    00169 {
    00170     if (aPos > mElementCount) return;
    00171
    00172     if (mElementCount >= mArraySize)
    00173         setArraySize(mArraySize + mSizeIncrement);
    00174
    00175     if (aPos < mElementCount)
    00176     {
    00177         UINT32 i;
    00178         for (i = mElementCount; i > aPos; --i)
    00179         {
    00180             mElements[i] = mElements[i - 1];
    00181         }
    00182     }
    00183
    00184     mElements[aPos] = aElement;
    00185
    00186     mElementCount++;
    00187 }
    00188
    00189
    00190 template<class T> T XFuDynamicArray<T>::removeIndex(const UINT32 aPos)
    00191 {
    00192     if (aPos >= mElementCount) return T();
    00193
    00194     T element = mElements[aPos];
    00195
    00196     UINT32 i, lastElement;
    00197     lastElement = mElementCount - 1;
    00198     for (i = aPos; i < lastElement; ++i)
    00199     {
    00200         mElements[i] = mElements[i + 1];
    00201     }
    00202
    00203     mElementCount--;
    00204
    00205     return element;
    00206 }
    00207
    00208
    00209 template<class T> T XFuDynamicArray<T>::remove()
    00210 {
    00211     return removeIndex(mElementCount - 1);
    00212 }
    00213
    00214
    00215 template<class T> T XFuDynamicArray<T>::remove(T aElement)
    00216 {
    00217     UINT32 i;
    00218     for (i = 0; i < mElementCount; ++i)
    00219     {
    00220         if (mElements[i] == aElement) return removeIndex(i);
    00221     }
    00222
    00223     return T();
    00224 }
    00225
    00226
    00227 template<class T> void XFuDynamicArray<T>::clear()
    00228 {
    00229     mElementCount = 0;
    00230 }
    00231
    00232
    00233 template<class T>
    00234 XFuDynamicArray<T> * XFuDynamicArray<T>::create(const UINT32 aSize)
    00235 {
    00236     XFuDynamicArray<T> *xfuda = new XFuDynamicArray<T>();
    00237     if (xfuda == NULL) return NULL;
    00238     xfuda->mElements = (T *)malloc(aSize * sizeof(T));
    00239     xfuda->mElementCount = 0;
    00240     xfuda->mArraySize = aSize;
    00241     xfuda->mSizeIncrement = XFUDYNAMICARRAY_DEFAULT_SIZE_INCREMENT;
    00242
    00243     return xfuda;
    00244 }
    00245
    00246
    00247 template<class T> XFuDynamicArray<T>::~XFuDynamicArray()
    00248 {
    00249     if (mElements != NULL)
    00250     {
    00251         free(mElements);
    00252         mElements = NULL;
    00253     }
    00254 }
    00255
    00256
    00257 #endif // !XFUDYNAMICARRAY_H_INCLUDED
    00258

|  | X-Forge Documentation Confidential Copyright © 2002-2003 Fathammer |  | Documentation generated with doxygen by Dimitri van Heesch