Open
Graph Drawing
Framework

 v.2012.05
 

ClusterArray.h
Go to the documentation of this file.
00001 /*
00002  * $Revision: 2299 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2012-05-07 15:57:08 +0200 (Mon, 07 May 2012) $ 
00007  ***************************************************************/
00008  
00042 #ifdef _MSC_VER
00043 #pragma once
00044 #endif
00045 
00046 #ifndef OGDF_CLUSTER_ARRAY_H
00047 #define OGDF_CLUSTER_ARRAY_H
00048 
00049 
00050 #include <ogdf/basic/Array.h>
00051 #include <ogdf/cluster/ClusterGraph.h>
00052 
00053 
00054 namespace ogdf {
00055 
00056 
00057 //---------------------------------------------------------
00058 // ClusterArrayBase
00059 // base class for ClusterArray<T>, defines interface for event handling
00060 // used by Graph
00061 //---------------------------------------------------------
00063 
00067 class ClusterArrayBase {
00072     ListIterator<ClusterArrayBase*> m_it;
00073 
00074 public:
00075     const ClusterGraph *m_pClusterGraph; 
00076 
00078     ClusterArrayBase() : m_pClusterGraph(0) { }
00080     ClusterArrayBase(const ClusterGraph *pC) : m_pClusterGraph(pC) {
00081         if(pC) m_it = pC->registerArray(this);
00082     }
00083 
00084     // destructor, unregisters the array
00085     virtual ~ClusterArrayBase() {
00086         if (m_pClusterGraph) m_pClusterGraph->unregisterArray(m_it);
00087     }
00088 
00089     // event interface used by Graph
00091     virtual void enlargeTable(int newTableSize) = 0;
00093     virtual void reinit(int initTableSize) = 0;
00095     virtual void disconnect() = 0;
00096 
00098     void reregister(const ClusterGraph *pC) {
00099         if (m_pClusterGraph) m_pClusterGraph->unregisterArray(m_it);
00100         if ((m_pClusterGraph = pC) != 0) m_it = pC->registerArray(this);
00101     }
00102 }; // class ClusterArrayBase
00103 
00104 
00106 
00110 template<class T> class ClusterArray : private Array<T>, protected ClusterArrayBase {
00111     T m_x; 
00112 
00113 public:
00115     ClusterArray() : Array<T>(), ClusterArrayBase() { }
00117     ClusterArray(const ClusterGraph &C) : 
00118         Array<T>(C.clusterArrayTableSize()), 
00119         ClusterArrayBase(&C) { }
00121 
00125     ClusterArray(const ClusterGraph &C, const T &x) :
00126         Array<T>(0,C.clusterArrayTableSize()-1,x), 
00127         ClusterArrayBase(&C), m_x(x) { }
00130 
00134     ClusterArray(const ClusterGraph &C, const T &x, int size) :
00135         Array<T>(0,size-1,x),
00136         ClusterArrayBase(&C), m_x(x) { }
00137 
00139 
00142     ClusterArray(const ClusterArray<T> &A) : 
00143         Array<T>(A), 
00144         ClusterArrayBase(A.m_pClusterGraph), m_x(A.m_x) { }
00145 
00147     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00148     
00150     const ClusterGraph *graphOf() const {
00151         return m_pClusterGraph;
00152     }
00153 
00155     const T &operator[](cluster c) const {
00156         OGDF_ASSERT(c != 0 && c->graphOf() == m_pClusterGraph)
00157         return Array<T>::operator [](c->index());
00158     }
00159 
00161     T &operator[](cluster c) {
00162         OGDF_ASSERT(c != 0 && c->graphOf() == m_pClusterGraph)
00163         return Array<T>::operator [](c->index());
00164     }
00165 
00167 
00171     const T &operator[](int index) const {
00172         return Array<T>::operator [](index);
00173     }
00174 
00176 
00180     T &operator[](int index) {
00181         return Array<T>::operator [](index);
00182     }
00183 
00185     ClusterArray<T> &operator=(const ClusterArray<T> &a) {
00186         Array<T>::operator =(a);
00187         m_x = a.m_x;
00188         reregister(a.m_pClusterGraph);
00189         return *this;
00190     }
00191 
00193     void init() {
00194         Array<T>::init(); reregister(0);
00195     }
00196 
00198     void init(const ClusterGraph &C) {
00199         Array<T>::init( C.clusterArrayTableSize() ); reregister(&C);
00200     }
00201 
00203 
00207     void init(const ClusterGraph &C, const T &x) {
00208         Array<T>::init(0,C.clusterArrayTableSize()-1, m_x = x); reregister(&C);
00209     }
00210 
00212     void fill(const T &x) {
00213         int high = m_pClusterGraph->maxClusterIndex();
00214         if(high >= 0)
00215             Array<T>::fill(0,high,x);
00216     }
00217 
00218 private:
00219     virtual void enlargeTable(int newTableSize) {
00220         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00221     }
00222 
00223     virtual void reinit(int initTableSize) {
00224         Array<T>::init(0,initTableSize-1,m_x);
00225     }
00226 
00227     virtual void disconnect() {
00228         Array<T>::init();
00229         m_pClusterGraph = 0;
00230     }
00231 
00232     OGDF_NEW_DELETE
00233 
00234 }; // class ClusterArray<T>
00235 
00236 
00237 } // end namespace ogdf
00238 
00239 
00240 #endif