| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | #ifndef DYNAMICARRAY_H |
| 6 | |
| 7 | #define DYNAMICARRAY_H |
| 8 | |
| 9 | #include "memory.h" |
| 10 | |
| 11 | const int START_SIZE = 24 ; |
| 12 | const int MIN_SIZE = 8 ; |
| 13 | |
| 14 | template <class T> |
| 15 | class DynamicArray |
| 16 | { |
| 17 | public: |
| 18 | DynamicArray(int iSize = START_SIZE) ; |
| 19 | ~DynamicArray() ; |
| 20 | T& operator[](int i) ; |
| 21 | bool Error() ; |
| 22 | private: |
| 23 | T* m_pArray ; |
| 24 | int m_iMemSize ; |
| 25 | int m_iArraySize ; |
| 26 | bool m_bError ; |
| 27 | }; |
| 28 | |
| 29 | /************************************************************************ |
| 30 | * * |
| 31 | * Default constructor. User has option to pass in the size of the * |
| 32 | * initial array. * |
| 33 | * * |
| 34 | ************************************************************************/ |
| 35 | template<class T> DynamicArray<T>::DynamicArray(int iSize) |
| 36 | { |
| 37 | if( iSize < MIN_SIZE ) |
| 38 | { |
| 39 | iSize = MIN_SIZE ; |
| 40 | } |
| 41 | m_pArray = new T[iSize] ; |
| 42 | m_iMemSize = iSize ; |
| 43 | m_iArraySize = 0 ; |
| 44 | m_bError = false ; |
| 45 | } |
| 46 | |
| 47 | /************************************************************************ |
| 48 | * * |
| 49 | * Destructor. All it really has to do is delete the array. * |
| 50 | * * |
| 51 | ************************************************************************/ |
| 52 | template<class T> DynamicArray<T>::~DynamicArray() |
| 53 | { |
| 54 | if( m_pArray ) |
| 55 | { |
| 56 | delete [] m_pArray ; |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /************************************************************************ |
| 61 | * * |
| 62 | * operator [] to work on the left or right side of the equation. * |
| 63 | * * |
| 64 | ************************************************************************/ |
| 65 | template<class T> T& DynamicArray<T>::operator [](int iIndex) |
| 66 | { |
| 67 | if( iIndex < 0 ) |
| 68 | { |
| 69 | // Error, set error value to true and return the first element of the array |
| 70 | m_bError = true ; |
| 71 | return m_pArray[0] ; |
| 72 | } |
| 73 | else if ( iIndex >= m_iArraySize ) |
| 74 | { |
| 75 | if( iIndex >= m_iMemSize ) |
| 76 | { |
| 77 | int iNewSize ; |
| 78 | if( iIndex >= m_iMemSize * 2 ) |
| 79 | { |
| 80 | iNewSize = iIndex + 1 ; |
| 81 | } |
| 82 | else |
| 83 | { |
| 84 | iNewSize = m_iMemSize * 2 ; |
| 85 | } |
| 86 | |
| 87 | // We need to allocate more memory |
| 88 | T* pTmp = new T[iNewSize] ; |
| 89 | memcpy(pTmp, m_pArray, m_iMemSize * sizeof(T)) ; |
| 90 | delete [] m_pArray ; |
| 91 | m_pArray = pTmp ; |
| 92 | // Record the new memory size |
| 93 | m_iMemSize = iNewSize ; |
| 94 | } |
| 95 | |
| 96 | //ZeroMemory(&m_pArray[iIndex], sizeof(T)) ; |
| 97 | |
| 98 | ++m_iArraySize ; |
| 99 | } |
| 100 | |
| 101 | return m_pArray[iIndex] ; |
| 102 | } |
| 103 | |
| 104 | template<class T> bool DynamicArray<T>::Error() |
| 105 | { |
| 106 | return m_bError ; |
| 107 | } |
| 108 | |
| 109 | #endif |
| 110 | |