Open
Graph Drawing
Framework

 v. 2020.02 (Catalpa)
 

SortedSequence.h
Go to the documentation of this file.
1 
32 #pragma once
33 
34 #include <random>
35 #include <ogdf/basic/comparer.h>
36 #include <ogdf/basic/memory.h>
37 #include <ogdf/basic/Reverse.h>
38 
39 namespace ogdf {
40 
41 template<class KEY, class INFO, class CMP, bool isConst, bool isReverse>
43 
44 template<class KEY, class INFO, class CMP>
46 
47 template<class KEY, class INFO, class CMP>
49 
50 template<class KEY, class INFO, class CMP>
52 
53 template<class KEY, class INFO, class CMP>
55 
57 
64 template<class KEY, class INFO, class CMP = StdComparer<KEY> >
66 
67  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,false,false>;
68  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,true,false>;
69  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,false,true>;
70  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,true,true>;
71 
73  struct Element {
74 
75  KEY m_key;
76  INFO m_info;
77  int m_height;
78 
81 
83  Element(const KEY &key, const INFO &info, int height)
84  : m_key(key), m_info(info), m_height(height)
85  {
86  m_next = (Element**)malloc(height * sizeof(Element*));
87  m_prev = (Element**)malloc(height * sizeof(Element*));
88  }
89 
91 
95  Element(int height) : m_height(0)
96  {
97  m_next = (Element**)malloc(height * sizeof(Element*));
98  m_prev = (Element**)malloc(height * sizeof(Element*));
99  }
100 
101  Element(const Element &) = delete;
102 
105  free(m_prev);
106  free(m_next);
107  }
108 
110  void grow(int newHeight) {
111  Element **p = static_cast<Element**>( realloc(m_next, newHeight * sizeof(Element*)) );
112  if (p == nullptr) OGDF_THROW(InsufficientMemoryException);
113  m_next = p;
114 
115  p = static_cast<Element**>(realloc(m_prev, newHeight * sizeof(Element*)));
116  if (p == nullptr) OGDF_THROW(InsufficientMemoryException);
117  m_prev = p;
118  }
119 
121  };
122 
123 public:
124 
133 
135  SortedSequence(const CMP &comparer = CMP()) : m_comparer(comparer), m_rng(randomSeed()), m_randomBits(0,1) {
136  initEmpty();
137  }
138 
140  SortedSequence(std::initializer_list < std::pair < KEY, INFO >> initList);
141 
144 
146 
150 
151 
154  clear();
155  delete m_dummy;
156  }
157 
158 
160 
166 
168  int size() const { return m_size; }
169 
171  bool empty() const { return m_size == 0; }
172 
174  iterator begin();
175 
177  const_iterator begin() const;
178 
180  const_iterator cbegin() const { return begin(); }
181 
183  iterator end();
184 
186  const_iterator end() const;
187 
189  const_iterator cend() const { return end(); }
190 
193 
196 
198  const_reverse_iterator crbegin() const { return rbegin(); }
199 
202 
205 
207  const_reverse_iterator crend() const { return rend(); }
208 
209 
215 
217  iterator lookup(const KEY &key);
218 
220  const_iterator lookup(const KEY &key) const;
221 
223  iterator locate(const KEY &key);
224 
226  const_iterator locate(const KEY &key) const;
227 
229 
232  iterator minItem() { return begin(); }
233 
235 
238  const_iterator minItem() const { return begin(); }
239 
241 
245 
247 
250  const_reverse_iterator maxItem() const { return rbegin(); }
251 
252 
254 
259 
261  iterator insert(const KEY &key, const INFO &info);
262 
264  void del(const KEY &key);
265 
267  void delItem(iterator it);
268 
270  void clear() {
271  Element* item = m_dummy->m_next[0];
272  while(item != m_dummy) {
273  Element* old = item;
274  item = item->m_next[0];
275  delete old;
276  }
277  m_size = 0;
278  m_height = 1;
279  m_dummy->m_next[0] = m_dummy->m_prev[0] = m_dummy;
280  }
281 
283 
288 
291 
293 
297 
299 
303 
305 
309  return !operator==(S);
310  }
311 
313 
318 
320 
325  iterator insertAfter(iterator it, const KEY &key, const INFO &info);
326 
328 
338  void reverseItems(iterator itBegin, iterator itEnd) {
339  reverseElements(itBegin.m_pElement, itEnd.m_pElement);
340  }
341 
343 
344 private:
346  int m_size;
348  int m_height;
350 
351  std::minstd_rand m_rng;
352  std::uniform_int_distribution<> m_randomBits;
353 
354 
355  void initEmpty() {
356  m_size = 0;
357  m_realHeight = 5;
358  m_height = 1;
359 
360  m_dummy = new Element(m_realHeight);
361  m_dummy->m_next[0] = m_dummy;
362  m_dummy->m_prev[0] = m_dummy;
363  }
364 
365  int randomHeightAndGrow();
366  void grow(int newHeight);
367 
368  const Element *_lookup(const KEY &key) const;
369  const Element *_locate(const KEY &key) const;
370 
371  void removeElement(Element *p);
373  void reverseElements(Element *p, Element *q);
374 };
375 
376 
378 template<class KEY, class INFO, class CMP, bool isConst, bool isReverse>
380  friend class SortedSequence<KEY,INFO,CMP>;
381  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,!isConst,isReverse>;
382  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,isConst,!isReverse>;
383  friend class SortedSequenceIteratorBase<KEY,INFO,CMP,!isConst,!isReverse>;
384 
385  using Element = typename std::conditional<isConst,
387  typename SortedSequence<KEY,INFO,CMP>::Element>::type;
388  Element *m_pElement;
389 
391  SortedSequenceIteratorBase(Element *pElement) : m_pElement(pElement) { }
392 
393 public:
395  SortedSequenceIteratorBase() : m_pElement(nullptr) { }
396 
400  : m_pElement(it.m_pElement) { }
401 
403  // gcc9 complains since it cannot match the templated constructor above (-Werror=deprecated-copy).
405  : m_pElement(it.m_pElement) { }
406 
408  const KEY &key() const {
409  OGDF_ASSERT(valid());
410  return m_pElement->m_key;
411  }
412 
415  OGDF_ASSERT(valid());
416  return m_pElement->m_info;
417  }
418 
420  bool valid() const { return m_pElement != nullptr; }
421 
424  m_pElement = isReverse ? predElement() : succElement();
425  return *this;
426  }
427 
431  m_pElement = isReverse ? predElement() : succElement();
432  return it;
433  }
434 
437  m_pElement = isReverse ? succElement() : predElement();
438  return *this;
439  }
440 
444  m_pElement = isReverse ? succElement() : predElement();
445  return it;
446  }
447 
450  return isReverse ? predElement() : succElement();
451  }
452 
455  return isReverse ? succElement() : predElement();
456  }
457 
460  m_pElement = it.m_pElement;
461  return *this;
462  }
463 
466  return m_pElement == it.m_pElement;
467  }
468 
471  return m_pElement != it.m_pElement;
472  }
473 
474 private:
475  typename SortedSequence<KEY,INFO,CMP>::Element *succElement() const {
476  OGDF_ASSERT(valid());
477  return (m_pElement->m_next[0]->m_height > 0) ? m_pElement->m_next[0] : nullptr;
478  }
479 
480  typename SortedSequence<KEY,INFO,CMP>::Element *predElement() const {
481  OGDF_ASSERT(valid());
482  return (m_pElement->m_prev[0]->m_height > 0) ? m_pElement->m_prev[0] : nullptr;
483  }
484 };
485 
486 template<class KEY, class INFO, class CMP>
487 SortedSequence<KEY, INFO, CMP>::SortedSequence(std::initializer_list < std::pair < KEY, INFO >> initList)
488  : SortedSequence()
489 {
490  for (const auto &p : initList)
491  insert(p.first, p.second);
492 }
493 
494 
495 template<class KEY, class INFO, class CMP>
498 {
499  initEmpty();
500 
501  iterator it = m_dummy;
502  for(Element *pS = S.m_dummy->m_next[0]; pS != S.m_dummy; pS = pS->m_next[0])
503  it = insertAfter(it, pS->m_key, pS->m_info);
504 }
505 
506 
507 template<class KEY, class INFO, class CMP>
510 {
511  // move all elements to this sequence
512  m_dummy = S.m_dummy;
513 
514  // initalize S with an empty sequence
515  S.initEmpty();
516 }
517 
518 
519 template<class KEY, class INFO, class CMP>
521 {
522  clear();
523 
524  iterator it = m_dummy;
525  for(Element *pS = S.m_dummy->m_next[0]; pS != S.m_dummy; pS = pS->m_next[0])
526  it = insertAfter(it, pS->m_key, pS->m_info);
527 
528  return *this;
529 }
530 
531 
532 template<class KEY, class INFO, class CMP>
534 {
535  // clear this sequence
536  Element* item = m_dummy->m_next[0];
537  while(item != m_dummy) {
538  Element* old = item;
539  item = item->m_next[0];
540  delete old;
541  }
542  delete m_dummy;
543 
544  // move elements from S to this sequence
545  m_comparer = S.m_comparer;
546  m_size = S.m_size;
547  m_height = S.m_height;
548  m_realHeight = S.m_realHeight;
549  m_dummy = S.m_dummy;
550 
551  // make S the empty sequence
552  S.initEmpty();
553 
554  return *this;
555 }
556 
557 
558 template<class KEY, class INFO, class CMP>
560 {
561  if(m_size != S.m_size)
562  return false;
563 
564  Element *p = m_dummy->m_next[0], *pS = S.m_dummy->m_next[0];
565  while(p != m_dummy) {
566  if (!m_comparer.equal(p->m_key,pS->m_key))
567  return false;
568  p = p ->m_next[0];
569  pS = pS->m_next[0];
570  }
571 
572  return true;
573 }
574 
575 
576 template<class KEY, class INFO, class CMP>
578 {
579  int h = m_height - 1;
580  Element** pElement = m_dummy->m_next;
581 
582  while(true) {
583  if( pElement[h] != m_dummy && m_comparer.less(pElement[h]->m_key, key) )
584  pElement = pElement[h]->m_next;
585  else if(--h < 0) {
586  if( pElement[0] != m_dummy && m_comparer.equal(pElement[0]->m_key, key) )
587  return pElement[0];
588  return nullptr;
589  }
590  }
591 }
592 
593 template<class KEY, class INFO, class CMP>
595 {
596  return iterator(const_cast<Element *>(_lookup(key)));
597 }
598 
599 template<class KEY, class INFO, class CMP>
601 {
602  return const_iterator(_lookup(key));
603 }
604 
605 
606 template<class KEY, class INFO, class CMP>
608 {
609  int h = m_height - 1;
610  Element** pElement = m_dummy->m_next;
611 
612  while(true) {
613  if( pElement[h] != m_dummy && m_comparer.less(pElement[h]->m_key, key) )
614  pElement = pElement[h]->m_next;
615  else if(--h < 0) {
616  Element *p = (pElement[0] != m_dummy) ? pElement[0] : nullptr;
617  return p;
618  }
619  }
620 }
621 
622 template<class KEY, class INFO, class CMP>
624 {
625  return iterator(const_cast<Element *>(_locate(key)));
626 }
627 
628 template<class KEY, class INFO, class CMP>
630 {
631  return const_iterator(_locate(key));
632 }
633 
634 
635 template<class KEY, class INFO, class CMP>
637 {
638  if(newHeight > m_realHeight) {
639  m_realHeight = newHeight;
640  m_dummy->grow(newHeight);
641  }
642 
643  for(int i = newHeight; i-- > m_height; ) {
644  m_dummy->m_next[i] = m_dummy;
645  m_dummy->m_prev[i] = m_dummy;
646  }
647  m_height = newHeight;
648 }
649 
650 template<class KEY, class INFO, class CMP>
652 {
653  int h = 1;
654  while(m_randomBits(m_rng) == 1)
655  h++;
656 
657  if(h > m_height)
658  grow(h);
659 
660  return h;
661 }
662 
663 
664 template<class KEY, class INFO, class CMP>
666 {
667  int h = m_height - 1;
668  Element *pCurrent = m_dummy;
669 
670  while(true) {
671  if( pCurrent->m_next[h] != m_dummy && m_comparer.less(pCurrent->m_next[h]->m_key, key) ) {
672  pCurrent = pCurrent->m_next[h];
673 
674  } else {
675  if(--h < 0) {
676  // found element?
677  if( pCurrent->m_next[0] != m_dummy && m_comparer.equal(pCurrent->m_next[0]->m_key, key) ) {
678  pCurrent->m_next[0]->m_info = info;
679  return iterator(pCurrent->m_next[0]);
680  }
681  break;
682  }
683  }
684  }
685 
686  // add new element (key,inf)
687  m_size++;
688 
689  int nh = randomHeightAndGrow();
690 
691  Element* pNewElement = new Element(key, info, nh);
692  insertElementAfterElement(pNewElement, pCurrent);
693 
694  return iterator(pNewElement);
695 }
696 
697 
698 template<class KEY, class INFO, class CMP>
700 {
701  iterator it = lookup(key);
702  if(it.valid())
703  delItem(it);
704 }
705 
706 
707 template<class KEY, class INFO, class CMP>
709 {
710  m_size++;
711 
712  int nh = randomHeightAndGrow();
713 
714  Element* pNewElement = new Element(key, info, nh);
715  insertElementAfterElement(pNewElement, it.m_pElement);
716 
717  return pNewElement;
718 }
719 
720 
721 template<class KEY, class INFO, class CMP>
723 {
725 
726  for(int h = 0; h < p->m_height; ++h) {
727  while(q != m_dummy && q->m_height <= h)
728  q = q->m_prev[h-1];
729 
730  Element *r = q->m_next[h];
731  p->m_next[h] = r;
732  p->m_prev[h] = q;
733  q->m_next[h] = r->m_prev[h] = p;
734  }
735 }
736 
737 
738 template<class KEY, class INFO, class CMP>
740 {
741  while(p != q) {
742  Element *r = p;
743  p = p->m_next[0];
744  removeElement(r);
746  }
747 }
748 
749 
750 template<class KEY, class INFO, class CMP>
752 {
753  OGDF_ASSERT(p != nullptr);
754  OGDF_ASSERT(p != m_dummy);
755 
756  for(int h = 0; h < p->m_height; ++h) {
757  Element *pPred = p->m_prev[h], *pSucc = p->m_next[h];
758  pPred->m_next[h] = pSucc;
759  pSucc->m_prev[h] = pPred;
760  }
761 }
762 
763 
764 template<class KEY, class INFO, class CMP>
766 {
767  Element *p = it.m_pElement;
768  removeElement(p);
769 
770  m_size--;
771  delete p;
772 }
773 
774 
775 template<class KEY, class INFO, class CMP>
778 {
779  return (m_dummy->m_next[0] != m_dummy) ? m_dummy->m_next[0] : nullptr;
780 }
781 
782 template<class KEY, class INFO, class CMP>
785 {
786  return (m_dummy->m_next[0] != m_dummy) ? m_dummy->m_next[0] : 0;
787 }
788 
789 
790 template<class KEY, class INFO, class CMP>
793 {
795 }
796 
797 template<class KEY, class INFO, class CMP>
800 {
802 }
803 
804 
805 template<class KEY, class INFO, class CMP>
808 {
809  return (m_dummy->m_prev[0] != m_dummy) ? m_dummy->m_prev[0] : 0;
810 }
811 
812 template<class KEY, class INFO, class CMP>
815 {
816  return (m_dummy->m_prev[0] != m_dummy) ? m_dummy->m_prev[0] : 0;
817 }
818 
819 
820 template<class KEY, class INFO, class CMP>
823 {
825 }
826 
827 template<class KEY, class INFO, class CMP>
830 {
832 }
833 
834 }
void removeElement(Element *p)
iterator end()
Returns an iterator pointing to one past the last element.
Maintains a sequence of (key,info) pairs sorted by key.
Declarations for Comparer objects.
iterator begin()
Returns an iterator pointing to the first element.
Element(int height)
Creates a dummy (stop) element with given height.
Iterators for sorted sequences.
SortedSequence(const CMP &comparer=CMP())
Constructs an initially empty sorted sequence.
Declaration of memory manager for allocating small pieces of memory.
int m_size
number of elements stored in the sequence.
const_iterator cend() const
Returns a const-iterator pointing to one past the last element.
bool empty() const
Returns true if the sequence is empty, false otherwise.
const_iterator minItem() const
Returns a const-iterator to the element with minimal key if the sequence is not empty, an invalid const-iterator otherwise.
std::minstd_rand m_rng
Random number generator.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > & operator--()
Moves the iterator one item backward (prefix notation)
typename std::conditional< isConst, const typename SortedSequence< KEY, INFO, CMP >::Element, typename SortedSequence< KEY, INFO, CMP >::Element >::type Element
Element * m_dummy
dummy element representing the head and tail of the skiplist.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > & operator++()
Move the iterator one item forward (prefix notation)
iterator insertAfter(iterator it, const KEY &key, const INFO &info)
Adds a new element <key, info> after element it.
#define OGDF_THROW(CLASS)
Replacement for throw.
Definition: exceptions.h:65
const_reverse_iterator maxItem() const
Returns a const reverse iterator to the element with maximal key if the sequence is not empty...
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > & operator=(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it)
Assignment operator.
iterator locate(const KEY &key)
Returns an iterator to the element < k1, i1 > such that k1 is minimal with k1 ≥ key, or a null iterator if no such element exists.
SortedSequence< KEY, INFO, CMP >::Element * succElement() const
bool valid() const
Returns true if the iterator points to an element.
void clear()
Removes all elements from the sorted sequence.
std::conditional< isConst, const INFO, INFO >::type & info() const
Returns the info of the sequence element pointed to.
iterator minItem()
Returns an iterator to the element with minimal key if the sequence is not empty, an invalid iterator...
const Element * _lookup(const KEY &key) const
SortedSequenceIteratorBase(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it)
Copy constructor.
SortedSequence< KEY, INFO, CMP >::Element * predElement() const
int m_realHeight
actual height of head/tail arrays.
Element(const KEY &key, const INFO &info, int height)
Creates a skiplist element for(key,info) and given height.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > operator++(int)
Moves the iterator one item forward (postfix notation)
const_reverse_iterator crend() const
Returns a const reverse iterator pointing to one before the first element.
SortedSequenceConstIterator< KEY, INFO, CMP > const_iterator
The const-iterator type for sorted sequences (bidirectional iterator).
iterator lookup(const KEY &key)
Returns an iterator to the element with key key, or a null iterator if no such element exists...
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > operator--(int)
Moves the iterator one item backward (postfix notation)
reverse_iterator rbegin()
Returns a reverse iterator pointing to the last element.
Implementation of the Reverse class for the reverse iteration of containers.
bool operator==(const SortedSequence< KEY, INFO, CMP > &S)
Returns true if the keys stored in this sequence equal the keys in S, false otherwise.
const KEY & key() const
Returns the key of the sequence element pointed to.
SortedSequenceIteratorBase()
Creates an invalid (null-) iterator.
iterator insert(const KEY &key, const INFO &info)
Updates information for key if contained in sequence, or adds a new element <key, info>...
int m_height
current height of head/tail.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > pred() const
Returns an iterator pointing to the previous element in the sequence.
bool operator!=(const SortedSequence< KEY, INFO, CMP > &S)
Returns false if the keys stored in this sequence equal the keys in S, true otherwise.
Exception thrown when not enough memory is available to execute an algorithm.
Definition: exceptions.h:213
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
Element ** m_prev
array of predecessor elements.
Element ** m_next
array of successor elements.
void reverseElements(Element *p, Element *q)
const_reverse_iterator crbegin() const
Returns a const reverse iterator pointing to the last element.
void insertElementAfterElement(Element *p, Element *q)
bool operator==(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it) const
Equality operator.
void del(const KEY &key)
Removes the element with key key (if such an element exists).
bool operator!=(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it) const
Inequality operator.
int size() const
Returns the current size of the sequence, i.e., the number of stored elements.
SortedSequenceIteratorBase(const SortedSequenceIteratorBase< KEY, INFO, CMP, isArgConst, isArgReverse > &it)
Copy constructor.
void grow(int newHeight)
Increases the element&#39;s height to newHeight.
KEY m_key
stores the key.
reverse_iterator rend()
Returns a reverse iterator pointing to one before the first element.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > succ() const
Returns an iterator pointing to the next element in the sequence.
void grow(int newHeight)
SortedSequenceIterator< KEY, INFO, CMP > iterator
The iterator type for sorted sequences (bidirectional iterator).
INFO m_info
stores the associated information.
int m_height
the height of the skiplist element.
const Element * _locate(const KEY &key) const
reverse_iterator maxItem()
Returns a reverse iterator to the element with maximal key if the sequence is not empty...
The namespace for all OGDF objects.
SortedSequence< KEY, INFO, CMP > & operator=(const SortedSequence< KEY, INFO, CMP > &S)
Assignment operator.
SortedSequenceIteratorBase(Element *pElement)
Creates an iterator pointing to pElement.
void reverseItems(iterator itBegin, iterator itEnd)
Reverses the items in the subsequence from itBegin to itEnd (inclusive).
Internal structure to hold the items and internal forward/backward pointers of the skiplist...
std::uniform_int_distribution m_randomBits
void delItem(iterator it)
Removes the element to which it points from the sequence.
const_iterator cbegin() const
Returns a const-iterator pointing to the first element.
~SortedSequence()
Destructor.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41