Open
Graph Drawing
Framework

 v.2012.07
 

NodeArray.h
Go to the documentation of this file.
1 /*
2  * $Revision: 2615 $
3  *
4  * last checkin:
5  * $Author: gutwenger $
6  * $Date: 2012-07-16 14:23:36 +0200 (Mo, 16. Jul 2012) $
7  ***************************************************************/
8 
44 #ifdef _MSC_VER
45 #pragma once
46 #endif
47 
48 #ifndef OGDF_NODE_ARRAY_H
49 #define OGDF_NODE_ARRAY_H
50 
51 
52 #include <ogdf/basic/Graph_d.h>
53 
54 
55 namespace ogdf {
56 
57 
59 
69 
70 public:
71  const Graph *m_pGraph;
72 
76  NodeArrayBase(const Graph *pG) : m_pGraph(pG) {
77  if(pG) m_it = pG->registerArray(this);
78  }
79 
80  // destructor, unregisters the array
81  virtual ~NodeArrayBase() {
83  }
84 
85  // event interface used by Graph
87  virtual void enlargeTable(int newTableSize) = 0;
89  virtual void reinit(int initTableSize) = 0;
91  virtual void disconnect() = 0;
92 
94  void reregister(const Graph *pG) {
96  if ((m_pGraph = pG) != 0) m_it = pG->registerArray(this);
97  }
98 }; // class NodeArrayBase
99 
100 
102 
108 template<class T> class NodeArray : private Array<T>, protected NodeArrayBase {
109  T m_x;
110 
111 public:
113  NodeArray() : Array<T>(), NodeArrayBase() { }
115  NodeArray(const Graph &G) : Array<T>(G.nodeArrayTableSize()), NodeArrayBase(&G) { }
117 
121  NodeArray(const Graph &G, const T &x) :
122  Array<T>(0,G.nodeArrayTableSize()-1,x), NodeArrayBase(&G), m_x(x) { }
124 
127  NodeArray(const NodeArray<T> &A) : Array<T>(A), NodeArrayBase(A.m_pGraph), m_x(A.m_x) { }
128 
130  bool valid() const { return (Array<T>::low() <= Array<T>::high()); }
131 
133  const Graph *graphOf() const {
134  return m_pGraph;
135  }
136 
138  const T &operator[](node v) const {
139  OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
140  return Array<T>::operator [](v->index());
141  }
142 
144  T &operator[](node v) {
145  OGDF_ASSERT(v != 0 && v->graphOf() == m_pGraph)
146  return Array<T>::operator [](v->index());
147  }
148 
150 
154  const T &operator[](int index) const {
155  return Array<T>::operator [](index);
156  }
157 
159 
163  T &operator[](int index) {
164  return Array<T>::operator [](index);
165  }
166 
170  m_x = a.m_x;
171  reregister(a.m_pGraph);
172  return *this;
173  }
174 
176  void init() {
178  }
179 
181  void init(const Graph &G) {
183  }
184 
186 
190  void init(const Graph &G, const T &x) {
191  Array<T>::init(0,G.nodeArrayTableSize()-1, m_x = x); reregister(&G);
192  }
193 
195  void fill(const T &x) {
196  int high = m_pGraph->maxNodeIndex();
197  if(high >= 0)
198  Array<T>::fill(0,high,x);
199  }
200 
201 private:
202  virtual void enlargeTable(int newTableSize) {
203  Array<T>::grow(newTableSize-Array<T>::size(),m_x);
204  }
205 
206  virtual void reinit(int initTableSize) {
207  Array<T>::init(0,initTableSize-1,m_x);
208  }
209 
210  virtual void disconnect() {
211  Array<T>::init();
212  m_pGraph = 0;
213  }
214 
216 
217 }; // class NodeArray<T>
218 
219 
220 } // end namespace ogdf
221 
222 #include <ogdf/basic/Graph.h>
223 
224 #endif