-
Notifications
You must be signed in to change notification settings - Fork 0
/
bufferList.hpp
108 lines (92 loc) · 2.52 KB
/
bufferList.hpp
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
#ifndef BPTREE_HPP_BUFFERLIST_HPP
#define BPTREE_HPP_BUFFERLIST_HPP
template<class T, int L = 50>
class bufferList {
private:
int size = 0;
struct linkNode {
T val;
linkNode *nxt, *fro;
linkNode(const T &tmp, linkNode *nxt_ = nullptr, linkNode *fro_ = nullptr) {
val = tmp, nxt = nxt_, fro = fro_;
}
};
linkNode *head, *rear;
public:
bufferList() {
size = 0;
T tmp;
head = new linkNode(tmp, nullptr, nullptr);
rear = new linkNode(tmp, nullptr, head);
head->nxt = rear;
}
~bufferList() {
delete head;
delete rear;
}
//查找是否存在相关元素
std::pair<bool, T> find(int pos) {
linkNode *p = head->nxt;
while (p != rear) {
if (pos == p->val.pos)return std::make_pair(true, p->val);
p = p->nxt;
}
T tmp;
return std::make_pair(false, tmp);
}
void remove(int pos) {
linkNode *p = head->nxt, *del;
while (p != rear) {
del = p, p = p->nxt;
if (pos == del->val.pos) {
size--;
del->fro->nxt = del->nxt;
del->nxt->fro = del->fro;
delete del;
}
}
}
//插入元素 并判断有没有需要写入文件的节点弹出
std::pair<bool, T> insert(const T &val) {
remove(val.pos);
size++;
head->nxt = new linkNode(val, head->nxt, head);
head->nxt->nxt->fro = head->nxt;
if (size == L) {
size--;
linkNode *del = rear->fro;
del->fro->nxt = rear;
rear->fro = del->fro;
std::pair<bool, T> tmp= std::make_pair(true,del->val);
delete del;
return tmp;
}
return std::make_pair(false, val);
}
//判断bufferlist是否为空
bool empty() {
return size == 0;
}
//弹出需要写入的节点 从尾弹出
T pop_back() {
linkNode *p = head->nxt;
p->fro->nxt = p->nxt;
p->nxt->fro = p->fro;
linkNode tmp=*p;
size--;
delete p;
return tmp.val;
}
void clear(){
linkNode *p,*del;
p=head->nxt;
while(p!=rear){
del=p;
p=p->nxt;
delete del;
}
head->nxt=rear;
rear->fro=head;
}
};
#endif //BPTREE_HPP_BUFFERLIST_HPP