40#include <initializer_list>
47template<
class KEY,
class INFO,
class CMP,
bool isConst,
bool isReverse>
48class SortedSequenceIteratorBase;
50template<
class KEY,
class INFO,
class CMP>
53template<
class KEY,
class INFO,
class CMP>
56template<
class KEY,
class INFO,
class CMP>
59template<
class KEY,
class INFO,
class CMP>
70template<
class KEY,
class INFO,
class CMP = StdComparer<KEY>>
87 Element(
const KEY& key,
const INFO& info,
int height)
146 SortedSequence(std::initializer_list<std::pair<KEY, INFO>> initList);
273 void del(
const KEY& key);
372 void grow(
int newHeight);
374 const Element*
_lookup(
const KEY& key)
const;
375 const Element*
_locate(
const KEY& key)
const;
383template<
class KEY,
class INFO,
class CMP,
bool isConst,
bool isReverse>
391 typename std::conditional<isConst, const typename SortedSequence<KEY, INFO, CMP>::Element,
403 template<bool isArgConst, typename std::enable_if<isConst || !isArgConst, int>::type = 0,
bool isArgReverse>
421 typename std::conditional<isConst, const INFO, INFO>::type&
info()
const {
494template<
class KEY,
class INFO,
class CMP>
497 for (
const auto& p : initList) {
498 insert(p.first, p.second);
502template<
class KEY,
class INFO,
class CMP>
504 : m_comparer(S.m_comparer), m_rng(
randomSeed()), m_randomBits(0, 1) {
513template<
class KEY,
class INFO,
class CMP>
515 : m_comparer(S.m_comparer)
517 , m_height(S.m_height)
518 , m_realHeight(S.m_realHeight)
520 , m_randomBits(0, 1) {
528template<
class KEY,
class INFO,
class CMP>
535 it = insertAfter(it, pS->m_key, pS->m_info);
541template<
class KEY,
class INFO,
class CMP>
546 while (item != m_dummy) {
554 m_comparer = S.m_comparer;
557 m_realHeight = S.m_realHeight;
566template<
class KEY,
class INFO,
class CMP>
573 while (p != m_dummy) {
574 if (!m_comparer.equal(p->
m_key, pS->m_key)) {
584template<
class KEY,
class INFO,
class CMP>
586 const KEY& key)
const {
587 int h = m_height - 1;
591 if (pElement[h] != m_dummy && m_comparer.less(pElement[h]->
m_key, key)) {
592 pElement = pElement[h]->
m_next;
593 }
else if (--h < 0) {
594 if (pElement[0] != m_dummy && m_comparer.equal(pElement[0]->
m_key, key)) {
602template<
class KEY,
class INFO,
class CMP>
608template<
class KEY,
class INFO,
class CMP>
610 const KEY& key)
const {
614template<
class KEY,
class INFO,
class CMP>
616 const KEY& key)
const {
617 int h = m_height - 1;
621 if (pElement[h] != m_dummy && m_comparer.less(pElement[h]->
m_key, key)) {
622 pElement = pElement[h]->
m_next;
623 }
else if (--h < 0) {
624 Element* p = (pElement[0] != m_dummy) ? pElement[0] :
nullptr;
630template<
class KEY,
class INFO,
class CMP>
636template<
class KEY,
class INFO,
class CMP>
638 const KEY& key)
const {
642template<
class KEY,
class INFO,
class CMP>
644 if (newHeight > m_realHeight) {
645 m_realHeight = newHeight;
646 m_dummy->grow(newHeight);
649 for (
int i = newHeight; i-- > m_height;) {
650 m_dummy->m_next[i] = m_dummy;
651 m_dummy->m_prev[i] = m_dummy;
653 m_height = newHeight;
656template<
class KEY,
class INFO,
class CMP>
659 while (m_randomBits(m_rng) == 1) {
670template<
class KEY,
class INFO,
class CMP>
672 const KEY& key,
const INFO& info) {
673 int h = m_height - 1;
677 if (pCurrent->
m_next[h] != m_dummy && m_comparer.less(pCurrent->
m_next[h]->
m_key, key)) {
678 pCurrent = pCurrent->
m_next[h];
683 if (pCurrent->
m_next[0] != m_dummy
684 && m_comparer.equal(pCurrent->
m_next[0]->
m_key, key)) {
696 int nh = randomHeightAndGrow();
699 insertElementAfterElement(pNewElement, pCurrent);
704template<
class KEY,
class INFO,
class CMP>
712template<
class KEY,
class INFO,
class CMP>
714 iterator it,
const KEY& key,
const INFO& info) {
717 int nh = randomHeightAndGrow();
720 insertElementAfterElement(pNewElement, it.
m_pElement);
725template<
class KEY,
class INFO,
class CMP>
729 for (
int h = 0; h < p->
m_height; ++h) {
730 while (q != m_dummy && q->
m_height <= h) {
741template<
class KEY,
class INFO,
class CMP>
747 insertElementAfterElement(
r, q);
751template<
class KEY,
class INFO,
class CMP>
756 for (
int h = 0; h < p->
m_height; ++h) {
763template<
class KEY,
class INFO,
class CMP>
772template<
class KEY,
class INFO,
class CMP>
774 return (m_dummy->m_next[0] != m_dummy) ? m_dummy->m_next[0] :
nullptr;
777template<
class KEY,
class INFO,
class CMP>
780 return (m_dummy->m_next[0] != m_dummy) ? m_dummy->m_next[0] : 0;
783template<
class KEY,
class INFO,
class CMP>
788template<
class KEY,
class INFO,
class CMP>
794template<
class KEY,
class INFO,
class CMP>
797 return (m_dummy->m_prev[0] != m_dummy) ? m_dummy->m_prev[0] : 0;
800template<
class KEY,
class INFO,
class CMP>
803 return (m_dummy->m_prev[0] != m_dummy) ? m_dummy->m_prev[0] : 0;
806template<
class KEY,
class INFO,
class CMP>
811template<
class KEY,
class INFO,
class CMP>
Basic declarations, included by all source files.
Exception thrown when not enough memory is available to execute an algorithm.
Maintains a sequence of (key,info) pairs sorted by key.
reverse_iterator rend()
Returns a reverse iterator pointing to one before the first element.
SortedSequence(const CMP &comparer=CMP())
Constructs an initially empty sorted sequence.
iterator insertAfter(iterator it, const KEY &key, const INFO &info)
Adds a new element <key, info> after element it.
void delItem(iterator it)
Removes the element to which it points from the sequence.
std::minstd_rand m_rng
Random number generator.
const_iterator cend() const
Returns a const-iterator pointing to one past the last element.
bool operator==(const SortedSequence< KEY, INFO, CMP > &S)
Returns true if the keys stored in this sequence equal the keys in S, false otherwise.
int m_realHeight
actual height of head/tail arrays.
const_iterator minItem() const
Returns a const-iterator to the element with minimal key if the sequence is not empty,...
void reverseElements(Element *p, Element *q)
const_reverse_iterator crend() const
Returns a const reverse iterator pointing to one before the first element.
SortedSequence< KEY, INFO, CMP > & operator=(const SortedSequence< KEY, INFO, CMP > &S)
Assignment operator.
int m_size
number of elements stored in the sequence.
const_reverse_iterator maxItem() const
Returns a const reverse iterator to the element with maximal key if the sequence is not empty,...
void reverseItems(iterator itBegin, iterator itEnd)
Reverses the items in the subsequence from itBegin to itEnd (inclusive).
const Element * _lookup(const KEY &key) const
SortedSequenceIterator< KEY, INFO, CMP > iterator
The iterator type for sorted sequences (bidirectional iterator).
const Element * _locate(const KEY &key) const
iterator lookup(const KEY &key)
Returns an iterator to the element with key key, or a null iterator if no such element exists.
iterator begin()
Returns an iterator pointing to the first element.
int size() const
Returns the current size of the sequence, i.e., the number of stored elements.
bool operator!=(const SortedSequence< KEY, INFO, CMP > &S)
Returns false if the keys stored in this sequence equal the keys in S, true otherwise.
Element * m_dummy
dummy element representing the head and tail of the skiplist.
iterator insert(const KEY &key, const INFO &info)
Updates information for key if contained in sequence, or adds a new element <key, info>.
iterator locate(const KEY &key)
Returns an iterator to the element < k1, i1 > such that k1 is minimal with k1 ≥ key,...
reverse_iterator maxItem()
Returns a reverse iterator to the element with maximal key if the sequence is not empty,...
iterator minItem()
Returns an iterator to the element with minimal key if the sequence is not empty, an invalid iterator...
SortedSequenceReverseIterator< KEY, INFO, CMP > reverse_iterator
The reverse iterator type for sorted sequences (bidirectional iterator).
int randomHeightAndGrow()
SortedSequenceConstReverseIterator< KEY, INFO, CMP > const_reverse_iterator
The const reverse iterator type for sorted sequences (bidirectional iterator).
~SortedSequence()
Destructor.
std::uniform_int_distribution m_randomBits
void del(const KEY &key)
Removes the element with key key (if such an element exists).
void insertElementAfterElement(Element *p, Element *q)
SortedSequenceConstIterator< KEY, INFO, CMP > const_iterator
The const-iterator type for sorted sequences (bidirectional iterator).
bool empty() const
Returns true if the sequence is empty, false otherwise.
const_reverse_iterator crbegin() const
Returns a const reverse iterator pointing to the last element.
void clear()
Removes all elements from the sorted sequence.
iterator end()
Returns an iterator pointing to one past the last element.
int m_height
current height of head/tail.
reverse_iterator rbegin()
Returns a reverse iterator pointing to the last element.
void removeElement(Element *p)
const_iterator cbegin() const
Returns a const-iterator pointing to the first element.
Iterators for sorted sequences.
SortedSequenceIteratorBase(const SortedSequenceIteratorBase< KEY, INFO, CMP, isArgConst, isArgReverse > &it)
Copy constructor.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > operator++(int)
Moves the iterator one item forward (postfix notation)
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > & operator++()
Move the iterator one item forward (prefix notation)
std::conditional< isConst, constINFO, INFO >::type & info() const
Returns the info of the sequence element pointed to.
bool operator==(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it) const
Equality operator.
const KEY & key() const
Returns the key of the sequence element pointed to.
SortedSequenceIteratorBase(Element *pElement)
Creates an iterator pointing to pElement.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > operator--(int)
Moves the iterator one item backward (postfix notation)
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > & operator--()
Moves the iterator one item backward (prefix notation)
SortedSequenceIteratorBase()
Creates an invalid (null-) iterator.
SortedSequenceIteratorBase(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it)
Copy constructor.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > & operator=(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it)
Assignment operator.
bool valid() const
Returns true if the iterator points to an element.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > pred() const
Returns an iterator pointing to the previous element in the sequence.
typename std::conditional< isConst, const typename SortedSequence< KEY, INFO, CMP >::Element, typename SortedSequence< KEY, INFO, CMP >::Element >::type Element
bool operator!=(const SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > &it) const
Inequality operator.
SortedSequenceIteratorBase< KEY, INFO, CMP, isConst, isReverse > succ() const
Returns an iterator pointing to the next element in the sequence.
SortedSequence< KEY, INFO, CMP >::Element * succElement() const
SortedSequence< KEY, INFO, CMP >::Element * predElement() const
Declarations for Comparer objects.
Definition of exception classes.
#define OGDF_THROW(CLASS)
Replacement for throw.
#define OGDF_ASSERT(expr)
Assert condition expr. See doc/build.md for more information.
#define OGDF_NEW_DELETE
Makes the class use OGDF's memory allocator.
long unsigned int randomSeed()
Returns a random value suitable as initial seed for a random number engine.
Declaration of memory manager for allocating small pieces of memory.
The namespace for all OGDF objects.
Internal structure to hold the items and internal forward/backward pointers of the skiplist.
int m_height
the height of the skiplist element.
Element ** m_next
array of successor elements.
Element ** m_prev
array of predecessor elements.
void grow(int newHeight)
Increases the element's height to newHeight.
Element(int height)
Creates a dummy (stop) element with given height.
Element(const Element &)=delete
INFO m_info
stores the associated information.
Element(const KEY &key, const INFO &info, int height)
Creates a skiplist element for(key,info) and given height.