Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

EList.h
Go to the documentation of this file.
1 
84 #pragma once
85 
86 namespace ogdf {
87 
88 // EStack forward declaration
89 template<typename S, typename E, E* S::*first, E* E::*next> class EStack;
90 // EListIterator forward declaration
91 template<typename E, E* E::*prev, E* E::*next> class EListIterator;
92 // EList forward declaration
93 template<typename L, typename E, int L::*numElem, E* L::*first, E* L::*last, E* E::*next, E* E::*prev> class EList;
94 
96 template<typename S, typename E, E* S::*first, E* E::*next>
97 class EStack
98 {
99 public:
101  static inline void init(S* pStack)
102  {
103  pStack->*first = 0;
104  }
105 
107  static inline void pop(S* pStack)
108  {
109  pStack->*first = pStack->*first->*next;
110  }
111 
113  static inline E* popRet(S* pStack)
114  {
115  E* res = pStack->*first;
116  pStack->*first = pStack->*first->*next;
117  return res;
118  }
119 
121  static inline void push(S* pStack, E* pElem)
122  {
123  pElem->*next = pStack->*first;
124  pStack->*first = pElem;
125  }
126 
128  static inline E* top(const S* pStack)
129  {
130  return pStack->*first;
131  }
132 
134  static inline bool empty(const S* pStack)
135  {
136  return pStack->*first == 0;
137  }
138 };
139 
140 
142 template<typename E, E* E::*prev, E* E::*next>
143 class EListIterator
144 {
145 public:
147  EListIterator( ) : m_ptr(nullptr) {}
148 
150  explicit EListIterator(E* ptr) : m_ptr(ptr) {}
151 
153  EListIterator(const EListIterator<E, prev, next>& other) : m_ptr(other.m_ptr) {}
154 
156  inline bool valid() const { return m_ptr != nullptr; }
157 
159  inline bool operator==(const EListIterator<E, prev, next>& other) const { return m_ptr == other.m_ptr; }
160 
162  inline bool operator!=(const EListIterator<E, prev, next>& other) const { return m_ptr != other.m_ptr; }
163 
165  inline EListIterator<E, prev, next> succ() const { return m_ptr->*next; }
166 
168  inline EListIterator<E, prev, next> pred() const { return m_ptr->*prev; }
169 
170 #if 0
171  E& operator*() const { return *m_ptr; }
173 #endif
174 
176  E* operator*() const { return m_ptr; }
177 
180  {
181  m_ptr = other.m_ptr;
182  return *this;
183  }
184 
187  {
188  m_ptr = m_ptr->*next;
189  return *this;
190  }
191 
194  {
195  EListIterator<E, prev, next> it = *this;
196  m_ptr = m_ptr->*next;
197  return it;
198  }
199 
202  {
203  m_ptr = m_ptr->*prev;
204  return *this;
205  }
206 
209  {
210  EListIterator<E, prev, next> it = *this;
211  m_ptr = m_ptr->*prev;
212  return it;
213  }
214 
215 private:
217  E* m_ptr;
218 };
219 
220 
222 template<
223  typename L,
224  typename E,
225  int L::*numElem,
226  E* L::*first,
227  E* L::*last,
228  E* E::*next,
229  E* E::*prev
230 >
231 class EList
232 {
233 public:
236 
238  static inline void init(L* pList)
239  {
240  pList->*first = 0;
241  pList->*last = 0;
242  pList->*numElem = 0;
243  }
244 
246  static inline int size(const L* pList) { return pList->*numElem; }
247 
249  static inline bool empty(const L* pList) { return pList->*first == 0; }
250 
252  static inline E* front(const L* pList) { return pList->*first; }
253 
255  static inline E* back(const L* pList) { return pList->*last; }
256 
258  static inline iterator pushBack(L* pList, E* elem)
259  {
260  elem->*next = 0;
261  elem->*prev = pList->*last;
262  if (pList->*last)
263  pList->*last = pList->*last->*next = elem;
264  else
265  pList->*last = pList->*first = elem;
266 
267  (pList->*numElem)++;
268  return iterator(elem);
269  }
270 
272  static inline iterator pushFront(L* pList, E* elem)
273  {
274  elem->*next = pList->*first;
275  elem->*prev = 0;
276 
277  if (pList->*first)
278  pList->*first = pList->*first->*prev = elem;
279  else
280  pList->*first = pList->*last = elem;
281 
282  (pList->*numElem)++;
283 
284  return iterator(elem);
285  }
286 
288  static inline iterator insertBefore(L* pList, E* elem, E* pNext)
289  {
290  E* pPrev;
291 
292  if (pNext)
293  pPrev = pNext->*prev;
294  else
295  pPrev = 0;
296 
297  elem->*next = pNext;
298  elem->*prev = pPrev;
299 
300  if (pNext)
301  pNext->*prev = elem;
302  else
303  pList->*last = elem;
304  if (pPrev)
305  pPrev->*next = elem;
306  else
307  pList->*first = elem;
308 
309  (pList->*numElem)++;
310  return iterator(elem);
311  }
312 
314  static inline iterator insertBefore(L* pList, E* elem, const iterator& itNext)
315  {
316  return insertBefore(pList, elem, (E*)(*itNext));
317  }
318 
320  static inline iterator insertAfter(L* pList, E* elem, E* pPrev)
321  {
322  E* pNext;
323 
324  if (pPrev)
325  pNext = pPrev->*next;
326  else
327  pNext = 0;
328 
329  elem->*next = pNext;
330  elem->*prev = pPrev;
331 
332  if (pNext)
333  pNext->*prev = elem;
334  else
335  pList->*last = elem;
336  if (pPrev)
337  pPrev->*next = elem;
338  else
339  pList->*first = elem;
340 
341  (pList->*numElem)++;
342  return iterator(elem);
343  }
344 
346  static inline iterator insertAfter(L* pList, E* elem, const iterator& itPrev)
347  {
348  return insertAfter(pList, elem, (E*)(*itPrev));
349  }
350 
352  inline static void popFront(L* pList)
353  {
354  if (front(pList))
355  remove(pList, front(pList));
356  }
357 
359  inline static void popBack(L* pList)
360  {
361  if (back(pList))
362  remove(pList, back(pList));
363  }
364 
366  static inline iterator remove(L* pList, E* elem)
367  {
368  E* pPrev = elem->*prev;
369  E* pNext = elem->*next;
370  if (pPrev)
371  pPrev->*next = pNext;
372  else
373  pList->*first = pNext;
374  if (pNext)
375  pNext->*prev = pPrev;
376  else
377  pList->*last = pPrev;
378 
379  (pList->*numElem)--;
380  return iterator(pNext);
381  }
382 
384  static inline iterator remove(L* pList, const iterator& it)
385  {
386  return remove(pList, (E*)(*it));
387  }
388 
389  template<
390  typename L_other,
391  E* L_other::*other_first,
392  E* L_other::*other_last,
393  int L_other::*other_numElem
394  >
395  inline static void appendFrom( L* pList, L_other* pListOther )
396  {
397  if (!pListOther->*other_first)
398  return;
399 
400  if (empty(pList))
401  {
402  pList->*first = pListOther->*other_first;
403  pList->*last = pListOther->*other_last;
404  } else
405  {
406  // link the pList->last element to other->first
407  pList->*last->*next = pListOther->*other_first;
408  pListOther->*other_first->*prev = pList->*last;
409 
410  // link the pList->last element to other->first
411  pList->*last = pListOther->*other_first;
412  }
413 
414  // add the size of the other pList
415  pList->*numElem += pListOther->*other_numElem;
416  // clear other pList
417  pListOther->*other_numElem = 0;
418  pListOther->*other_first = 0;
419  pListOther->*other_last = 0;
420  }
421 
423  static inline iterator begin(const L* pList) { return iterator(pList->*first); }
424 
426  static inline iterator end(const L* pList) { return iterator(); }
427 
429  static inline iterator rbegin(const L* pList) { return iterator(pList->*last); }
430 
432  static inline iterator rend(const L* pList) { return iterator(); }
433 
434  template<typename Func>
435  static inline void forall(const L* pList, const Func& func)
436  {
437  for(iterator it = begin(pList);it.valid();it++)
438  {
439  func(*it);
440  }
441  }
442 
443  template<typename A1>
444  static inline void forall_call(const L* pList, void (E::*func)( A1 ), const A1& a1)
445  {
446  for(iterator it = begin(pList);it.valid();it++)
447  {
448  ((*it)->*func)(a1);
449  }
450  }
451 
453  inline EList(L* pList) : m_pList(pList) { }
454 
455  inline void init() { init(m_pList); }
456 
457  inline int size() const { return size(m_pList); }
458  inline bool empty() const { return empty(m_pList); }
459 
460  inline E* front() const { return front(m_pList); }
461  inline E* back() const { return back(m_pList); }
462 
463  inline iterator pushBack(E* elem) { return pushBack(m_pList, elem); }
464  inline iterator pushFront(E* elem) { return pushFront(m_pList, elem); }
465 
466  inline iterator insertBefore(E* elem, E* pNext) { return insertBefore(m_pList, elem, pNext); }
467  inline iterator insertBefore(E* elem, const iterator& it) { return insertBefore(m_pList, elem, it); }
468 
469  inline iterator insertAfter(E* elem, E* pPrev) { return insertAfter(m_pList, elem, pPrev); } ;
470  inline iterator insertAfter(E* elem, const iterator& it) { return insertAfter(m_pList, elem, it); } ;
471 
472  inline void popFront() { popFront(m_pList); }
473  inline void popBack() { popBack(m_pList); }
474 
475  void operator<<(E* elem) { pushBack(elem); }
476 
477  inline iterator remove(E* elem) { return remove(m_pList, elem); }
478  inline iterator remove(const iterator& it) { return remove(m_pList, (E*)(*it)); }
479 
480  inline iterator begin() const { return begin(m_pList); }
481  inline iterator end() const { return end(m_pList); }
482  inline iterator rbegin() const { return rbegin(m_pList); }
483  inline iterator rend() const { return rend(m_pList); }
484 
485 private:
487 };
488 
489 
490 } // end of namespace ogdf
iterator rbegin() const
Definition: EList.h:482
static E * front(const L *pList)
Returns a pointer to the first element.
Definition: EList.h:252
void popFront()
Definition: EList.h:472
int size() const
Definition: EList.h:457
iterator insertAfter(E *elem, E *pPrev)
Definition: EList.h:469
The embedded list template.
Definition: EList.h:93
EListIterator< E, prev, next > pred() const
Returns predecessor iterator.
Definition: EList.h:168
void popBack()
Definition: EList.h:473
iterator rend() const
Definition: EList.h:483
EListIterator< E, prev, next > & operator--()
Decrement operator (prefix).
Definition: EList.h:201
static iterator rbegin(const L *pList)
Returns a reverse iterator pointing at the last element of pList.
Definition: EList.h:429
static bool empty(const L *pList)
Returns true if /a pList is empty.
Definition: EList.h:249
L * m_pList
Definition: EList.h:486
static void forall(const L *pList, const Func &func)
Definition: EList.h:435
EListIterator< E, prev, next > & operator=(const EListIterator< E, prev, next > &other)
Assignment operator.
Definition: EList.h:179
static void popFront(L *pList)
Removes the first element of pList.
Definition: EList.h:352
EListIterator< E, prev, next > & operator++()
Increment operator (prefix).
Definition: EList.h:186
void init()
Definition: EList.h:455
iterator insertAfter(E *elem, const iterator &it)
Definition: EList.h:470
static iterator pushBack(L *pList, E *elem)
Appends the element elem to the end of pList.
Definition: EList.h:258
static iterator insertBefore(L *pList, E *elem, const iterator &itNext)
Inserts elem into pList before position itNext.
Definition: EList.h:314
EListIterator< E, prev, next > succ() const
Returns successor iterator.
Definition: EList.h:165
static iterator pushFront(L *pList, E *elem)
Adds element elem at the begin of the list.
Definition: EList.h:272
EListIterator(const EListIterator< E, prev, next > &other)
constructs an iterator pointing at the same element as other
Definition: EList.h:153
EListIterator< E, prev, next > operator++(int)
Increment operator (postfix).
Definition: EList.h:193
static void forall_call(const L *pList, void(E::*func)(A1), const A1 &a1)
Definition: EList.h:444
static void pop(S *pStack)
Removes the top element from pStack without returning it.
Definition: EList.h:107
static void init(S *pStack)
Initializes pStack as empty stack.
Definition: EList.h:101
void operator<<(E *elem)
Definition: EList.h:475
bool empty() const
Definition: EList.h:458
E * m_ptr
The pointer to the element the iterator is pointing at.
Definition: EList.h:217
static E * back(const L *pList)
Returns a pointer to the last element.
Definition: EList.h:255
static iterator insertBefore(L *pList, E *elem, E *pNext)
Inserts elem into pList before pNext.
Definition: EList.h:288
static void init(L *pList)
Initializes pList as an empty embedded list.
Definition: EList.h:238
static E * popRet(S *pStack)
Removes the top element and returns it.
Definition: EList.h:113
EList(L *pList)
Constructor.
Definition: EList.h:453
static E * top(const S *pStack)
Returns a pointer to the top element of pStack.
Definition: EList.h:128
E * front() const
Definition: EList.h:460
Implementation of an embedded list iterator used by ogdf::EList.
Definition: EList.h:91
bool valid() const
returns false if the iterator points at nullptr
Definition: EList.h:156
The embedded stack class template.
Definition: EList.h:89
static iterator rend(const L *pList)
Returns a reverse iterator pointing at nullptr.
Definition: EList.h:432
EListIterator()
Constructs an iterator pointing at nullptr.
Definition: EList.h:147
static void popBack(L *pList)
Removes the last element of pList.
Definition: EList.h:359
static bool empty(const S *pStack)
Returns true if pStack is empty.
Definition: EList.h:134
iterator insertBefore(E *elem, const iterator &it)
Definition: EList.h:467
bool operator==(const EListIterator< E, prev, next > &other) const
Equality operator.
Definition: EList.h:159
iterator begin() const
Definition: EList.h:480
iterator pushBack(E *elem)
Definition: EList.h:463
static iterator begin(const L *pList)
Returns an iterator pointing at the first element of pList.
Definition: EList.h:423
iterator end() const
Definition: EList.h:481
static iterator end(const L *pList)
Returns an iterator pointing at nullptr.
Definition: EList.h:426
static iterator insertAfter(L *pList, E *elem, const iterator &itPrev)
Inserts elem into pList after position itPrev.
Definition: EList.h:346
E * back() const
Definition: EList.h:461
static void appendFrom(L *pList, L_other *pListOther)
Definition: EList.h:395
static void push(S *pStack, E *pElem)
Pushes the new element pElem onto pStack.
Definition: EList.h:121
iterator pushFront(E *elem)
Definition: EList.h:464
The namespace for all OGDF objects.
iterator insertBefore(E *elem, E *pNext)
Definition: EList.h:466
bool operator!=(const EListIterator< E, prev, next > &other) const
Inequality operator.
Definition: EList.h:162
static int size(const L *pList)
Returns the number of elements in this embedded list by reading the numElem var.
Definition: EList.h:246
EListIterator(E *ptr)
Constructs an iterator pointing at ptr.
Definition: EList.h:150
E * operator*() const
Returns a pointer to the element.
Definition: EList.h:176
static iterator insertAfter(L *pList, E *elem, E *pPrev)
Inserts elem into pList before pPrev.
Definition: EList.h:320
EListIterator< E, prev, next > operator--(int)
Decrement operator (postfix).
Definition: EList.h:208