Open
Graph Drawing
Framework

 v.2007.11
 

NodeArray.h

Go to the documentation of this file.
00001 /*
00002  * $Revision: 1.3 $
00003  * 
00004  * last checkin:
00005  *   $Author: gutwenger $ 
00006  *   $Date: 2007-11-08 16:10:37 +0100 (Do, 08 Nov 2007) $ 
00007  ***************************************************************/
00008  
00051 #ifdef _MSC_VER
00052 #pragma once
00053 #endif
00054 
00055 #ifndef OGDF_NODE_ARRAY_H
00056 #define OGDF_NODE_ARRAY_H
00057 
00058 
00059 #include <ogdf/basic/Graph_d.h>
00060 
00061 
00062 namespace ogdf {
00063 
00064 
00066 
00070 class NodeArrayBase {
00075     ListIterator<NodeArrayBase*> m_it;
00076 
00077 public:
00078     const Graph *m_pGraph; 
00079 
00081     NodeArrayBase() : m_pGraph(0) { }
00083     NodeArrayBase(const Graph *pG) : m_pGraph(pG) {
00084         m_it = pG->registerArray(this);
00085     }
00086 
00087     // destructor, unregisters the array
00088     virtual ~NodeArrayBase() {
00089         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00090     }
00091 
00092     // event interface used by Graph
00094     virtual void enlargeTable(int newTableSize) = 0;
00096     virtual void reinit(int initTableSize) = 0;
00098     virtual void disconnect() = 0;
00099 
00101     void reregister(const Graph *pG) {
00102         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00103         if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
00104     }
00105 }; // class NodeArrayBase
00106 
00107 
00109 
00113 template<class T> class NodeArray : private Array<T>, protected NodeArrayBase {
00114     T m_x; 
00115 
00116 public:
00118     NodeArray() : Array<T>(), NodeArrayBase() { }
00120     NodeArray(const Graph &G) : Array<T>(G.nodeArrayTableSize()), NodeArrayBase(&G) { }
00122 
00126     NodeArray(const Graph &G, const T &x) :
00127         Array<T>(0,G.nodeArrayTableSize()-1,x), NodeArrayBase(&G), m_x(x) { }
00129 
00132     NodeArray(const NodeArray<T> &A) : Array<T>(A), NodeArrayBase(A.m_pGraph), m_x(A.m_x) { }
00133 
00135     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00136     
00138     const Graph *graphOf() const {
00139         return m_pGraph;
00140     }
00141 
00143     const T &operator[](node v) const {
00144         OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
00145         return Array<T>::operator [](v->index());
00146     }
00147 
00149     T &operator[](node v) {
00150         OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
00151         return Array<T>::operator [](v->index());
00152     }
00153 
00155 
00159     const T &operator[](int index) const {
00160         return Array<T>::operator [](index);
00161     }
00162 
00164 
00168     T &operator[](int index) {
00169         return Array<T>::operator [](index);
00170     }
00171 
00173     NodeArray<T> &operator=(const NodeArray<T> &a) {
00174         Array<T>::operator =(a);
00175         m_x = a.m_x;
00176         reregister(a.m_pGraph);
00177         return *this;
00178     }
00179 
00181     void init() {
00182         Array<T>::init(); reregister(0);
00183     }
00184 
00186     void init(const Graph &G) {
00187         Array<T>::init(G.nodeArrayTableSize()); reregister(&G);
00188     }
00189 
00191 
00195     void init(const Graph &G, const T &x) {
00196         Array<T>::init(0,G.nodeArrayTableSize()-1, m_x = x); reregister(&G);
00197     }
00198 
00200     void fill(const T &x) {
00201         int high = m_pGraph->maxNodeIndex();
00202         if(high >= 0)
00203             Array<T>::fill(0,high,x);
00204     }
00205 
00206 private:
00207     virtual void enlargeTable(int newTableSize) {
00208         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00209     }
00210 
00211     virtual void reinit(int initTableSize) {
00212         Array<T>::init(0,initTableSize-1,m_x);
00213     }
00214 
00215     virtual void disconnect() {
00216         Array<T>::init();
00217         m_pGraph = 0;
00218     }
00219 
00220     OGDF_NEW_DELETE
00221 
00222 }; // class NodeArray<T>
00223 
00224 
00225 } // end namespace ogdf
00226 
00227 #include <ogdf/basic/Graph.h>
00228 
00229 #endif


© 1999-2007 by oreas GmbH, © 2005-2007 by University Dortmund and University Cologne.

Generated on Thu Nov 22 19:40:01 2007 by doxygen 1.5.4.