Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

GraphList.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <ogdf/basic/List.h>
36 
37 namespace ogdf {
38 
39 class Graph;
40 class ClusterGraph;
41 class ConstCombinatorialEmbedding;
42 class CombinatorialEmbedding;
43 
44 namespace internal {
45 
46 class OGDF_EXPORT GraphListBase;
47 
49 
55 
56  friend class ogdf::Graph;
57  friend class GraphListBase;
58 
59 protected:
60 
63 
65 };
66 
69 
70 protected:
71 
72  int m_size;
75 
76 public:
77 
79  GraphListBase() { m_head = m_tail = nullptr; m_size = 0; }
80 
83 
85  int size() const
86  {
87  return m_size;
88  }
89 
91  void pushBack(GraphElement *pX) {
92  pX->m_next = nullptr;
93  pX->m_prev = m_tail;
94  if (m_head)
95  m_tail = m_tail->m_next = pX;
96  else
97  m_tail = m_head = pX;
98  ++m_size;
99  }
100 
103  pX->m_prev = pY;
104  GraphElement *pYnext = pX->m_next = pY->m_next;
105  pY->m_next = pX;
106  if (pYnext) pYnext->m_prev = pX;
107  else m_tail = pX;
108  ++m_size;
109  }
110 
113  pX->m_next = pY;
114  GraphElement *pYprev = pX->m_prev = pY->m_prev;
115  pY->m_prev = pX;
116  if (pYprev) pYprev->m_next = pX;
117  else m_head = pX;
118  ++m_size;
119  }
120 
122  void del(GraphElement *pX) {
123  GraphElement *pxPrev = pX->m_prev, *pxNext = pX->m_next;
124 
125  if (pxPrev)
126  pxPrev->m_next = pxNext;
127  else
128  m_head = pxNext;
129  if (pxNext)
130  pxNext->m_prev = pxPrev;
131  else
132  m_tail = pxPrev;
133  m_size--;
134  }
135 
137  template<class LIST>
138  void sort(const LIST &newOrder) {
139  GraphElement *pPred = nullptr;
140  typename LIST::const_iterator it = newOrder.begin();
141  if (!it.valid()) return;
142 
143  m_head = *it;
144  for (; it.valid(); ++it) {
145  GraphElement *p = *it;
146  if ((p->m_prev = pPred) != nullptr) pPred->m_next = p;
147  pPred = p;
148  }
149 
150  (m_tail = pPred)->m_next = nullptr;
151  }
152 
154  void reverse() {
155  GraphElement *pX = m_head;
156  m_head = m_tail;
157  m_tail = pX;
158  while (pX) {
159  GraphElement *pY = pX->m_next;
160  pX->m_next = pX->m_prev;
161  pX = pX->m_prev = pY;
162  }
163  }
164 
166  void swap(GraphElement *pX, GraphElement *pY) {
167  if (pX->m_next == pY) {
168  pX->m_next = pY->m_next;
169  pY->m_prev = pX->m_prev;
170  pY->m_next = pX;
171  pX->m_prev = pY;
172 
173  }
174  else if (pY->m_next == pX) {
175  pY->m_next = pX->m_next;
176  pX->m_prev = pY->m_prev;
177  pX->m_next = pY;
178  pY->m_prev = pX;
179 
180  }
181  else {
182  std::swap(pX->m_next, pY->m_next);
183  std::swap(pX->m_prev, pY->m_prev);
184  }
185 
186  if (pX->m_prev)
187  pX->m_prev->m_next = pX;
188  else
189  m_head = pX;
190  if (pX->m_next)
191  pX->m_next->m_prev = pX;
192  else
193  m_tail = pX;
194 
195  if (pY->m_prev)
196  pY->m_prev->m_next = pY;
197  else
198  m_head = pY;
199  if (pY->m_next)
200  pY->m_next->m_prev = pY;
201  else
202  m_tail = pY;
203 
204 #ifdef OGDF_DEBUG
205  consistencyCheck();
206 #endif
207  }
208 
209 #ifdef OGDF_DEBUG
210  void consistencyCheck() const {
212  OGDF_ASSERT((m_head == nullptr) == (m_tail == nullptr));
213 
214  if (m_head != nullptr) {
215  OGDF_ASSERT(m_head->m_prev == nullptr);
216  OGDF_ASSERT(m_tail->m_next == nullptr);
217 
218  for (GraphElement *pX = m_head; pX; pX = pX->m_next) {
219  if (pX->m_prev) {
220  OGDF_ASSERT(pX->m_prev->m_next == pX);
221  } else {
222  OGDF_ASSERT(pX == m_head);
223  }
224 
225  if (pX->m_next) {
226  OGDF_ASSERT(pX->m_next->m_prev == pX);
227  } else {
228  OGDF_ASSERT(pX == m_tail);
229  }
230  }
231  }
232  }
233 #endif
234 
236 };
237 
239 
242 template<class T> class GraphList : protected GraphListBase {
243 
244 public:
245 
247  GraphList() { }
248 
251  if (m_head)
252  OGDF_ALLOCATOR::deallocateList(sizeof(T), m_head, m_tail);
253  }
254 
256  int size() const { return m_size; }
257 
259  T *head() const { return static_cast<T *>(m_head); }
260 
262  T *tail() const { return static_cast<T *>(m_tail); }
263 
265  bool empty() const { return m_size == 0; }
266 
268  void pushBack(T *pX) {
270  }
271 
273  void insertAfter(T *pX, T *pY) {
275  }
276 
278  void insertBefore(T *pX, T *pY) {
280  }
281 
283  void move(T *pX, GraphList<T> &L, T *pY, Direction dir) {
284  GraphListBase::del(pX);
285  if (dir == Direction::after)
286  L.insertAfter(pX, pY);
287  else
288  L.insertBefore(pX, pY);
289  }
290 
292  void move(T *pX, GraphList<T> &L) {
293  GraphListBase::del(pX);
294  L.pushBack(pX);
295  }
296 
298  void moveAfter(T *pX, T *pY){
299  GraphListBase::del(pX);
300  insertAfter(pX, pY);
301  }
302 
304  void moveBefore(T *pX, T *pY){
305  GraphListBase::del(pX);
306  insertBefore(pX, pY);
307  }
308 
310  void del(T *pX) {
311  GraphListBase::del(pX);
312  delete pX;
313  }
314 
316  void delPure(T *pX) {
317  GraphListBase::del(pX);
318  }
319 
321  void clear() {
322  if (m_head) {
323  OGDF_ALLOCATOR::deallocateList(sizeof(T), m_head, m_tail);
324  m_head = m_tail = nullptr;
325  m_size = 0;
326  }
327  }
328 
330  template<class T_LIST>
331  void sort(const T_LIST &newOrder) {
332  GraphListBase::sort(newOrder);
333  }
334 
335 
337  void reverse() {
339  }
340 
342  void swap(T *pX, T *pY) {
343  GraphListBase::swap(pX, pY);
344  }
345 
346 #ifdef OGDF_DEBUG
347  using GraphListBase::consistencyCheck;
348 #endif
349 
351 };
352 
353 template<class GraphObject>
354 class GraphObjectContainer : private GraphList<GraphObject> {
355 
356  friend class ogdf::Graph;
357  friend class ogdf::ClusterGraph;
360 
361 #if 0
362  GraphList<GraphObject> m_list;
363 #endif
364 
365 public:
367  using value_type = GraphObject*;
372 
375 
377  iterator end() const { return iterator(); }
378 
381 
383  reverse_iterator rend() const { return reverse_iterator(); }
384 
386  int size() const { return GraphList<GraphObject>::size(); }
387 
390 };
391 
392 }
393 }
void sort(const LIST &newOrder)
Sorts the list according to newOrder.
Definition: GraphList.h:138
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:492
#define OGDF_EXPORT
Specifies that a function or class is exported by the OGDF DLL.
Definition: config.h:99
int size() const
Returns the size of the list.
Definition: GraphList.h:256
GraphElement * m_next
The successor in the list.
Definition: GraphList.h:61
void insertBefore(T *pX, T *pY)
Inserts element pX before element pY.
Definition: GraphList.h:278
Combinatorial embeddings of planar graphs.
void insertAfter(GraphElement *pX, GraphElement *pY)
Inserts element pX after element pY.
Definition: GraphList.h:102
void pushBack(T *pX)
Adds element pX at the end of the list.
Definition: GraphList.h:268
void moveAfter(T *pX, T *pY)
Moves element pX from its current position to a position after pY.
Definition: GraphList.h:298
void reverse()
Reverses the order of the list elements.
Definition: GraphList.h:337
void del(T *pX)
Removes element pX from the list and deletes it.
Definition: GraphList.h:310
void swap(GraphElement *pX, GraphElement *pY)
Exchanges the positions of pX and pY in the list.
Definition: GraphList.h:166
reverse_iterator rbegin() const
Returns a reverse iterator to the last element in the container.
Definition: GraphList.h:380
Decralation of graph iterators.
iterator begin() const
Returns an iterator to the first element in the container.
Definition: GraphList.h:374
T * tail() const
Returns the last element in the list.
Definition: GraphList.h:262
int m_size
The size of the list.
Definition: GraphList.h:72
void sort(const T_LIST &newOrder)
Sorts all elements according to newOrder.
Definition: GraphList.h:331
int size() const
Returns the size of the list.
Definition: GraphList.h:85
int size() const
Returns the number of elements in the container.
Definition: GraphList.h:386
Representation of clustered graphs.
Definition: ClusterGraph.h:291
iterator end() const
Returns an iterator to the one-past-last element in the container.
Definition: GraphList.h:377
T * head() const
Returns the first element in the list.
Definition: GraphList.h:259
void insertBefore(GraphElement *pX, GraphElement *pY)
Inserts element pX before element pY.
Definition: GraphList.h:112
void reverse()
Reverses the order of the list elements.
Definition: GraphList.h:154
void clear()
Removes all elements from the list and deletes them.
Definition: GraphList.h:321
Direction
Definition: basic.h:126
Base class for GraphElement lists.
Definition: GraphList.h:68
void pushBack(GraphElement *pX)
Adds element pX at the end of the list.
Definition: GraphList.h:91
The base class for objects used by (hyper)graphs.
Definition: GraphList.h:54
void moveBefore(T *pX, T *pY)
Moves element pX from its current position to a position before pY.
Definition: GraphList.h:304
void swap(T *pX, T *pY)
Exchanges the positions of pX and pY in the list.
Definition: GraphList.h:342
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
GraphListBase()
Constructs an empty list.
Definition: GraphList.h:79
bool empty() const
Returns true iff the list is empty.
Definition: GraphList.h:265
void del(GraphElement *pX)
Removes element pX from the list.
Definition: GraphList.h:122
void delPure(T *pX)
Only removes element pX from the list; does not delete it.
Definition: GraphList.h:316
GraphElement * m_head
Pointer to the first element in the list.
Definition: GraphList.h:73
GraphElement * m_prev
The predecessor in the list.
Definition: GraphList.h:62
GraphElement * m_tail
Pointer to the last element in the list.
Definition: GraphList.h:74
~GraphListBase()
Destruction.
Definition: GraphList.h:82
The namespace for all OGDF objects.
~GraphList()
Destruction: deletes all elements.
Definition: GraphList.h:250
Lists of graph objects (like nodes, edges, etc.).
Definition: GraphList.h:242
void move(T *pX, GraphList< T > &L, T *pY, Direction dir)
Moves element pX to list L and inserts it before or after pY.
Definition: GraphList.h:283
Combinatorial embeddings of planar graphs with modification functionality.
Class for adjacency list elements.
Definition: Graph_d.h:79
reverse_iterator rend() const
Returns a reverse iterator to the one-before-first element in the container.
Definition: GraphList.h:383
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
void move(T *pX, GraphList< T > &L)
Moves element pX to list L and inserts it at the end.
Definition: GraphList.h:292
GraphList()
Constructs an empty list.
Definition: GraphList.h:247
void insertAfter(T *pX, T *pY)
Inserts element pX after element pY.
Definition: GraphList.h:273
Declaration of doubly linked lists and iterators.