Index   Main   Namespaces   Classes   Hierarchy   Annotated   Files   Compound   Global   Pages  

SgSortedArray.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00002 /** @file SgSortedArray.h
00003     Sorted array.
00004 */
00005 //----------------------------------------------------------------------------
00006 
00007 #ifndef SG_SORTEDARRAY_H
00008 #define SG_SORTEDARRAY_H
00009 
00010 #include "SgVector.h"
00011 
00012 //----------------------------------------------------------------------------
00013 
00014 /** Sorted array.
00015     Implements an array of type <code>T</code> elements that can be sorted by
00016     keys of type <code>K</code>.
00017     Class <code>K</code> needs to support comparison.
00018     Class <code>T</code> currently needs to be a simple type.
00019 */
00020 template <class T, class K, int size>
00021 class SgSortedArray
00022 {
00023 public: 
00024     SgSortedArray()
00025     {
00026         m_numElt = 0;
00027     }
00028 
00029     void AddItem(T elt, K key)
00030     {
00031         SG_ASSERT(m_numElt < size);
00032         m_elt[m_numElt] = elt;
00033         m_key[m_numElt] = key;
00034         ++m_numElt;
00035     }
00036 
00037     void SetToMin(T elt, K key)
00038     {
00039         for (int i = 0; i < m_numElt; ++i)
00040         {
00041             if (m_elt[i] == elt)
00042             {
00043                 if (m_key[i] > key) m_key[i] = key;
00044                 return;
00045             }
00046         }
00047         AddItem(elt, key);
00048     }
00049 
00050     void SetToMax(T elt, K key)
00051     {
00052         for (int i = 0; i < m_numElt; ++i)
00053         {
00054             if (m_elt[i] == elt)
00055             {
00056                 if (m_key[i] < key)
00057                     m_key[i] = key;
00058                 return;
00059             }
00060         }
00061         AddItem(elt, key);
00062     }
00063 
00064     void SetTo(T elt, K key)
00065     {
00066         for (int i = 0; i < m_numElt; ++i)
00067         {
00068             if (m_elt[i] == elt)
00069             {
00070                 m_key[i] = key;
00071                 return;
00072             }
00073         }
00074         AddItem(elt, key);
00075     }
00076 
00077     void ReduceSizeTo(int newSize)
00078     {
00079         SG_ASSERT(0 <= newSize && newSize <= m_numElt);
00080         m_numElt = newSize;
00081     }
00082 
00083     void SortMaximize()
00084     {
00085         for (int i = 0; i < m_numElt-1; ++i)
00086         {
00087             int maxIndex = i;
00088             K maxKey = m_key[maxIndex];
00089             for (int j = i+1; j <= m_numElt-1; ++j)
00090             {
00091                 if (maxKey < m_key[j])
00092                 {
00093                     maxIndex = j;
00094                     maxKey = m_key[maxIndex];
00095                 }
00096             }
00097             std::swap(m_key[i], m_key[maxIndex]);
00098             std::swap(m_elt[i], m_elt[maxIndex]);
00099         }
00100     }
00101 
00102     void SortMinimize()
00103     {
00104         for (int i = 0; i < m_numElt-1; ++i)
00105         {
00106             int minIndex = i;
00107             K minKey = m_key[minIndex];
00108             for (int j = i+1; j <= m_numElt-1; ++j)
00109             {
00110                 if (m_key[j] < minKey)
00111                 {
00112                     minIndex = j;
00113                     minKey = m_key[minIndex];
00114                 }
00115             }
00116             std::swap(m_key[i], m_key[minIndex]);
00117             std::swap(m_elt[i], m_elt[minIndex]);
00118         }
00119     }
00120 
00121     void GetElements(SgVector<T>* listOfElts) const
00122     {
00123         listOfElts->SetTo(m_elt, m_numElt);
00124     }
00125 
00126     void GetKeys(SgVector<K>* listOfKeys) const
00127     {
00128         listOfKeys->SetTo(m_key, m_numElt);
00129     }
00130 
00131     T operator[](int index) const
00132     {
00133         return m_elt[index];
00134     }
00135 
00136     K GetKey(int index) const
00137     {
00138         return m_key[index];
00139     }
00140 
00141     int Size() const
00142     {
00143         return m_numElt;
00144     }
00145 
00146     bool IsEmpty() const
00147     {
00148         return m_numElt == 0;
00149     }
00150 
00151     bool NonEmpty() const
00152     {
00153         return m_numElt != 0;
00154     }
00155 
00156     bool IsFull() const
00157     {
00158         return size <= m_numElt;
00159     }
00160 
00161 private:
00162     int m_numElt;
00163 
00164     T m_elt[size];
00165 
00166     K m_key[size];
00167 
00168     /** not implemented */
00169     SgSortedArray(const SgSortedArray&);
00170 
00171     /** not implemented */
00172     SgSortedArray& operator=(const SgSortedArray&);
00173 };
00174 
00175 //----------------------------------------------------------------------------
00176 
00177 #endif // SG_SORTEDARRAY_H


17 Jun 2010 Doxygen 1.4.7