Wm4TSmallUnorderedSet.inl

Go to the documentation of this file.
00001 // Wild Magic Source Code
00002 // David Eberly
00003 // http://www.geometrictools.com
00004 // Copyright (c) 1998-2007
00005 //
00006 // This library is free software; you can redistribute it and/or modify it
00007 // under the terms of the GNU Lesser General Public License as published by
00008 // the Free Software Foundation; either version 2.1 of the License, or (at
00009 // your option) any later version.  The license is available for reading at
00010 // either of the locations:
00011 //     http://www.gnu.org/copyleft/lgpl.html
00012 //     http://www.geometrictools.com/License/WildMagicLicense.pdf
00013 // The license applies to versions 0 through 4 of Wild Magic.
00014 //
00015 // Version: 4.0.0 (2006/06/28)
00016 
00017 namespace Wm4
00018 {
00019 //----------------------------------------------------------------------------
00020 template <class T>
00021 TSmallUnorderedSet<T>::TSmallUnorderedSet ()
00022 {
00023     m_iMaxQuantity = 1;
00024     m_iGrowBy = 1;
00025     m_iQuantity = 0;
00026     m_atElement = WM4_NEW T[1];
00027 }
00028 //----------------------------------------------------------------------------
00029 template <class T>
00030 TSmallUnorderedSet<T>::TSmallUnorderedSet (int iMaxQuantity, int iGrowBy)
00031 {
00032     assert(iMaxQuantity > 0 && iGrowBy > 0);
00033     if (iMaxQuantity <= 0)
00034     {
00035         iMaxQuantity = 1;
00036     }
00037     if (iGrowBy <= 0)
00038     {
00039         iGrowBy = 1;
00040     }
00041 
00042     m_iMaxQuantity = iMaxQuantity;
00043     m_iGrowBy = iGrowBy;
00044     m_iQuantity = 0;
00045     m_atElement = WM4_NEW T[iMaxQuantity];
00046 }
00047 //----------------------------------------------------------------------------
00048 template <class T>
00049 TSmallUnorderedSet<T>::TSmallUnorderedSet (const TSmallUnorderedSet& rkSet)
00050 {
00051     m_iMaxQuantity = rkSet.m_iMaxQuantity;
00052     m_iGrowBy = rkSet.m_iGrowBy;
00053     m_iQuantity = rkSet.m_iQuantity;
00054     m_atElement = WM4_NEW T[m_iMaxQuantity];
00055     for (int i = 0; i < m_iMaxQuantity; i++)
00056     {
00057         m_atElement[i] = rkSet.m_atElement[i];
00058     }
00059 }
00060 //----------------------------------------------------------------------------
00061 template <class T>
00062 TSmallUnorderedSet<T>::~TSmallUnorderedSet ()
00063 {
00064     WM4_DELETE[] m_atElement;
00065 }
00066 //----------------------------------------------------------------------------
00067 template <class T>
00068 TSmallUnorderedSet<T>& TSmallUnorderedSet<T>::operator= (
00069     const TSmallUnorderedSet& rkSet)
00070 {
00071     WM4_DELETE[] m_atElement;
00072     m_iMaxQuantity = rkSet.m_iMaxQuantity;
00073     m_iGrowBy = rkSet.m_iGrowBy;
00074     m_iQuantity = rkSet.m_iQuantity;
00075     m_atElement = WM4_NEW T[m_iMaxQuantity];
00076     for (int i = 0; i < m_iMaxQuantity; i++)
00077     {
00078         m_atElement[i] = rkSet.m_atElement[i];
00079     }
00080 
00081     return *this;
00082 }
00083 //----------------------------------------------------------------------------
00084 template <class T>
00085 int TSmallUnorderedSet<T>::GetMaxQuantity () const
00086 {
00087     return m_iMaxQuantity;
00088 }
00089 //----------------------------------------------------------------------------
00090 template <class T>
00091 int TSmallUnorderedSet<T>::GetGrowBy () const
00092 {
00093     return m_iGrowBy;
00094 }
00095 //----------------------------------------------------------------------------
00096 template <class T>
00097 int TSmallUnorderedSet<T>::GetQuantity () const
00098 {
00099     return m_iQuantity;
00100 }
00101 //----------------------------------------------------------------------------
00102 template <class T>
00103 T* TSmallUnorderedSet<T>::GetElements ()
00104 {
00105     return m_atElement;
00106 }
00107 //----------------------------------------------------------------------------
00108 template <class T>
00109 const T* TSmallUnorderedSet<T>::GetElements () const
00110 {
00111     return m_atElement;
00112 }
00113 //----------------------------------------------------------------------------
00114 template <class T>
00115 T& TSmallUnorderedSet<T>::operator[] (int i)
00116 {
00117     assert(0 <= i && i < m_iQuantity);
00118     if (i < 0)
00119     {
00120         i = 0;
00121     }
00122     else if (i >= m_iQuantity)
00123     {
00124         i = m_iQuantity-1;
00125     }
00126 
00127     return m_atElement[i];
00128 }
00129 //----------------------------------------------------------------------------
00130 template <class T>
00131 const T& TSmallUnorderedSet<T>::operator[] (int i) const
00132 {
00133     assert(0 <= i && i < m_iQuantity);
00134     if (i < 0)
00135     {
00136         i = 0;
00137     }
00138     else if (i >= m_iQuantity)
00139     {
00140         i = m_iQuantity-1;
00141     }
00142 
00143     return m_atElement[i];
00144 }
00145 //----------------------------------------------------------------------------
00146 template <class T>
00147 bool TSmallUnorderedSet<T>::Insert (const T& rkElement)
00148 {
00149     for (int i = 0; i < m_iQuantity; i++)
00150     {
00151         if (rkElement == m_atElement[i])
00152         {
00153             return false;
00154         }
00155     }
00156 
00157     if (m_iQuantity == m_iMaxQuantity)
00158     {
00159         // array is full, resize it
00160         int iNewMaxQuantity = m_iMaxQuantity + m_iGrowBy;
00161         T* atNewElement = WM4_NEW T[iNewMaxQuantity];
00162         for (int i = 0; i < m_iMaxQuantity; i++)
00163         {
00164             atNewElement[i] = m_atElement[i];
00165         }
00166 
00167         WM4_DELETE[] m_atElement;
00168         m_atElement = atNewElement;
00169         m_iMaxQuantity = iNewMaxQuantity;
00170     }
00171 
00172     m_atElement[m_iQuantity++] = rkElement;
00173     return true;
00174 }
00175 //----------------------------------------------------------------------------
00176 template <class T>
00177 void TSmallUnorderedSet<T>::InsertNoCheck (const T& rkElement)
00178 {
00179     if (m_iQuantity == m_iMaxQuantity)
00180     {
00181         // array is full, resize it
00182         int iNewMaxQuantity = m_iMaxQuantity + m_iGrowBy;
00183         T* atNewElement = WM4_NEW T[iNewMaxQuantity];
00184         for (int i = 0; i < m_iMaxQuantity; i++)
00185         {
00186             atNewElement[i] = m_atElement[i];
00187         }
00188 
00189         WM4_DELETE[] m_atElement;
00190         m_atElement = atNewElement;
00191         m_iMaxQuantity = iNewMaxQuantity;
00192     }
00193 
00194     m_atElement[m_iQuantity++] = rkElement;
00195 }
00196 //----------------------------------------------------------------------------
00197 template <class T>
00198 bool TSmallUnorderedSet<T>::Remove (const T& rkElement)
00199 {
00200     for (int i = 0; i < m_iQuantity; i++)
00201     {
00202         if (rkElement == m_atElement[i])
00203         {
00204             // element exists, shift array to fill in empty slot
00205             for (int j = i+1; j < m_iQuantity; j++, i++)
00206             {
00207                 m_atElement[i] = m_atElement[j];
00208             }
00209 
00210             m_atElement[m_iQuantity-1] = T();
00211             m_iQuantity--;
00212             return true;
00213         }
00214     }
00215 
00216     return false;
00217 }
00218 //----------------------------------------------------------------------------
00219 template <class T>
00220 bool TSmallUnorderedSet<T>::Exists (const T& rkElement)
00221 {
00222     for (int i = 0; i < m_iQuantity; i++)
00223     {
00224         if (rkElement == m_atElement[i])
00225         {
00226             return true;
00227         }
00228     }
00229 
00230     return false;
00231 }
00232 //----------------------------------------------------------------------------
00233 template <class T>
00234 void TSmallUnorderedSet<T>::Clear ()
00235 {
00236     for (int i = 0; i < m_iQuantity; i++)
00237     {
00238         m_atElement[i] = T();
00239     }
00240 
00241     m_iQuantity = 0;
00242 }
00243 //----------------------------------------------------------------------------
00244 template <class T>
00245 void TSmallUnorderedSet<T>::Clear (int iMaxQuantity, int iGrowBy)
00246 {
00247     assert(iMaxQuantity > 0 && iGrowBy > 0);
00248     if (iMaxQuantity <= 0)
00249     {
00250         iMaxQuantity = 1;
00251     }
00252     if (iGrowBy <= 0)
00253     {
00254         iGrowBy = 1;
00255     }
00256 
00257     WM4_DELETE[] m_atElement;
00258     m_iMaxQuantity = iMaxQuantity;
00259     m_iGrowBy = iGrowBy;
00260     m_iQuantity = 0;
00261     m_atElement = WM4_NEW T[iMaxQuantity];
00262 }
00263 //----------------------------------------------------------------------------
00264 } //namespace Wm4

Generated on Wed Nov 23 19:01:10 2011 for FreeCAD by  doxygen 1.6.1