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


Sun Mar 13 2011 Doxygen 1.7.1