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