Open
Graph Drawing
Framework

 v.2010.10
 

AdjEntryArray.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 2027 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2010-09-01 11:55:17 +0200 (Wed, 01 Sep 2010) $ 
00007  ***************************************************************/
00008  
00053 #ifdef _MSC_VER
00054 #pragma once
00055 #endif
00056 
00057 #ifndef OGDF_ADJ_ENTRY_ARRAY_H
00058 #define OGDF_ADJ_ENTRY_ARRAY_H
00059 
00060 
00061 #include <ogdf/basic/Graph.h>
00062 
00063 
00064 namespace ogdf {
00065 
00066 
00068 
00072 class AdjEntryArrayBase {
00077     ListIterator<AdjEntryArrayBase*> m_it;
00078 
00079 public:
00080     const Graph *m_pGraph; 
00081 
00083     AdjEntryArrayBase() : m_pGraph(0) { }
00085     AdjEntryArrayBase(const Graph *pG) : m_pGraph(pG) {
00086         if(pG) m_it = pG->registerArray(this);
00087     }
00088 
00089     // destructor, unregisters the array
00090     virtual ~AdjEntryArrayBase() {
00091         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00092     }
00093 
00094     // event interface used by Graph
00096     virtual void enlargeTable(int newTableSize) = 0;
00098     virtual void reinit(int initTableSize) = 0;
00100     virtual void disconnect() = 0;
00102     virtual void resetIndex(int newIndex, int oldIndex) = 0;
00103 
00105     void reregister(const Graph *pG) {
00106         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00107         if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
00108     }
00109 }; // class AdjEntryArrayBase
00110 
00111 
00113 
00117 template<class T> class AdjEntryArray : private Array<T>, protected AdjEntryArrayBase {
00118     T m_x; 
00119 
00120 public:
00122     AdjEntryArray() : Array<T>(), AdjEntryArrayBase() { }
00124     AdjEntryArray(const Graph &G) : Array<T>(G.adjEntryArrayTableSize()), AdjEntryArrayBase(&G) { }
00126 
00130     AdjEntryArray(const Graph &G, const T &x) :
00131         Array<T>(0,G.adjEntryArrayTableSize()-1,x), AdjEntryArrayBase(&G), m_x(x) { }
00133 
00136     AdjEntryArray(const AdjEntryArray<T> &A) : Array<T>(A), AdjEntryArrayBase(A.m_pGraph), m_x(A.m_x) { }
00137 
00139     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00140 
00142     const T &operator[](adjEntry adj) const {
00143         OGDF_ASSERT(adj != 0 && adj->graphOf() == m_pGraph)
00144         return Array<T>::operator [](adj->index());
00145     }
00146 
00148     T &operator[](adjEntry adj) {
00149         OGDF_ASSERT(adj != 0 && adj->graphOf() == m_pGraph)
00150         return Array<T>::operator [](adj->index());
00151     }
00152 
00154 
00158     const T &operator[](int index) const {
00159         return Array<T>::operator [](index);
00160     }
00161 
00163 
00167     T &operator[](int index) {
00168         return Array<T>::operator [](index);
00169     }
00170 
00172     AdjEntryArray<T> &operator=(const AdjEntryArray<T> &A) {
00173         Array<T>::operator =(A);
00174         m_x = A.m_x;
00175         reregister(A.m_pGraph);
00176         return *this;
00177     }
00178 
00180     void init() {
00181         Array<T>::init(); reregister(0);
00182     }
00183 
00185     void init(const Graph &G) {
00186         Array<T>::init(G.adjEntryArrayTableSize()); reregister(&G);
00187     }
00188 
00190 
00194     void init(const Graph &G, const T &x) {
00195         Array<T>::init(0,G.adjEntryArrayTableSize()-1, m_x = x); reregister(&G);
00196     }
00197 
00199     void fill(const T &x) {
00200         int high = m_pGraph->maxAdjEntryIndex();
00201         if(high >= 0)
00202             Array<T>::fill(0,high,x);
00203     }
00204 
00205 private:
00206     virtual void enlargeTable(int newTableSize) {
00207         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00208     }
00209 
00210     virtual void reinit(int initTableSize) {
00211         Array<T>::init(0,initTableSize-1,m_x);
00212     }
00213 
00214     virtual void resetIndex(int newIndex, int oldIndex) {
00215         Array<T>::operator [](newIndex) = Array<T>::operator [](oldIndex);
00216     }
00217 
00218     virtual void disconnect() {
00219         Array<T>::init();
00220         m_pGraph = 0;
00221     }
00222 
00223     OGDF_NEW_DELETE
00224 
00225 }; // class AdjEntryArray<T>
00226 
00227 
00228 } // end namespace ogdf
00229 
00230 
00231 #endif