当前位置:   article > 正文

跳跃表SkipList(简单的C++实现)_c++ skiplist

c++ skiplist

不懂跳跃表的请直接百度
直接上实现

#include <iostream>
#include <string.h>
#include <ctime>

using namespace std;

template<typename T>
struct SkipNode {
    inline static constexpr int Rate=3;
    inline static constexpr int MaxHeight=8;
    inline static constexpr intmax_t Inf= INT_MAX;
    inline static SkipNode<T>* update[MaxHeight];

    intmax_t key;
    T val;
    SkipNode<T> *pre[MaxHeight], *nex[MaxHeight];

    SkipNode<T>(intmax_t k=Inf,const T& v = {})
        : key(k), val(v)
    {
        init_pos(nullptr);
    }

    auto init_pos(SkipNode<T>* ptr=nullptr)
    {
        for (int h=0; h<MaxHeight; ++h) {
            pre[h]=nex[h]=ptr;
        }
        return this;
    }

    SkipNode<T>* escape()
    {
        for(int h=0; h<MaxHeight; ++h) {
            auto *p=pre[h], *x=nex[h];
            pre[h]=nex[h]=nullptr;
            if(p)p->nex[h]=x;
            if(x)x->pre[h]=p;
        }
        return this;
    }

    intmax_t upper_bound(intmax_t key__, SkipNode<T> *(&update__)[MaxHeight]=update)
    {
        memset(update__,0,sizeof(update__));
        auto *p = this;
        for(int h=MaxHeight-1; h>=0; --h) {
            while(p->nex[h] && p->nex[h]!=this && p->nex[h]->key > key__) p=p->nex[h];
            update__[h]=p;
        }
        return p->nex[0] ? p->nex[0]->key : Inf;
    }

    void into(SkipNode<T> *(&update__)[MaxHeight]=update)
    {
        this->init_pos(nullptr);
        for(int h=0; h<MaxHeight; ++h) {
            if (update__[h]) {
                auto *x = update__[h]->nex[h];
                update__[h]->nex[h]=this;
                this->nex[h]=x;
                if(x)x->pre[h]=this;
                if(this)this->pre[h]=update__[h];
            }
            if(rand()%Rate) break;
        }
    }

    void print()
    {
        for(auto *p = this->nex[0]; p && p!=this; p=p->nex[0]) {
            cout << "("<<p->key << ":" << p->val<<"), ";
        }
        endl(cout);
    }

};

template<typename ValType>
void skiplist_insert(SkipNode<ValType> &hand,intmax_t key,ValType val)
{
    decltype(hand.update) assist{};
    if(key == hand.upper_bound(key,assist)) {
        assist[0]->nex[0]->val = val;
    } else {
        (new SkipNode<ValType>(key,val))->into(assist);
    }
}

template<typename ValType>
bool skiplist_erase(SkipNode<ValType> &hand,intmax_t key) {
    decltype(hand.update) assist{};
    if (key == hand.upper_bound(key,assist)) {
        delete assist[0]->nex[0]->escape();
        return true;
    }
    return false;
}

void test_insert0()
{
    SkipNode<int> hand;
    hand.init_pos(&hand);
    srand(time(0));
    skiplist_insert<int>(hand,32,78);
    skiplist_insert<int>(hand,36,78);
    skiplist_insert<int>(hand,32,71);
    skiplist_insert<int>(hand,33,78);
    skiplist_insert<int>(hand,311,766);
    hand.print();
}

void test_erase0() {
    SkipNode<int> hand;
    hand.init_pos(&hand);
    srand(time(0));
    skiplist_insert<int>(hand,32,78);
    skiplist_insert<int>(hand,36,78);
    skiplist_insert<int>(hand,32,71);
    skiplist_insert<int>(hand,33,78);
    skiplist_insert<int>(hand,311,766);
    hand.print();
    skiplist_erase(hand,100);
    hand.print();
    skiplist_erase(hand,311);
    hand.print();
}

int main()
{
    test_insert0();
    test_erase0();
    return 0;
}


  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/酷酷是懒虫/article/detail/756222
推荐阅读
相关标签
  

闽ICP备14008679号