Open
Graph Drawing
Framework

 v.2010.10
 

EdgeArray.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  
00052 #ifdef _MSC_VER
00053 #pragma once
00054 #endif
00055 
00056 #ifndef OGDF_EDGE_ARRAY_H
00057 #define OGDF_EDGE_ARRAY_H
00058 
00059 
00060 #include <ogdf/basic/Graph_d.h>
00061 
00062 
00063 namespace ogdf {
00064 
00065 
00067 
00071 class EdgeArrayBase {
00076     ListIterator<EdgeArrayBase*> m_it;
00077 
00078 public:
00079     const Graph *m_pGraph; 
00080 
00082     EdgeArrayBase() : m_pGraph(0) { }
00084     EdgeArrayBase(const Graph *pG) : m_pGraph(pG) {
00085         if(pG) m_it = pG->registerArray(this);
00086     }
00087 
00088     // destructor, unregisters the array
00089     virtual ~EdgeArrayBase() {
00090         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00091     }
00092 
00093     // event interface used by Graph
00095     virtual void enlargeTable(int newTableSize) = 0;
00097     virtual void reinit(int initTableSize) = 0;
00099     virtual void disconnect() = 0;
00100 
00102     void reregister(const Graph *pG) {
00103         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00104         if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
00105     }
00106 }; // class EdgeArrayBase
00107 
00108 
00110 
00114 template<class T> class EdgeArray : private Array<T>, protected EdgeArrayBase {
00115     T m_x; 
00116 
00117 public:
00119     EdgeArray() : Array<T>(), EdgeArrayBase() { }
00121     EdgeArray(const Graph &G) : Array<T>(G.edgeArrayTableSize()), EdgeArrayBase(&G) { }
00123 
00127     EdgeArray(const Graph &G, const T &x) :
00128         Array<T>(0,G.edgeArrayTableSize()-1,x), EdgeArrayBase(&G), m_x(x) { }
00130 
00133     EdgeArray(const EdgeArray<T> &A) : Array<T>(A), EdgeArrayBase(A.m_pGraph), m_x(A.m_x) { }
00134 
00136     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00137 
00139     const Graph *graphOf() const {
00140         return m_pGraph;
00141     }
00142 
00144     const T &operator[](edge e) const {
00145         OGDF_ASSERT(e != 0 && e->graphOf() == m_pGraph)
00146         return Array<T>::operator [](e->index());
00147     }
00148 
00150     T &operator[](edge e) {
00151         OGDF_ASSERT(e != 0 && e->graphOf() == m_pGraph)
00152         return Array<T>::operator [](e->index());
00153     }
00154 
00156     const T &operator[](adjEntry adj) const {
00157         OGDF_ASSERT(adj != 0)
00158         return Array<T>::operator [](adj->index() >> 1);
00159     }
00160 
00162     T &operator[](adjEntry adj) {
00163         OGDF_ASSERT(adj != 0)
00164         return Array<T>::operator [](adj->index() >> 1);
00165     }
00166 
00168 
00172     const T &operator[](int index) const {
00173         return Array<T>::operator [](index);
00174     }
00175 
00177 
00181     T &operator[](int index) {
00182         return Array<T>::operator [](index);
00183     }
00184 
00186     EdgeArray<T> &operator=(const EdgeArray<T> &a) {
00187         Array<T>::operator =(a);
00188         m_x = a.m_x;
00189         reregister(a.m_pGraph);
00190         return *this;
00191     }
00192 
00194     void init() {
00195         Array<T>::init(); reregister(0);
00196     }
00197 
00199     void init(const Graph &G) {
00200         Array<T>::init(G.edgeArrayTableSize()); reregister(&G);
00201     }
00202 
00204 
00208     void init(const Graph &G, const T &x) {
00209         Array<T>::init(0,G.edgeArrayTableSize()-1, m_x = x); reregister(&G);
00210     }
00211 
00213     void fill(const T &x) {
00214         int high = m_pGraph->maxEdgeIndex();
00215         if(high >= 0)
00216             Array<T>::fill(0,high,x);
00217     }
00218 
00219 private:
00220     virtual void enlargeTable(int newTableSize) {
00221         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00222     }
00223 
00224     virtual void reinit(int initTableSize) {
00225         Array<T>::init(0,initTableSize-1,m_x);
00226     }
00227 
00228     virtual void disconnect() {
00229         Array<T>::init();
00230         m_pGraph = 0;
00231     }
00232 
00233     OGDF_NEW_DELETE
00234 
00235 }; // class EdgeArray<T>
00236 
00237 
00239 
00243 class OGDF_EXPORT BucketEdgeArray : public BucketFunc<edge>
00244 {
00245     const EdgeArray<int> *m_pEdgeArray; 
00246 
00247 public:
00249 
00253     BucketEdgeArray(const EdgeArray<int> &edgeArray) :
00254       m_pEdgeArray(&edgeArray) { }
00255 
00257     int getBucket(const edge &e) { return (*m_pEdgeArray)[e]; }
00258 };
00259 
00260 
00261 } // end namespace ogdf
00262 #include <ogdf/basic/Graph.h>
00263 
00264 #endif