Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

AdjEntryArray.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/Graph.h>
35 
36 
37 namespace ogdf {
38 
39 
41 
51 
52 public:
53  const Graph *m_pGraph;
54 
56  AdjEntryArrayBase() : m_pGraph(nullptr) { }
57 
59  explicit AdjEntryArrayBase(const Graph *pG) : m_pGraph(pG) {
60  if(pG) m_it = pG->registerArray(this);
61  }
62 
64  AdjEntryArrayBase(AdjEntryArrayBase &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 
71  virtual ~AdjEntryArrayBase() {
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;
83  virtual void resetIndex(int newIndex, int oldIndex) = 0;
84 
86  void reregister(const Graph *pG) {
87  if (m_pGraph) m_pGraph->unregisterArray(m_it);
88  if ((m_pGraph = pG) != nullptr) m_it = pG->registerArray(this);
89  }
90 
93  if (m_pGraph) m_pGraph->unregisterArray(m_it);
94  m_pGraph = base.m_pGraph;
95  m_it = base.m_it;
96  base.m_pGraph = nullptr;
98  if (m_pGraph != nullptr)
99  m_pGraph->moveRegisterArray(m_it, this);
100  }
101 
102 }; // class AdjEntryArrayBase
103 
104 
106 
114 template<class T> class AdjEntryArray : private Array<T>, protected AdjEntryArrayBase {
115  T m_x;
116 
117 public:
121  using value_type = T;
122 
127 
128 
131 
133  explicit AdjEntryArray(const Graph &G) : Array<T>(G.adjEntryArrayTableSize()), AdjEntryArrayBase(&G) { }
134 
136 
140  AdjEntryArray(const Graph &G, const T &x) :
141  Array<T>(0,G.adjEntryArrayTableSize()-1,x), AdjEntryArrayBase(&G), m_x(x) { }
142 
144 
147  AdjEntryArray(const AdjEntryArray<T> &A) : Array<T>(A), AdjEntryArrayBase(A.m_pGraph), m_x(A.m_x) { }
148 
150 
153  AdjEntryArray(AdjEntryArray<T> &&A) : Array<T>(std::move(A)), AdjEntryArrayBase(A), m_x(A.m_x) { }
154 
155 
161 
163  bool valid() const { return Array<T>::low() <= Array<T>::high(); }
164 
166  const Graph *graphOf() const {
167  return m_pGraph;
168  }
169 
171  const T &operator[](adjEntry adj) const {
172  OGDF_ASSERT(adj != nullptr);
173  OGDF_ASSERT(adj->graphOf() == m_pGraph);
174  return Array<T>::operator [](adj->index());
175  }
176 
179  OGDF_ASSERT(adj != nullptr);
180  OGDF_ASSERT(adj->graphOf() == m_pGraph);
181  return Array<T>::operator [](adj->index());
182  }
183 
185  const T &operator()(adjEntry adj) const {
186  OGDF_ASSERT(adj != nullptr);
187  OGDF_ASSERT(adj->graphOf() == m_pGraph);
188  return Array<T>::operator [](adj->index());
189  }
190 
193  OGDF_ASSERT(adj != nullptr);
194  OGDF_ASSERT(adj->graphOf() == m_pGraph);
195  return Array<T>::operator [](adj->index());
196  }
197 
198 
200 
205 
207 
211  return iterator(findFirstKey(), this);
212  }
213 
215 
218  const_iterator begin() const { return const_iterator(findFirstKey(), this); }
219 
221 
224  const_iterator cbegin() const { return const_iterator(findFirstKey(), this); }
225 
227 
230  iterator end() { return iterator(nullptr, this); }
231 
233 
236  const_iterator end() const { return const_iterator(nullptr, this); }
237 
239 
242  const_iterator cend() const { return const_iterator(nullptr, this); }
243 
245 
249  return iterator(findLastKey(), this);
250  }
251 
253 
256  const_iterator rbegin() const { return const_iterator(findLastKey(), this); }
257 
259 
262  const_iterator crbegin() const { return const_iterator(findLastKey(), this); }
263 
265 
268  iterator rend() { return iterator(nullptr, this); }
269 
271 
274  const_iterator rend() const { return const_iterator(nullptr, this); }
275 
277 
280  const_iterator crend() const { return const_iterator(nullptr, this); }
281 
282 
284 
289 
291  void init() {
292  Array<T>::init(); reregister(nullptr);
293  }
294 
296  void init(const Graph &G) {
298  }
299 
301 
305  void init(const Graph &G, const T &x) {
306  Array<T>::init(0,G.adjEntryArrayTableSize()-1, m_x = x); reregister(&G);
307  }
308 
310  void fill(const T &x) {
311  int high = m_pGraph->maxAdjEntryIndex();
312  if(high >= 0)
313  Array<T>::fill(0,high,x);
314  }
315 
319  m_x = A.m_x;
320  reregister(A.m_pGraph);
321  return *this;
322  }
323 
325 
330  m_x = a.m_x;
331  moveRegister(a);
332  return *this;
333  }
334 
335 
337 
342 
345  if(adj->succ() != nullptr)
346  return adj->succ();
347  node v = adj->theNode();
348  for(v = v->succ(); v != nullptr && v->firstAdj() == nullptr; v = v->succ())
349  ;
350  return (v != nullptr) ? v->firstAdj() : nullptr;
351  }
352 
355  if(adj->pred() != nullptr)
356  return adj->pred();
357  node v = adj->theNode();
358  for(v = v->pred(); v != nullptr && v->lastAdj() == nullptr; v = v->pred())
359  ;
360  return (v != nullptr) ? v->lastAdj() : nullptr;
361  }
362 
364 
365 private:
368  node v = m_pGraph->firstNode();
369  while(v != nullptr && v->firstAdj() == nullptr)
370  v = v->succ();
371  adjEntry key = (v != nullptr) ? v->firstAdj() : nullptr;
372  return key;
373  }
374 
377  node v = m_pGraph->lastNode();
378  while(v != nullptr && v->lastAdj() == nullptr)
379  v = v->pred();
380  adjEntry key = (v != nullptr) ? v->lastAdj() : nullptr;
381  return key;
382  }
383 
384  virtual void enlargeTable(int newTableSize) {
385  Array<T>::grow(newTableSize-Array<T>::size(),m_x);
386  }
387 
388  virtual void reinit(int initTableSize) {
389  Array<T>::init(0,initTableSize-1,m_x);
390  }
391 
392  virtual void resetIndex(int newIndex, int oldIndex) {
393  Array<T>::operator [](newIndex) = Array<T>::operator [](oldIndex);
394  }
395 
396  virtual void disconnect() {
397  Array<T>::init();
398  m_pGraph = nullptr;
399  }
400 
402 
403 }; // class AdjEntryArray<T>
404 
405 
406 } // end namespace ogdf
const_iterator rend() const
Returns a const iterator to one-before-first entry in the array.
iterator end()
Returns an iterator to one-past-last entry in the array.
const_iterator end() const
Returns a const iterator to one-past-last entry in the array.
const Graph * m_pGraph
The associated graph.
Definition: AdjEntryArray.h:53
virtual void resetIndex(int newIndex, int oldIndex)=0
Virtual function called when the index of an adjacency entry is changed.
void reregister(const Graph *pG)
Associates the array with a new graph.
Definition: AdjEntryArray.h:86
adjEntry pred() const
Returns the predecessor in the adjacency list.
Definition: Graph_d.h:147
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:56
node pred() const
Returns the predecessor in the list of all nodes.
Definition: Graph_d.h:218
const Graph * graphOf() const
Returns a pointer to the associated graph.
void init(const Graph &G, const T &x)
Reinitializes the array. Associates the array with G.
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
iterator begin()
Returns an iterator to the first entry in the array.
iterator rbegin()
Returns an iterator to the last entry in the array.
const_iterator rbegin() const
Returns a const iterator to the last entry in the array.
virtual void enlargeTable(int newTableSize)=0
Virtual function called when table size has to be enlarged.
void init()
Reinitializes the array to an array with empty index set.
Definition: Array.h:206
static adjEntry findSuccKey(adjEntry adj)
Returns the key succeeding adj.
INDEX high() const
Returns the maximal array index.
Definition: Array.h:131
virtual ~AdjEntryArrayBase()
Destructor, unregisters the array.
Definition: AdjEntryArray.h:71
int maxAdjEntryIndex() const
Returns the largest used adjEntry index.
Definition: Graph_d.h:588
ListIterator< AdjEntryArrayBase * > m_it
Pointer to list element in the list of all registered adjacency entry arrays which references this ar...
Definition: AdjEntryArray.h:50
AdjEntryArrayBase()
Initializes an adjacency entry array not associated with a graph.
Definition: AdjEntryArray.h:56
node firstNode() const
Returns the first node in the list of all nodes.
Definition: Graph_d.h:598
const_iterator begin() const
Returns a const iterator to the first entry in the array.
Dynamic arrays indexed with adjacency entries.
AdjEntryArray(const Graph &G, const T &x)
Constructs an adjacency entry array associated with G.
void fill(const T &x)
Sets all array elements to x.
node succ() const
Returns the successor in the list of all nodes.
Definition: Graph_d.h:216
void init(const Graph &G)
Reinitializes the array. Associates the array with G.
const_iterator crbegin() const
Returns a const iterator to the last entry in the array.
AdjEntryArray(const AdjEntryArray< T > &A)
Constructs an adjacency entry array that is a copy of A.
AdjEntryArray< T > & operator=(AdjEntryArray< T > &&a)
Assignment operator (move semantics).
ListIterator< NodeArrayBase * > registerArray(NodeArrayBase *pNodeArray) const
Registers a node array.
void unregisterArray(ListIterator< NodeArrayBase *> it) const
Unregisters a node array.
virtual void reinit(int initTableSize)=0
Virtual function called when table has to be reinitialized.
int adjEntryArrayTableSize() const
Returns the table size of adjEntry arrays associated with this graph.
Definition: Graph_d.h:595
iterator rend()
Returns an iterator to one-before-first entry in the array.
virtual void reinit(int initTableSize)
Virtual function called when table has to be reinitialized.
virtual void disconnect()
Virtual function called when array is disconnected from the graph.
bool valid() const
Returns true iff the array is associated with a graph.
Abstract base class for adjacency entry arrays.
Definition: AdjEntryArray.h:45
adjEntry findLastKey() const
Returns the last key (adjacency entry in the graph).
AdjElement * adjEntry
The type of adjacency entries.
Definition: Graph_d.h:70
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:611
AdjEntryArray(AdjEntryArray< T > &&A)
Constructs an adjacency entry array containing the elements of A (move semantics).
const T & operator[](adjEntry adj) const
Returns a reference to the element with index adj.
AdjEntryArray()
Constructs an empty adjacency entry array associated with no graph.
AdjEntryArrayBase(AdjEntryArrayBase &base)
Moves adjacency entry array base to this adjacency entry array.
Definition: AdjEntryArray.h:64
const T & operator()(adjEntry adj) const
Returns a reference to the element with index adj.
const_iterator cend() const
Returns a const iterator to one-past-last entry in the array.
adjEntry lastAdj() const
Returns the last entry in the adjacency list.
Definition: Graph_d.h:213
Class for the representation of nodes.
Definition: Graph_d.h:167
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
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
AdjEntryArray< T > & operator=(const AdjEntryArray< T > &A)
Assignment operator.
const T & move(const T &v)
Definition: backward.hpp:243
virtual void resetIndex(int newIndex, int oldIndex)
Virtual function called when the index of an adjacency entry is changed.
virtual void enlargeTable(int newTableSize)
Virtual function called when table size has to be enlarged.
virtual void disconnect()=0
Virtual function called when array is disconnected from the graph.
Includes declaration of graph class.
const_iterator crend() const
Returns a const iterator to one-before-first entry in the array.
void moveRegister(AdjEntryArrayBase &base)
Moves array registration from base to this array.
Definition: AdjEntryArray.h:92
T & operator()(adjEntry adj)
Returns a reference to the element with index adj.
adjEntry succ() const
Returns the successor in the adjacency list.
Definition: Graph_d.h:145
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)...
T & operator[](adjEntry adj)
Returns a reference to the element with index adj.
void init()
Reinitializes the array. Associates the array with no graph.
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.
T m_x
The default value for array elements.
adjEntry findFirstKey() const
Returns the first key (adjacency entry in the graph).
const_iterator cbegin() const
Returns a const iterator to the first entry in the array.
AdjEntryArrayBase(const Graph *pG)
Initializes an adjacency entry array associated with pG.
Definition: AdjEntryArray.h:59
int index() const
Returns the index of this adjacency element.
Definition: Graph_d.h:108
static adjEntry findPredKey(adjEntry adj)
Returns the key preceeding adj.
adjEntry firstAdj() const
Returns the first entry in the adjaceny list.
Definition: Graph_d.h:211
INDEX low() const
Returns the minimal array index.
Definition: Array.h:128
AdjEntryArray(const Graph &G)
Constructs an adjacency entry array associated with G.
node theNode() const
Returns the node whose adjacency list contains this element.
Definition: Graph_d.h:99
Class for adjacency list elements.
Definition: Graph_d.h:78
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41