|
Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
|
Skip List - a probabilistic ordered data structure. More...
#include <tpl_skipList.H>
Classes | |
| class | HeaderNode |
| class | Iterator |
| Iterator for traversing SkipList elements in ascending order. More... | |
| class | Node |
Public Member Functions | |
| void | set_seed (unsigned long seed) |
Seeds the Mersenne Twister of the list, as gsl_rng_set() did: the levels are from then on those of GSL for the seed. | |
| Random_Source & | random_source () noexcept |
| The source of random numbers, which draws the levels. | |
| const Random_Source & | random_source () const noexcept |
| The source of random numbers, to look at it: its engine, or whether it is equal to another one. | |
| Gsl_Compatible_Rng & | rng () |
| The Mersenne Twister of the list, which a list that was given no seed makes here, and draws from it from then on. | |
| SkipList (unsigned long seed, double p=defaultProbability) | |
| Construct a SkipList with given seed and probability, whose levels are those of GSL for the seed: it draws them from a Mersenne Twister, in dynamic memory. | |
| SkipList (double p=defaultProbability) | |
| Construct a SkipList that draws its levels from a SplitMix64 with a seed of its own, without allocating. | |
| SkipList (const SkipList &)=delete | |
| SkipList & | operator= (const SkipList &)=delete |
| SkipList (SkipList &&other) | |
| Take the nodes and the generator of another list, which is left empty, and draws from then on from a SplitMix64 with a seed of its own. | |
| SkipList & | operator= (SkipList &&other) noexcept |
| Exchange this list with another, which is left with the previous nodes and generator of this one. | |
| void | swap (SkipList &other) noexcept |
| Exchange the nodes, the generators and the probabilities of this list and another. | |
| Node * | search (const Key &searchKey) noexcept |
| Search for a key in the skip list. | |
| Node * | insert (Node *p) noexcept |
| Insert a node into the skip list. | |
| Node * | get_first () noexcept |
| Get the first node in the list. | |
| int | get_level () const noexcept |
| Return the level of the list: the number of levels in use. | |
| Node * | remove (const Key &searchKey) noexcept |
| Remove a key from the skip list. | |
| int | generateRandomLevel () noexcept |
| Generate a random level for a new node. | |
| bool | checkSkipList () const noexcept |
| Verify skip list invariants (for debugging). | |
| Node * | new_node () noexcept |
| Placeholder for node allocation (override in derived classes). | |
| Iterator | begin () const noexcept |
| Get iterator to first element. | |
| Iterator | end () const noexcept |
| Get iterator past the last element. | |
Static Public Attributes | |
| static const int | maxLevel = 32 |
| Maximum level for any node (supports up to 2^32 elements efficiently) | |
| static const double | defaultProbability = 0.5 |
| Default probability for level generation (0.5 = geometric distribution) | |
Static Private Member Functions | |
| static unsigned long | generator_seed (const unsigned long seed) noexcept |
Private Attributes | |
| HeaderNode | header |
| HeaderNode * | headerPtr |
| Random_Source | r |
| double | probability |
| int | level |
Skip List - a probabilistic ordered data structure.
A Skip List is an ordered list where each node contains an array of forward pointers. The structure was proposed by William Pugh (1990).
Each node has a randomly determined level that defines how many forward pointers it contains. Higher levels allow faster traversal, giving O(log n) expected time for search, insert, and delete.
Key properties:
The levels come from a Random_Source: by default, from a SplitMix64 inside the list, with a seed of its own; with a seed, from a Gsl_Compatible_Rng, the Mersenne twister with the numbers of the one of GSL - GNU Scientific Library, which it used before: for a seed, the levels are the same.
sizeof(Node) + sizeof(Node*) * level| Key | Key type (must be comparable with <, ==) |
| Type | Data type stored alongside keys |
Definition at line 95 of file tpl_skipList.H.
|
inline |
Construct a SkipList with given seed and probability, whose levels are those of GSL for the seed: it draws them from a Mersenne Twister, in dynamic memory.
| [in] | seed | Random seed for level generation, taken modulo 2^32 - 1, as it was for GSL. |
| [in] | p | Probability for level increase (default 0.5). |
| std::bad_alloc | if there is not enough memory for the Mersenne Twister. |
Definition at line 316 of file tpl_skipList.H.
References Aleph::and, and Aleph::SkipList< Key, Type >::probability.
|
inlineexplicit |
Construct a SkipList that draws its levels from a SplitMix64 with a seed of its own, without allocating.
| [in] | p | Probability for level increase (default 0.5). |
Definition at line 329 of file tpl_skipList.H.
References Aleph::and, and Aleph::SkipList< Key, Type >::probability.
|
delete |
|
inline |
Take the nodes and the generator of another list, which is left empty, and draws from then on from a SplitMix64 with a seed of its own.
It does not allocate.
| [in,out] | other | the list to move from. |
| what | the construction of the header throws: the copy of the sentinel key or the default construction of the data; then other is unchanged. |
Definition at line 350 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::swap().
|
inlinenoexcept |
Get iterator to first element.
Definition at line 686 of file tpl_skipList.H.
|
inlinenoexcept |
Verify skip list invariants (for debugging).
Definition at line 526 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::Node::get_key(), Aleph::SkipList< Key, Type >::Node::get_next(), and Aleph::SkipList< Key, Type >::header.
Referenced by Aleph::SkipList< Key, Type >::insert(), and Aleph::SkipList< Key, Type >::remove().
|
inlinenoexcept |
Get iterator past the last element.
Definition at line 689 of file tpl_skipList.H.
|
inlinenoexcept |
Generate a random level for a new node.
Uses the Mersenne Twister of rng() for high-quality randomness.
Definition at line 514 of file tpl_skipList.H.
References Aleph::and, l, Aleph::SkipList< Key, Type >::maxLevel, Aleph::SkipList< Key, Type >::probability, Aleph::SkipList< Key, Type >::r, and Aleph::Random_Source::uniform().
|
inlinestaticprivatenoexcept |
Definition at line 254 of file tpl_skipList.H.
References Aleph::Gsl_Compatible_Rng::max(), and seed.
|
inlinenoexcept |
Get the first node in the list.
Definition at line 446 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::headerPtr.
|
inlinenoexcept |
Return the level of the list: the number of levels in use.
It lets check that removals bring it down, so that searches do not start at levels that are empty.
Definition at line 458 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::Node::level.
|
inlinenoexcept |
Insert a node into the skip list.
| [in] | p | Node to insert (must be pre-allocated with correct level). |
Definition at line 406 of file tpl_skipList.H.
References Aleph::and, Aleph::SkipList< Key, Type >::checkSkipList(), Aleph::SkipList< Key, Type >::Node::get_key(), Aleph::SkipList< Key, Type >::Node::getForward(), Aleph::SkipList< Key, Type >::Node::getLevel(), Aleph::SkipList< Key, Type >::headerPtr, Aleph::SkipList< Key, Type >::Node::level, and Aleph::SkipList< Key, Type >::maxLevel.
|
inlinenoexcept |
Placeholder for node allocation (override in derived classes).
Definition at line 542 of file tpl_skipList.H.
|
delete |
|
inlinenoexcept |
Exchange this list with another, which is left with the previous nodes and generator of this one.
| [in,out] | other | the list to move from. |
Definition at line 360 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::swap().
|
inlinenoexcept |
The source of random numbers, to look at it: its engine, or whether it is equal to another one.
Definition at line 279 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::r.
|
inlinenoexcept |
The source of random numbers, which draws the levels.
Definition at line 275 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::r.
|
inlinenoexcept |
Remove a key from the skip list.
The level of the list goes down to that of its tallest node left, so that the next searches do not start at levels that are empty.
| [in] | searchKey | Key to remove. |
searchKey. The sentinel, whose key is computeMaxKey(), is never removed; a node inserted with that key is. Definition at line 471 of file tpl_skipList.H.
References Aleph::and, Aleph::SkipList< Key, Type >::checkSkipList(), Aleph::SkipList< Key, Type >::Node::get_key(), Aleph::SkipList< Key, Type >::Node::getForward(), Aleph::SkipList< Key, Type >::Node::getForward(), Aleph::SkipList< Key, Type >::HeaderNode::getForward(), Aleph::SkipList< Key, Type >::headerPtr, Aleph::SkipList< Key, Type >::Node::level, and Aleph::SkipList< Key, Type >::maxLevel.
|
inline |
The Mersenne Twister of the list, which a list that was given no seed makes here, and draws from it from then on.
| std::bad_alloc | If the list has no Mersenne Twister and there is not enough memory for it. |
Definition at line 292 of file tpl_skipList.H.
References Aleph::Random_Source::mersenne_twister(), and Aleph::SkipList< Key, Type >::r.
Referenced by TEST().
|
inlinenoexcept |
Search for a key in the skip list.
| [in] | searchKey | Key to search for. |
Definition at line 384 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::Node::get_key(), Aleph::SkipList< Key, Type >::Node::getForward(), Aleph::SkipList< Key, Type >::headerPtr, and Aleph::SkipList< Key, Type >::Node::level.
|
inline |
Seeds the Mersenne Twister of the list, as gsl_rng_set() did: the levels are from then on those of GSL for the seed.
A list that was given no seed makes its Mersenne Twister here.
| [in] | seed | The seed. |
| std::bad_alloc | If the list has no Mersenne Twister and there is not enough memory for it; then it is unchanged. |
Definition at line 269 of file tpl_skipList.H.
References Aleph::Random_Source::Mersenne_Twister, Aleph::SkipList< Key, Type >::r, Aleph::Random_Source::seed(), and seed.
Referenced by TEST().
|
inlinenoexcept |
Exchange the nodes, the generators and the probabilities of this list and another.
| [in,out] | other | the list to exchange with. |
Definition at line 371 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::header, Aleph::SkipList< Key, Type >::Node::level, Aleph::SkipList< Key, Type >::maxLevel, Aleph::SkipList< Key, Type >::probability, Aleph::SkipList< Key, Type >::r, and Aleph::Random_Source::swap().
Referenced by Aleph::SkipList< Key, Type >::SkipList(), and Aleph::SkipList< Key, Type >::operator=().
|
static |
Default probability for level generation (0.5 = geometric distribution)
Definition at line 102 of file tpl_skipList.H.
|
private |
Definition at line 247 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::checkSkipList(), and Aleph::SkipList< Key, Type >::swap().
|
private |
Definition at line 248 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::get_first(), Aleph::SkipList< Key, Type >::insert(), Aleph::SkipList< Key, Type >::remove(), Aleph::SkipList< Key, Type >::Iterator::reset_first(), and Aleph::SkipList< Key, Type >::search().
|
private |
Definition at line 251 of file tpl_skipList.H.
|
static |
Maximum level for any node (supports up to 2^32 elements efficiently)
Definition at line 99 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::HeaderNode::HeaderNode(), Aleph::SkipList< Key, Type >::generateRandomLevel(), Aleph::SkipList< Key, Type >::insert(), Aleph::SkipList< Key, Type >::remove(), and Aleph::SkipList< Key, Type >::swap().
|
private |
Definition at line 250 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::SkipList(), Aleph::SkipList< Key, Type >::SkipList(), Aleph::SkipList< Key, Type >::generateRandomLevel(), and Aleph::SkipList< Key, Type >::swap().
|
private |
Definition at line 249 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::generateRandomLevel(), Aleph::SkipList< Key, Type >::random_source(), Aleph::SkipList< Key, Type >::random_source(), Aleph::SkipList< Key, Type >::rng(), Aleph::SkipList< Key, Type >::set_seed(), Aleph::SkipList< Key, Type >::swap(), TEST(), and TYPED_TEST().