68# ifndef TPL_DYNSKIPLIST_H
69# define TPL_DYNSKIPLIST_H
73# include <type_traits>
75# include <initializer_list>
121template <
typename Key,
class Compare = std::less<Key>>
159 for (
int i = 0; i < lvl; ++i)
166 for (
int i = 0; i < lvl; ++i)
174 for (
int i = 0; i < lvl; ++i)
206 bool keys_equal(
const Key & k1,
const Key & k2)
const noexcept
208 return not
cmp(k1, k2)
and not
cmp(k2, k1);
231 if constexpr (std::is_copy_constructible_v<Compare>)
232 return static_cast<const Compare &
>(c);
240 std::swap(
header, other.header);
279 [[deprecated(
"it makes a Mersenne Twister in a list that has none: "
280 "use random_source(); it goes in 8.0.0")]]
292 [[deprecated(
"the generator is no longer of GSL: use random_source(); it goes in 8.0.0")]]
293 gsl_rng * gsl_rng_object() {
return r.gsl(); }
312 <<
"DynSkipList: probability " << p <<
" is not in (0, 1)";
328 <<
"DynSkipList: probability " << p <<
" is not in (0, 1)";
353 for (
auto it = other.
begin(); it.has_curr(); it.next())
408 for (
auto it = other.
begin(); it.has_curr(); it.next())
460 while (curr !=
nullptr)
527 Node * new_node =
new Node(key, lvl);
529 for (
int i = 0; i < lvl; ++i)
532 update[i]->
forward[i] = new_node;
536 return &new_node->
key;
571 Node * new_node =
new Node(std::move(key), lvl);
573 for (
int i = 0; i < lvl; ++i)
576 update[i]->
forward[i] = new_node;
580 return &new_node->
key;
594 <<
"DynSkipList::insert_unique: key already exists";
610 if (Key * ptr =
insert(std::move(key)); ptr !=
nullptr)
626 Key * existing =
search(key);
627 if (existing !=
nullptr)
637 [[nodiscard]]
const Key *
search(
const Key & key)
const noexcept
655 [[nodiscard]] Key *
search(
const Key & key)
noexcept
657 return const_cast<Key *
>(std::as_const(*this).search(key));
661 [[nodiscard]]
bool has(
const Key & key)
const noexcept
663 return search(key) !=
nullptr;
672 [[nodiscard]]
bool contains(
const Key & key)
const noexcept {
return has(key); }
675 [[nodiscard]]
bool exist(
const Key & key)
const noexcept {
return has(key); }
683 [[nodiscard]] Key &
find(
const Key & key)
687 <<
"DynSkipList::find: key not found";
692 [[nodiscard]]
const Key &
find(
const Key & key)
const
694 const Key * ptr =
search(key);
696 <<
"DynSkipList::find: key not found";
724 if (update[i]->forward[i] != x)
759 <<
"DynSkipList::del: key not found";
761 Key ret_val = std::move(x->
key);
765 if (update[i]->forward[i] != x)
780 [[nodiscard]]
const Key &
min()
const
790 [[nodiscard]]
const Key &
max()
const
795 while (x->
forward[0] !=
nullptr)
815 for (
auto it = other.
begin(); it.has_curr(); it.next())
816 result.
insert(it.get_curr());
829 if (other.
has(it.get_curr()))
830 result.
insert(it.get_curr());
843 if (not other.
has(it.get_curr()))
844 result.
insert(it.get_curr());
870 [[nodiscard]]
bool has_curr() const noexcept {
return curr !=
nullptr; }
914 return curr == it.curr;
919 return curr != it.curr;
961 template <
class Operation>
967 if (not
static_cast<bool>(op(
static_cast<const Key &
>(it.get_curr_ne()))))
973 template <
class Operation>
978 return traverse<Operation>(op);
Variadic constructor macros for containers.
Container traversal and functional operation mixins.
Exception handling system with formatted messages for Aleph-w.
#define ah_overflow_error_if(C)
Throws std::overflow_error if condition holds.
#define ah_domain_error_if(C)
Throws std::domain_error if condition holds.
STL-compatible iterator adapters for Aleph containers.
The random number generators of Aleph: the one that gives the numbers of the gsl_rng_mt19937 of GSL,...
DRY (Don't Repeat Yourself) utilities and macros.
#define Special_Ctors(Set_Type, Type)
Generates special constructors for containers.
Functional programming utilities for Aleph-w containers.
Iterator traits and STL-compatible iterator wrappers.
Forward iterator for DynSkipList.
Iterator & operator=(const Iterator &) noexcept=default
bool operator!=(const Iterator &it) const noexcept
bool is_last() const noexcept
const Key * operator->() const
const Key & get_curr_ne() const noexcept
Iterator operator++(int) noexcept
const Key & get_curr() const
Iterator() noexcept=default
void reset_first() noexcept
const Key & get_key() const
const DynSkipList * list_ptr
bool operator==(const Iterator &it) const noexcept
const Key & operator*() const
bool has_curr() const noexcept
Iterator & operator++() noexcept
Dynamic ordered set implemented with a Skip List.
Key & append(const Key &key)
Append method for Special_Ctors compatibility (aliases to insert)
Key * search_or_insert(const Key &key)
Search and insert if not found.
static unsigned long generator_seed(const unsigned long seed) noexcept
size_t remove(const Key &key)
Remove a key from the set.
void empty() noexcept
Remove all elements from the set.
static constexpr int maxLevel
Maximum number of levels (supports up to 2^32 elements efficiently)
DynSkipList(const DynSkipList &other)
Copy constructor: the copy draws the levels of its nodes from a SplitMix64 with a seed of its own,...
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.
Iterator begin() const noexcept
DynSkipList(const double p, Compare &&c)
static constexpr double defaultProbability
Default probability for level generation (0.5 = geometric distribution)
bool exist(const Key &key) const noexcept
Alias for has()
void clear() noexcept
Empties the container.
bool traverse(Operation &op) const noexcept(Aleph::NothrowPredicateWith< Operation &, const Key & >)
Traverse the keys in increasing order.
Key * insert(Key &&key)
Insert an element into the set (move version).
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...
Key del(const Key &key)
Remove and return a key.
Key Item_Type
The type of element that stores the container.
DynSkipList & operator=(DynSkipList &&other) noexcept(noexcept(swap(other)))
Move assignment: free the nodes of this list, and exchange it with another, which is left empty with ...
Iterator get_it() const noexcept
Alias for begin() - for generic programming.
static decltype(auto) comparison_to_take(Compare &c) noexcept
Random_Source & random_source() noexcept
The source of random numbers, which draws the levels.
Key & append(Key &&key)
See append(const Key & key).
Compare & get_compare() noexcept
Get the comparison functor.
bool contains(const Key &key) const noexcept
Checks if a key exists in the set.
DynSkipList(double p=defaultProbability)
Construct a DynSkipList that draws its levels from a SplitMix64 with a seed of its own.
DynSkipList(const double p, const Compare &c)
DynSkipList & operator=(const DynSkipList &other)
Copy assignment operator.
~DynSkipList()
Destructor - frees all nodes and the random number generator.
Key & find(const Key &key)
Find a key, throwing if not found.
Key * search(const Key &key) noexcept
This is an overloaded member function, provided for convenience. It differs from the above function o...
DynSkipList intersect(const DynSkipList &other) const
Compute the intersection of two sets.
DynSkipList diff(const DynSkipList &other) const
Compute the difference of two sets.
const Key & min() const
Return the minimum element (first in sorted order)
void swap(DynSkipList &other) noexcept(std::is_nothrow_swappable_v< Compare >)
Swap contents with another DynSkipList.
const Compare & get_compare() const noexcept
Get the comparison functor (const version)
Key * insert(const Key &key)
Insert an element into the set.
Key & insert_unique(const Key &key)
Insert an element, throwing if it already exists.
DynSkipList join(const DynSkipList &other) const
Compute the union of two sets.
Iterator end() const noexcept
bool is_empty() const noexcept
Return true if the set is empty.
DynSkipList(DynSkipList &&other)
Move constructor: take the nodes, the generator and the comparison criterion of another list,...
const Key & find(const Key &key) const
Const version of find()
size_t size() const noexcept
Return the number of elements in the set.
bool traverse(Operation &&op=Operation()) const noexcept(Aleph::NothrowPredicateWith< Operation &, const Key & >)
See traverse(Operation & op) const.
bool keys_equal(const Key &k1, const Key &k2) const noexcept
DynSkipList(unsigned long seed, double p=defaultProbability)
Construct a DynSkipList with given seed and probability, whose levels are those of GSL for the seed: ...
void swap_but_comparison(DynSkipList &other) noexcept
bool has(const Key &key) const noexcept
Return true if the key exists in the set.
const Key & get_first() const
Alias for min()
const Key & max() const
Return the maximum element (last in sorted order)
const Key * search(const Key &key) const noexcept
Search for a key in the set.
const Key & get_last() const
Alias for max()
int random_level() const noexcept
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...
Key Key_Type
The type of element that stores the container.
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.
Common methods to the Aleph-w ( ) containers.
and
Insert a new element into the container by constructing it from the given args.
Common sequential searching methods on containers.
Mixin that adds STL begin()/end() and cbegin()/cend() to Aleph containers.
PredicateWith<F, Args...>, and neither the call nor the conversion of its result to bool can throw.
f(args...) is a valid call whose result is usable as a condition.
Main namespace for Aleph-w library functions.
void next()
Advance all underlying iterators (bounds-checked).
Node(const Key &k, int lvl)
Generic list of items stored in a container.
Generic traversal of the container through its iterator.
Special constructors common to Aleph-w ( ) containers.