-
Notifications
You must be signed in to change notification settings - Fork 0
/
bst.cc
212 lines (196 loc) · 5.29 KB
/
bst.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
#ifndef BST_H_
#include "bst.h"
#endif
#include <sys/mman.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <errno.h>
BSTree::BSTree(std::string po_name) {
po_name_ = po_name;
pod_ = -1;
meta_ = NULL;
}
int BSTree::Open() {
/* just the owner has read and write privilege */
pod_ = po_open(po_name_.c_str(), O_CREAT|O_RDWR, S_IRUSR|S_IWUSR);
if(pod_==-1) {
std::cout<<"open po failed, errno: "<<errno<<std::endl;
return -1;
}
/* get po state */
struct po_stat statbuf;
if(po_fstat(pod_, &statbuf) == -1) {
std::cout<<"get po state failed, errno: "<<errno<<std::endl;
return -1;
}
/* map po */
if(statbuf.st_size != 0) {
unsigned long chunks[1];
chunks[0] = (unsigned long)NULL;
bool is_first_chunk = true;
do {
if (po_chunk_next(pod_, chunks[0], 1, chunks) < 0) {
std::cout << "po chunk next error, errno: " << errno << std::endl;
return -1;
}
if (chunks[0] == (unsigned long)NULL)
break;
if (is_first_chunk) {
meta_ = (BSTreeMetadata*)(chunks[0]);
is_first_chunk = false;
}
if (po_chunk_mmap(pod_, chunks[0], PROT_READ|PROT_WRITE, MAP_PRIVATE) < 0) {
std::cout << "po chunk mmap error, errno: " << errno << std::endl;
return -1;
}
} while(true);
}else {
/* the first is used to store metadata */
meta_ = (BSTreeMetadata *)po_extend(pod_, PAGE_SIZE, PROT_READ|PROT_WRITE, MAP_PRIVATE);
if(meta_ == NULL) {
std::cout<<"malloc from po failed, errno: "<<errno<<std::endl;
return -1;
}
meta_->root_node_ = NULL;
meta_->size_ = 0;
#ifdef USE_SLAB
/* init slab */
po_memory_alloc_init(&meta_->s, sizeof(struct BSTreeNode));
meta_->s.pod = pod_;
#endif
}
return 0;
}
int BSTree::Insert(Value_t val) {
if(meta_ == NULL) {
return -1;
}
BSTreeNode *tmp;
#ifdef USE_SLAB
tmp = (BSTreeNode *)po_malloc(&meta_->s);
#else
tmp = (BSTreeNode*)po_malloc(pod_, sizeof(struct BSTreeNode));
#endif
if(tmp == NULL) {
std::cout<<"malloc from po failed, errno: "<<errno<<std::endl;
return -1;
}
tmp->value_ = val;
tmp->left_ = NULL;
tmp->right_ = NULL;
tmp->parent_ = NULL;
if(meta_->root_node_ == NULL) {
meta_->root_node_ = tmp;
meta_->size_++;
return 0;
}
BSTreeNode *curr, *parent;
curr = parent = meta_->root_node_;
while(curr != NULL) {
parent = curr;
if(DefaultCmp(curr->value_, val) >= 0) {
curr = curr->left_;
}else {
curr = curr->right_;
}
}
tmp->parent_ = parent;
if(DefaultCmp(parent->value_, val) >= 0) {
parent->left_ = tmp;
}else {
parent->right_ = tmp;
}
meta_->size_++;
return 0;
}
bool BSTree::Delete(Value_t val) {
BSTreeNode *found_node = Find(val);
if(found_node == NULL) {
return false;
}
if(found_node->left_ == NULL) {
Transplant(found_node, found_node->right_);
}else if(found_node->right_ == NULL) {
Transplant(found_node, found_node->left_);
}else {
BSTreeNode *successor = Minimum((found_node->right_));
if(found_node->right_ != successor) {
Transplant(successor, successor->right_);
successor->right_ = found_node->right_;
successor->right_->parent_ = successor;
}
Transplant(found_node, successor);
successor->left_ = found_node->left_;
successor->left_->parent_ = successor;
}
meta_->size_--;
#ifdef USE_SLAB
po_free(&meta_->s, found_node);
#else
po_free(pod_, found_node);
#endif
return true;
}
bool BSTree::Search(Value_t val) {
BSTreeNode *found_node = Find(val);
if (found_node == NULL)
return false;
else
return true;
}
BSTreeNode* BSTree::Find(Value_t val) {
if (meta_ == NULL) {
return NULL;
}
if (meta_->root_node_ == NULL) {
return NULL;
}
BSTreeNode *curr = meta_->root_node_;
int cmp_result;
while(curr != NULL) {
cmp_result = DefaultCmp(curr->value_, val);
if(cmp_result == 0) {
return curr;
}else if(cmp_result > 0) {
curr = curr->left_;
}else {
curr = curr->right_;
}
}
return NULL;
}
BSTreeNode* BSTree::Minimum(BSTreeNode *curr) {
if(curr == NULL) {
return NULL;
}
while(curr->left_!=NULL) {
curr = curr->left_;
}
return curr;
}
void BSTree::Transplant(BSTreeNode *old_node, BSTreeNode *new_node) {
if(old_node->parent_ == NULL) {
meta_->root_node_ = new_node;
}else if(old_node->parent_->left_ == old_node) {
old_node->parent_->left_ = new_node;
}else {
old_node->parent_->right_ = new_node;
}
if(new_node != NULL) {
new_node->parent_ = old_node->parent_;
}
}
int BSTree::DefaultCmp(Value_t val1, Value_t val2) {
if(val1 == val2) {
return 0;
}else if(val1 > val2) {
return 1;
}else {
return -1;
}
}
int BSTree::Size() {
if (meta_ != nullptr)
return meta_->size_;
return -1;
}