00001 // **************************************************************************** 00002 // This file is part of the Integrating Vision Toolkit (IVT). 00003 // 00004 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT) 00005 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de). 00006 // 00007 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT). 00008 // All rights reserved. 00009 // 00010 // Redistribution and use in source and binary forms, with or without 00011 // modification, are permitted provided that the following conditions are met: 00012 // 00013 // 1. Redistributions of source code must retain the above copyright 00014 // notice, this list of conditions and the following disclaimer. 00015 // 00016 // 2. Redistributions in binary form must reproduce the above copyright 00017 // notice, this list of conditions and the following disclaimer in the 00018 // documentation and/or other materials provided with the distribution. 00019 // 00020 // 3. Neither the name of the KIT nor the names of its contributors may be 00021 // used to endorse or promote products derived from this software 00022 // without specific prior written permission. 00023 // 00024 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY 00025 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00026 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00027 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY 00028 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 00029 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00030 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND 00031 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 00032 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 00033 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00034 // **************************************************************************** 00035 // **************************************************************************** 00036 // Filename: DynamicArrayTemplatePointer.h 00037 // Author: Pedram Azad 00038 // Date: 11.02.2010 00039 // **************************************************************************** 00040 00041 #ifndef _DYNAMIC_ARRAY_TEMPLATE_POINTER_H_ 00042 #define _DYNAMIC_ARRAY_TEMPLATE_POINTER_H_ 00043 00044 00045 // **************************************************************************** 00046 // Necessary includes 00047 // **************************************************************************** 00048 00049 #include <new> // for explicitly using correct new/delete operators on VC DSPs 00050 #include <stdio.h> 00051 00052 00053 00054 // **************************************************************************** 00055 // CDynamicArrayTemplatePointer 00056 // **************************************************************************** 00057 00058 template <typename T> class CDynamicArrayTemplatePointer 00059 { 00060 public: 00061 // constructors 00062 CDynamicArrayTemplatePointer(bool bManageMemory, int nInitialSize = 100) 00063 { 00064 m_bManageMemory = bManageMemory; 00065 00066 m_nElements = 0; 00067 m_nCurrentSize = nInitialSize; 00068 00069 m_ppElements = new T*[m_nCurrentSize]; 00070 for (int i = 0; i < m_nCurrentSize; i++) 00071 m_ppElements[i] = 0; 00072 } 00073 00074 // CDynamicArray 00075 ~CDynamicArrayTemplatePointer() 00076 { 00077 if (m_bManageMemory) 00078 { 00079 for (int i = 0; i < m_nElements; i++) 00080 delete m_ppElements[i]; 00081 } 00082 00083 delete [] m_ppElements; 00084 } 00085 00086 00087 // public methods 00088 void AddElement(T *pElement) 00089 { 00090 if (m_nElements == m_nCurrentSize) 00091 SetCurrentSize(m_nCurrentSize << 1); 00092 00093 m_ppElements[m_nElements++] = pElement; 00094 } 00095 00096 bool DeleteElement(int nIndex) 00097 { 00098 if (nIndex < 0 || nIndex >= m_nElements) 00099 return false; 00100 00101 if (m_bManageMemory) 00102 delete m_ppElements[nIndex]; 00103 00104 for (int i = nIndex; i < m_nElements - 1; i++) 00105 m_ppElements[i] = m_ppElements[i + 1]; 00106 00107 m_nElements--; 00108 00109 return true; 00110 } 00111 00112 void Clear() 00113 { 00114 if (m_bManageMemory) 00115 { 00116 for (int i = 0; i < m_nElements; i++) 00117 delete m_ppElements[i]; 00118 } 00119 00120 m_nElements = 0; 00121 } 00122 00123 void ClearAndResize(int nSize) 00124 { 00125 Clear(); 00126 00127 m_nCurrentSize = nSize; 00128 00129 delete [] m_ppElements; 00130 m_ppElements = new T[m_nCurrentSize]; 00131 } 00132 00133 int GetSize() const { return m_nElements; } 00134 int GetStorageSize() const { return m_nCurrentSize; } 00135 00136 // operators 00137 inline const T* operator[](const int nElement) const { return m_ppElements[nElement]; } 00138 inline T*& operator[](const int nElement) { return m_ppElements[nElement]; } 00139 00140 00141 00142 private: 00143 // private methods 00144 void SetCurrentSize(int nCurrentSize) 00145 { 00146 if (nCurrentSize <= m_nCurrentSize) 00147 { 00148 printf("error: tried to set size smaller than current size in CDynamicArray::SetCurrentSize\n"); 00149 return; 00150 } 00151 00152 m_nCurrentSize = nCurrentSize; 00153 00154 T **ppElements = new T*[nCurrentSize]; 00155 00156 for (int i = 0; i < m_nElements; i++) 00157 ppElements[i] = m_ppElements[i]; 00158 00159 delete [] m_ppElements; 00160 m_ppElements = ppElements; 00161 } 00162 00163 00164 // private attribute 00165 int m_nCurrentSize; 00166 int m_nElements; 00167 T **m_ppElements; 00168 00169 bool m_bManageMemory; 00170 }; 00171 00172 00173 00174 #endif /* _DYNAMIC_ARRAY_TEMPLATE_POINTER_H_ */