Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

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 
104 
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 
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 
250 
252  void init() {
253  Array<T>::init(); reregister(nullptr);
254  }
255 
257  void init(const Graph &G) {
259  }
260 
262 
266  void init(const Graph &G, const T &x) {
267  Array<T>::init(0,G.adjEntryArrayTableSize()-1, m_x = x); reregister(&G);
268  }
269 
271  void fill(const T &x) {
272  int high = m_pGraph->maxAdjEntryIndex();
273  if(high >= 0)
274  Array<T>::fill(0,high,x);
275  }
276 
280  m_x = A.m_x;
281  reregister(A.m_pGraph);
282  return *this;
283  }
284 
286 
291  m_x = a.m_x;
292  moveRegister(a);
293  return *this;
294  }
295 
296 
298 
303 
306  if(adj->succ() != nullptr)
307  return adj->succ();
308  node v = adj->theNode();
309  for(v = v->succ(); v != nullptr && v->firstAdj() == nullptr; v = v->succ())
310  ;
311  return (v != nullptr) ? v->firstAdj() : nullptr;
312  }
313 
316  if(adj->pred() != nullptr)
317  return adj->pred();
318  node v = adj->theNode();
319  for(v = v->pred(); v != nullptr && v->lastAdj() == nullptr; v = v->pred())
320  ;
321  return (v != nullptr) ? v->lastAdj() : nullptr;
322  }
323 
325 
326 private:
329  node v = m_pGraph->firstNode();
330  while(v != nullptr && v->firstAdj() == nullptr)
331  v = v->succ();
332  adjEntry key = (v != nullptr) ? v->firstAdj() : nullptr;
333  return key;
334  }
335 
338  node v = m_pGraph->lastNode();
339  while(v != nullptr && v->lastAdj() == nullptr)
340  v = v->pred();
341  adjEntry key = (v != nullptr) ? v->lastAdj() : nullptr;
342  return key;
343  }
344 
345  virtual void enlargeTable(int newTableSize) {
346  Array<T>::grow(newTableSize-Array<T>::size(),m_x);
347  }
348 
349  virtual void reinit(int initTableSize) {
350  Array<T>::init(0,initTableSize-1,m_x);
351  }
352 
353  virtual void resetIndex(int newIndex, int oldIndex) {
354  Array<T>::operator [](newIndex) = Array<T>::operator [](oldIndex);
355  }
356 
357  virtual void disconnect() {
358  Array<T>::init();
359  m_pGraph = nullptr;
360  }
361 
363 };
364 
365 }
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:150
The parameterized class Array implements dynamic arrays of type E.
Definition: Array.h:200
node pred() const
Returns the predecessor in the list of all nodes.
Definition: Graph_d.h:220
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:492
iterator begin()
Returns an iterator to the first 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:354
static adjEntry findSuccKey(adjEntry adj)
Returns the key succeeding adj.
INDEX high() const
Returns the maximal array index.
Definition: Array.h:279
virtual ~AdjEntryArrayBase()
Destructor, unregisters the array.
Definition: AdjEntryArray.h:71
int maxAdjEntryIndex() const
Returns the largest used adjEntry index.
Definition: Graph_d.h:608
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:618
Definition: GML.h:110
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:218
void init(const Graph &G)
Reinitializes the array. Associates the array with G.
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 moveRegisterArray(ListIterator< ArrayBase *> it, ArrayBase *pArray) const
Move the registration it of an graph element array to pArray (used with move semantics for graph elem...
Definition: Graph_d.h:1251
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:615
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:71
void grow(INDEX add, const E &x)
Enlarges the array by add elements and sets new elements to x.
Definition: Array.h:789
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:215
Class for the representation of nodes.
Definition: Graph_d.h:169
Encapsulates a pointer to a list element.
Definition: List.h:42
node lastNode() const
Returns the last node in the list of all nodes.
Definition: Graph_d.h:620
#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.
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:148
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:432
const_reference operator[](INDEX i) const
Returns a reference to the element at position i.
Definition: Array.h:288
void fill(const E &x)
Sets all elements to x.
Definition: Array.h:383
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:111
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:213
INDEX low() const
Returns the minimal array index.
Definition: Array.h:276
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:100
Class for adjacency list elements.
Definition: Graph_d.h:79
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41