Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

EdgeArray.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  EdgeArrayBase() : m_pGraph(nullptr) { }
57 
59  explicit EdgeArrayBase(const Graph *pG) : m_pGraph(pG) {
60  if(pG) m_it = pG->registerArray(this);
61  }
62 
64  EdgeArrayBase(EdgeArrayBase &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 ~EdgeArrayBase() {
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 EdgeArrayBase
101 
102 
104 
112 template<class T> class EdgeArray : private Array<T>, protected EdgeArrayBase {
113  T m_x;
114 
115 public:
117  using key_type = edge;
119  using value_type = T;
120 
125 
126 
128  EdgeArray() : Array<T>(), EdgeArrayBase() { }
129 
131  explicit EdgeArray(const Graph &G) : Array<T>(G.edgeArrayTableSize()), EdgeArrayBase(&G) { }
132 
134 
138  EdgeArray(const Graph &G, const T &x) :
139  Array<T>(0,G.edgeArrayTableSize()-1,x), EdgeArrayBase(&G), m_x(x) { }
140 
142 
145  EdgeArray(const EdgeArray<T> &A) : Array<T>(A), EdgeArrayBase(A.m_pGraph), m_x(A.m_x) { }
146 
148 
151  EdgeArray(EdgeArray<T> &&A) : Array<T>(std::move(A)), EdgeArrayBase(A), m_x(A.m_x) { }
152 
153 
159 
161  bool valid() const { return Array<T>::low() <= Array<T>::high(); }
162 
164  const Graph *graphOf() const {
165  return m_pGraph;
166  }
167 
169  const T &operator[](edge e) const {
170  OGDF_ASSERT(e != nullptr);
171  OGDF_ASSERT(e->graphOf() == m_pGraph);
172  return Array<T>::operator [](e->index());
173  }
174 
176  T &operator[](edge e) {
177  OGDF_ASSERT(e != nullptr);
178  OGDF_ASSERT(e->graphOf() == m_pGraph);
179  return Array<T>::operator [](e->index());
180  }
181 
183  const T &operator()(edge e) const {
184  OGDF_ASSERT(e != nullptr);
185  OGDF_ASSERT(e->graphOf() == m_pGraph);
186  return Array<T>::operator [](e->index());
187  }
188 
190  T &operator()(edge e) {
191  OGDF_ASSERT(e != nullptr);
192  OGDF_ASSERT(e->graphOf() == m_pGraph);
193  return Array<T>::operator [](e->index());
194  }
195 
197  const T &operator[](adjEntry adj) const {
198  OGDF_ASSERT(adj != nullptr);
199  return Array<T>::operator [](adj->index() >> 1);
200  }
201 
204  OGDF_ASSERT(adj != nullptr);
205  return Array<T>::operator [](adj->index() >> 1);
206  }
207 
209  const T &operator()(adjEntry adj) const {
210  OGDF_ASSERT(adj != nullptr);
211  return Array<T>::operator [](adj->index() >> 1);
212  }
213 
216  OGDF_ASSERT(adj != nullptr);
217  return Array<T>::operator [](adj->index() >> 1);
218  }
219 
222  OGDF_DEPRECATED("Edge arrays should be indexed by an edge, not an integer index.")
223  const T &operator[](int index) const
224  { return Array<T>::operator [](index); }
225 
228  OGDF_DEPRECATED("Edge arrays should be indexed by an edge, not an integer index.")
229  T &operator[](int index)
230  { return Array<T>::operator [](index); }
231 
233 
238 
240 
243  iterator begin() { return iterator(m_pGraph->firstEdge(), this); }
244 
246 
249  const_iterator begin() const { return const_iterator(m_pGraph->firstEdge(), this); }
250 
252 
255  const_iterator cbegin() const { return const_iterator(m_pGraph->firstEdge(), this); }
256 
258 
261  iterator end() { return iterator(nullptr, this); }
262 
264 
267  const_iterator end() const { return const_iterator(nullptr, this); }
268 
270 
273  const_iterator cend() const { return const_iterator(nullptr, this); }
274 
276 
279  iterator rbegin() { return iterator(m_pGraph->lastEdge(), this); }
280 
282 
285  const_iterator rbegin() const { return const_iterator(m_pGraph->lastEdge(), this); }
286 
288 
291  const_iterator crbegin() const { return const_iterator(m_pGraph->lastEdge(), this); }
292 
294 
297  iterator rend() { return iterator(nullptr, this); }
298 
300 
303  const_iterator rend() const { return const_iterator(nullptr, this); }
304 
306 
309  const_iterator crend() const { return const_iterator(nullptr, this); }
310 
311 
313 
318 
320  void init() {
321  Array<T>::init(); reregister(nullptr);
322  }
323 
325  void init(const Graph &G) {
327  }
328 
330 
334  void init(const Graph &G, const T &x) {
335  Array<T>::init(0,G.edgeArrayTableSize()-1, m_x = x); reregister(&G);
336  }
337 
339  void fill(const T &x) {
340  int high = m_pGraph->maxEdgeIndex();
341  if(high >= 0)
342  Array<T>::fill(0,high,x);
343  }
344 
348  m_x = a.m_x;
349  reregister(a.m_pGraph);
350  return *this;
351  }
352 
354 
359  m_x = a.m_x;
360  moveRegister(a);
361  return *this;
362  }
363 
364 
366 
371 
372  static key_type findSuccKey(key_type key) { return key->succ(); }
373  static key_type findPredKey(key_type key) { return key->pred(); }
374 
376 
377 private:
378  virtual void enlargeTable(int newTableSize) {
379  Array<T>::resize(newTableSize,m_x);
380  }
381 
382  virtual void reinit(int initTableSize) {
383  Array<T>::init(0,initTableSize-1,m_x);
384  }
385 
386  virtual void disconnect() {
387  Array<T>::init();
388  m_pGraph = nullptr;
389  }
390 
392 
393 }; // class EdgeArray<T>
394 
395 
397 
402 {
404 
405 public:
407 
411  explicit BucketEdgeArray(const EdgeArray<int> &edgeArray) : m_pEdgeArray(&edgeArray) { }
412 
414  int getBucket(const edge &e) override { return (*m_pEdgeArray)[e]; }
415 };
416 
417 
418 } // end namespace ogdf
T & operator()(adjEntry adj)
Returns a reference to the element with index edge of adj.
Definition: EdgeArray.h:215
iterator begin()
Returns an iterator to the first entry in the edge array.
Definition: EdgeArray.h:243
virtual void enlargeTable(int newTableSize)=0
Virtual function called when table size has to be enlarged.
EdgeElement * edge
The type of edges.
Definition: Graph_d.h:66
const T & operator()(edge e) const
Returns a reference to the element with index e.
Definition: EdgeArray.h:183
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
iterator rend()
Returns an iterator to one-before-first entry in the edge array.
Definition: EdgeArray.h:297
const Graph * graphOf() const
Returns a pointer to the associated graph.
Definition: EdgeArray.h:164
void init()
Reinitializes the array. Associates the array with no graph.
Definition: EdgeArray.h:320
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
const_iterator cend() const
Returns a const iterator to one-past-last entry in the edge array.
Definition: EdgeArray.h:273
void init(const Graph &G, const T &x)
Reinitializes the array. Associates the array with G.
Definition: EdgeArray.h:334
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
EdgeArray(const Graph &G)
Constructs an edge array associated with G.
Definition: EdgeArray.h:131
EdgeArray(const Graph &G, const T &x)
Constructs an edge array associated with G.
Definition: EdgeArray.h:138
EdgeArrayBase()
Initializes an edge array not associated with a graph.
Definition: EdgeArray.h:56
INDEX high() const
Returns the maximal array index.
Definition: Array.h:131
BucketEdgeArray(const EdgeArray< int > &edgeArray)
Constructs a bucket function.
Definition: EdgeArray.h:411
const_iterator begin() const
Returns a const iterator to the first entry in the edge array.
Definition: EdgeArray.h:249
const Graph * m_pGraph
The associated graph.
Definition: EdgeArray.h:53
int index() const
Returns the index of the edge.
Definition: Graph_d.h:323
iterator end()
Returns an iterator to one-past-last entry in the edge array.
Definition: EdgeArray.h:261
Dynamic arrays indexed with edges.
Definition: EdgeArray.h:112
E * iterator
Provides a random-access iterator that can read or modify any element in an array.
Definition: Array.h:71
const E * const_iterator
Provides a random-access iterator that can read a const element in an array.
Definition: Array.h:69
bool valid() const
Returns true iff the array is associated with a graph.
Definition: EdgeArray.h:161
const_iterator rend() const
Returns a const iterator to one-before-first entry in the edge array.
Definition: EdgeArray.h:303
void fill(const T &x)
Sets all array elements to x.
Definition: EdgeArray.h:339
edge lastEdge() const
Returns the last edge in the list of all edges.
Definition: Graph_d.h:605
const_iterator end() const
Returns a const iterator to one-past-last entry in the edge array.
Definition: EdgeArray.h:267
void reregister(const Graph *pG)
Associates the array with a new graph.
Definition: EdgeArray.h:84
Bucket function for edges.
Definition: EdgeArray.h:401
T & operator[](adjEntry adj)
Returns a reference to the element with index edge of adj.
Definition: EdgeArray.h:203
ListIterator< NodeArrayBase * > registerArray(NodeArrayBase *pNodeArray) const
Registers a node array.
EdgeArray()
Constructs an empty edge array associated with no graph.
Definition: EdgeArray.h:128
void unregisterArray(ListIterator< NodeArrayBase *> it) const
Unregisters a node array.
EdgeArray(const EdgeArray< T > &A)
Constructs an edge array that is a copy of A.
Definition: EdgeArray.h:145
EdgeArrayBase(const Graph *pG)
Initializes an edge array associated with pG.
Definition: EdgeArray.h:59
E value_type
Represents the data type stored in an array element.
Definition: Array.h:63
int edgeArrayTableSize() const
Returns the table size of edge arrays associated with this graph.
Definition: Graph_d.h:593
EdgeArray< T > & operator=(const EdgeArray< T > &a)
Assignment operator.
Definition: EdgeArray.h:346
edge firstEdge() const
Returns the first edge in the list of all edges.
Definition: Graph_d.h:603
ListIterator< EdgeArrayBase * > m_it
Pointer to list element in the list of all registered edge arrays which references this array...
Definition: EdgeArray.h:50
EdgeArrayBase(EdgeArrayBase &base)
Moves edge array base to this edge array.
Definition: EdgeArray.h:64
void init(const Graph &G)
Reinitializes the array. Associates the array with G.
Definition: EdgeArray.h:325
Class for the representation of edges.
Definition: Graph_d.h:291
EdgeArray< T > & operator=(EdgeArray< T > &&a)
Assignment operator (move semantics).
Definition: EdgeArray.h:357
Encapsulates a pointer to a list element.
Definition: List.h:41
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
const T & operator[](adjEntry adj) const
Returns a reference to the element with index edge of adj.
Definition: EdgeArray.h:197
virtual void enlargeTable(int newTableSize)
Virtual function called when table size has to be enlarged.
Definition: EdgeArray.h:378
const T & move(const T &v)
Definition: backward.hpp:243
const T & operator[](edge e) const
Returns a reference to the element with index e.
Definition: EdgeArray.h:169
void moveRegister(EdgeArrayBase &base)
Moves array registration from base to this array.
Definition: EdgeArray.h:90
const EdgeArray< int > * m_pEdgeArray
Pointer to edge array.
Definition: EdgeArray.h:403
Pure declaration header, find template implementation in Graph.h.
Abstract base class for bucket functions.
Definition: basic.h:255
edge pred() const
Returns the predecessor in the list of all edges.
Definition: Graph_d.h:342
int getBucket(const edge &e) override
Returns bucket of edge e.
Definition: EdgeArray.h:414
virtual void disconnect()=0
Virtual function called when array is disconnected from the graph.
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
static key_type findSuccKey(key_type key)
Definition: EdgeArray.h:372
edge succ() const
Returns the successor in the list of all edges.
Definition: Graph_d.h:340
iterator rbegin()
Returns an iterator to the last entry in the edge array.
Definition: EdgeArray.h:279
const_iterator rbegin() const
Returns a const iterator to the last entry in the edge array.
Definition: EdgeArray.h:285
const_iterator cbegin() const
Returns a const iterator to the first entry in the edge array.
Definition: EdgeArray.h:255
virtual ~EdgeArrayBase()
Definition: EdgeArray.h:71
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)...
virtual void reinit(int initTableSize)
Virtual function called when table has to be reinitialized.
Definition: EdgeArray.h:382
#define OGDF_DEPRECATED(reason)
Mark a class / member / function as deprecated.
Definition: config.h:118
const_iterator crbegin() const
Returns a const iterator to the last entry in the edge array.
Definition: EdgeArray.h:291
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
Abstract base class for edge arrays.
Definition: EdgeArray.h:45
void fill(const E &x)
Sets all elements to x.
Definition: Array.h:235
The namespace for all OGDF objects.
const_iterator crend() const
Returns a const iterator to one-before-first entry in the edge array.
Definition: EdgeArray.h:309
int maxEdgeIndex() const
Returns the largest used edge index.
Definition: Graph_d.h:586
T m_x
The default value for array elements.
Definition: EdgeArray.h:113
T & operator()(edge e)
Returns a reference to the element with index e.
Definition: EdgeArray.h:190
int index() const
Returns the index of this adjacency element.
Definition: Graph_d.h:108
EdgeArray(EdgeArray< T > &&A)
Constructs an edge array containing the elements of A (move semantics).
Definition: EdgeArray.h:151
INDEX low() const
Returns the minimal array index.
Definition: Array.h:128
T & operator[](edge e)
Returns a reference to the element with index e.
Definition: EdgeArray.h:176
Class for adjacency list elements.
Definition: Graph_d.h:78
const T & operator()(adjEntry adj) const
Returns a reference to the element with index edge of adj.
Definition: EdgeArray.h:209
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
virtual void reinit(int initTableSize)=0
Virtual function called when table has to be reinitialized.
virtual void disconnect()
Virtual function called when array is disconnected from the graph.
Definition: EdgeArray.h:386
static key_type findPredKey(key_type key)
Definition: EdgeArray.h:373