51# ifndef TPL_SKIPLIST_H
52# define TPL_SKIPLIST_H
56# include <gsl/gsl_rng.h>
92 template <
class Key,
class Type>
122 return reinterpret_cast<Node **
>(
reinterpret_cast<char *
>(
this) +
sizeof(*
this));
127 return reinterpret_cast<Node *
const *
>(
128 reinterpret_cast<const char *
>(
this) +
sizeof(*
this));
133 for (
int i = 0; i <
level; i++)
338 for (
int i =
level - 1; i >= 0; i--)
364 p->fillForwardnullptr();
366 for (i =
level - 1; i >= 0; i--)
376 if (p->getLevel() >
level)
378 for (i =
level; i < p->getLevel(); i++)
384 for (i = 0; i < p->getLevel(); i++)
398 return headerPtr->HeaderNode::forward[0];
412 for (i =
level - 1; i >= 0; i--)
423 for (i = 0; i <
level; i++)
460 while (node !=
nullptr)
503 : list_ptr(&list), curr(list.headerPtr->
HeaderNode::forward[0])
506 if (curr == Node::NullPtr)
512 [[nodiscard]]
bool has_curr() const noexcept {
return curr !=
nullptr; }
520 return next ==
nullptr;
563 if (list_ptr !=
nullptr)
565 curr = list_ptr->
headerPtr->HeaderNode::forward[0];
566 if (curr == Node::NullPtr)
572 void reset() noexcept { reset_first(); }
577 list_ptr = it.list_ptr;
585 return curr == it.curr;
591 return curr != it.curr;
626 template <
class Key,
class Type>
627 typename SkipList<Key, Type>::Node SkipList<Key, Type>::Node::nodeSentinel;
629 template <
class Key,
class Type>
630 typename SkipList<Key, Type>::Node *SkipList<Key, Type>::Node::NullPtr =
631 &SkipList<Key, Type>::Node::nodeSentinel;
634 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.
#define ah_bad_alloc_if(C)
Throws std::bad_alloc if condition holds.
Core header for the Aleph-w library.
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.
~SkipList()
Destructor - frees GSL random number generator.
void set_seed(unsigned long seed) noexcept
Set the random number generator seed.
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.
void init(unsigned long seed)
Iterator begin() const noexcept
Get iterator to first element.
SkipList & operator=(SkipList &&other) noexcept
SkipList(const SkipList &)=delete
Node * search(const Key &searchKey) noexcept
Search for a key in the skip list.
static const double defaultProbability
Default probability for level generation (0.5 = geometric distribution)
Iterator end() const noexcept
Get iterator past the last element.
SkipList(SkipList &&other) noexcept
SkipList(double p=defaultProbability)
Construct a SkipList with time-based seed.
SkipList(unsigned long seed, double p=defaultProbability)
Construct a SkipList with given seed and probability.
Node * new_node() noexcept
Placeholder for node allocation (override in derived classes).
Node * insert(Node *p) noexcept
Insert a node into the skip list.
gsl_rng * gsl_rng_object() noexcept
Get a pointer to the GSL random number generator.
SkipList & operator=(const SkipList &)=delete
Main namespace for Aleph-w library functions.
void next_ne() noexcept
Advance all underlying iterators (no-throw variant).
auto get_curr() const
Return the current tuple (bounds-checked).
void next()
Advance all underlying iterators (bounds-checked).
DynList< T > maps(const C &c, Op op)
Classic map operation.