Open
Graph Drawing
Framework

 v.2010.10
 

NodeArray.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_NODE_ARRAY_H
00058 #define OGDF_NODE_ARRAY_H
00059 
00060 
00061 #include <ogdf/basic/Graph_d.h>
00062 
00063 
00064 namespace ogdf {
00065 
00066 
00068 
00072 class NodeArrayBase {
00077     ListIterator<NodeArrayBase*> m_it;
00078 
00079 public:
00080     const Graph *m_pGraph; 
00081 
00083     NodeArrayBase() : m_pGraph(0) { }
00085     NodeArrayBase(const Graph *pG) : m_pGraph(pG) {
00086         if(pG) m_it = pG->registerArray(this);
00087     }
00088 
00089     // destructor, unregisters the array
00090     virtual ~NodeArrayBase() {
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;
00101 
00103     void reregister(const Graph *pG) {
00104         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00105         if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
00106     }
00107 }; // class NodeArrayBase
00108 
00109 
00111 
00115 template<class T> class NodeArray : private Array<T>, protected NodeArrayBase {
00116     T m_x; 
00117 
00118 public:
00120     NodeArray() : Array<T>(), NodeArrayBase() { }
00122     NodeArray(const Graph &G) : Array<T>(G.nodeArrayTableSize()), NodeArrayBase(&G) { }
00124 
00128     NodeArray(const Graph &G, const T &x) :
00129         Array<T>(0,G.nodeArrayTableSize()-1,x), NodeArrayBase(&G), m_x(x) { }
00131 
00134     NodeArray(const NodeArray<T> &A) : Array<T>(A), NodeArrayBase(A.m_pGraph), m_x(A.m_x) { }
00135 
00137     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00138     
00140     const Graph *graphOf() const {
00141         return m_pGraph;
00142     }
00143 
00145     const T &operator[](node v) const {
00146         OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
00147         return Array<T>::operator [](v->index());
00148     }
00149 
00151     T &operator[](node v) {
00152         OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
00153         return Array<T>::operator [](v->index());
00154     }
00155 
00157 
00161     const T &operator[](int index) const {
00162         return Array<T>::operator [](index);
00163     }
00164 
00166 
00170     T &operator[](int index) {
00171         return Array<T>::operator [](index);
00172     }
00173 
00175     NodeArray<T> &operator=(const NodeArray<T> &a) {
00176         Array<T>::operator =(a);
00177         m_x = a.m_x;
00178         reregister(a.m_pGraph);
00179         return *this;
00180     }
00181 
00183     void init() {
00184         Array<T>::init(); reregister(0);
00185     }
00186 
00188     void init(const Graph &G) {
00189         Array<T>::init(G.nodeArrayTableSize()); reregister(&G);
00190     }
00191 
00193 
00197     void init(const Graph &G, const T &x) {
00198         Array<T>::init(0,G.nodeArrayTableSize()-1, m_x = x); reregister(&G);
00199     }
00200 
00202     void fill(const T &x) {
00203         int high = m_pGraph->maxNodeIndex();
00204         if(high >= 0)
00205             Array<T>::fill(0,high,x);
00206     }
00207 
00208 private:
00209     virtual void enlargeTable(int newTableSize) {
00210         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00211     }
00212 
00213     virtual void reinit(int initTableSize) {
00214         Array<T>::init(0,initTableSize-1,m_x);
00215     }
00216 
00217     virtual void disconnect() {
00218         Array<T>::init();
00219         m_pGraph = 0;
00220     }
00221 
00222     OGDF_NEW_DELETE
00223 
00224 }; // class NodeArray<T>
00225 
00226 
00227 } // end namespace ogdf
00228 
00229 #include <ogdf/basic/Graph.h>
00230 
00231 #endif