Open
Graph Drawing
Framework

 v. snapshot-2017-02-16
 

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 
38 namespace ogdf {
39 
40 template<class KEY, class INFO, class CMP>
42 
43 template<class KEY, class INFO, class CMP>
45 
47 
54 template<class KEY, class INFO, class CMP = StdComparer<KEY> >
56 
57  friend class SortedSequenceIterator<KEY,INFO,CMP>;
58  friend class SortedSequenceConstIterator<KEY,INFO,CMP>;
59 
61  struct Element {
62 
63  KEY m_key;
64  INFO m_info;
65  int m_height;
66 
69 
71  Element(const KEY &key, const INFO &info, int height)
72  : m_key(key), m_info(info), m_height(height)
73  {
74  m_next = (Element**)malloc(height * sizeof(Element*));
75  m_prev = (Element**)malloc(height * sizeof(Element*));
76  }
77 
79 
83  Element(int height) : m_height(0)
84  {
85  m_next = (Element**)malloc(height * sizeof(Element*));
86  m_prev = (Element**)malloc(height * sizeof(Element*));
87  }
88 
89  Element(const Element &) = delete;
90 
93  free(m_prev);
94  free(m_next);
95  }
96 
98  void grow(int newHeight) {
99  Element **p = static_cast<Element**>( realloc(m_next, newHeight * sizeof(Element*)) );
100  if (p == nullptr) OGDF_THROW(InsufficientMemoryException);
101  m_next = p;
102 
103  p = static_cast<Element**>(realloc(m_prev, newHeight * sizeof(Element*)));
104  if (p == nullptr) OGDF_THROW(InsufficientMemoryException);
105  m_prev = p;
106  }
107 
109  };
110 
111 public:
112 
117 
119  SortedSequence(const CMP &comparer = CMP()) : m_comparer(comparer), m_rng(randomSeed()), m_randomBits(0,1) {
120  initEmpty();
121  }
122 
124  SortedSequence(std::initializer_list < std::pair < KEY, INFO >> initList);
125 
128 
130 
134 
135 
138  clear();
139  delete m_dummy;
140  }
141 
142 
144 
150 
152  int size() const { return m_size; }
153 
155  int empty() const { return m_size == 0; }
156 
158  iterator begin();
159 
161  const_iterator begin() const;
162 
164  const_iterator cbegin() const { return begin(); }
165 
167  iterator end();
168 
170  const_iterator end() const;
171 
173  const_iterator cend() const { return end(); }
174 
176  iterator rbegin();
177 
179  const_iterator rbegin() const;
180 
182  const_iterator crbegin() const { return rbegin(); }
183 
185  iterator rend();
186 
188  const_iterator rend() const;
189 
191  const_iterator crend() const { return rend(); }
192 
193 
199 
201  iterator lookup(const KEY &key);
202 
204  const_iterator lookup(const KEY &key) const;
205 
207  iterator locate(const KEY &key);
208 
210  const_iterator locate(const KEY &key) const;
211 
213 
216  iterator minItem() { return begin(); }
217 
219 
222  const_iterator minItem() const { return begin(); }
223 
225 
228  iterator maxItem() { return rbegin(); }
229 
231 
234  const_iterator maxItem() const { return rbegin(); }
235 
236 
238 
243 
245  iterator insert(const KEY &key, const INFO &info);
246 
248  void del(const KEY &key);
249 
251  void delItem(iterator it);
252 
254  void clear() {
255  Element* item = m_dummy->m_next[0];
256  while(item != m_dummy) {
257  Element* old = item;
258  item = item->m_next[0];
259  delete old;
260  }
261  m_size = 0;
262  m_height = 1;
263  m_dummy->m_next[0] = m_dummy->m_prev[0] = m_dummy;
264  }
265 
267 
272 
275 
277 
281 
283 
287 
289 
293  return !operator==(S);
294  }
295 
297 
302 
304 
309  iterator insertAfter(iterator it, const KEY &key, const INFO &info);
310 
312 
322  void reverseItems(iterator itBegin, iterator itEnd) {
323  reverseElements(itBegin.m_pElement, itEnd.m_pElement);
324  }
325 
327 
328 private:
330  int m_size;
332  int m_height;
334 
335  std::minstd_rand m_rng;
336  std::uniform_int_distribution<> m_randomBits;
337 
338 
339  void initEmpty() {
340  m_size = 0;
341  m_realHeight = 5;
342  m_height = 1;
343 
344  m_dummy = new Element(m_realHeight);
345  m_dummy->m_next[0] = m_dummy;
346  m_dummy->m_prev[0] = m_dummy;
347  }
348 
349  int randomHeightAndGrow();
350  void grow(int newHeight);
351 
352  const Element *_lookup(const KEY &key) const;
353  const Element *_locate(const KEY &key) const;
354 
355  void removeElement(Element *p);
357  void reverseElements(Element *p, Element *q);
358 };
359 
360 
362 template<class KEY, class INFO, class CMP>
364  friend class SortedSequence<KEY,INFO,CMP>;
365  friend class SortedSequenceConstIterator<KEY,INFO,CMP>;
366 
368 
371  : m_pElement(pElement) { }
372 
373 public:
375  SortedSequenceIterator() : m_pElement(nullptr) { }
376 
379  : m_pElement(it.m_pElement) { }
380 
382  const KEY &key() const { return m_pElement->m_key; }
383 
385  INFO &info() const { return m_pElement->m_info; }
386 
387 
389  bool valid() const { return m_pElement != nullptr; }
390 
393  m_pElement = succElement();
394  return *this;
395  }
396 
400  m_pElement = succElement();
401  return it;
402  }
403 
406  m_pElement = predElement();
407  return *this;
408  }
409 
413  m_pElement = predElement();
414  return it;
415  }
416 
419  return succElement();
420  }
421 
424  return predElement();
425  }
426 
429  m_pElement = it.m_pElement;
430  return *this;
431  }
432 
435  return m_pElement == it.m_pElement;
436  }
437 
440  return m_pElement != it.m_pElement;
441  }
442 
443 private:
445  return (m_pElement->m_next[0]->m_height > 0) ? m_pElement->m_next[0] : nullptr;
446  }
447 
449  return (m_pElement->m_prev[0]->m_height > 0) ? m_pElement->m_prev[0] : nullptr;
450  }
451 };
452 
453 
455 template<class KEY, class INFO, class CMP>
457  friend class SortedSequence<KEY,INFO,CMP>;
458 
460 
463  : m_pElement(pElement) { }
464 
465 public:
467  SortedSequenceConstIterator() : m_pElement(nullptr) { }
468 
471  : m_pElement(it.m_pElement) { }
472 
475  : m_pElement(it.m_pElement) { }
476 
478  const KEY &key() const { return m_pElement->m_key; }
479 
481  const INFO &info() const { return m_pElement->m_info; }
482 
483 
485  bool valid() const { return m_pElement != 0; }
486 
489  m_pElement = succElement();
490  return *this;
491  }
492 
496  m_pElement = succElement();
497  return it;
498  }
499 
502  m_pElement = predElement();
503  return *this;
504  }
505 
509  m_pElement = predElement();
510  return it;
511  }
512 
515  return succElement();
516  }
517 
520  return predElement();
521  }
522 
525  m_pElement = it.m_pElement;
526  return *this;
527  }
528 
531  return m_pElement == it.m_pElement;
532  }
533 
536  return m_pElement != it.m_pElement;
537  }
538 
539 private:
541  return (m_pElement->m_next[0]->m_height > 0) ? m_pElement->m_next[0] : 0;
542  }
543 
545  return (m_pElement->m_prev[0]->m_height > 0) ? m_pElement->m_prev[0] : 0;
546  }
547 };
548 
549 
550 template<class KEY, class INFO, class CMP>
551 SortedSequence<KEY, INFO, CMP>::SortedSequence(std::initializer_list < std::pair < KEY, INFO >> initList)
552  : SortedSequence()
553 {
554  for (const auto &p : initList)
555  insert(p.first, p.second);
556 }
557 
558 
559 template<class KEY, class INFO, class CMP>
562 {
563  initEmpty();
564 
565  iterator it = m_dummy;
566  for(Element *pS = S.m_dummy->m_next[0]; pS != S.m_dummy; pS = pS->m_next[0])
567  it = insertAfter(it, pS->m_key, pS->m_info);
568 }
569 
570 
571 template<class KEY, class INFO, class CMP>
574 {
575  // move all elements to this sequence
576  m_dummy = S.m_dummy;
577 
578  // initalize S with an empty sequence
579  S.initEmpty();
580 }
581 
582 
583 template<class KEY, class INFO, class CMP>
585 {
586  clear();
587 
588  iterator it = m_dummy;
589  for(Element *pS = S.m_dummy->m_next[0]; pS != S.m_dummy; pS = pS->m_next[0])
590  it = insertAfter(it, pS->m_key, pS->m_info);
591 
592  return *this;
593 }
594 
595 
596 template<class KEY, class INFO, class CMP>
598 {
599  // clear this sequence
600  Element* item = m_dummy->m_next[0];
601  while(item != m_dummy) {
602  Element* old = item;
603  item = item->m_next[0];
604  delete old;
605  }
606  delete m_dummy;
607 
608  // move elements from S to this sequence
609  m_comparer = S.m_comparer;
610  m_size = S.m_size;
611  m_height = S.m_height;
612  m_realHeight = S.m_realHeight;
613  m_dummy = S.m_dummy;
614 
615  // make S the empty sequence
616  S.initEmpty();
617 
618  return *this;
619 }
620 
621 
622 template<class KEY, class INFO, class CMP>
624 {
625  if(m_size != S.m_size)
626  return false;
627 
628  Element *p = m_dummy->m_next[0], *pS = S.m_dummy->m_next[0];
629  while(p != m_dummy) {
630  if(m_comparer.equal(p->m_key,pS->m_key) == false)
631  return false;
632  p = p ->m_next[0];
633  pS = pS->m_next[0];
634  }
635 
636  return true;
637 }
638 
639 
640 template<class KEY, class INFO, class CMP>
642 {
643  int h = m_height - 1;
644  Element** pElement = m_dummy->m_next;
645 
646  while(true) {
647  if( pElement[h] != m_dummy && m_comparer.less(pElement[h]->m_key, key) )
648  pElement = pElement[h]->m_next;
649  else if(--h < 0) {
650  if( pElement[0] != m_dummy && m_comparer.equal(pElement[0]->m_key, key) )
651  return pElement[0];
652  return nullptr;
653  }
654  }
655 }
656 
657 template<class KEY, class INFO, class CMP>
659 {
660  return iterator(const_cast<Element *>(_lookup(key)));
661 }
662 
663 template<class KEY, class INFO, class CMP>
665 {
666  return const_iterator(_lookup(key));
667 }
668 
669 
670 template<class KEY, class INFO, class CMP>
672 {
673  int h = m_height - 1;
674  Element** pElement = m_dummy->m_next;
675 
676  while(true) {
677  if( pElement[h] != m_dummy && m_comparer.less(pElement[h]->m_key, key) )
678  pElement = pElement[h]->m_next;
679  else if(--h < 0) {
680  Element *p = (pElement[0] != m_dummy) ? pElement[0] : nullptr;
681  return p;
682  }
683  }
684 }
685 
686 template<class KEY, class INFO, class CMP>
688 {
689  return iterator(const_cast<Element *>(_locate(key)));
690 }
691 
692 template<class KEY, class INFO, class CMP>
694 {
695  return const_iterator(_locate(key));
696 }
697 
698 
699 template<class KEY, class INFO, class CMP>
701 {
702  if(newHeight > m_realHeight) {
703  m_realHeight = newHeight;
704  m_dummy->grow(newHeight);
705  }
706 
707  for(int i = newHeight; i-- > m_height; ) {
708  m_dummy->m_next[i] = m_dummy;
709  m_dummy->m_prev[i] = m_dummy;
710  }
711  m_height = newHeight;
712 }
713 
714 template<class KEY, class INFO, class CMP>
716 {
717  int h = 1;
718  while(m_randomBits(m_rng) == 1)
719  h++;
720 
721  if(h > m_height)
722  grow(h);
723 
724  return h;
725 }
726 
727 
728 template<class KEY, class INFO, class CMP>
730 {
731  int h = m_height - 1;
732  Element *pCurrent = m_dummy;
733 
734  while(true) {
735  if( pCurrent->m_next[h] != m_dummy && m_comparer.less(pCurrent->m_next[h]->m_key, key) ) {
736  pCurrent = pCurrent->m_next[h];
737 
738  } else {
739  if(--h < 0) {
740  // found element?
741  if( pCurrent->m_next[0] != m_dummy && m_comparer.equal(pCurrent->m_next[0]->m_key, key) ) {
742  pCurrent->m_next[0]->m_info = info;
743  return iterator(pCurrent->m_next[0]);
744  }
745  break;
746  }
747  }
748  }
749 
750  // add new element (key,inf)
751  m_size++;
752 
753  int nh = randomHeightAndGrow();
754 
755  Element* pNewElement = new Element(key, info, nh);
756  insertElementAfterElement(pNewElement, pCurrent);
757 
758  return iterator(pNewElement);
759 }
760 
761 
762 template<class KEY, class INFO, class CMP>
764 {
765  iterator it = lookup(key);
766  if(it.valid())
767  delItem(it);
768 }
769 
770 
771 template<class KEY, class INFO, class CMP>
773 {
774  m_size++;
775 
776  int nh = randomHeightAndGrow();
777 
778  Element* pNewElement = new Element(key, info, nh);
779  insertElementAfterElement(pNewElement, it.m_pElement);
780 
781  return pNewElement;
782 }
783 
784 
785 template<class KEY, class INFO, class CMP>
787 {
789 
790  for(int h = 0; h < p->m_height; ++h) {
791  while(q != m_dummy && q->m_height <= h)
792  q = q->m_prev[h-1];
793 
794  Element *r = q->m_next[h];
795  p->m_next[h] = r;
796  p->m_prev[h] = q;
797  q->m_next[h] = r->m_prev[h] = p;
798  }
799 }
800 
801 
802 template<class KEY, class INFO, class CMP>
804 {
805  while(p != q) {
806  Element *r = p;
807  p = p->m_next[0];
808  removeElement(r);
810  }
811 }
812 
813 
814 template<class KEY, class INFO, class CMP>
816 {
817  OGDF_ASSERT(p != nullptr);
818  OGDF_ASSERT(p != m_dummy);
819 
820  for(int h = 0; h < p->m_height; ++h) {
821  Element *pPred = p->m_prev[h], *pSucc = p->m_next[h];
822  pPred->m_next[h] = pSucc;
823  pSucc->m_prev[h] = pPred;
824  }
825 }
826 
827 
828 template<class KEY, class INFO, class CMP>
830 {
831  Element *p = it.m_pElement;
832  removeElement(p);
833 
834  m_size--;
835  delete p;
836 }
837 
838 
839 template<class KEY, class INFO, class CMP>
842 {
843  return (m_dummy->m_next[0] != m_dummy) ? m_dummy->m_next[0] : nullptr;
844 }
845 
846 template<class KEY, class INFO, class CMP>
849 {
850  return (m_dummy->m_next[0] != m_dummy) ? m_dummy->m_next[0] : 0;
851 }
852 
853 
854 template<class KEY, class INFO, class CMP>
857 {
859 }
860 
861 template<class KEY, class INFO, class CMP>
864 {
866 }
867 
868 
869 template<class KEY, class INFO, class CMP>
872 {
873  return (m_dummy->m_prev[0] != m_dummy) ? m_dummy->m_prev[0] : 0;
874 }
875 
876 template<class KEY, class INFO, class CMP>
879 {
880  return (m_dummy->m_prev[0] != m_dummy) ? m_dummy->m_prev[0] : 0;
881 }
882 
883 
884 template<class KEY, class INFO, class CMP>
887 {
889 }
890 
891 template<class KEY, class INFO, class CMP>
894 {
896 }
897 
898 
899 } // end namespace ogdf
void removeElement(Element *p)
SortedSequence< KEY, INFO, CMP >::Element * succElement() const
iterator end()
Returns an iterator pointing to one past the last element.
Maintains a sequence of (key,info) pairs sorted by key.
int empty() const
Returns true if the sequence is empty, false otherwise.
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.
SortedSequence(const CMP &comparer=CMP())
Constructs an initially empty sorted sequence.
iterator maxItem()
Returns an iterator to the element with maximal key if the sequence is not empty, an invalid iterator...
Declaration of memory manager for allocating small pieces of memory.
SortedSequenceIterator< KEY, INFO, CMP > & operator--()
Moves the iterator one item backward (prefix notation)
int m_size
number of elements stored in the sequence.
SortedSequenceConstIterator< KEY, INFO, CMP > & operator=(const SortedSequenceConstIterator< KEY, INFO, CMP > &it)
Assignment operator.
const_iterator cend() const
Returns a const-iterator pointing to one past the last element.
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.
bool operator==(const SortedSequenceConstIterator< KEY, INFO, CMP > &it) const
Equality operator.
Element * m_dummy
dummy element representing the head and tail of the skiplist.
SortedSequenceIterator()
Creates an invalid (null-) iterator.
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:60
SortedSequenceConstIterator(const typename SortedSequence< KEY, INFO, CMP >::Element *pElement)
Creates an iterator pointing to pElement.
SortedSequenceIterator< KEY, INFO, CMP > succ() const
Returns an iterator pointing to the next element in the sequence.
const SortedSequence< KEY, INFO, CMP >::Element * m_pElement
const_iterator maxItem() const
Returns a const-iterator to the element with maximal key if the sequence is not empty, an invalid const-iterator otherwise.
const_iterator crend() const
Returns a const-iterator pointing to one before the first element.
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.
SortedSequenceConstIterator(const SortedSequenceIterator< KEY, INFO, CMP > &it)
Creates an iterator pointing to the same element as it.
void clear()
Removes all elements from the sorted sequence.
const_iterator crbegin() const
Returns a const-iterator pointing to the last element.
iterator minItem()
Returns an iterator to the element with minimal key if the sequence is not empty, an invalid iterator...
SortedSequenceIterator< KEY, INFO, CMP > & operator=(const SortedSequenceIterator< KEY, INFO, CMP > &it)
Assignment operator.
const Element * _lookup(const KEY &key) const
SortedSequenceIterator< KEY, INFO, CMP > & operator++()
Move the iterator one item forward (prefix notation)
SortedSequence< KEY, INFO, CMP >::Element * m_pElement
SortedSequenceIterator(const SortedSequenceIterator< KEY, INFO, CMP > &it)
Copy constructor.
bool operator==(const SortedSequenceIterator< KEY, INFO, CMP > &it) const
Equality operator.
SortedSequenceConstIterator< KEY, INFO, CMP > & operator++()
Moves the iterator one item forward (prefix notation).
SortedSequenceConstIterator< KEY, INFO, CMP > operator--(int)
Moves the iterator one item backward (postfix notation).
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.
const INFO & info() const
Returns the info of the sequence element pointed to.
SortedSequenceConstIterator(const SortedSequenceConstIterator< KEY, INFO, CMP > &it)
Copy constructor.
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...
SortedSequence< KEY, INFO, CMP >::Element * predElement() const
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
bool operator!=(const SortedSequenceConstIterator< KEY, INFO, CMP > &it) const
Inequality operator.
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 iterators for sorted sequences.
Iterators for sorted sequences.
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.
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:235
#define OGDF_NEW_DELETE
Makes the class use OGDF&#39;s memory allocator.
Definition: memory.h:84
Element ** m_prev
array of predecessor elements.
iterator rbegin()
Returns an iterator pointing to the last element.
Element ** m_next
array of successor elements.
void reverseElements(Element *p, Element *q)
void insertElementAfterElement(Element *p, Element *q)
SortedSequenceIterator< KEY, INFO, CMP > pred() const
Returns an iterator pointing to the previous element in the sequence.
void del(const KEY &key)
Removes the element with key key (if such an element exists).
bool valid() const
Returns true if the iterator points to an element.
int size() const
Returns the current size of the sequence, i.e., the number of stored elements.
SortedSequence< KEY, INFO, CMP >::Element * predElement() const
void grow(int newHeight)
Increases the element&#39;s height to newHeight.
SortedSequenceConstIterator< KEY, INFO, CMP > operator++(int)
Moves the iterator one item forward (postfix notation).
KEY m_key
stores the key.
SortedSequenceConstIterator()
Creates an invalid (null-) iterator.
iterator rend()
Returns an iterator pointing to one before the first element.
void grow(int newHeight)
SortedSequenceIterator< KEY, INFO, CMP > operator--(int)
Moves the iterator one item backward (postfix notation)
const KEY & key() const
Returns the key of the sequence element pointed to.
SortedSequenceConstIterator< KEY, INFO, CMP > pred() const
Returns an iterator pointing to the previous element in the sequence.
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.
INFO & info() const
Returns the info of the sequence element pointed to.
const Element * _locate(const KEY &key) const
bool valid() const
Returns true if the iterator points to an element.
SortedSequenceIterator(typename SortedSequence< KEY, INFO, CMP >::Element *pElement)
Creates an iterator pointing to pElement.
The namespace for all OGDF objects.
SortedSequence< KEY, INFO, CMP > & operator=(const SortedSequence< KEY, INFO, CMP > &S)
Assignment operator.
SortedSequenceConstIterator< KEY, INFO, CMP > succ() const
Returns an iterator pointing to the next element in the sequence.
void reverseItems(iterator itBegin, iterator itEnd)
Reverses the items in the subsequence from itBegin to itEnd (inclusive).
SortedSequenceConstIterator< KEY, INFO, CMP > & operator--()
Moves the iterator one item backward (prefix notation).
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.
bool operator!=(const SortedSequenceIterator< KEY, INFO, CMP > &it) const
Inequality operator.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
Definition: basic.h:41
SortedSequence< KEY, INFO, CMP >::Element * succElement() const
const KEY & key() const
Returns the key of the sequence element pointed to.
SortedSequenceIterator< KEY, INFO, CMP > operator++(int)
Moves the iterator one item forward (postfix notation)