This repository was archived by the owner on Oct 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSkipList.h
177 lines (158 loc) · 5.48 KB
/
SkipList.h
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#ifndef SKIPLIST_H
#define SKIPLIST_H
#include<cmath>
#include<random>
#include<algorithm>
#include<limits>
#include<map>
#define SKIPLIST_MAX_LEVEL 32
template<class Key,class Value>
class SkipList;
template<class Key,class Value>
class SkipListNode{
friend class SkipList<Key,Value>;
SkipListNode **forward;
Key key;
Value value;
int level;
inline SkipListNode(Key k=Key(),Value v=Value(),int l=SKIPLIST_MAX_LEVEL,SkipListNode *nxt=nullptr)
:key(k),value(v),level(l){
forward=new SkipListNode *[l+1];
for(int i=0;i<l+1;i++)forward[i]=nxt;
}
inline ~SkipListNode(){
if(forward!=nullptr)delete[] forward;
}
};
template<class Key,class Value>
class SkipList{
private:
//static constexpr int MAX_LEVEL=32;
const double GROW_THRESHOLD;
int siz=0,level=0;
std::mt19937 rgen;
std::uniform_real_distribution<> rdis=std::uniform_real_distribution<>(0.0,1.0);
SkipListNode<Key,Value> *head,*tail;
inline bool ok_to_grow(){
return rdis(rgen)<GROW_THRESHOLD;
}
inline int rand_level(){
int lv=1;
while(ok_to_grow())lv++;
return std::min(lv,SKIPLIST_MAX_LEVEL);//should not exceed the limit
}
public:
SkipList(double threshold=0.5):GROW_THRESHOLD(threshold){
std::random_device rdev;
rgen.seed(rdev());
tail=new SkipListNode<Key,Value>(std::numeric_limits<Key>::max(),Value(),0);
head=new SkipListNode<Key,Value>(std::numeric_limits<Key>::max(),Value(),SKIPLIST_MAX_LEVEL,tail);
}
~SkipList(){
if(head!=nullptr){
SkipListNode<Key,Value> *tmpnext,*tmpcur=head;
while(tmpcur!=tail){
tmpnext=tmpcur->forward[0];
delete tmpcur;
tmpcur=tmpnext;
}
delete tail;
}
}
void clear(){
if(head!=nullptr){
SkipListNode<Key,Value> *tmpnext,*tmpcur=head;
while(tmpcur!=tail){
tmpnext=tmpcur->forward[0];
delete tmpcur;
tmpcur=tmpnext;
}
delete tail;
}
std::random_device rdev;
rgen.seed(rdev());
tail=new SkipListNode<Key,Value>(std::numeric_limits<Key>::max(),Value(),0);
head=new SkipListNode<Key,Value>(std::numeric_limits<Key>::max(),Value(),SKIPLIST_MAX_LEVEL,tail);
siz=0;
level=0;
}
void insert(const Key &k,Value v){
SkipListNode<Key,Value> *update[SKIPLIST_MAX_LEVEL+1];
SkipListNode<Key,Value> *cur_ptr=head;
for(int i=level;i>=0;i--){
while(cur_ptr->forward[i]!=nullptr&&cur_ptr->forward[i]->key<k){
cur_ptr=cur_ptr->forward[i];
}
update[i]=cur_ptr;
}
if(cur_ptr->key==k){
cur_ptr->value=v;
return;
}
int lv=rand_level();
if(lv>level){
lv=++level;//grow, lv=new max level
update[lv]=head;
}
SkipListNode<Key,Value> *new_node=new SkipListNode<Key,Value>(k,v,lv);//the forward will be updated later
for(int i=lv;i>=0;i--){
cur_ptr=update[i];
new_node->forward[i]=cur_ptr->forward[i];
cur_ptr->forward[i]=new_node;
}
siz++;
}
Value *at(const Key &k,int *steps=nullptr) {
int cnt=0;//bool ret=true;
SkipListNode<Key,Value> *cur_ptr=head;
for(int i=level;i>=0;i--){
while(cur_ptr->forward[i]!=nullptr&&cur_ptr->forward[i]->key<k){
cur_ptr=cur_ptr->forward[i];
cnt++;
}
}
//cur_ptr->key<k
cur_ptr=cur_ptr->forward[0];
cnt++;
if(steps!=nullptr)*steps=cnt;
if(cur_ptr->key==k)return &(cur_ptr->value);
return nullptr;
}
inline int size(){return this->siz;}
class iterator{
friend class SkipList;
private:
SkipList *skipList;
SkipListNode<Key,Value> *skipListNode;
iterator(SkipList *skipList,SkipListNode<Key,Value> *skipListNode):skipList(skipList),skipListNode(skipListNode){}
public:
iterator operator++(){
this->skipListNode=this->skipListNode->forward[0];
return *this;
}
iterator operator++(int){
iterator ret=*this;
this->skipListNode=this->skipListNode->forward[0];
return ret;
}
bool operator==(const iterator &right)const{
return this->skipList==right.skipList&&this->skipListNode==right.skipListNode;
}
bool operator!=(const iterator &right)const{
return !(this->operator==(right));
}
const Key &getKey()const{
return this->skipListNode->key;
}
const Value &getValue()const{
return this->skipListNode->value;
}
};
iterator begin(){
return iterator(this,this->head->forward[0]);
}
iterator end(){
return iterator(this,this->tail);
}
};
#endif