-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathULL_new.hpp
355 lines (307 loc) · 10.6 KB
/
ULL_new.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
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
//
// Created by JerryGuo on 2021/12/22.
//
#ifndef PARSER_CPP__ULL_NEW_HPP_
#define PARSER_CPP__ULL_NEW_HPP_
#include <algorithm>
#include <cstring>
#include <string>
#include <vector>
#include "Char.hpp"
#include "MemoryRiver.hpp"
#define BLOCK_SIZE 600
#define BLOCK_SPLIT_LIMIT 550
#define BLOCK_SPLIT_LEFT 300
#define BLOCK_MERGE_LIMIT 157
#define KEY_SIZE 65
#define BLOCK_NUM_LIMIT 317
template<class NodeType>
class Block {
private:
int num = 0;
NodeType array[BLOCK_SIZE];
public:
Block() = default;
Block(const int &arr_num_, const NodeType *array_);
const int &Size() const;
const NodeType &Begin() const;
const NodeType &End() const;
bool Add(const NodeType &);
bool Del(const NodeType &);
void Split(Block &);
void Erase(const int &);
void Merge(Block &);
void Find(const std::string &, std::vector<int> &) const;
NodeType &operator[](const int &index_);
};
template<class NodeType>
struct BlockInfo {
int position = 0;//存的是在info_length后多少
NodeType tail;
BlockInfo() = default;
BlockInfo(const Block<NodeType> &, int position_ = 0);
BlockInfo(const NodeType &, int position_ = 0);
bool operator<(const BlockInfo &) const;
};
template<class NodeType>
struct BlockGallery {
public:
int block_num = 1;
BlockInfo<NodeType> arr[BLOCK_NUM_LIMIT];
void Add(const BlockInfo<NodeType> &);
//void Del(const BlockInfo &);
void Del(const int &);
void Update(const BlockInfo<NodeType> &, const int &);
int FindPosition(const NodeType &) const;
};
template<class NodeType>
class UnrolledLinkedList {
private:
MemoryRiver<Block<NodeType>, BlockGallery<NodeType>, sizeof(BlockGallery<NodeType>)> block_list;
BlockGallery<NodeType> block_info;
void DeleteBlock(const int &);
public:
UnrolledLinkedList();
explicit UnrolledLinkedList(const std::string &);
void GetInfo();
void PutInfo();
bool Add(const NodeType &);
bool Del(const NodeType &);
bool Query(const std::string &, std::vector<int> &);
void GetAll(std::vector<NodeType> &find_list_);
};
//............class block..............
template<class NodeType>
Block<NodeType>::Block(const int &arr_num_, const NodeType *array_) : num(arr_num_) {
memcpy(array, array_, (num) * sizeof(NodeType));
}
template<class NodeType>
const int &Block<NodeType>::Size() const { return num; }
template<class NodeType>
const NodeType &Block<NodeType>::Begin() const { return array[0]; }
template<class NodeType>
const NodeType &Block<NodeType>::End() const { return (num != 0) ? array[num - 1] : array[0]; }
template<class NodeType>
bool Block<NodeType>::Add(const NodeType &x) {
int pos = static_cast<int>(std::lower_bound(array, array + num, x) - array);
if (array[pos] == x)return false;//插入失败
else {
for (int i = num - 1; i >= pos; --i) array[i + 1] = array[i];
array[pos] = x;
++num;
return true;
}
}
template<class NodeType>
bool Block<NodeType>::Del(const NodeType &x) {
int pos = static_cast<int>(std::lower_bound(array, array + num, x) - array);
if (array[pos] != x) return false;
else {
for (int i = pos + 1; i < num; ++i) {
array[i - 1] = array[i];
}
array[--num] = NodeType();
return true;
}
}
template<class NodeType>
void Block<NodeType>::Erase(const int &start_point) {
//for (int i = start_point; i < num; ++i) { array[i] = Node(); }
}
template<class NodeType>
void Block<NodeType>::Split(Block &receiver) {
int new_block_num = num - BLOCK_SPLIT_LEFT;
receiver = Block(new_block_num, array + BLOCK_SPLIT_LEFT);
Erase(BLOCK_SPLIT_LEFT);
num = BLOCK_SPLIT_LEFT;
}
template<class NodeType>
void Block<NodeType>::Merge(Block &x) {//默认x在this后
for (int i = num; i < num + x.num; ++i) { array[i] = x.array[i - num]; }
x.Erase(0);
num += x.num;
x.num = 0;
}
template<class NodeType>
void Block<NodeType>::Find(const std::string &key_, std::vector<int> &find_list) const {
Char<KEY_SIZE> key_carrier(key_);
for (int i = 0; i < num; ++i) {
if (key_carrier == array[i].key) {
find_list.push_back(array[i].value);
// int value_look=find_list.back();
// std::cout<<value_look<<std::endl;
}
}
}
template<class NodeType>
NodeType &Block<NodeType>::operator[](const int &index_) {
return array[index_];
}
template<class NodeType>
BlockInfo<NodeType>::BlockInfo(const Block<NodeType> &x, int position_) {
tail = x.End();
position = position_;
}
template<class NodeType>
bool BlockInfo<NodeType>::operator<(const BlockInfo<NodeType> &x) const { return tail < x.tail; }
template<class NodeType>
BlockInfo<NodeType>::BlockInfo(const NodeType &x, int position_) {
tail = x;
position = position_;
}
template<class NodeType>
void BlockGallery<NodeType>::Add(const BlockInfo<NodeType> &x) {
int pos;
//block_num++;
pos = static_cast<int>(std::lower_bound(arr, arr + block_num - 1, x) - arr);
if (x.tail < arr[block_num - 1].tail) { pos = pos; } else { pos++; }
block_num++;
for (int i = block_num - 1; i >= pos; --i) { arr[i + 1] = arr[i]; }
arr[pos] = x;
}
//void BlockGallery::Del(const BlockInfo &x) {
// int pos;
// pos = static_cast<int>(std::lower_bound(arr, arr + block_num, x) - arr);
// for (int i = pos; i < block_num - 1; ++i) { arr[i] = arr[i + 1]; }
// arr[block_num - 1] = BlockInfo();
// block_num--;
//}
template<class NodeType>
void BlockGallery<NodeType>::Del(const int &index_) {
for (int i = index_; i < block_num - 1; ++i) { arr[i] = arr[i + 1]; }
arr[block_num - 1] = BlockInfo<NodeType>();
block_num--;
}
template<class NodeType>
void BlockGallery<NodeType>::Update(const BlockInfo<NodeType> &x, const int &pos_) { arr[pos_] = x; }
template<class NodeType>
int BlockGallery<NodeType>::FindPosition(const NodeType &x) const {
int pos;
pos = static_cast<int>(std::lower_bound(arr, arr + block_num - 1, x) - arr);
return pos;
}
//.......ULL...........
template<class NodeType>
void UnrolledLinkedList<NodeType>::DeleteBlock(const int &index_) {
GetInfo();
block_list.Delete(block_info.arr[index_].position);
block_info.Del(index_);
PutInfo();
}
template<class NodeType>
UnrolledLinkedList<NodeType>::UnrolledLinkedList() = default;
template<class NodeType>
UnrolledLinkedList<NodeType>::UnrolledLinkedList(const string &file_name_) : block_list(file_name_) {}
template<class NodeType>
void UnrolledLinkedList<NodeType>::GetInfo() { block_list.GetInfo(block_info); }
template<class NodeType>
void UnrolledLinkedList<NodeType>::PutInfo() { block_list.WriteInfo(block_info); }
template<class NodeType>
bool UnrolledLinkedList<NodeType>::Add(const NodeType &x) {
GetInfo();
int pos = block_info.FindPosition(x);
Block<NodeType> target_block;
block_list.Read(target_block, block_info.arr[pos].position);
// for (int i = 0; i < block_info.block_num; i++) {
// Block test_block;
// block_list.Read(test_block, block_info.arr[i].position);
// std::cerr << "# " << test_block.Begin().key << '\n';
// std::cerr << "# " << test_block.Begin().value << '\n';
// std::cerr << "# " << test_block.End().key << '\n';
// std::cerr << "# " << test_block.End().value << '\n';
// }
if (target_block.Add(x)) {
block_info.arr[pos].tail = target_block.End();
if (target_block.Size() >= BLOCK_SPLIT_LIMIT) {
Block<NodeType> new_block;
target_block.Split(new_block);
BlockInfo<NodeType> new_block_info;
new_block_info.tail = new_block.End();
new_block_info.position = block_list.Write(new_block);
block_info.arr[pos].tail = target_block.End();
block_info.Add(new_block_info);
}
block_list.Update(target_block, block_info.arr[pos].position);
PutInfo();
return true;
}
return false;
}
template<class NodeType>
bool UnrolledLinkedList<NodeType>::Del(const NodeType &x) {
GetInfo();
int pos = block_info.FindPosition(x);
Block<NodeType> target_block;
block_list.Read(target_block, block_info.arr[pos].position);
// for (int i = 0; i < block_info.block_num; i++) {
// Block test_block;
// block_list.Read(test_block, block_info.arr[i].position);
// std::cerr << "# " << test_block.Begin().key << '\n';
// std::cerr << "# " << test_block.Begin().value << '\n';
// std::cerr << "# " << test_block.End().key << '\n';
// std::cerr << "# " << test_block.End().value << '\n';
// }
if (target_block.Del(x)) {
if (pos != (block_info.block_num - 1)) {
block_info.arr[pos].tail = target_block.End();
Block<NodeType> next_block;
block_list.Read(next_block, block_info.arr[pos + 1].position);
if (target_block.Size() <= BLOCK_MERGE_LIMIT &&
target_block.Size() + next_block.Size() <= BLOCK_SPLIT_LIMIT) {
target_block.Merge(next_block);
block_list.Delete(block_info.arr[pos + 1].position);
block_info.Del(pos + 1);
block_list.Update(target_block, block_info.arr[pos].position);
block_info.arr[pos].tail = target_block.End();
PutInfo();
return true;
}
}
if (target_block.Size() == 0 && block_info.block_num > 1) {
block_list.Delete(block_info.arr[pos].position);
block_info.Del(pos);
PutInfo();
return true;
}
block_list.Update(target_block, block_info.arr[pos].position);
PutInfo();
return true;
}
return false;
}
template<class NodeType>
bool UnrolledLinkedList<NodeType>::Query(const string &key_, std::vector<int> &find_list_) {
GetInfo();
Char<KEY_SIZE> key_carrier(key_);
int lp, rp;
rp = block_info.block_num - 1;
lp = 0;
Block<NodeType> this_block;
for (int i = 0; i < block_info.block_num; ++i) {
if (key_carrier > block_info.arr[i].tail.key) { lp = i; }
if (key_carrier < block_info.arr[i].tail.key) {
rp = i;
break;
}
}
for (int i = lp; i < rp + 1; i++) {
block_list.Read(this_block, block_info.arr[i].position);
this_block.Find(key_, find_list_);
}
return find_list_.empty();
}
template<class NodeType>
void UnrolledLinkedList<NodeType>::GetAll(std::vector<NodeType> &find_list_){
GetInfo();
int lp, rp;
rp = block_info.block_num - 1;
lp = 0;
Block<NodeType> this_block;
for (int i = lp; i < rp + 1; i++) {
block_list.Read(this_block, block_info.arr[i].position);
for(int j=0;j<this_block.Size();++j){
find_list_.push_back(this_block[j]);
}
}
}
#endif //PARSER_CPP__ULL_NEW_HPP_