Maintains a sequence of (key,info) pairs sorted by key. More...
#include <ogdf/basic/SortedSequence.h>
Classes | |
struct | Element |
Internal structure to hold the items and internal forward/backward pointers of the skiplist. More... | |
Public Types | |
using | const_iterator = SortedSequenceConstIterator< KEY, INFO, CMP > |
The const-iterator type for sorted sequences (bidirectional iterator). More... | |
using | const_reverse_iterator = SortedSequenceConstReverseIterator< KEY, INFO, CMP > |
The const reverse iterator type for sorted sequences (bidirectional iterator). More... | |
using | iterator = SortedSequenceIterator< KEY, INFO, CMP > |
The iterator type for sorted sequences (bidirectional iterator). More... | |
using | reverse_iterator = SortedSequenceReverseIterator< KEY, INFO, CMP > |
The reverse iterator type for sorted sequences (bidirectional iterator). More... | |
Public Member Functions | |
SortedSequence (const CMP &comparer=CMP()) | |
Constructs an initially empty sorted sequence. More... | |
SortedSequence (const SortedSequence< KEY, INFO, CMP > &S) | |
Copy constructor. More... | |
SortedSequence (SortedSequence< KEY, INFO, CMP > &&S) | |
Copy constructor (move semantics). More... | |
SortedSequence (std::initializer_list< std::pair< KEY, INFO >> initList) | |
Constructs a sorted sequence containing the elements in initList . More... | |
~SortedSequence () | |
Destructor. More... | |
General information and standard iterators | |
These methods provide basic information like the number of elements in the list, as well as iterators to the begin and end of the sequence allowing forward and backward iteration over the sequence. | |
int | size () const |
Returns the current size of the sequence, i.e., the number of stored elements. More... | |
bool | empty () const |
Returns true if the sequence is empty, false otherwise. More... | |
iterator | begin () |
Returns an iterator pointing to the first element. More... | |
const_iterator | begin () const |
Returns a const-iterator pointing to the first element. More... | |
const_iterator | cbegin () const |
Returns a const-iterator pointing to the first element. More... | |
iterator | end () |
Returns an iterator pointing to one past the last element. More... | |
const_iterator | end () const |
Returns a const-iterator pointing to one past the last element. More... | |
const_iterator | cend () const |
Returns a const-iterator pointing to one past the last element. More... | |
reverse_iterator | rbegin () |
Returns a reverse iterator pointing to the last element. More... | |
const_reverse_iterator | rbegin () const |
Returns a const reverse iterator pointing to the last element. More... | |
const_reverse_iterator | crbegin () const |
Returns a const reverse iterator pointing to the last element. More... | |
reverse_iterator | rend () |
Returns a reverse iterator pointing to one before the first element. More... | |
const_reverse_iterator | rend () const |
Returns a const reverse iterator pointing to one before the first element. More... | |
const_reverse_iterator | crend () const |
Returns a const reverse iterator pointing to one before the first element. More... | |
Lookup operations | |
These methods can be used to find elements by key. They return iterators pointing to the respective element in the sequence. | |
iterator | lookup (const KEY &key) |
Returns an iterator to the element with key key , or a null iterator if no such element exists. More... | |
const_iterator | lookup (const KEY &key) const |
Returns a const-iterator to the element with key key , or a null iterator if no such element exists. More... | |
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. More... | |
const_iterator | locate (const KEY &key) const |
Returns a const-iterator to the element < k1, i1 > such that k1 is minimal with k1 ≥ key , or a null iterator if no such element exists. More... | |
iterator | minItem () |
Returns an iterator to the element with minimal key if the sequence is not empty, an invalid iterator otherwise. More... | |
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. More... | |
reverse_iterator | maxItem () |
Returns a reverse iterator to the element with maximal key if the sequence is not empty, an invalid reverse iterator otherwise. More... | |
const_reverse_iterator | maxItem () const |
Returns a const reverse iterator to the element with maximal key if the sequence is not empty, an invalid const reverse iterator otherwise. More... | |
Insertion and deletion | |
These method provide basic modification methods, like inserting new elements or removing elements from the sequence. | |
iterator | insert (const KEY &key, const INFO &info) |
Updates information for key if contained in sequence, or adds a new element <key , info> . More... | |
void | del (const KEY &key) |
Removes the element with key key (if such an element exists). More... | |
void | delItem (iterator it) |
Removes the element to which it points from the sequence. More... | |
void | clear () |
Removes all elements from the sorted sequence. More... | |
Operators | |
The following operators are overloeded for sorted sequences. | |
SortedSequence< KEY, INFO, CMP > & | operator= (const SortedSequence< KEY, INFO, CMP > &S) |
Assignment operator. More... | |
SortedSequence< KEY, INFO, CMP > & | operator= (SortedSequence< KEY, INFO, CMP > &&S) |
Assignment operator (move semantics). More... | |
bool | operator== (const SortedSequence< KEY, INFO, CMP > &S) |
Returns true if the keys stored in this sequence equal the keys in S , false otherwise. More... | |
bool | operator!= (const SortedSequence< KEY, INFO, CMP > &S) |
Returns false if the keys stored in this sequence equal the keys in S , true otherwise. More... | |
Special modification methods | |
These methods must be handled with care; they are only useful in very specific scenarios. First read their documentation carefully! | |
iterator | insertAfter (iterator it, const KEY &key, const INFO &info) |
Adds a new element <key , info> after element it . More... | |
void | reverseItems (iterator itBegin, iterator itEnd) |
Reverses the items in the subsequence from itBegin to itEnd (inclusive). More... | |
Private Member Functions | |
const Element * | _locate (const KEY &key) const |
const Element * | _lookup (const KEY &key) const |
void | grow (int newHeight) |
void | initEmpty () |
void | insertElementAfterElement (Element *p, Element *q) |
int | randomHeightAndGrow () |
void | removeElement (Element *p) |
void | reverseElements (Element *p, Element *q) |
Private Attributes | |
CMP | m_comparer |
Element * | m_dummy |
dummy element representing the head and tail of the skiplist. More... | |
int | m_height |
current height of head/tail. More... | |
std::uniform_int_distribution | m_randomBits |
int | m_realHeight |
actual height of head/tail arrays. More... | |
std::minstd_rand | m_rng |
Random number generator. More... | |
int | m_size |
number of elements stored in the sequence. More... | |
Maintains a sequence of (key,info) pairs sorted by key.
Sorted sequences a implemented by doubly linked skiplists. Operations lookup, locate, insert, del, delItem take expected time O(log n), where n is the current size of the sequence.
Definition at line 71 of file SortedSequence.h.
using ogdf::SortedSequence< KEY, INFO, CMP >::const_iterator = SortedSequenceConstIterator<KEY, INFO, CMP> |
The const-iterator type for sorted sequences (bidirectional iterator).
Definition at line 133 of file SortedSequence.h.
using ogdf::SortedSequence< KEY, INFO, CMP >::const_reverse_iterator = SortedSequenceConstReverseIterator<KEY, INFO, CMP> |
The const reverse iterator type for sorted sequences (bidirectional iterator).
Definition at line 137 of file SortedSequence.h.
using ogdf::SortedSequence< KEY, INFO, CMP >::iterator = SortedSequenceIterator<KEY, INFO, CMP> |
The iterator type for sorted sequences (bidirectional iterator).
Definition at line 131 of file SortedSequence.h.
using ogdf::SortedSequence< KEY, INFO, CMP >::reverse_iterator = SortedSequenceReverseIterator<KEY, INFO, CMP> |
The reverse iterator type for sorted sequences (bidirectional iterator).
Definition at line 135 of file SortedSequence.h.
|
inline |
Constructs an initially empty sorted sequence.
Definition at line 140 of file SortedSequence.h.
ogdf::SortedSequence< KEY, INFO, CMP >::SortedSequence | ( | std::initializer_list< std::pair< KEY, INFO >> | initList | ) |
Constructs a sorted sequence containing the elements in initList
.
Definition at line 495 of file SortedSequence.h.
ogdf::SortedSequence< KEY, INFO, CMP >::SortedSequence | ( | const SortedSequence< KEY, INFO, CMP > & | S | ) |
Copy constructor.
Definition at line 503 of file SortedSequence.h.
ogdf::SortedSequence< KEY, INFO, CMP >::SortedSequence | ( | SortedSequence< KEY, INFO, CMP > && | S | ) |
Copy constructor (move semantics).
The sequence S
is empty afterwards.
Definition at line 514 of file SortedSequence.h.
|
inline |
Destructor.
Definition at line 158 of file SortedSequence.h.
|
private |
Definition at line 615 of file SortedSequence.h.
|
private |
Definition at line 585 of file SortedSequence.h.
|
inline |
Returns an iterator pointing to the first element.
Definition at line 773 of file SortedSequence.h.
const_iterator ogdf::SortedSequence< KEY, INFO, CMP >::begin | ( | ) | const |
Returns a const-iterator pointing to the first element.
|
inline |
Returns a const-iterator pointing to the first element.
Definition at line 183 of file SortedSequence.h.
|
inline |
Returns a const-iterator pointing to one past the last element.
Definition at line 192 of file SortedSequence.h.
|
inline |
Removes all elements from the sorted sequence.
Definition at line 279 of file SortedSequence.h.
|
inline |
Returns a const reverse iterator pointing to the last element.
Definition at line 201 of file SortedSequence.h.
|
inline |
Returns a const reverse iterator pointing to one before the first element.
Definition at line 210 of file SortedSequence.h.
void ogdf::SortedSequence< KEY, INFO, CMP >::del | ( | const KEY & | key | ) |
Removes the element with key key
(if such an element exists).
Definition at line 705 of file SortedSequence.h.
void ogdf::SortedSequence< KEY, INFO, CMP >::delItem | ( | iterator | it | ) |
Removes the element to which it
points from the sequence.
Definition at line 764 of file SortedSequence.h.
|
inline |
Returns true if the sequence is empty, false otherwise.
Definition at line 174 of file SortedSequence.h.
|
inline |
Returns an iterator pointing to one past the last element.
Definition at line 784 of file SortedSequence.h.
const_iterator ogdf::SortedSequence< KEY, INFO, CMP >::end | ( | ) | const |
Returns a const-iterator pointing to one past the last element.
|
private |
Definition at line 643 of file SortedSequence.h.
|
inlineprivate |
Definition at line 361 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP >::iterator ogdf::SortedSequence< KEY, INFO, CMP >::insert | ( | const KEY & | key, |
const INFO & | info | ||
) |
Updates information for key
if contained in sequence, or adds a new element <key
, info>
.
Definition at line 671 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP >::iterator ogdf::SortedSequence< KEY, INFO, CMP >::insertAfter | ( | iterator | it, |
const KEY & | key, | ||
const INFO & | info | ||
) |
Adds a new element <key
, info>
after element it
.
it
points to an element in the sequence that shall appear before <key
, info>
in the sorted sequence, and its current successor itSucc shall appear after <key
, info>
, i.e., it's
key is smaller than key
and itSucc's key is greater than key
. Definition at line 713 of file SortedSequence.h.
|
private |
Definition at line 726 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP >::iterator ogdf::SortedSequence< KEY, INFO, CMP >::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.
Definition at line 631 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP >::const_iterator ogdf::SortedSequence< KEY, INFO, CMP >::locate | ( | const KEY & | key | ) | const |
Returns a const-iterator to the element < k1, i1 > such that k1 is minimal with k1 ≥ key
, or a null iterator if no such element exists.
Definition at line 637 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP >::iterator ogdf::SortedSequence< KEY, INFO, CMP >::lookup | ( | const KEY & | key | ) |
Returns an iterator to the element with key key
, or a null iterator if no such element exists.
Definition at line 603 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP >::const_iterator ogdf::SortedSequence< KEY, INFO, CMP >::lookup | ( | const KEY & | key | ) | const |
Returns a const-iterator to the element with key key
, or a null iterator if no such element exists.
Definition at line 609 of file SortedSequence.h.
|
inline |
Returns a reverse iterator to the element with maximal key if the sequence is not empty, an invalid reverse iterator otherwise.
Calling this method is equivalent to calling rbegin(), but has a more intuitive name.
Definition at line 253 of file SortedSequence.h.
|
inline |
Returns a const reverse iterator to the element with maximal key if the sequence is not empty, an invalid const reverse iterator otherwise.
Calling this method is equivalent to calling rbegin(), but has a more intuitive name.
Definition at line 260 of file SortedSequence.h.
|
inline |
Returns an iterator to the element with minimal key if the sequence is not empty, an invalid iterator otherwise.
Calling this method is equivalent to calling begin(), but has a more intuitive name.
Definition at line 239 of file SortedSequence.h.
|
inline |
Returns a const-iterator to the element with minimal key if the sequence is not empty, an invalid const-iterator otherwise.
Calling this method is equivalent to calling begin(), but has a more intuitive name.
Definition at line 246 of file SortedSequence.h.
|
inline |
Returns false if the keys stored in this sequence equal the keys in S
, true otherwise.
Uses the given comparer object to compare keys.
Definition at line 317 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP > & ogdf::SortedSequence< KEY, INFO, CMP >::operator= | ( | const SortedSequence< KEY, INFO, CMP > & | S | ) |
Assignment operator.
Definition at line 529 of file SortedSequence.h.
SortedSequence< KEY, INFO, CMP > & ogdf::SortedSequence< KEY, INFO, CMP >::operator= | ( | SortedSequence< KEY, INFO, CMP > && | S | ) |
Assignment operator (move semantics).
The sequence S
is empty afterwards.
Definition at line 542 of file SortedSequence.h.
bool ogdf::SortedSequence< KEY, INFO, CMP >::operator== | ( | const SortedSequence< KEY, INFO, CMP > & | S | ) |
Returns true if the keys stored in this sequence equal the keys in S
, false otherwise.
Uses the given comparer object to compare keys.
Definition at line 567 of file SortedSequence.h.
|
private |
Definition at line 657 of file SortedSequence.h.
|
inline |
Returns a reverse iterator pointing to the last element.
Definition at line 796 of file SortedSequence.h.
const_reverse_iterator ogdf::SortedSequence< KEY, INFO, CMP >::rbegin | ( | ) | const |
Returns a const reverse iterator pointing to the last element.
|
private |
Definition at line 752 of file SortedSequence.h.
|
inline |
Returns a reverse iterator pointing to one before the first element.
Definition at line 807 of file SortedSequence.h.
const_reverse_iterator ogdf::SortedSequence< KEY, INFO, CMP >::rend | ( | ) | const |
Returns a const reverse iterator pointing to one before the first element.
|
private |
Definition at line 742 of file SortedSequence.h.
|
inline |
Reverses the items in the subsequence from itBegin
to itEnd
(inclusive).
itBegin
appears before itEnd
in the sequence.Definition at line 345 of file SortedSequence.h.
|
inline |
Returns the current size of the sequence, i.e., the number of stored elements.
Definition at line 171 of file SortedSequence.h.
|
friend |
Definition at line 72 of file SortedSequence.h.
|
friend |
Definition at line 74 of file SortedSequence.h.
|
friend |
Definition at line 73 of file SortedSequence.h.
|
friend |
Definition at line 75 of file SortedSequence.h.
|
private |
Definition at line 352 of file SortedSequence.h.
|
private |
dummy element representing the head and tail of the skiplist.
Definition at line 354 of file SortedSequence.h.
|
private |
current height of head/tail.
Definition at line 355 of file SortedSequence.h.
|
private |
Definition at line 359 of file SortedSequence.h.
|
private |
actual height of head/tail arrays.
Definition at line 356 of file SortedSequence.h.
|
private |
Random number generator.
Definition at line 358 of file SortedSequence.h.
|
private |
number of elements stored in the sequence.
Definition at line 353 of file SortedSequence.h.