Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

NodeArray.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph_d.h>
35 
36 
37 namespace ogdf {
38 
39 
41 
51 
52 public:
53  const Graph *m_pGraph;
54 
56  NodeArrayBase() : m_pGraph(nullptr) { }
57 
59  explicit NodeArrayBase(const Graph *pG) : m_pGraph(pG) {
60  if(pG) m_it = pG->registerArray(this);
61  }
62 
64  NodeArrayBase(NodeArrayBase &base) : m_it(base.m_it), m_pGraph(base.m_pGraph) {
65  if(m_pGraph) m_pGraph->moveRegisterArray(m_it, this);
66  base.m_pGraph = nullptr;
68  }
69 
70  // destructor, unregisters the array
71  virtual ~NodeArrayBase() {
72  if (m_pGraph) m_pGraph->unregisterArray(m_it);
73  }
74 
75  // event interface used by Graph
77  virtual void enlargeTable(int newTableSize) = 0;
79  virtual void reinit(int initTableSize) = 0;
81  virtual void disconnect() = 0;
82 
84  void reregister(const Graph *pG) {
85  if (m_pGraph) m_pGraph->unregisterArray(m_it);
86  if ((m_pGraph = pG) != nullptr) m_it = pG->registerArray(this);
87  }
88 
91  if (m_pGraph) m_pGraph->unregisterArray(m_it);
92  m_pGraph = base.m_pGraph;
93  m_it = base.m_it;
94  base.m_pGraph = nullptr;
96  if (m_pGraph != nullptr)
97  m_pGraph->moveRegisterArray(m_it, this);
98  }
99 
100 }; // class NodeArrayBase
101 
102 
104 
112 template<class T> class NodeArray : private Array<T>, protected NodeArrayBase {
113  T m_x;
114 
115 public:
116  using key_type = node;
117  using value_type = T;
118 
121 
122 
124  NodeArray() : Array<T>(), NodeArrayBase() { }
125 
127  NodeArray(const Graph &G) : Array<T>(G.nodeArrayTableSize()), NodeArrayBase(&G) { }
128 
130 
134  NodeArray(const Graph &G, const T &x) :
135  Array<T>(0,G.nodeArrayTableSize()-1,x), NodeArrayBase(&G), m_x(x) { }
136 
138 
141  NodeArray(const NodeArray<T> &A) : Array<T>(A), NodeArrayBase(A.m_pGraph), m_x(A.m_x) { }
142 
144 
147  NodeArray(NodeArray<T> &&A) : Array<T>(std::move(A)), NodeArrayBase(A), m_x(A.m_x) { }
148 
149 
155 
157  bool valid() const { return Array<T>::low() <= Array<T>::high(); }
158 
160  const Graph *graphOf() const {
161  return m_pGraph;
162  }
163 
165  const T &operator[](node v) const {
166  OGDF_ASSERT(v != nullptr);
167  OGDF_ASSERT(v->graphOf() == m_pGraph);
168  return Array<T>::operator [](v->index());
169  }
170 
172  T &operator[](node v) {
173  OGDF_ASSERT(v != nullptr);
174  OGDF_ASSERT(v->graphOf() == m_pGraph);
175  return Array<T>::operator [](v->index());
176  }
177 
179  const T &operator()(node v) const {
180  OGDF_ASSERT(v != nullptr);
181  OGDF_ASSERT(v->graphOf() == m_pGraph);
182  return Array<T>::operator [](v->index());
183  }
184 
186  T &operator()(node v) {
187  OGDF_ASSERT(v != nullptr);
188  OGDF_ASSERT(v->graphOf() == m_pGraph);
189  return Array<T>::operator [](v->index());
190  }
191 
194  OGDF_DEPRECATED("NodeArrays should be indexed by a node, not an integer index.")
195  const T &operator[](int index) const
196  { return Array<T>::operator [](index); }
197 
200  OGDF_DEPRECATED("NodeArrays should be indexed by a node, not an integer index.")
201  T &operator[](int index)
202  { return Array<T>::operator [](index); }
203 
205 
210 
212 
215  iterator begin() { return iterator(m_pGraph->firstNode(), this); }
216 
218 
221  const_iterator begin() const { return const_iterator(m_pGraph->firstNode(), this); }
222 
224 
227  const_iterator cbegin() const { return const_iterator(m_pGraph->firstNode(), this); }
228 
230 
233  iterator end() { return iterator(nullptr, this); }
234 
236 
239  const_iterator end() const { return const_iterator(nullptr, this); }
240 
242 
245  const_iterator cend() const { return const_iterator(nullptr, this); }
246 
248 
251  iterator rbegin() { return iterator(m_pGraph->lastNode(), this); }
252 
254 
257  const_iterator rbegin() const { return const_iterator(m_pGraph->lastNode(), this); }
258 
260 
263  const_iterator crbegin() const { return const_iterator(m_pGraph->lastNode(), this); }
264 
266 
269  iterator rend() { return iterator(nullptr, this); }
270 
272 
275  const_iterator rend() const { return const_iterator(nullptr, this); }
276 
278 
281  const_iterator crend() const { return const_iterator(nullptr, this); }
282 
283 
285 
290 
292  void init() {
293  Array<T>::init(); reregister(nullptr);
294  }
295 
297  void init(const Graph &G) {
299  }
300 
302 
306  void init(const Graph &G, const T &x) {
307  Array<T>::init(0,G.nodeArrayTableSize()-1, m_x = x); reregister(&G);
308  }
309 
311  void fill(const T &x) {
312  int high = m_pGraph->maxNodeIndex();
313  if(high >= 0)
314  Array<T>::fill(0,high,x);
315  }
316 
320  m_x = a.m_x;
321  reregister(a.m_pGraph);
322  return *this;
323  }
324 
326 
331  m_x = a.m_x;
332  moveRegister(a);
333  return *this;
334  }
335 
336 
338 
343 
344  static key_type findSuccKey(key_type key) { return key->succ(); }
345  static key_type findPredKey(key_type key) { return key->pred(); }
346 
348 
349 private:
350  virtual void enlargeTable(int newTableSize) {
351  Array<T>::resize(newTableSize,m_x);
352  }
353 
354  virtual void reinit(int initTableSize) {
355  Array<T>::init(0,initTableSize-1,m_x);
356  }
357 
358  virtual void disconnect() {
359  Array<T>::init();
360  m_pGraph = nullptr;
361  }
362 
364 
365 }; // class NodeArray<T>
366 
367 
368 } // end namespace ogdf
virtual void enlargeTable(int newTableSize)
Virtual function called when table size has to be enlarged.
Definition: NodeArray.h:350
Abstract base class for node arrays.
Definition: NodeArray.h:45
virtual void reinit(int initTableSize)
Virtual function called when table has to be reinitialized.
Definition: NodeArray.h:354
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
T & operator[](node v)
Returns a reference to the element with index v.
Definition: NodeArray.h:172
node pred() const
Returns the predecessor in the list of all nodes.
Definition: Graph_d.h:218
NodeArray< T > & operator=(const NodeArray< T > &a)
Assignment operator.
Definition: NodeArray.h:318
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
void init()
Reinitializes the array. Associates the array with no graph.
Definition: NodeArray.h:292
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
INDEX high() const
Returns the maximal array index.
Definition: Array.h:131
NodeArray(const NodeArray< T > &A)
Constructs a node array that is a copy of A.
Definition: NodeArray.h:141
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:598
const_iterator rend() const
Returns a const iterator to one-before-first entry in the node array.
Definition: NodeArray.h:275
static key_type findSuccKey(key_type key)
Definition: NodeArray.h:344
const_iterator crend() const
Returns a const iterator to one-before-first entry in the node array.
Definition: NodeArray.h:281
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:216
void reregister(const Graph *pG)
Associates the array with a new graph.
Definition: NodeArray.h:84
NodeArrayBase()
Initializes an node array not associated with a graph.
Definition: NodeArray.h:56
const_iterator cend() const
Returns a const iterator to one-past-last entry in the node array.
Definition: NodeArray.h:245
ListIterator< NodeArrayBase * > registerArray(NodeArrayBase *pNodeArray) const
Registers a node array.
void moveRegister(NodeArrayBase &base)
Moves array registration from base to this array.
Definition: NodeArray.h:90
void unregisterArray(ListIterator< NodeArrayBase *> it) const
Unregisters a node array.
NodeArrayBase(const Graph *pG)
Initializes an node array associated with pG.
Definition: NodeArray.h:59
Dynamic arrays indexed with nodes.
Definition: Graph_d.h:423
const_iterator end() const
Returns a const iterator to one-past-last entry in the node array.
Definition: NodeArray.h:239
NodeArray(NodeArray< T > &&A)
Constructs a node array containing the elements of A (move semantics).
Definition: NodeArray.h:147
const_iterator crbegin() const
Returns a const iterator to the last entry in the node array.
Definition: NodeArray.h:263
const_iterator rbegin() const
Returns a const iterator to the last entry in the node array.
Definition: NodeArray.h:257
NodeArray(const Graph &G, const T &x)
Constructs a node array associated with G.
Definition: NodeArray.h:134
virtual void enlargeTable(int newTableSize)=0
Virtual function called when table size has to be enlarged.
virtual void disconnect()=0
Virtual function called when array is disconnected from the graph.
NodeArray< T > & operator=(NodeArray< T > &&a)
Assignment operator (move semantics).
Definition: NodeArray.h:329
virtual void reinit(int initTableSize)=0
Virtual function called when table has to be reinitialized.
T & operator()(node v)
Returns a reference to the element with index v.
Definition: NodeArray.h:186
NodeElement * node
The type of nodes.
Definition: Graph_d.h:62
Class for the representation of nodes.
Definition: Graph_d.h:167
bool valid() const
Returns true iff the array is associated with a graph.
Definition: NodeArray.h:157
const Graph * m_pGraph
The associated graph.
Definition: NodeArray.h:53
Encapsulates a pointer to a list element.
Definition: List.h:41
node lastNode() const
Returns the last node in the list of all nodes.
Definition: Graph_d.h:600
T m_x
The default value for array elements.
Definition: NodeArray.h:113
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
const T & move(const T &v)
Definition: backward.hpp:243
void init(const Graph &G)
Reinitializes the array. Associates the array with G.
Definition: NodeArray.h:297
virtual void disconnect()
Virtual function called when array is disconnected from the graph.
Definition: NodeArray.h:358
iterator rbegin()
Returns an iterator to the last entry in the node array.
Definition: NodeArray.h:251
Pure declaration header, find template implementation in Graph.h.
ListIterator< NodeArrayBase * > m_it
Pointer to list element in the list of all registered node arrays which references this array...
Definition: NodeArray.h:50
iterator end()
Returns an iterator to one-past-last entry in the node array.
Definition: NodeArray.h:233
virtual ~NodeArrayBase()
Definition: NodeArray.h:71
int index() const
Returns the (unique) node index.
Definition: Graph_d.h:201
void resize(INDEX newSize, const E &x)
Resizes (enlarges or shrinks) the array to hold newSize elements and sets new elements to x...
Definition: Array.h:274
NodeArray()
Constructs an empty node array associated with no graph.
Definition: NodeArray.h:124
int nodeArrayTableSize() const
Returns the table size of node arrays associated with this graph.
Definition: Graph_d.h:591
const_iterator begin() const
Returns a const iterator to the first entry in the node array.
Definition: NodeArray.h:221
iterator begin()
Returns an iterator to the first entry in the node array.
Definition: NodeArray.h:215
void moveRegisterArray(ListIterator< NodeArrayBase *> it, NodeArrayBase *pNodeArray) const
Move the registration it of a node array to pNodeArray (used with move semantics for node arrays)...
int maxNodeIndex() const
Returns the largest used node index.
Definition: Graph_d.h:584
const Graph * graphOf() const
Returns a pointer to the associated graph.
Definition: NodeArray.h:160
void init(const Graph &G, const T &x)
Reinitializes the array. Associates the array with G.
Definition: NodeArray.h:306
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition: config.h:118
Array< E, INDEX > & operator=(const Array< E, INDEX > &A)
Assignment operator.
Definition: Array.h:284
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:140
void fill(const E &x)
Sets all elements to x.
Definition: Array.h:235
The namespace for all OGDF objects.
static key_type findPredKey(key_type key)
Definition: NodeArray.h:345
const T & operator()(node v) const
Returns a reference to the element with index v.
Definition: NodeArray.h:179
iterator rend()
Returns an iterator to one-before-first entry in the node array.
Definition: NodeArray.h:269
NodeArrayBase(NodeArrayBase &base)
Moves node array base to this node array.
Definition: NodeArray.h:64
const T & operator[](node v) const
Returns a reference to the element with index v.
Definition: NodeArray.h:165
INDEX low() const
Returns the minimal array index.
Definition: Array.h:128
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void fill(const T &x)
Sets all array elements to x.
Definition: NodeArray.h:311
const_iterator cbegin() const
Returns a const iterator to the first entry in the node array.
Definition: NodeArray.h:227
NodeArray(const Graph &G)
Constructs a node array associated with G.
Definition: NodeArray.h:127