51# ifndef TPL_SKIPLIST_H
52# define TPL_SKIPLIST_H
94 template <
class Key,
class Type>
124 return reinterpret_cast<Node **
>(
reinterpret_cast<char *
>(
this) +
sizeof(*
this));
129 return reinterpret_cast<Node *
const *
>(
130 reinterpret_cast<const char *
>(
this) +
sizeof(*
this));
135 for (
int i = 0; i <
level; i++)
189 [[nodiscard]]
const Key &
get_key() const noexcept {
return key; }
231 assert(this->
getLevel() <= maxLevel);
290 [[deprecated(
"it makes a Mersenne Twister in a list that has none: "
291 "use random_source(); it goes in 8.0.0")]]
303 [[deprecated(
"the generator is no longer of GSL: use random_source(); it goes in 8.0.0")]]
304 gsl_rng * gsl_rng_object() {
return r.gsl(); }
374 std::swap(
header.HeaderNode::forward[i], other.header.HeaderNode::forward[i]);
377 std::swap(
level, other.level);
388 for (
int i =
level - 1; i >= 0; i--)
395 if (x == Node::NullPtr || x->
get_key() != searchKey)
412 assert(p->getLevel() > 0
and p->getLevel() <=
maxLevel);
414 p->fillForwardnullptr();
416 for (i =
level - 1; i >= 0; i--)
426 if (p->getLevel() >
level)
428 for (i =
level; i < p->getLevel(); i++)
434 for (i = 0; i < p->getLevel(); i++)
448 return headerPtr->HeaderNode::forward[0];
477 for (i =
level - 1; i >= 0; i--)
488 if (x != Node::NullPtr
and x->
get_key() == searchKey)
490 for (i = 0; i <
level; i++)
529 while (node !=
nullptr)
572 : list_ptr(&list), curr(list.headerPtr->
HeaderNode::forward[0])
575 if (curr == Node::NullPtr)
581 [[nodiscard]]
bool has_curr() const noexcept {
return curr !=
nullptr; }
589 return next ==
nullptr;
632 if (list_ptr !=
nullptr)
634 curr = list_ptr->
headerPtr->HeaderNode::forward[0];
635 if (curr == Node::NullPtr)
641 void reset() noexcept { reset_first(); }
646 list_ptr = it.list_ptr;
654 return curr == it.curr;
660 return curr != it.curr;
695 template <
class Key,
class Type>
696 typename SkipList<Key, Type>::Node SkipList<Key, Type>::Node::nodeSentinel;
698 template <
class Key,
class Type>
699 typename SkipList<Key, Type>::Node *SkipList<Key, Type>::Node::NullPtr =
700 &SkipList<Key, Type>::Node::nodeSentinel;
703 template <
class Key,
class Type>
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
Core header for the Aleph-w library.
A random number generator with the sequences of GSL's gsl_rng_mt19937.
static constexpr result_type max() noexcept
The greatest number of operator()(): 2^32 - 1.
The source of random numbers of a randomized structure, which draws from one of two generators.
@ Mersenne_Twister
A Gsl_Compatible_Rng, in dynamic memory.
Gsl_Compatible_Rng & mersenne_twister()
The Mersenne Twister of the source, which is its engine from now on.
double uniform() noexcept
A real number in [0, 1).
void seed(const std::uint64_t s) noexcept
Draws from now on from the SplitMix64 of a seed.
void swap(Random_Source &o) noexcept
Swaps the generators, with their states.
Iterator for traversing SkipList elements in ascending order.
const Type & get_data() const
Get current data (throws if no current).
Node * operator->() const
Arrow operator (returns node pointer).
Iterator & operator=(const Iterator &it) noexcept
Assignment operator.
bool is_last() const noexcept
Check if iterator is on the last element.
Iterator operator++(int) noexcept
Postfix increment operator.
bool operator==(const Iterator &it) const noexcept
Equality comparison.
Iterator() noexcept=default
Default constructor (invalid iterator).
void next_ne() noexcept
Advance to next element without exception check.
Node * get_curr() const
Get current node (throws if no current).
Node * operator*() const
Dereference operator (returns node pointer).
Iterator & operator++() noexcept
Prefix increment operator.
bool has_curr() const noexcept
Check if iterator has a current element.
void next()
Advance to next element (throws if no current).
void reset_first() noexcept
Reset iterator to first element.
Node * get_curr_ne() const noexcept
Get current node without exception check.
const Key & get_key() const
Get current key (throws if no current).
void reset() noexcept
Reset iterator to first element (alias).
bool operator!=(const Iterator &it) const noexcept
Inequality comparison.
Node(const Key &_key, const Type &_data, int n)
Node(const Key &_key, int n)
Node * get_next() const noexcept
Node *& getForward(int i)
const Key & get_key() const noexcept
int getLevel() const noexcept
Node ** forward_ptr() noexcept
void fillForwardnullptr()
static Key computeMaxKey() noexcept
Compute the maximum possible key value (used for sentinel).
Node *const * forward_ptr() const noexcept
Type & get_data() noexcept
const Type & get_data() const noexcept
Skip List - a probabilistic ordered data structure.
int get_level() const noexcept
Return the level of the list: the number of levels in use.
Node * get_first() noexcept
Get the first node in the list.
bool checkSkipList() const noexcept
Verify skip list invariants (for debugging).
Node * remove(const Key &searchKey) noexcept
Remove a key from the skip list.
static const int maxLevel
Maximum level for any node (supports up to 2^32 elements efficiently)
int generateRandomLevel() noexcept
Generate a random level for a new node.
Gsl_Compatible_Rng & rng()
The Mersenne Twister of the list, which a list that was given no seed makes here, and draws from it f...
Iterator begin() const noexcept
Get iterator to first element.
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 GS...
SkipList(SkipList &&other)
Take the nodes and the generator of another list, which is left empty, and draws from then on from a ...
SkipList & operator=(SkipList &&other) noexcept
Exchange this list with another, which is left with the previous nodes and generator of this one.
Random_Source & random_source() noexcept
The source of random numbers, which draws the levels.
static unsigned long generator_seed(const unsigned long seed) noexcept
SkipList(const SkipList &)=delete
Node * search(const Key &searchKey) noexcept
Search for a key in the skip list.
void swap(SkipList &other) noexcept
Exchange the nodes, the generators and the probabilities of this list and another.
static const double defaultProbability
Default probability for level generation (0.5 = geometric distribution)
Iterator end() const noexcept
Get iterator past the last element.
SkipList(double p=defaultProbability)
Construct a SkipList that draws its levels from a SplitMix64 with a seed of its own,...
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 ...
Node * new_node() noexcept
Placeholder for node allocation (override in derived classes).
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.
Node * insert(Node *p) noexcept
Insert a node into the skip list.
SkipList & operator=(const SkipList &)=delete
Main namespace for Aleph-w library functions.
auto get_curr() const
Return the current tuple (bounds-checked).
and
Check uniqueness with explicit hash + equality functors.
void next()
Advance all underlying iterators (bounds-checked).
void next_ne() noexcept(noexcept(std::declval< Aleph::const_iterator_t< HeadC > & >().next_ne()) and noexcept(std::declval< ZipIterator< TailC... > & >().next_ne()))
Advance all underlying iterators (unchecked variant).