Aleph-w 7.10.0
A C++ Library for Data Structures and Algorithms
Loading...
Searching...
No Matches
Aleph::SkipList< Key, Type > Class Template Reference

Skip List - a probabilistic ordered data structure. More...

#include <tpl_skipList.H>

Collaboration diagram for Aleph::SkipList< Key, Type >:
[legend]

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
 

Detailed Description

template<class Key, class Type>
class Aleph::SkipList< Key, Type >

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:

  • Expected O(log n) search, insert, delete
  • O(n) space complexity
  • Simple implementation compared to balanced trees
  • Good cache locality for sequential access

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.

Warning
Nodes must be allocated with extra space for forward pointers: sizeof(Node) + sizeof(Node*) * level
Template Parameters
KeyKey type (must be comparable with <, ==)
TypeData type stored alongside keys
See also
Pugh, W. "Skip Lists: A Probabilistic Alternative to Balanced Trees" Communications of the ACM, 1990.

Definition at line 95 of file tpl_skipList.H.

Constructor & Destructor Documentation

◆ SkipList() [1/4]

template<class Key , class Type >
Aleph::SkipList< Key, Type >::SkipList ( unsigned long  seed,
double  p = defaultProbability 
)
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.

Parameters
[in]seedRandom seed for level generation, taken modulo 2^32 - 1, as it was for GSL.
[in]pProbability for level increase (default 0.5).
Exceptions
std::bad_allocif 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.

◆ SkipList() [2/4]

template<class Key , class Type >
Aleph::SkipList< Key, Type >::SkipList ( double  p = defaultProbability)
inlineexplicit

Construct a SkipList that draws its levels from a SplitMix64 with a seed of its own, without allocating.

Parameters
[in]pProbability for level increase (default 0.5).

Definition at line 329 of file tpl_skipList.H.

References Aleph::and, and Aleph::SkipList< Key, Type >::probability.

◆ SkipList() [3/4]

template<class Key , class Type >
Aleph::SkipList< Key, Type >::SkipList ( const SkipList< Key, Type > &  )
delete

◆ SkipList() [4/4]

template<class Key , class Type >
Aleph::SkipList< Key, Type >::SkipList ( SkipList< Key, Type > &&  other)
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.

Parameters
[in,out]otherthe list to move from.
Exceptions
whatthe 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().

Member Function Documentation

◆ begin()

template<class Key , class Type >
Iterator Aleph::SkipList< Key, Type >::begin ( ) const
inlinenoexcept

Get iterator to first element.

Definition at line 686 of file tpl_skipList.H.

◆ checkSkipList()

template<class Key , class Type >
bool Aleph::SkipList< Key, Type >::checkSkipList ( ) const
inlinenoexcept

Verify skip list invariants (for debugging).

Returns
true if all keys are in ascending order.

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().

◆ end()

template<class Key , class Type >
Iterator Aleph::SkipList< Key, Type >::end ( ) const
inlinenoexcept

Get iterator past the last element.

Definition at line 689 of file tpl_skipList.H.

◆ generateRandomLevel()

template<class Key , class Type >
int Aleph::SkipList< Key, Type >::generateRandomLevel ( )
inlinenoexcept

Generate a random level for a new node.

Uses the Mersenne Twister of rng() for high-quality randomness.

Returns
Random level in range [1, maxLevel].

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().

◆ generator_seed()

template<class Key , class Type >
static unsigned long Aleph::SkipList< Key, Type >::generator_seed ( const unsigned long  seed)
inlinestaticprivatenoexcept

Definition at line 254 of file tpl_skipList.H.

References Aleph::Gsl_Compatible_Rng::max(), and seed.

◆ get_first()

template<class Key , class Type >
Node * Aleph::SkipList< Key, Type >::get_first ( )
inlinenoexcept

Get the first node in the list.

Returns
First node, or NullPtr if empty.

Definition at line 446 of file tpl_skipList.H.

References Aleph::SkipList< Key, Type >::headerPtr.

◆ get_level()

template<class Key , class Type >
int Aleph::SkipList< Key, Type >::get_level ( ) const
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.

Returns
the level of the tallest node, or 0 if the list is empty.

Definition at line 458 of file tpl_skipList.H.

References Aleph::SkipList< Key, Type >::Node::level.

◆ insert()

template<class Key , class Type >
Node * Aleph::SkipList< Key, Type >::insert ( Node *  p)
inlinenoexcept

Insert a node into the skip list.

Parameters
[in]pNode to insert (must be pre-allocated with correct level).
Returns
The inserted node.
Precondition
p->getLevel() > 0 and p->getLevel() <= maxLevel

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.

◆ new_node()

template<class Key , class Type >
Node * Aleph::SkipList< Key, Type >::new_node ( )
inlinenoexcept

Placeholder for node allocation (override in derived classes).

Definition at line 542 of file tpl_skipList.H.

◆ operator=() [1/2]

template<class Key , class Type >
SkipList & Aleph::SkipList< Key, Type >::operator= ( const SkipList< Key, Type > &  )
delete

◆ operator=() [2/2]

template<class Key , class Type >
SkipList & Aleph::SkipList< Key, Type >::operator= ( SkipList< Key, Type > &&  other)
inlinenoexcept

Exchange this list with another, which is left with the previous nodes and generator of this one.

Parameters
[in,out]otherthe list to move from.
Returns
a reference to this list.

Definition at line 360 of file tpl_skipList.H.

References Aleph::SkipList< Key, Type >::swap().

◆ random_source() [1/2]

template<class Key , class Type >
const Random_Source & Aleph::SkipList< Key, Type >::random_source ( ) const
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.

◆ random_source() [2/2]

template<class Key , class Type >
Random_Source & Aleph::SkipList< Key, Type >::random_source ( )
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.

◆ remove()

template<class Key , class Type >
Node * Aleph::SkipList< Key, Type >::remove ( const Key &  searchKey)
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.

Parameters
[in]searchKeyKey to remove.
Returns
Pointer to removed node, or nullptr if no node of the list has searchKey. The sentinel, whose key is computeMaxKey(), is never removed; a node inserted with that key is.
Note
Caller is responsible for deallocating the returned node.

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.

◆ rng()

template<class Key , class Type >
Gsl_Compatible_Rng & Aleph::SkipList< Key, Type >::rng ( )
inline

The Mersenne Twister of the list, which a list that was given no seed makes here, and draws from it from then on.

Deprecated:
It makes a generator of some 2500 bytes in a list that has none; use random_source(). It goes in 8.0.0.
Returns
The Mersenne Twister: Random_Source::mersenne_twister().
Exceptions
std::bad_allocIf 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().

◆ search()

template<class Key , class Type >
Node * Aleph::SkipList< Key, Type >::search ( const Key &  searchKey)
inlinenoexcept

Search for a key in the skip list.

Parameters
[in]searchKeyKey to search for.
Returns
Pointer to node containing the key, or nullptr if not found.

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.

◆ set_seed()

template<class Key , class Type >
void Aleph::SkipList< Key, Type >::set_seed ( unsigned long  seed)
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.

Parameters
[in]seedThe seed.
Exceptions
std::bad_allocIf 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().

◆ swap()

template<class Key , class Type >
void Aleph::SkipList< Key, Type >::swap ( SkipList< Key, Type > &  other)
inlinenoexcept

Exchange the nodes, the generators and the probabilities of this list and another.

Parameters
[in,out]otherthe list to exchange with.
Note
O(maxLevel): the forward pointers of the headers.

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=().

Member Data Documentation

◆ defaultProbability

template<class Key , class Type >
const double Aleph::SkipList< Key, Type >::defaultProbability = 0.5
static

Default probability for level generation (0.5 = geometric distribution)

Definition at line 102 of file tpl_skipList.H.

◆ header

template<class Key , class Type >
HeaderNode Aleph::SkipList< Key, Type >::header
private

◆ headerPtr

◆ level

template<class Key , class Type >
int Aleph::SkipList< Key, Type >::level
private

Definition at line 251 of file tpl_skipList.H.

◆ maxLevel

template<class Key , class Type >
const int Aleph::SkipList< Key, Type >::maxLevel = 32
static

◆ probability

◆ r


The documentation for this class was generated from the following file: