DynamicArray.cpp
Go to the documentation of this file.
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:  DynamicArray.cpp
00037 // Author:    Pedram Azad
00038 // Date:      2006
00039 // ****************************************************************************
00040 
00041 
00042 // ****************************************************************************
00043 // Includes
00044 // ****************************************************************************
00045 
00046 #include <new> // for explicitly using correct new/delete operators on VC DSPs
00047 
00048 #include "DynamicArray.h"
00049 
00050 #include <stdio.h>
00051 
00052 
00053 
00054 // ****************************************************************************
00055 // Constructor / Destructor
00056 // ****************************************************************************
00057 
00058 CDynamicArray::CDynamicArray(int nInitialSize)
00059 {
00060         m_nElements = 0;
00061         m_nCurrentSize = nInitialSize;
00062         m_ppElements = new CDynamicArrayElement*[nInitialSize];
00063         
00064         for (int i = 0; i < nInitialSize; i++)
00065                 m_ppElements[i] = 0;
00066 }
00067 
00068 CDynamicArray::~CDynamicArray()
00069 {
00070         for (int i = 0; i < m_nElements; i++)
00071                 if (m_ppElements[i]->bDelete)
00072                         delete m_ppElements[i];
00073 
00074         delete [] m_ppElements;
00075         
00076 }
00077 
00078 
00079 // ****************************************************************************
00080 // Methods
00081 // ****************************************************************************
00082 
00083 void CDynamicArray::DontManageMemory(int nElement)
00084 {
00085         m_ppElements[nElement]->bDelete = false;
00086 }
00087 
00088 void CDynamicArray::Clear()
00089 {
00090         for (int i = 0; i < m_nElements; i++)
00091         {
00092                 if (m_ppElements[i]->bDelete)
00093                         delete m_ppElements[i];
00094 
00095                 m_ppElements[i] = 0;
00096         }
00097 
00098         m_nElements = 0;
00099         
00100 }
00101 
00102 void CDynamicArray::SetSize(int nSize)
00103 {
00104         if (nSize <= m_nCurrentSize)
00105         {
00106                 printf("error: tried to set size smaller than current size in CDynamicArray::SetSize\n");
00107                 return;
00108         }
00109         
00110         m_nCurrentSize = nSize;
00111 
00112         CDynamicArrayElement **ppElements = new CDynamicArrayElement*[nSize];
00113         
00114         int i;
00115         
00116         for (i = 0; i < m_nElements; i++)
00117                 ppElements[i] = m_ppElements[i];
00118                 
00119         for (i = m_nElements; i < nSize; i++)
00120                 ppElements[i] = 0;
00121         
00122         delete [] m_ppElements;
00123         m_ppElements = ppElements;
00124 }
00125 
00126 bool CDynamicArray::AddElement(CDynamicArrayElement *pElement, bool bAddUniqueOnly, bool bManageMemory)
00127 {
00128         if (bAddUniqueOnly && FindFirstMatch(pElement))
00129                 return false;
00130                 
00131         if (m_nElements > (m_nCurrentSize * 3 / 4))
00132                 SetSize(m_nCurrentSize * 2);
00133                 
00134         pElement->bDelete = bManageMemory;
00135         m_ppElements[m_nElements++] = pElement;
00136                 
00137         return true;
00138 }
00139 
00140 bool CDynamicArray::DeleteElement(int nIndex)
00141 {
00142         if (nIndex < 0 || nIndex >= m_nElements)
00143                 return false;
00144 
00145         delete m_ppElements[nIndex];
00146         m_ppElements[nIndex] = 0;
00147         
00148         for (int i = nIndex; i < m_nElements - 1; i++)
00149                 m_ppElements[i] = m_ppElements[i + 1];
00150 
00151         m_nElements--;
00152 
00153         return true;
00154 }
00155 
00156 int CDynamicArray::DeleteFirstMatch(const CDynamicArrayElement *pElement)
00157 {
00158         int nIndex = _FindFirstMatch(pElement);
00159         
00160         if (nIndex == -1)
00161                 return 0;
00162                 
00163         DeleteElement(nIndex);
00164                 
00165         return 1;
00166 }
00167 
00168 int CDynamicArray::DeleteAllMatches(const CDynamicArrayElement *pElement)
00169 {
00170         int nIndex, nElementsDeleted = 0;
00171         
00172         while ((nIndex = _FindFirstMatch(pElement)) != -1)
00173         {
00174                 DeleteElement(nIndex);
00175                 nElementsDeleted++;
00176         }
00177         
00178         return nElementsDeleted;
00179 }
00180 
00181 CDynamicArrayElement* CDynamicArray::FindFirstMatch(const CDynamicArrayElement *pElement)
00182 {
00183         for (int i = 0; i < m_nElements; i++)
00184         {
00185                 if (pElement->Equivalent(m_ppElements[i]))
00186                         return m_ppElements[i];
00187         }
00188                 
00189         return 0;
00190 }
00191 
00192 int CDynamicArray::_FindFirstMatch(const CDynamicArrayElement *pElement)
00193 {
00194         for (int i = 0; i < m_nElements; i++)
00195         {
00196                 if (pElement->Equivalent(m_ppElements[i]))
00197                         return i;
00198         }
00199                 
00200         return -1;
00201 }
00202 
00203 CDynamicArrayElement* CDynamicArray::FindBestMatch(const CDynamicArrayElement *pElement, float &fResultError)
00204 {
00205         float best_error = 9999999;
00206         int best_i = -1;
00207         
00208         for (int i = 0; i < m_nElements; i++)
00209         {
00210                 const float error = pElement->Error(m_ppElements[i]);
00211                 
00212                 if (error < best_error)
00213                 {
00214                         best_error = error;
00215                         best_i = i;
00216                 }
00217         }
00218         
00219         if (best_i == -1)
00220                 return 0;
00221                 
00222         fResultError = best_error;
00223         
00224         return m_ppElements[best_i];
00225 }


asr_ivt
Author(s): Allgeyer Tobias, Hutmacher Robin, Kleinert Daniel, Meißner Pascal, Scholz Jonas, Stöckle Patrick
autogenerated on Thu Jun 6 2019 21:46:57