|
Aleph-w 3.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) noexcept |
| Set the random number generator seed. | |
| gsl_rng * | gsl_rng_object () noexcept |
| Get a pointer to the GSL random number generator. | |
| SkipList (unsigned long seed, double p=defaultProbability) | |
| Construct a SkipList with given seed and probability. | |
| SkipList (double p=defaultProbability) | |
| Construct a SkipList with time-based seed. | |
| ~SkipList () | |
| Destructor - frees GSL random number generator. | |
| SkipList (const SkipList &)=delete | |
| SkipList & | operator= (const SkipList &)=delete |
| SkipList (SkipList &&other) noexcept | |
| SkipList & | operator= (SkipList &&other) noexcept |
| 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. | |
| 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) | |
Private Member Functions | |
| void | init (unsigned long seed) |
Private Attributes | |
| HeaderNode | header |
| HeaderNode * | headerPtr |
| gsl_rng * | 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 class internally uses the gsl random number generator of GSL - GNU Scientific Library. By default, the Mersenne twister is used and the seed is taken from system time.
sizeof(Node) + sizeof(Node*) * level| Key | Key type (must be comparable with <, ==) |
| Type | Data type stored alongside keys |
Definition at line 93 of file tpl_skipList.H.
|
inline |
Construct a SkipList with given seed and probability.
| [in] | seed | Random seed for level generation. |
| [in] | p | Probability for level increase (default 0.5). |
Definition at line 270 of file tpl_skipList.H.
References Aleph::init, Aleph::maps(), and Aleph::SkipList< Key, Type >::probability.
|
inlineexplicit |
Construct a SkipList with time-based seed.
| [in] | p | Probability for level increase (default 0.5). |
Definition at line 283 of file tpl_skipList.H.
Destructor - frees GSL random number generator.
Definition at line 290 of file tpl_skipList.H.
References Aleph::maps(), and Aleph::SkipList< Key, Type >::r.
|
delete |
|
inlinenoexcept |
Definition at line 301 of file tpl_skipList.H.
References Aleph::maps().
|
inlinenoexcept |
Get iterator to first element.
Definition at line 617 of file tpl_skipList.H.
|
inlinenoexcept |
Verify skip list invariants (for debugging).
Definition at line 457 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 620 of file tpl_skipList.H.
|
inlinenoexcept |
Generate a random level for a new node.
Uses GSL Mersenne Twister for high-quality randomness.
Definition at line 445 of file tpl_skipList.H.
References l, Aleph::maps(), Aleph::SkipList< Key, Type >::maxLevel, Aleph::SkipList< Key, Type >::probability, and Aleph::SkipList< Key, Type >::r.
Get the first node in the list.
Definition at line 396 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::headerPtr.
|
inlinenoexcept |
Get a pointer to the GSL random number generator.
Definition at line 264 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::r.
|
inlineprivate |
Definition at line 252 of file tpl_skipList.H.
References ah_bad_alloc_if, Aleph::maps(), and Aleph::SkipList< Key, Type >::r.
|
inlinenoexcept |
Insert a node into the skip list.
| [in] | p | Node to insert (must be pre-allocated with correct level). |
Definition at line 356 of file tpl_skipList.H.
References 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, Aleph::maps(), and Aleph::SkipList< Key, Type >::maxLevel.
Placeholder for node allocation (override in derived classes).
Definition at line 473 of file tpl_skipList.H.
|
delete |
|
inlinenoexcept |
Definition at line 313 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::header, Aleph::SkipList< Key, Type >::headerPtr, Aleph::SkipList< Key, Type >::Node::level, Aleph::maps(), Aleph::SkipList< Key, Type >::probability, and Aleph::SkipList< Key, Type >::r.
|
inlinenoexcept |
Remove a key from the skip list.
| [in] | searchKey | Key to remove. |
Definition at line 406 of file tpl_skipList.H.
References 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, Aleph::maps(), and Aleph::SkipList< Key, Type >::maxLevel.
|
inlinenoexcept |
Search for a key in the skip list.
| [in] | searchKey | Key to search for. |
Definition at line 334 of file tpl_skipList.H.
References Aleph::SkipList< Key, Type >::Node::get_key(), Aleph::SkipList< Key, Type >::Node::getForward(), Aleph::SkipList< Key, Type >::headerPtr, Aleph::SkipList< Key, Type >::Node::level, and Aleph::maps().
|
inlinenoexcept |
Set the random number generator seed.
Definition at line 261 of file tpl_skipList.H.
References Aleph::maps(), and Aleph::SkipList< Key, Type >::r.
|
static |
Default probability for level generation (0.5 = geometric distribution)
Definition at line 100 of file tpl_skipList.H.
|
private |
Definition at line 245 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::checkSkipList(), and Aleph::SkipList< Key, Type >::operator=().
|
private |
Definition at line 246 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::get_first(), Aleph::SkipList< Key, Type >::insert(), Aleph::SkipList< Key, Type >::operator=(), Aleph::SkipList< Key, Type >::remove(), Aleph::SkipList< Key, Type >::Iterator::reset_first(), and Aleph::SkipList< Key, Type >::search().
Definition at line 249 of file tpl_skipList.H.
Maximum level for any node (supports up to 2^32 elements efficiently)
Definition at line 97 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::HeaderNode::HeaderNode(), Aleph::SkipList< Key, Type >::generateRandomLevel(), Aleph::SkipList< Key, Type >::insert(), and Aleph::SkipList< Key, Type >::remove().
Definition at line 248 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::SkipList(), Aleph::SkipList< Key, Type >::generateRandomLevel(), and Aleph::SkipList< Key, Type >::operator=().
Definition at line 247 of file tpl_skipList.H.
Referenced by Aleph::SkipList< Key, Type >::~SkipList(), Aleph::SkipList< Key, Type >::generateRandomLevel(), Aleph::SkipList< Key, Type >::gsl_rng_object(), Aleph::SkipList< Key, Type >::init(), Aleph::SkipList< Key, Type >::operator=(), and Aleph::SkipList< Key, Type >::set_seed().