-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskiplist.cpp
More file actions
203 lines (181 loc) · 3.76 KB
/
skiplist.cpp
File metadata and controls
203 lines (181 loc) · 3.76 KB
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
#include "skiplist.h"
#include <time.h>
#include <chrono>
#include <random>
skiplist::skiplist():max_level_(1)
{
head_ = new node_t(INT_MIN, 1);
srand(time(0));
}
skiplist::~skiplist()
{
auto p = head_;
while (p)
{
auto d = p;
p = p->next[0];
delete d;
}
}
void skiplist::insert(int val)
{
int level = random_generate_level();
auto p = new node_t(val, level);
node_t** pre = new node_t*[level];
for (int i = 0; i < level; ++i)
{
pre[i] = head_;
}
//
node_t* cur = head_;
for (auto i = level - 1; i >= 0; --i)//最高层开始
{
//每层遍历 找到小于val的最后一个节点
while (cur->next[i] != nullptr && cur->next[i]->val < val)
cur = cur->next[i];
//
pre[i] = cur;
}
//
for (size_t i = 0; i < level; i++)
{
p->next[i] = pre[i]->next[i];
pre[i]->next[i] = p;
}
max_level_ = level > max_level_ ? level : max_level_;
delete[] pre;
}
void skiplist::earse(int val)
{
node_t** pre = new node_t*[SKIP_LIST_MAX_LEVEL];
for (auto i = 0; i < SKIP_LIST_MAX_LEVEL; ++i)
{
pre[i] = nullptr;
}
auto cur = head_;
for (auto i = max_level_ - 1; i >= 0; i--)//最高层开始
{
//每层遍历 找到小于val的最后一个节点
while (cur->next[i] != nullptr && cur->next[i]->val < val)
cur = cur->next[i];
//
pre[i] = cur;
}
//
if (cur->next[0] != nullptr && cur->next[0]->val == val)
{
auto t = cur->next[0];
for (auto i = SKIP_LIST_MAX_LEVEL - 1; i >= 0; i--)
{
if (pre[i] == nullptr)
continue;
//
if (pre[i]->next[i] != nullptr && pre[i]->next[i]->val == val)
pre[i]->next[i] = pre[i]->next[i]->next[i];
}
delete t;
}
//
while (max_level_ > 1 && head_->next[max_level_ - 1] == nullptr)
--max_level_;
delete[] pre;
}
int skiplist::rank(int val)
{
node_t* p = head_;
int r = 0;
while (p->next[0] != nullptr && p->next[0]->val < val)
{
p = p->next[0];
r++;
}
return r;
}
int skiplist::rank(int val, node_t*& lower, node_t* &upper)
{
lower = lower_bound(val);
upper = upper_bound(val);
return rank(val);
}
node_t* skiplist::get(int idx)
{
node_t* p = head_;
int r = 0;
while (p->next[0] != nullptr && --idx)
{
p = p->next[0];
}
//
if (idx == 0)
return p;
return nullptr;
}
node_t* skiplist::find(int key)
{
auto p = head_;
for (size_t i = 0; i < max_level_; i++) // lower_bound
{
while (p->next[i] && p->next[i]->val < key)
p = p->next[i];
}
if (p->next[0] != nullptr && p->next[0]->val == key)
{
return p->next[0];
}
return nullptr;
}
void skiplist::print()
{
for (auto i = max_level_ - 1; i >=0; --i)
{
auto p = head_->next[i];
printf("Level %d: %d", i, head_->val);
while (p)
{
printf("-> %d", p->val);
p = p->next[i];
}
printf("\n");
}
printf("\n");
}
size_t skiplist::level()
{
return max_level_;
}
int skiplist::random_generate_level()
{
int level = 1;
const unsigned int seed = std::chrono::system_clock::now().time_since_epoch().count(); //获取系统时间戳,单位微秒(microsecond)。
std::default_random_engine generator(seed); //系统时间戳作为种子
std::uniform_real_distribution<float> distribution(0, 1);
float random_num = distribution(generator); //均匀分布,生成区间[0,1]的浮点数
while (random_num < rate_ && level < SKIP_LIST_MAX_LEVEL) { //模拟每一次都是50%的概率小于probability
random_num = distribution(generator);
++level;
}
return level;
}
node_t* skiplist::lower_bound(int val)
{
node_t* p = head_;
for (auto i = max_level_ - 1; i >= 0; i--)//最高层开始
{
//每层遍历 找到小于val的最后一个节点
while (p->next[i] != nullptr && p->next[i]->val < val)
p = p->next[i];
}
return p;
}
node_t* skiplist::upper_bound(int val)
{
node_t* p = head_;
for (auto i = max_level_ - 1; i >= 0; i--)//最高层开始
{
//每层遍历 找到小于val的最后一个节点
while (p->next[i] != nullptr && p->next[i]->val <= val)
p = p->next[i];
}
//
return p->next[0]; //第一个大于 val
}