Open
Graph Drawing
Framework

 v.2012.05
 

NodeArray.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  
00043 #ifdef _MSC_VER
00044 #pragma once
00045 #endif
00046 
00047 #ifndef OGDF_NODE_ARRAY_H
00048 #define OGDF_NODE_ARRAY_H
00049 
00050 
00051 #include <ogdf/basic/Graph_d.h>
00052 
00053 
00054 namespace ogdf {
00055 
00056 
00058 
00062 class NodeArrayBase {
00067     ListIterator<NodeArrayBase*> m_it;
00068 
00069 public:
00070     const Graph *m_pGraph; 
00071 
00073     NodeArrayBase() : m_pGraph(0) { }
00075     NodeArrayBase(const Graph *pG) : m_pGraph(pG) {
00076         if(pG) m_it = pG->registerArray(this);
00077     }
00078 
00079     // destructor, unregisters the array
00080     virtual ~NodeArrayBase() {
00081         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00082     }
00083 
00084     // event interface used by Graph
00086     virtual void enlargeTable(int newTableSize) = 0;
00088     virtual void reinit(int initTableSize) = 0;
00090     virtual void disconnect() = 0;
00091 
00093     void reregister(const Graph *pG) {
00094         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00095         if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
00096     }
00097 }; // class NodeArrayBase
00098 
00099 
00101 
00105 template<class T> class NodeArray : private Array<T>, protected NodeArrayBase {
00106     T m_x; 
00107 
00108 public:
00110     NodeArray() : Array<T>(), NodeArrayBase() { }
00112     NodeArray(const Graph &G) : Array<T>(G.nodeArrayTableSize()), NodeArrayBase(&G) { }
00114 
00118     NodeArray(const Graph &G, const T &x) :
00119         Array<T>(0,G.nodeArrayTableSize()-1,x), NodeArrayBase(&G), m_x(x) { }
00121 
00124     NodeArray(const NodeArray<T> &A) : Array<T>(A), NodeArrayBase(A.m_pGraph), m_x(A.m_x) { }
00125 
00127     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00128     
00130     const Graph *graphOf() const {
00131         return m_pGraph;
00132     }
00133 
00135     const T &operator[](node v) const {
00136         OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
00137         return Array<T>::operator [](v->index());
00138     }
00139 
00141     T &operator[](node v) {
00142         OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
00143         return Array<T>::operator [](v->index());
00144     }
00145 
00147 
00151     const T &operator[](int index) const {
00152         return Array<T>::operator [](index);
00153     }
00154 
00156 
00160     T &operator[](int index) {
00161         return Array<T>::operator [](index);
00162     }
00163 
00165     NodeArray<T> &operator=(const NodeArray<T> &a) {
00166         Array<T>::operator =(a);
00167         m_x = a.m_x;
00168         reregister(a.m_pGraph);
00169         return *this;
00170     }
00171 
00173     void init() {
00174         Array<T>::init(); reregister(0);
00175     }
00176 
00178     void init(const Graph &G) {
00179         Array<T>::init(G.nodeArrayTableSize()); reregister(&G);
00180     }
00181 
00183 
00187     void init(const Graph &G, const T &x) {
00188         Array<T>::init(0,G.nodeArrayTableSize()-1, m_x = x); reregister(&G);
00189     }
00190 
00192     void fill(const T &x) {
00193         int high = m_pGraph->maxNodeIndex();
00194         if(high >= 0)
00195             Array<T>::fill(0,high,x);
00196     }
00197 
00198 private:
00199     virtual void enlargeTable(int newTableSize) {
00200         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00201     }
00202 
00203     virtual void reinit(int initTableSize) {
00204         Array<T>::init(0,initTableSize-1,m_x);
00205     }
00206 
00207     virtual void disconnect() {
00208         Array<T>::init();
00209         m_pGraph = 0;
00210     }
00211 
00212     OGDF_NEW_DELETE
00213 
00214 }; // class NodeArray<T>
00215 
00216 
00217 } // end namespace ogdf
00218 
00219 #include <ogdf/basic/Graph.h>
00220 
00221 #endif