DynamicArray.cpp
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: DynamicArray.cpp
37 // Author: Pedram Azad
38 // Date: 2006
39 // ****************************************************************************
40 
41 
42 // ****************************************************************************
43 // Includes
44 // ****************************************************************************
45 
46 #include <new> // for explicitly using correct new/delete operators on VC DSPs
47 
48 #include "DynamicArray.h"
49 
50 #include <stdio.h>
51 
52 
53 
54 // ****************************************************************************
55 // Constructor / Destructor
56 // ****************************************************************************
57 
59 {
60  m_nElements = 0;
61  m_nCurrentSize = nInitialSize;
62  m_ppElements = new CDynamicArrayElement*[nInitialSize];
63 
64  for (int i = 0; i < nInitialSize; i++)
65  m_ppElements[i] = 0;
66 }
67 
69 {
70  for (int i = 0; i < m_nElements; i++)
71  if (m_ppElements[i]->bDelete)
72  delete m_ppElements[i];
73 
74  delete [] m_ppElements;
75 
76 }
77 
78 
79 // ****************************************************************************
80 // Methods
81 // ****************************************************************************
82 
84 {
85  m_ppElements[nElement]->bDelete = false;
86 }
87 
89 {
90  for (int i = 0; i < m_nElements; i++)
91  {
92  if (m_ppElements[i]->bDelete)
93  delete m_ppElements[i];
94 
95  m_ppElements[i] = 0;
96  }
97 
98  m_nElements = 0;
99 
100 }
101 
102 void CDynamicArray::SetSize(int nSize)
103 {
104  if (nSize <= m_nCurrentSize)
105  {
106  printf("error: tried to set size smaller than current size in CDynamicArray::SetSize\n");
107  return;
108  }
109 
110  m_nCurrentSize = nSize;
111 
112  CDynamicArrayElement **ppElements = new CDynamicArrayElement*[nSize];
113 
114  int i;
115 
116  for (i = 0; i < m_nElements; i++)
117  ppElements[i] = m_ppElements[i];
118 
119  for (i = m_nElements; i < nSize; i++)
120  ppElements[i] = 0;
121 
122  delete [] m_ppElements;
123  m_ppElements = ppElements;
124 }
125 
126 bool CDynamicArray::AddElement(CDynamicArrayElement *pElement, bool bAddUniqueOnly, bool bManageMemory)
127 {
128  if (bAddUniqueOnly && FindFirstMatch(pElement))
129  return false;
130 
131  if (m_nElements > (m_nCurrentSize * 3 / 4))
132  SetSize(m_nCurrentSize * 2);
133 
134  pElement->bDelete = bManageMemory;
135  m_ppElements[m_nElements++] = pElement;
136 
137  return true;
138 }
139 
141 {
142  if (nIndex < 0 || nIndex >= m_nElements)
143  return false;
144 
145  delete m_ppElements[nIndex];
146  m_ppElements[nIndex] = 0;
147 
148  for (int i = nIndex; i < m_nElements - 1; i++)
149  m_ppElements[i] = m_ppElements[i + 1];
150 
151  m_nElements--;
152 
153  return true;
154 }
155 
157 {
158  int nIndex = _FindFirstMatch(pElement);
159 
160  if (nIndex == -1)
161  return 0;
162 
163  DeleteElement(nIndex);
164 
165  return 1;
166 }
167 
169 {
170  int nIndex, nElementsDeleted = 0;
171 
172  while ((nIndex = _FindFirstMatch(pElement)) != -1)
173  {
174  DeleteElement(nIndex);
175  nElementsDeleted++;
176  }
177 
178  return nElementsDeleted;
179 }
180 
182 {
183  for (int i = 0; i < m_nElements; i++)
184  {
185  if (pElement->Equivalent(m_ppElements[i]))
186  return m_ppElements[i];
187  }
188 
189  return 0;
190 }
191 
193 {
194  for (int i = 0; i < m_nElements; i++)
195  {
196  if (pElement->Equivalent(m_ppElements[i]))
197  return i;
198  }
199 
200  return -1;
201 }
202 
204 {
205  float best_error = 9999999;
206  int best_i = -1;
207 
208  for (int i = 0; i < m_nElements; i++)
209  {
210  const float error = pElement->Error(m_ppElements[i]);
211 
212  if (error < best_error)
213  {
214  best_error = error;
215  best_i = i;
216  }
217  }
218 
219  if (best_i == -1)
220  return 0;
221 
222  fResultError = best_error;
223 
224  return m_ppElements[best_i];
225 }
CDynamicArrayElement * FindBestMatch(const CDynamicArrayElement *pElement, float &fResultError)
int DeleteAllMatches(const CDynamicArrayElement *pElement)
void SetSize(int nSize)
bool DeleteElement(int nIndex)
bool AddElement(CDynamicArrayElement *pElement, bool bAddUniqueOnly=false, bool bManageMemory=true)
int DeleteFirstMatch(const CDynamicArrayElement *pElement)
CDynamicArrayElement * FindFirstMatch(const CDynamicArrayElement *pElement)
void DontManageMemory(int nElement)
int _FindFirstMatch(const CDynamicArrayElement *pElement)
CDynamicArray(int nInititalSize)
CDynamicArrayElement ** m_ppElements
Definition: DynamicArray.h:114
virtual bool Equivalent(const CDynamicArrayElement *pElement) const
Definition: DynamicArray.h:67
virtual float Error(const CDynamicArrayElement *pElement) const
Definition: DynamicArray.h:68


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