Open
Graph Drawing
Framework

 v.2012.05
 

EdgeArray.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_EDGE_ARRAY_H
00047 #define OGDF_EDGE_ARRAY_H
00048 
00049 
00050 #include <ogdf/basic/Graph_d.h>
00051 
00052 
00053 namespace ogdf {
00054 
00055 
00057 
00061 class EdgeArrayBase {
00066     ListIterator<EdgeArrayBase*> m_it;
00067 
00068 public:
00069     const Graph *m_pGraph; 
00070 
00072     EdgeArrayBase() : m_pGraph(0) { }
00074     EdgeArrayBase(const Graph *pG) : m_pGraph(pG) {
00075         if(pG) m_it = pG->registerArray(this);
00076     }
00077 
00078     // destructor, unregisters the array
00079     virtual ~EdgeArrayBase() {
00080         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00081     }
00082 
00083     // event interface used by Graph
00085     virtual void enlargeTable(int newTableSize) = 0;
00087     virtual void reinit(int initTableSize) = 0;
00089     virtual void disconnect() = 0;
00090 
00092     void reregister(const Graph *pG) {
00093         if (m_pGraph) m_pGraph->unregisterArray(m_it);
00094         if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
00095     }
00096 }; // class EdgeArrayBase
00097 
00098 
00100 
00104 template<class T> class EdgeArray : private Array<T>, protected EdgeArrayBase {
00105     T m_x; 
00106 
00107 public:
00109     EdgeArray() : Array<T>(), EdgeArrayBase() { }
00111     EdgeArray(const Graph &G) : Array<T>(G.edgeArrayTableSize()), EdgeArrayBase(&G) { }
00113 
00117     EdgeArray(const Graph &G, const T &x) :
00118         Array<T>(0,G.edgeArrayTableSize()-1,x), EdgeArrayBase(&G), m_x(x) { }
00120 
00123     EdgeArray(const EdgeArray<T> &A) : Array<T>(A), EdgeArrayBase(A.m_pGraph), m_x(A.m_x) { }
00124 
00126     bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
00127 
00129     const Graph *graphOf() const {
00130         return m_pGraph;
00131     }
00132 
00134     const T &operator[](edge e) const {
00135         OGDF_ASSERT(e != 0 && e->graphOf() == m_pGraph)
00136         return Array<T>::operator [](e->index());
00137     }
00138 
00140     T &operator[](edge e) {
00141         OGDF_ASSERT(e != 0 && e->graphOf() == m_pGraph)
00142         return Array<T>::operator [](e->index());
00143     }
00144 
00146     const T &operator[](adjEntry adj) const {
00147         OGDF_ASSERT(adj != 0)
00148         return Array<T>::operator [](adj->index() >> 1);
00149     }
00150 
00152     T &operator[](adjEntry adj) {
00153         OGDF_ASSERT(adj != 0)
00154         return Array<T>::operator [](adj->index() >> 1);
00155     }
00156 
00158 
00162     const T &operator[](int index) const {
00163         return Array<T>::operator [](index);
00164     }
00165 
00167 
00171     T &operator[](int index) {
00172         return Array<T>::operator [](index);
00173     }
00174 
00176     EdgeArray<T> &operator=(const EdgeArray<T> &a) {
00177         Array<T>::operator =(a);
00178         m_x = a.m_x;
00179         reregister(a.m_pGraph);
00180         return *this;
00181     }
00182 
00184     void init() {
00185         Array<T>::init(); reregister(0);
00186     }
00187 
00189     void init(const Graph &G) {
00190         Array<T>::init(G.edgeArrayTableSize()); reregister(&G);
00191     }
00192 
00194 
00198     void init(const Graph &G, const T &x) {
00199         Array<T>::init(0,G.edgeArrayTableSize()-1, m_x = x); reregister(&G);
00200     }
00201 
00203     void fill(const T &x) {
00204         int high = m_pGraph->maxEdgeIndex();
00205         if(high >= 0)
00206             Array<T>::fill(0,high,x);
00207     }
00208 
00209 private:
00210     virtual void enlargeTable(int newTableSize) {
00211         Array<T>::grow(newTableSize-Array<T>::size(),m_x);
00212     }
00213 
00214     virtual void reinit(int initTableSize) {
00215         Array<T>::init(0,initTableSize-1,m_x);
00216     }
00217 
00218     virtual void disconnect() {
00219         Array<T>::init();
00220         m_pGraph = 0;
00221     }
00222 
00223     OGDF_NEW_DELETE
00224 
00225 }; // class EdgeArray<T>
00226 
00227 
00229 
00233 class OGDF_EXPORT BucketEdgeArray : public BucketFunc<edge>
00234 {
00235     const EdgeArray<int> *m_pEdgeArray; 
00236 
00237 public:
00239 
00243     BucketEdgeArray(const EdgeArray<int> &edgeArray) :
00244       m_pEdgeArray(&edgeArray) { }
00245 
00247     int getBucket(const edge &e) { return (*m_pEdgeArray)[e]; }
00248 };
00249 
00250 
00251 } // end namespace ogdf
00252 #include <ogdf/basic/Graph.h>
00253 
00254 #endif