41 template<
typename K_TYPE,
60 friend class skipList;
69 explicit skipListNode(
const K_TYPE& key = K_TYPE{},
const V_TYPE& value = V_TYPE{},
70 u_integer levels = 1, std::initializer_list<skipListNode*> next = {});
156 mutable std::mt19937
gen_{std::random_device{}()};
157 mutable std::uniform_real_distribution<floating>
dis_{0.0, 1.0};
248 skipListNode*
createNode(
const K_TYPE& key = K_TYPE{},
const V_TYPE& value = V_TYPE{},
249 u_integer levels = 1, std::initializer_list<skipListNode*> next = {})
const;
282 static bool equal(
const K_TYPE& key, skipListNode* next);
332 skipListNode*
find(
const K_TYPE& key)
const;
340 bool modify(
const K_TYPE& key,
const V_TYPE& value);
349 bool insert(
const K_TYPE& key,
const V_TYPE& value);
373template <typename K_TYPE, typename V_TYPE, typename ALLOC, typename
Compare>
377 if (next.size() != 0 &&
static_cast<u_integer>(next.size()) != levels) {
381 for (
auto&& ptr: next) {
382 this->next_[i] = ptr;
387template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
388original::couple<const K_TYPE, V_TYPE>&
393template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
399template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
402 return this->data_.template get<0>();
405template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
408 return this->data_.template get<1>();
411template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
414 return this->data_.template get<1>();
417template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
419 return this->next_.
size();
422template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
427 auto increment = new_levels - this->
getLevels();
428 for (
u_integer i = 0; i < increment; ++i) {
433template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
435 if (new_levels >= this->
getLevels() || new_levels == 0){
438 auto decrement = this->
getLevels() - new_levels;
439 for (
u_integer i = 0; i < decrement; ++i) {
444template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
446 this->data_.template
set<1>(value);
449template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
452 return this->next_[levels - 1];
455template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
458 this->next_[levels - 1] = next;
461template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
469template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
473template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
478template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
489template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
491 return this->
cur_->getPNext(1);
494template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
496 this->
cur_ = this->
cur_->getPNext(1);
499template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
505template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
511 for (
integer i = 0; i < steps; ++i) {
516template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
520 pos_dis != std::numeric_limits<integer>::max())
return pos_dis;
522 neg_dis != std::numeric_limits<integer>::max())
return -neg_dis;
524 std::numeric_limits<integer>::max() :
525 std::numeric_limits<integer>::min();
528template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
535 return this->
cur_->getVal();
538template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
545 return this->
cur_->getVal();
548template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
553template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
559 while (s->isValid()){
560 if (s->cur_ == e->cur_){
567 dis = std::numeric_limits<integer>::max();
572template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
575 std::initializer_list<skipListNode*> next)
const {
577 this->
rebind_alloc.construct(node, key, value, levels, next);
581template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
587template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
596template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
605template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
612 return key == next->
getKey();
615template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
620 while (this->
dis_(this->
gen_) < p) {
626template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
629 return this->head_->getLevels();
632template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
634 this->head_->expandLevels(new_levels);
637template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
639 this->head_->shrinkLevels(new_levels);
642template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
646 this->
createNode(this->head_->getKey(), this->head_->getValue(), this->getCurLevels());
649 auto src_cur = this->head_;
650 while (src_cur->getPNext(1)){
651 auto src_next = src_cur->getPNext(1);
652 auto copied_next = this->
createNode(src_next->getKey(), src_next->getValue(), src_next->getLevels());
653 for (
u_integer i = 0; i < src_next->getLevels(); ++i) {
655 copied_curs[i] = copied_next;
663template<
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
666 auto cur = this->head_;
667 while (cur->getPNext(1)){
668 cur = cur->getPNext(1);
673template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
677template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
681 if (this->size_ == 0){
686 auto cur_p = this->head_;
691 if (
equal(key, next_p)){
701 return equal(key, next_p) ? next_p :
nullptr;
704template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
707 auto cur_p = this->
find(key);
712 cur_p->setValue(value);
716template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
728 while (cur->getPNext(i) && !this->highPriority(key, cur->getPNext(i))) {
729 cur = cur->getPNext(i);
731 if (i <= new_levels) {
734 if (
equal(key, cur) && cur != this->head_) {
740 auto new_node = this->
createNode(key, value, new_levels);
741 for (
u_integer i = 0; i < new_levels; ++i) {
742 auto new_next = update[i]->getPNext(i + 1);
751template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
754 auto cur_p = this->
find(key);
759 auto cur_levels = cur_p->getLevels();
762 for (
u_integer i = 0; i < cur_levels; ++i) {
763 next_nodes[i] = cur_p->getPNext(i + 1);
767 if (!next_node || !this->
highPriority(next_node, cur_p)){
770 prev_nodes[i] = next_node;
773 for (
u_integer i = 0; i < cur_levels; ++i) {
780 if (this->head_->getPNext(i)){
792template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
795 auto cur = this->head_;
797 auto next = cur->getPNext(1);
803template <
typename K_TYPE,
typename V_TYPE,
typename ALLOC,
typename Compare>
Default memory allocator using allocators utilities.
Definition allocator.h:154
Container for two heterogeneous elements.
Definition couple.h:37
Comparator for increasing comparison (less than).
Definition comparator.h:63
Exception for container index out-of-range errors.
Definition error.h:84
Unique ownership smart pointer with move semantics.
Definition ownerPtr.h:30
Abstract base class for unique element containers.
Definition set.h:44
Forward iterator for skipList.
Definition skipList.h:164
Iterator & operator=(const Iterator &other)
Copy assignment operator.
Definition skipList.h:480
static integer ptrDistance(const Iterator *start, const Iterator *end)
Calculates distance between two iterators.
Definition skipList.h:555
integer operator-(const Iterator &other) const
Calculates distance between iterators.
Definition skipList.h:518
void operator+=(integer steps) const
Advances iterator by steps.
Definition skipList.h:506
couple< const K_TYPE, V_TYPE > & get()
Gets current element (non-const)
Definition skipList.h:530
skipListNode * cur_
Current node pointer.
Definition skipList.h:166
Iterator(skipListNode *cur=nullptr)
Constructs iterator.
Definition skipList.h:470
void next() const
Moves to next element.
Definition skipList.h:495
bool isValid() const
Checks if iterator is valid.
Definition skipList.h:549
Iterator * clone() const
Creates a copy of this iterator.
Definition skipList.h:501
bool hasNext() const
Checks if more elements exist forward.
Definition skipList.h:490
Internal node class for Skip List.
Definition skipList.h:54
void expandLevels(u_integer new_levels)
Expands node to more levels.
Definition skipList.h:423
void setPNext(u_integer levels, skipListNode *next)
Sets next node at specified level.
Definition skipList.h:456
void shrinkLevels(u_integer new_levels)
Shrinks node to fewer levels.
Definition skipList.h:434
static void connect(u_integer levels, skipListNode *prev, skipListNode *next)
Connects two nodes at specified level.
Definition skipList.h:462
u_integer getLevels() const
Gets number of levels for this node.
Definition skipList.h:418
const V_TYPE & getValue() const
Gets value (const)
Definition skipList.h:407
couple< const K_TYPE, V_TYPE > & getVal()
Gets key-value pair (non-const)
Definition skipList.h:389
const K_TYPE & getKey() const
Gets key.
Definition skipList.h:401
skipListNode(const K_TYPE &key=K_TYPE{}, const V_TYPE &value=V_TYPE{}, u_integer levels=1, std::initializer_list< skipListNode * > next={})
Constructs a new skipListNode.
Definition skipList.h:374
void setValue(const V_TYPE &value)
Sets new value.
Definition skipList.h:445
skipListNode * getPNext(u_integer levels) const
Gets next node at specified level.
Definition skipList.h:451
rebind_alloc_node rebind_alloc
Node allocator.
Definition skipList.h:155
skipListNode * findLastNode() const
Finds last node in list.
Definition skipList.h:665
void listDestroy() noexcept
Destroys entire list and deallocates all nodes.
Definition skipList.h:793
u_integer size_
Definition skipList.h:152
skipListNode * head_
Definition skipList.h:153
skipListNode * listCopy() const
Creates a deep copy of the list.
Definition skipList.h:644
u_integer getCurLevels() const
Gets current number of levels in list.
Definition skipList.h:628
bool insert(const K_TYPE &key, const V_TYPE &value)
Inserts new key-value pair.
Definition skipList.h:717
skipListNode * createNode(const K_TYPE &key=K_TYPE{}, const V_TYPE &value=V_TYPE{}, u_integer levels=1, std::initializer_list< skipListNode * > next={}) const
Creates a new node with given parameters.
Definition skipList.h:574
void expandCurLevels(u_integer new_levels)
Expands list to more levels.
Definition skipList.h:633
void shrinkCurLevels(u_integer new_levels)
Shrinks list to fewer levels.
Definition skipList.h:638
skipListNode * find(const K_TYPE &key) const
Finds node with given key.
Definition skipList.h:679
std::uniform_real_distribution< floating > dis_
Definition skipList.h:157
bool modify(const K_TYPE &key, const V_TYPE &value)
Modifies value for existing key.
Definition skipList.h:705
static bool equal(const K_TYPE &key, skipListNode *next)
Checks if key matches node's key.
Definition skipList.h:606
typename ALLOC::template rebind_alloc< skipListNode * > rebind_alloc_pointer
Definition skipList.h:150
bool highPriority(const K_TYPE &key, skipListNode *next) const
Compares priority between a key and a node.
Definition skipList.h:597
Compare compare_
Definition skipList.h:154
bool highPriority(skipListNode *cur, skipListNode *next) const
Compares priority between two nodes.
Definition skipList.h:588
~skipList()
Destructor.
Definition skipList.h:804
void destroyNode(skipListNode *node) const
Destroys a node and deallocates memory.
Definition skipList.h:582
u_integer getRandomLevels()
Generates random number of levels for new node.
Definition skipList.h:616
bool erase(const K_TYPE &key)
Erases node with given key.
Definition skipList.h:752
skipList(Compare compare=Compare{})
Constructs skipList with given comparison function.
Definition skipList.h:674
std::mt19937 gen_
Definition skipList.h:156
typename ALLOC::template rebind_alloc< skipListNode > rebind_alloc_node
Definition skipList.h:149
Exception for unimplemented method calls.
Definition error.h:123
Dynamic array container with amortized constant time operations.
Definition vector.h:43
u_integer size() const override
Gets the size of the vector.
Definition vector.h:640
void pushEnd(const TYPE &e) override
Inserts an element at the end of the vector.
Definition vector.h:739
TYPE popEnd() override
Removes and returns the last element in the vector.
Definition vector.h:790
Comparator base class and concrete comparator classes.
Combines Comparable and CallbackOf for comparison callbacks.
Definition types.h:92
Generic pair container implementation.
Main namespace for the project Original.
Definition algorithms.h:21
std::uint32_t u_integer
32-bit unsigned integer type for sizes and indexes
Definition config.h:43
double floating
Double-precision floating-point type.
Definition config.h:51
std::int64_t integer
64-bit signed integer type for arithmetic operations
Definition config.h:35
Dynamic array container with automatic resizing.