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: DynamicArrayTemplate.h 00037 // Author: Pedram Azad 00038 // Date: 02.02.2009 00039 // **************************************************************************** 00040 00041 #ifndef _DYNAMIC_ARRAY_TEMPLATE_H_ 00042 #define _DYNAMIC_ARRAY_TEMPLATE_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 // CDynamicArrayTemplate 00056 // **************************************************************************** 00057 00058 template <typename T> class CDynamicArrayTemplate 00059 { 00060 public: 00061 // constructor 00062 CDynamicArrayTemplate(int nInitialSize = 10) 00063 { 00064 m_nElements = 0; 00065 m_nCurrentSize = nInitialSize; 00066 m_pElements = new T[m_nCurrentSize]; 00067 } 00068 00069 // destructor 00070 ~CDynamicArrayTemplate() 00071 { 00072 delete [] m_pElements; 00073 } 00074 00075 00076 // public methods 00077 void AddElement(const T &element) 00078 { 00079 if (m_nElements == m_nCurrentSize) 00080 SetCurrentSize(m_nCurrentSize << 1); 00081 00082 m_pElements[m_nElements++] = element; 00083 } 00084 00085 T& AddElement() 00086 { 00087 if (m_nElements == m_nCurrentSize) 00088 SetCurrentSize(m_nCurrentSize << 1); 00089 00090 return m_pElements[m_nElements++]; 00091 } 00092 00093 bool DeleteElement(int nIndex) 00094 { 00095 if (nIndex < 0 || nIndex >= m_nElements) 00096 return false; 00097 00098 for (int i = nIndex; i < m_nElements - 1; i++) 00099 m_pElements[i] = m_pElements[i + 1]; 00100 00101 m_nElements--; 00102 00103 return true; 00104 } 00105 00106 void Clear() 00107 { 00108 m_nElements = 0; 00109 } 00110 00111 void ClearAndResize(int nSize) 00112 { 00113 m_nElements = 0; 00114 00115 m_nCurrentSize = nSize; 00116 00117 delete [] m_pElements; 00118 m_pElements = new T[m_nCurrentSize]; 00119 } 00120 00121 int GetSize() const { return m_nElements; } 00122 int GetStorageSize() const { return m_nCurrentSize; } 00123 const T* GetElements() const { return m_pElements; } 00124 00125 // operators 00126 inline const T& operator[](const int nElement) const { return m_pElements[nElement]; } 00127 inline T& operator[](const int nElement) { return m_pElements[nElement]; } 00128 00129 00130 00131 private: 00132 // private methods 00133 void SetCurrentSize(int nCurrentSize) 00134 { 00135 if (nCurrentSize <= m_nCurrentSize) 00136 { 00137 printf("error: tried to set size smaller than current size in CDynamicArray::SetCurrentSize\n"); 00138 return; 00139 } 00140 00141 m_nCurrentSize = nCurrentSize; 00142 00143 T *pElements = new T[nCurrentSize]; 00144 00145 for (int i = 0; i < m_nElements; i++) 00146 pElements[i] = m_pElements[i]; 00147 00148 delete [] m_pElements; 00149 m_pElements = pElements; 00150 } 00151 00152 00153 // private attribute 00154 int m_nCurrentSize; 00155 int m_nElements; 00156 T *m_pElements; 00157 }; 00158 00159 00160 00161 #endif /* _DYNAMIC_ARRAY_TEMPLATE_H_ */