DynamicArrayTemplate.h
Go to the documentation of this file.
1 // ****************************************************************************
2 // This file is part of the Integrating Vision Toolkit (IVT).
3 //
4 // The IVT is maintained by the Karlsruhe Institute of Technology (KIT)
5 // (www.kit.edu) in cooperation with the company Keyetech (www.keyetech.de).
6 //
7 // Copyright (C) 2014 Karlsruhe Institute of Technology (KIT).
8 // All rights reserved.
9 //
10 // Redistribution and use in source and binary forms, with or without
11 // modification, are permitted provided that the following conditions are met:
12 //
13 // 1. Redistributions of source code must retain the above copyright
14 // notice, this list of conditions and the following disclaimer.
15 //
16 // 2. Redistributions in binary form must reproduce the above copyright
17 // notice, this list of conditions and the following disclaimer in the
18 // documentation and/or other materials provided with the distribution.
19 //
20 // 3. Neither the name of the KIT nor the names of its contributors may be
21 // used to endorse or promote products derived from this software
22 // without specific prior written permission.
23 //
24 // THIS SOFTWARE IS PROVIDED BY THE KIT AND CONTRIBUTORS “AS IS” AND ANY
25 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
26 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
27 // DISCLAIMED. IN NO EVENT SHALL THE KIT OR CONTRIBUTORS BE LIABLE FOR ANY
28 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
29 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
30 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
33 // THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 // ****************************************************************************
35 // ****************************************************************************
36 // Filename: DynamicArrayTemplate.h
37 // Author: Pedram Azad
38 // Date: 02.02.2009
39 // ****************************************************************************
40 
41 #ifndef _DYNAMIC_ARRAY_TEMPLATE_H_
42 #define _DYNAMIC_ARRAY_TEMPLATE_H_
43 
44 
45 // ****************************************************************************
46 // Necessary includes
47 // ****************************************************************************
48 
49 #include <new> // for explicitly using correct new/delete operators on VC DSPs
50 #include <stdio.h>
51 
52 
53 
54 // ****************************************************************************
55 // CDynamicArrayTemplate
56 // ****************************************************************************
57 
58 template <typename T> class CDynamicArrayTemplate
59 {
60 public:
61  // constructor
62  CDynamicArrayTemplate(int nInitialSize = 10)
63  {
64  m_nElements = 0;
65  m_nCurrentSize = nInitialSize;
66  m_pElements = new T[m_nCurrentSize];
67  }
68 
69  // destructor
71  {
72  delete [] m_pElements;
73  }
74 
75 
76  // public methods
77  void AddElement(const T &element)
78  {
81 
82  m_pElements[m_nElements++] = element;
83  }
84 
86  {
89 
90  return m_pElements[m_nElements++];
91  }
92 
93  bool DeleteElement(int nIndex)
94  {
95  if (nIndex < 0 || nIndex >= m_nElements)
96  return false;
97 
98  for (int i = nIndex; i < m_nElements - 1; i++)
99  m_pElements[i] = m_pElements[i + 1];
100 
101  m_nElements--;
102 
103  return true;
104  }
105 
106  void Clear()
107  {
108  m_nElements = 0;
109  }
110 
111  void ClearAndResize(int nSize)
112  {
113  m_nElements = 0;
114 
115  m_nCurrentSize = nSize;
116 
117  delete [] m_pElements;
118  m_pElements = new T[m_nCurrentSize];
119  }
120 
121  int GetSize() const { return m_nElements; }
122  int GetStorageSize() const { return m_nCurrentSize; }
123  const T* GetElements() const { return m_pElements; }
124 
125  // operators
126  inline const T& operator[](const int nElement) const { return m_pElements[nElement]; }
127  inline T& operator[](const int nElement) { return m_pElements[nElement]; }
128 
129 
130 
131 private:
132  // private methods
133  void SetCurrentSize(int nCurrentSize)
134  {
135  if (nCurrentSize <= m_nCurrentSize)
136  {
137  printf("error: tried to set size smaller than current size in CDynamicArray::SetCurrentSize\n");
138  return;
139  }
140 
141  m_nCurrentSize = nCurrentSize;
142 
143  T *pElements = new T[nCurrentSize];
144 
145  for (int i = 0; i < m_nElements; i++)
146  pElements[i] = m_pElements[i];
147 
148  delete [] m_pElements;
149  m_pElements = pElements;
150  }
151 
152 
153  // private attribute
157 };
158 
159 
160 
161 #endif /* _DYNAMIC_ARRAY_TEMPLATE_H_ */
const T & operator[](const int nElement) const
void SetCurrentSize(int nCurrentSize)
void ClearAndResize(int nSize)
T & operator[](const int nElement)
void AddElement(const T &element)
CDynamicArrayTemplate(int nInitialSize=10)
const T * GetElements() const
bool DeleteElement(int nIndex)


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Mon Dec 2 2019 03:47:27