Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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 }; // class GraphElement
67 
70 
71 protected:
72 
73  int m_size;
76 
77 public:
78 
80  GraphListBase() { m_head = m_tail = nullptr; m_size = 0; }
81 
84 
86  int size() const
87  {
88  return m_size;
89  }
90 
92  void pushBack(GraphElement *pX) {
93  pX->m_next = nullptr;
94  pX->m_prev = m_tail;
95  if (m_head)
96  m_tail = m_tail->m_next = pX;
97  else
98  m_tail = m_head = pX;
99  ++m_size;
100  }
101 
104  pX->m_prev = pY;
105  GraphElement *pYnext = pX->m_next = pY->m_next;
106  pY->m_next = pX;
107  if (pYnext) pYnext->m_prev = pX;
108  else m_tail = pX;
109  ++m_size;
110  }
111 
114  pX->m_next = pY;
115  GraphElement *pYprev = pX->m_prev = pY->m_prev;
116  pY->m_prev = pX;
117  if (pYprev) pYprev->m_next = pX;
118  else m_head = pX;
119  ++m_size;
120  }
121 
123  void del(GraphElement *pX) {
124  GraphElement *pxPrev = pX->m_prev, *pxNext = pX->m_next;
125 
126  if (pxPrev)
127  pxPrev->m_next = pxNext;
128  else
129  m_head = pxNext;
130  if (pxNext)
131  pxNext->m_prev = pxPrev;
132  else
133  m_tail = pxPrev;
134  m_size--;
135  }
136 
138  template<class LIST>
139  void sort(const LIST &newOrder) {
140  GraphElement *pPred = nullptr;
141  typename LIST::const_iterator it = newOrder.begin();
142  if (!it.valid()) return;
143 
144  m_head = *it;
145  for (; it.valid(); ++it) {
146  GraphElement *p = *it;
147  if ((p->m_prev = pPred) != nullptr) pPred->m_next = p;
148  pPred = p;
149  }
150 
151  (m_tail = pPred)->m_next = nullptr;
152  }
153 
155  void reverse() {
156  GraphElement *pX = m_head;
157  m_head = m_tail;
158  m_tail = pX;
159  while (pX) {
160  GraphElement *pY = pX->m_next;
161  pX->m_next = pX->m_prev;
162  pX = pX->m_prev = pY;
163  }
164  }
165 
167  void swap(GraphElement *pX, GraphElement *pY) {
168  if (pX->m_next == pY) {
169  pX->m_next = pY->m_next;
170  pY->m_prev = pX->m_prev;
171  pY->m_next = pX;
172  pX->m_prev = pY;
173 
174  }
175  else if (pY->m_next == pX) {
176  pY->m_next = pX->m_next;
177  pX->m_prev = pY->m_prev;
178  pX->m_next = pY;
179  pY->m_prev = pX;
180 
181  }
182  else {
183  ogdf::swap(pX->m_next, pY->m_next);
184  ogdf::swap(pX->m_prev, pY->m_prev);
185  }
186 
187  if (pX->m_prev)
188  pX->m_prev->m_next = pX;
189  else
190  m_head = pX;
191  if (pX->m_next)
192  pX->m_next->m_prev = pX;
193  else
194  m_tail = pX;
195 
196  if (pY->m_prev)
197  pY->m_prev->m_next = pY;
198  else
199  m_head = pY;
200  if (pY->m_next)
201  pY->m_next->m_prev = pY;
202  else
203  m_tail = pY;
204 
205  OGDF_ASSERT(consistencyCheck());
206  }
207 
210  if (m_head == nullptr) {
211  return m_tail == nullptr;
212  }
213  else if (m_tail == nullptr) {
214  return false;
215 
216  }
217  else {
218  if (m_head->m_prev != nullptr)
219  return false;
220  if (m_tail->m_next != nullptr)
221  return false;
222 
223  GraphElement *pX = m_head;
224  for (; pX; pX = pX->m_next) {
225  if (pX->m_prev) {
226  if (pX->m_prev->m_next != pX)
227  return false;
228  }
229  else if (pX != m_head)
230  return false;
231 
232  if (pX->m_next) {
233  if (pX->m_next->m_prev != pX)
234  return false;
235  }
236  else if (pX != m_tail)
237  return false;
238  }
239  }
240 
241  return true;
242  }
243 
245 
246 }; // class GraphListBase
247 
249 
252 template<class T> class GraphList : protected GraphListBase {
253 
254 public:
255 
257  GraphList() { }
258 
261  if (m_head)
262  OGDF_ALLOCATOR::deallocateList(sizeof(T), m_head, m_tail);
263  }
264 
266  int size() const { return m_size; }
267 
269  T *head() const { return static_cast<T *>(m_head); }
270 
272  T *tail() const { return static_cast<T *>(m_tail); }
273 
275  bool empty() const { return m_size == 0; }
276 
278  void pushBack(T *pX) {
280  }
281 
283  void insertAfter(T *pX, T *pY) {
285  }
286 
288  void insertBefore(T *pX, T *pY) {
290  }
291 
293  void move(T *pX, GraphList<T> &L, T *pY, Direction dir) {
294  GraphListBase::del(pX);
295  if (dir == Direction::after)
296  L.insertAfter(pX, pY);
297  else
298  L.insertBefore(pX, pY);
299  }
300 
302  void move(T *pX, GraphList<T> &L) {
303  GraphListBase::del(pX);
304  L.pushBack(pX);
305  }
306 
308  void moveAfter(T *pX, T *pY){
309  GraphListBase::del(pX);
310  insertAfter(pX, pY);
311  }
312 
314  void moveBefore(T *pX, T *pY){
315  GraphListBase::del(pX);
316  insertBefore(pX, pY);
317  }
318 
320  void del(T *pX) {
321  GraphListBase::del(pX);
322  delete pX;
323  }
324 
326  void delPure(T *pX) {
327  GraphListBase::del(pX);
328  }
329 
331  void clear() {
332  if (m_head) {
333  OGDF_ALLOCATOR::deallocateList(sizeof(T), m_head, m_tail);
334  m_head = m_tail = nullptr;
335  m_size = 0;
336  }
337  }
338 
340  template<class T_LIST>
341  void sort(const T_LIST &newOrder) {
342  GraphListBase::sort(newOrder);
343  }
344 
345 
347  void reverse() {
349  }
350 
352  void swap(T *pX, T *pY) {
353  GraphListBase::swap(pX, pY);
354  }
355 
356 
360  }
361 
363 
364 }; // class GraphList<T>
365 
366 
367 template<class GraphObject>
368 class GraphObjectContainer : private GraphList<GraphObject> {
369 
370  friend class ogdf::Graph;
371  friend class ogdf::ClusterGraph;
374 
375 #if 0
376  GraphList<GraphObject> m_list;
377 #endif
378 
379 public:
382 
385 
387  iterator end() const { return iterator(); }
388 
391 
393  iterator rend() const { return iterator(); }
394 
396  int size() const { return GraphList<GraphObject>::size(); }
397 
400 };
401 
402 } // end namespace internal
403 } // end namespace ogdf
void sort(const LIST &newOrder)
Sorts the list according to newOrder.
Definition: GraphList.h:139
Data type for general directed graphs (adjacency list representation).
Definition: Graph_d.h:472
#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:266
static const int Graph
Definition: GmlParser.h:59
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:288
Combinatorial embeddings of planar graphs.
void insertAfter(GraphElement *pX, GraphElement *pY)
Inserts element pX after element pY.
Definition: GraphList.h:103
void pushBack(T *pX)
Adds element pX at the end of the list.
Definition: GraphList.h:278
void moveAfter(T *pX, T *pY)
Moves element pX from its current position to a position after pY.
Definition: GraphList.h:308
void reverse()
Reverses the order of the list elements.
Definition: GraphList.h:347
void del(T *pX)
Removes element pX from the list and deletes it.
Definition: GraphList.h:320
void swap(GraphElement *pX, GraphElement *pY)
Exchanges the positions of pX and pY in the list.
Definition: GraphList.h:167
Decralation of graph iterators.
iterator begin() const
Returns an iterator to the first element in the container.
Definition: GraphList.h:384
T * tail() const
Returns the last element in the list.
Definition: GraphList.h:272
int m_size
The size of the list.
Definition: GraphList.h:73
void sort(const T_LIST &newOrder)
Sorts all elements according to newOrder.
Definition: GraphList.h:341
int size() const
Returns the size of the list.
Definition: GraphList.h:86
int size() const
Returns the number of elements in the container.
Definition: GraphList.h:396
bool consistencyCheck()
Checks consistency of graph list.
Definition: GraphList.h:209
Representation of clustered graphs.
Definition: ClusterGraph.h:291
bool consistencyCheck()
Checks consistency of graph list; returns true if ok.
Definition: GraphList.h:358
iterator end() const
Returns an iterator to the one-past-last element in the container.
Definition: GraphList.h:387
T * head() const
Returns the first element in the list.
Definition: GraphList.h:269
void insertBefore(GraphElement *pX, GraphElement *pY)
Inserts element pX before element pY.
Definition: GraphList.h:113
void reverse()
Reverses the order of the list elements.
Definition: GraphList.h:155
void clear()
Removes all elements from the list and deletes them.
Definition: GraphList.h:331
Direction
Definition: basic.h:132
Base class for GraphElement lists.
Definition: GraphList.h:69
void pushBack(GraphElement *pX)
Adds element pX at the end of the list.
Definition: GraphList.h:92
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:314
void swap(T *pX, T *pY)
Exchanges the positions of pX and pY in the list.
Definition: GraphList.h:352
#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:80
bool empty() const
Returns true iff the list is empty.
Definition: GraphList.h:275
void del(GraphElement *pX)
Removes element pX from the list.
Definition: GraphList.h:123
void delPure(T *pX)
Only removes element pX from the list; does not delete it.
Definition: GraphList.h:326
GraphElement * m_head
Pointer to the first element in the list.
Definition: GraphList.h:74
GraphElement * m_prev
The predecessor in the list.
Definition: GraphList.h:62
iterator rend() const
Returns an iterator to the one-before-first element in the container.
Definition: GraphList.h:393
GraphElement * m_tail
Pointer to the last element in the list.
Definition: GraphList.h:75
~GraphListBase()
Destruction.
Definition: GraphList.h:83
The namespace for all OGDF objects.
~GraphList()
Destruction: deletes all elements.
Definition: GraphList.h:260
Lists of graph objects (like nodes, edges, etc.).
Definition: GraphList.h:252
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:293
iterator rbegin() const
Returns an iterator to the last element in the container.
Definition: GraphList.h:390
Combinatorial embeddings of planar graphs with modification functionality.
#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:302
GraphList()
Constructs an empty list.
Definition: GraphList.h:257
void insertAfter(T *pX, T *pY)
Inserts element pX after element pY.
Definition: GraphList.h:283
Declaration of doubly linked lists and iterators.