-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSkipLists.cc
180 lines (165 loc) · 4.56 KB
/
SkipLists.cc
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
//
// Created by ENVY on 2022/3/9.
//
#include <iostream>
#include "SkipLists.h"
double SkipLists::my_rand()
{
s = (16807 * s) % 2147483647ULL;
return (s + 0.0) / 2147483647ULL;
}
int SkipLists::randomLevel()
{
int result = 1;
while (result < MAX_LEVEL && my_rand() < 0.5)
{
++result;
}
return result;
}
void SkipLists::scan(uint64_t key_start, uint64_t key_end, std::list<std::pair<uint64_t, std::string> > &list)
{
SKNode* x = head;
// -- loop invariant: head→key < searchKey
for (int i = MAX_LEVEL - 1; i >= 0 ; --i){
while(x->forwards[i]->key < key_start){
x = x->forwards[i];
}
}
// -- x→key < searchKey ≤ x→forward[1]→key
x = x->forwards[0];
if (x->key == key_start) { // find
if(x->val != "~DELETED~"){
std::pair<uint64_t, std::string> new_pair(x->key, x->val);
list.push_back(new_pair);
}
}
while(x->forwards[0]->key <= key_end){
x = x->forwards[0];
if(x->val != "~DELETED~"){
std::pair<uint64_t, std::string> new_pair(x->key, x->val);
list.push_back(new_pair);
}
}
}
void SkipLists::put(uint64_t key, const std::string &value)
{
SKNode* x = head;
std::vector<SKNode *> update;
for (int i = 0; i < MAX_LEVEL; ++i)
{
update.push_back(nullptr);
}
for (int i = MAX_LEVEL - 1; i >= 0; --i) {
while (x->forwards[i]->key < key) {
x = x->forwards[i];
}
update[i] = x;
}
// -- x→key < searchKey ≤ x→forward[i]→key
x = x->forwards[0];
if (x->key == key) {
// size 的增加由预测时做
this->setSize(this->getSize() - (x->val).length() - KEY_BYTES_SIZE - OFFSET_BYTES_SIZE);
x->val = value;
}
else{
int level = randomLevel();
x = new SKNode(key, value, SKNodeType::NORMAL);
for(int i = 0; i < level; ++i) {
x->forwards[i] = update[i]->forwards[i];
update[i]->forwards[i] = x;
}
}
}
std::string SkipLists::get(uint64_t key)
{
SKNode* x = head;
// -- loop invariant: head→key < searchKey
for (int i = MAX_LEVEL - 1; i >= 0 ; --i){
while(x->forwards[i]->key < key){
x = x->forwards[i];
}
}
// -- x→key < searchKey ≤ x→forward[1]→key
x = x->forwards[0];
if (x->key == key) { // found, return x->value
return x->val;
}
else { // not found, return failure
return "";
}
}
bool SkipLists::del(uint64_t key)
{
std::vector<SKNode *> update;
for (int i = 0; i < MAX_LEVEL; ++i)
{
update.push_back(nullptr);
}
SKNode* x = head;
for (int i = MAX_LEVEL - 1; i >= 0; --i){
while(x->forwards[i]->key < key){
x = x->forwards[i];
}
update[i] = x;
}
x = x->forwards[0];
if(x->key == key){
if(x->val == "~DELETED~") return false;
for(int i = 0; i < MAX_LEVEL; ++i){
if(update[i]->forwards[i] != x) break;
update[i]->forwards[i] = x->forwards[i];
}
this->setSize(this->getSize() - (x->val).length() - KEY_BYTES_SIZE - OFFSET_BYTES_SIZE);
delete x;
return true;
}
return false;
}
void SkipLists::display()
{
for (int i = MAX_LEVEL - 1; i >= 0; --i)
{
std::cout << "Level " << i + 1 << ":h";
SKNode *node = head->forwards[i];
while (node->type != SKNodeType::NIL)
{
std::cout << "-->(" << node->key << "," << node->val << ")";
node = node->forwards[i];
}
std::cout << "-->N" << std::endl;
}
}
void SkipLists::reset()
{
SKNode *n1 = head->forwards[0];
SKNode *n2;
while (n1->type != SKNodeType::NIL)
{
n2 = n1->forwards[0];
delete n1;
n1 = n2;
}
for (int i = 0; i < MAX_LEVEL; ++i)
{
head->forwards[i] = NIL;
}
setSize(INIT_BYTES_SIZE);
}
void SkipLists::getAll(std::list<std::pair<uint64_t, std::string> > &all, uint64_t &minKey, uint64_t &maxKey, uint64_t &numKey)
{
SKNode *n1 = head->forwards[0];
SKNode *n2;
numKey = 0;
minKey = n1->key;
while (n1->type != SKNodeType::NIL)
{
n2 = n1->forwards[0];
std::pair<uint64_t, std::string> new_pair(n1->key, n1->val);
++numKey;
all.push_back(new_pair);
maxKey = n1->key;
n1 = n2;
}
}