| 1 | /* |
|---|
| 2 | ----------------------------------------------------------------------------- |
|---|
| 3 | This source file is part of LEXIExporter |
|---|
| 4 | |
|---|
| 5 | Copyright 2006 NDS Limited |
|---|
| 6 | |
|---|
| 7 | Author(s): |
|---|
| 8 | Lasse Tassing |
|---|
| 9 | |
|---|
| 10 | This program is free software; you can redistribute it and/or modify it under |
|---|
| 11 | the terms of the GNU Lesser General Public License as published by the Free Software |
|---|
| 12 | Foundation; either version 2 of the License, or (at your option) any later |
|---|
| 13 | version. |
|---|
| 14 | |
|---|
| 15 | This program is distributed in the hope that it will be useful, but WITHOUT |
|---|
| 16 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
|---|
| 17 | FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
|---|
| 18 | |
|---|
| 19 | You should have received a copy of the GNU Lesser General Public License along with |
|---|
| 20 | this program; if not, write to the Free Software Foundation, Inc., 59 Temple |
|---|
| 21 | Place - Suite 330, Boston, MA 02111-1307, USA, or go to |
|---|
| 22 | http://www.gnu.org/copyleft/lesser.txt. |
|---|
| 23 | ----------------------------------------------------------------------------- |
|---|
| 24 | */ |
|---|
| 25 | |
|---|
| 26 | #ifndef __FAST_VECTOR__ |
|---|
| 27 | #define __FAST_VECTOR__ |
|---|
| 28 | |
|---|
| 29 | // NOTE! The fastvector<> template can NOT be used with classes (pointers are OK), since it does |
|---|
| 30 | // not use new/delete for storage allocation. |
|---|
| 31 | template<class _T> class fastvector |
|---|
| 32 | { |
|---|
| 33 | public: |
|---|
| 34 | virtual ~fastvector() { HeapFree(GetProcessHeap(), 0, m_pData); }; |
|---|
| 35 | |
|---|
| 36 | // Base constructor |
|---|
| 37 | fastvector(void) |
|---|
| 38 | { |
|---|
| 39 | m_iGrowSize=10; |
|---|
| 40 | m_iAllocSize=m_iGrowSize; |
|---|
| 41 | m_iSize=0; |
|---|
| 42 | m_pData=(_T*)HeapAlloc(GetProcessHeap(), 0, m_iAllocSize*sizeof(_T)); |
|---|
| 43 | } |
|---|
| 44 | |
|---|
| 45 | // Copy Constructor |
|---|
| 46 | fastvector(const fastvector<_T>& other) |
|---|
| 47 | { |
|---|
| 48 | m_iAllocSize=other.m_iAllocSize; |
|---|
| 49 | m_iGrowSize=other.m_iGrowSize; |
|---|
| 50 | m_iSize=other.m_iSize; |
|---|
| 51 | m_pData=(_T*)HeapAlloc(GetProcessHeap(), 0, m_iAllocSize*sizeof(_T)); |
|---|
| 52 | CopyMemory(m_pData,other.m_pData,m_iSize*sizeof(_T)); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | // Data assignment operator |
|---|
| 56 | fastvector(const _T *pData, unsigned iElemCount) |
|---|
| 57 | { |
|---|
| 58 | m_iAllocSize=iElemCount; |
|---|
| 59 | m_iGrowSize=10; |
|---|
| 60 | m_iSize=iElemCount; |
|---|
| 61 | m_pData=(_T*)HeapAlloc(GetProcessHeap(), 0, m_iAllocSize*sizeof(_T)); |
|---|
| 62 | CopyMemory(m_pData,pData,iElemCount*sizeof(_T)); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | // Assignment operator |
|---|
| 66 | fastvector<_T>& operator=(const fastvector<_T>& _X) |
|---|
| 67 | { |
|---|
| 68 | reserve(_X.m_iSize); |
|---|
| 69 | m_iSize=_X.m_iSize; |
|---|
| 70 | CopyMemory(m_pData,_X.m_pData,m_iSize*sizeof(_T)); |
|---|
| 71 | return (*this); |
|---|
| 72 | } |
|---|
| 73 | |
|---|
| 74 | void assign(const _T *pData, unsigned iElemCount) |
|---|
| 75 | { |
|---|
| 76 | reserve(iElemCount); |
|---|
| 77 | m_iSize=iElemCount; |
|---|
| 78 | CopyMemory(m_pData,pData,iElemCount*sizeof(_T)); |
|---|
| 79 | } |
|---|
| 80 | void assign(const fastvector<_T>& _X) |
|---|
| 81 | { |
|---|
| 82 | reserve(_X.m_iSize); |
|---|
| 83 | m_iSize=_X.m_iSize; |
|---|
| 84 | CopyMemory(m_pData,_X.m_pData,m_iSize*sizeof(_T)); |
|---|
| 85 | } |
|---|
| 86 | |
|---|
| 87 | void reserve(unsigned iElemCount) |
|---|
| 88 | { |
|---|
| 89 | if(iElemCount>m_iAllocSize) |
|---|
| 90 | { |
|---|
| 91 | m_iAllocSize=iElemCount; |
|---|
| 92 | m_pData=(_T*)HeapReAlloc(GetProcessHeap(), 0, (void*)m_pData, m_iAllocSize*sizeof(_T)); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void append(const fastvector<_T>& _X) |
|---|
| 97 | { |
|---|
| 98 | reserve(m_iSize + _X.m_iSize); |
|---|
| 99 | CopyMemory(m_pData+m_iSize,_X.m_pData, _X.m_iSize*sizeof(_T)); |
|---|
| 100 | m_iSize=m_iSize + _X.m_iSize; |
|---|
| 101 | } |
|---|
| 102 | |
|---|
| 103 | // Retrieve number of elements in the list |
|---|
| 104 | unsigned size(void) const { return m_iSize; }; |
|---|
| 105 | |
|---|
| 106 | // Check if list is empty |
|---|
| 107 | bool empty(void) const { return m_iSize==0; }; |
|---|
| 108 | |
|---|
| 109 | // Indexing operators |
|---|
| 110 | const _T& operator[](unsigned iIndex) const |
|---|
| 111 | { |
|---|
| 112 | return (*(m_pData + iIndex)); |
|---|
| 113 | } |
|---|
| 114 | _T& operator[](unsigned iIndex) |
|---|
| 115 | { |
|---|
| 116 | return (*(m_pData + iIndex)); |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | // Element insertion |
|---|
| 120 | void push_back(const _T& _X) |
|---|
| 121 | { |
|---|
| 122 | if(m_iSize+1>m_iAllocSize) |
|---|
| 123 | reserve(m_iAllocSize+m_iGrowSize); |
|---|
| 124 | m_pData[m_iSize]=_X; |
|---|
| 125 | m_iSize++; |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | const _T& pop_back(void) |
|---|
| 129 | { |
|---|
| 130 | return m_pData[--m_iSize]; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | void erase(int iIndex) |
|---|
| 134 | { |
|---|
| 135 | int iMoveSize=m_iSize-iIndex; |
|---|
| 136 | if(iMoveSize>1) MoveMemory( m_pData+iIndex, m_pData+iIndex+1, (iMoveSize-1)*sizeof(_T)); |
|---|
| 137 | m_iSize--; |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | // Clear contents of the list |
|---|
| 141 | void clear(void) |
|---|
| 142 | { |
|---|
| 143 | m_iSize=0; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void sort(int (__cdecl *compare )(const void *, const void *)) |
|---|
| 147 | { |
|---|
| 148 | qsort((void*)m_pData, m_iSize, sizeof(_T), compare); |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private: |
|---|
| 152 | unsigned int m_iGrowSize; |
|---|
| 153 | unsigned int m_iSize; |
|---|
| 154 | unsigned int m_iAllocSize; |
|---|
| 155 | _T *m_pData; |
|---|
| 156 | }; |
|---|
| 157 | |
|---|
| 158 | #endif // End sentry |
|---|