-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblockchain.h
458 lines (444 loc) · 17.8 KB
/
blockchain.h
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
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
//
// Created by lenovo on 2021/1/19.
//
#ifndef CODE_UNROLLEDLINKEDLIST_H
#define CODE_UNROLLEDLINKEDLIST_H
#include <iostream>
#include <string>
#include <algorithm>
#include <cstring>
#include <vector>
#include <fstream>
#define MAXN_NUM_OF_BLOCK 300
#define SPLIT_NUM_LEFT 150//设置成一样可以避免列快失败
#define MERGENUM 50
using std::string;
using namespace std;
class element{
public:int offset;
char key[100];
public:element(const int &offset_=-1,const string &key_=" ");
bool operator<(const element &a) const;
element &operator=(const element &right);
};
class block{
public:int nxt;
int pre;
int numofelment=0;
element array[MAXN_NUM_OF_BLOCK+5];
public:
void setnxt(int x){nxt=x;}
void setpre(int x){pre=x;}
void setnum(int x){numofelment=x;}
//block( int pre_=-1,int nxt_=-1, int numofelement_=0);
block();
block &operator=(const block &right);
};
class UnrolledLinkedList {
private:string filename;
fstream f1,f2,f3,f4,f5,f6;
int nxtblock(int offset){
f6.open(filename,ios::in | ios::out|ios::binary);
//cout<<offset<<"&&&&&&&"<<endl;
f6.seekg(offset);
int temp;
f6.read(reinterpret_cast<char*>(&temp),sizeof (int));
int temp1;
f6.read(reinterpret_cast<char*>(&temp1),sizeof (int));
//cout<<temp1<<"^^^^^^^"<<endl;
f6.read(reinterpret_cast<char*>(&temp1),sizeof (int));
// cout<<temp1<<"%%%%%%%%%"<<endl;
f6.close();
//cout<<temp<<" nxtblock"<<endl;
return temp;
}
int preblock(int offset){
f1.open(filename,ios::in | ios::out|ios::binary);
f1.seekg(offset+4);
int temp;
f1.read(reinterpret_cast<char*>(&temp),4);
f1.close();
return temp;
}
void splitblock(const int offset);//this offset means the location in the file(倒排文档的绝对位置)
void mergeblock(int offset1,int offset2);
public:
void debug_print_the_filenode(){
cout<<f1.is_open()<<" "<<f2.is_open()<<" "<<f3.is_open()<<" "<<f4.is_open()<<endl;
}
UnrolledLinkedList(const string &name=" ");
~UnrolledLinkedList();
void addelement(const element&index){
string temp=index.key;
// cout<<temp<<"(((((((((((((()))))))))))))))))))))))))))))"<<endl;
// cout<<f1.fail()<<"&&&&in debug addelement in the addelement begin"<<f2.fail()<<endl;
f1.open(filename,ios_base::binary|ios::in | ios::out);
if (!f1){f1.open(filename,ios::out|ios::binary);}
f2.open(filename,ios_base::binary|ios::in | ios::out);
if (!f2){f2.open(filename,ios::out|ios::binary);}
f3.open(filename,ios_base::binary|ios::in | ios::out);
if (!f3){f3.open(filename,ios::out|ios::binary);}
f4.open(filename,ios_base::binary|ios::in | ios::out);
if (!f4){f4.open(filename,ios::out|ios::binary);}
f5.open(filename,ios_base::binary|ios::in | ios::out);
if (!f5){f5.open(filename,ios::out|ios::binary);}
f6.open(filename,ios_base::binary|ios::in | ios::out);
if (!f6){f6.open(filename,ios::out|ios::binary);}
// cout<<f1.fail()<<"&&&&in debug addelement"<<f2.fail()<<endl;
// cout<<f1.is_open()<<" "<<f2.is_open()<<" "<<f3.is_open()<<" "<<f4.is_open()<<" "<<f5.is_open()<<" "<<f6.is_open()<<" debug in addelement"<<endl;
f1.seekg(16);
//cout<<f1.fail()<<"&&&&"<<endl;
//f1.seekg(0,ios::end),cout<<f1.tellg()<<endl;
char headname[100];
f2.seekg(0,ios::end);
// cout<<f1.fail()<<"&&&&in debug addelement"<<f2.fail()<<endl;
// cout<<f1.tellg()<<" "<<f2.tellg()<<"debug in addelement"<<endl;
if (f1.tellg()>f2.tellg()){
block tempblock;
//cout<<tempblock.nxt<<" addelemeny"<<endl;
tempblock.numofelment=1;
tempblock.array[0]=index;
f3.seekg(0);
f3.write(reinterpret_cast<char*>(&tempblock),sizeof (block));
f3.seekg(16);
f3.seekg(0);
int num;
f3.read(reinterpret_cast<char*>(&num),sizeof (int));
f3.clear();
//cout<<"select *********in addelement"<<endl;
//cout<<num<<"&&&&&&^^^^^^(((("<<endl;
// cout<<tempblock.nxt<<"%$$$$$$$$$^^^^^"<<endl;
// char temp1[100];
// f3.read(reinterpret_cast<char*>(&temp1),100);
// cout<<temp1<<" debug in findnode"<<endl;
} else{
int headoffset=0,lastoffset_in_the_block;
f4.seekg(headoffset+16);
f4.read(reinterpret_cast<char*>(&headname),100);
//cout<<f1.fail()<<" "<<f2.fail()<<" "<<f3.fail()<<" "<<f4.fail()<<" "<<f5.fail()<<" "<<f6.fail()<<endl;
// f1.seekg(0);
// int num;
// f1.read(reinterpret_cast<char *>(&num),4);
// cout<<num<<"^%%%%%%%%%%"<<endl;
// cout<<headname<<" "<<temp<<" addelement"<<endl;
int controloffset=0;
string str(headname);
// cout<<headname<<" ***************&&&&&&&&&&&&&&&&&%%#@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@"<<endl;
// cout<<str<<" "<<temp<<"**************^^^^^^^^^^^^^^^^^^^^%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%****"<<endl;
//cout<<strcmp(temp.c_str(),str.c_str())<<"^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^&&&&&&&&&&&&"<<endl;
while (strcmp(temp.c_str(),str.c_str())>0)//endless loop
{
//cout<<"********"<<endl;
// cout<<headoffset<<"pre &&&&"<<endl;
f4.seekg(headoffset);
controloffset=headoffset;
int tempoff;
f4.read(reinterpret_cast<char*>(&tempoff),4);
headoffset=tempoff;
//headoffset=nxtblock(headoffset);
// cout<<headoffset<<"&&&&&&"<<endl;
if (headoffset==-1) break;
f4.seekg(headoffset+16);
f4.read(reinterpret_cast<char*>(&headname),100);//logic may need to be rewrite
str=headname;
//cout<<str<<" "<<temp<<"&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&"<<endl;
}
//this headoffset means the block of the node and now to search the block
// cout<<controloffset<<"debug in addelement_____________________________"<<endl;
f4.seekg(controloffset);
block tempblock;
f4.read(reinterpret_cast<char*>(&tempblock),sizeof (block));
int location=upper_bound(tempblock.array,tempblock.array+tempblock.numofelment,index)-tempblock.array;//todo
//tempblock.numofelment++;
for (int i = tempblock.numofelment; i >=location ; --i) {
tempblock.array[i+1]=tempblock.array[i];
}
tempblock.array[location]=index;
tempblock.numofelment++;
//cout<<tempblock.array[0].key<<" mmm"<<tempblock.array[1].key<<endl;
f4.seekg(controloffset);
f4.write(reinterpret_cast<char*>(&tempblock),sizeof (block));
if (tempblock.numofelment>MAXN_NUM_OF_BLOCK)
{f1.close(),f2.close(),f3.close(),f4.close(),f5.close(),f6.close();
splitblock(controloffset);
return;
}
}
f1.close(),f2.close(),f3.close(),f4.close(),f5.close(),f6.close();
}
void deleteelment(const element&index){
//cout<<"deleteelement in begin"<<endl;
f1.open(filename,ios::in | ios::out|ios::binary);
f2.open(filename,ios::in | ios::out|ios::binary);
f3.open(filename,ios::in | ios::out|ios::binary);
f4.open(filename,ios::in | ios::out|ios::binary);
f5.open(filename,ios::in | ios::out|ios::binary);
//cout<<f1.fail()<<" in deleteelement begin f1.fail"<<endl;
f1.seekg(0,ios::end);
// cout<<f1.tellg()<<"in delete element f1.tellg"<<endl;//todo
if (f1.tellg()==0) {
f1.close(),f2.close(),f3.close(),f4.close();
f5.close();
return; }
char temp1[100];
strcpy(temp1,index.key);
// string temp1=index.key;
f1.seekg(16);
int headoffset=0,lastoffset_in_the_block;
char headname[100];
int tempheadoffsey=0;
f1.read(reinterpret_cast<char*>(&headname),100);
if (strcmp(temp1,headname)<0) {
f1.close(),f2.close(),f3.close(),f4.close();
f5.close();
return; }
//cout<<temp1<<" in deleteele"<<headname<<endl;
while (strcmp(temp1,headname)>0)
{ tempheadoffsey=headoffset;
headoffset=nxtblock(headoffset);
if (headoffset==-1) break;
f3.seekg(headoffset+16);
f3.read(reinterpret_cast<char*>(&headname),100);
}
f2.seekg(tempheadoffsey);
block tempblock;
f2.read(reinterpret_cast<char*>(&tempblock),sizeof (block));
int location=lower_bound(tempblock.array,tempblock.array+tempblock.numofelment,index)-tempblock.array;
int pos=location;
//cout<<"pos:"<<pos<<"delete element"<<endl;
//cout<<tempblock.numofelment<<" numofelement in element"<<endl;
while (pos!=tempblock.numofelment){
//cout<<tempblock.array[pos].key<<" "<<temp1<<" delete"<<endl;
//cout<<tempblock.array[pos].offset<<" "<<index.offset<<" delete"<<endl;
if ((strcmp(temp1,tempblock.array[pos].key)==0)&&tempblock.array[pos].offset==index.offset) {
//cout<<"succeed break in delete"<<endl;
break; }
pos++;
}
//cout<<" run out od the first while in deleteelement"<<endl;
//search in the next block
//below is not check
if (pos==tempblock.numofelment){
while (nxtblock(tempheadoffsey)!=-1&&pos==tempblock.numofelment){
tempheadoffsey=nxtblock(tempheadoffsey);
f2.seekg(tempheadoffsey);
f2.read(reinterpret_cast<char*>(&tempblock),sizeof (block));
location=lower_bound(tempblock.array,tempblock.array+tempblock.numofelment,index)-tempblock.array;
pos=location;
while (pos!=tempblock.numofelment){
if ((strcmp(temp1,tempblock.array[pos].key)==0)&&tempblock.array[pos].offset==index.offset) break;
pos++;
}
}
}
if (pos!=tempblock.numofelment){
//cout<<"in deleteelement pos!=tempblock"<<endl;
tempblock.numofelment--;
element tempele;
tempblock.array[pos]=tempele;
for (int i = pos; i <tempblock.numofelment ; ++i) {
tempblock.array[i]=tempblock.array[i+1];
}
f4.seekg(tempheadoffsey);
f4.write(reinterpret_cast<char*>(&tempblock),sizeof (block));
}
//below is not check
if (nxtblock(tempheadoffsey)!=-1){
f5.seekg(nxtblock(tempheadoffsey)+8);
int nxtnum;
f5.read(reinterpret_cast<char*>(&nxtnum),4);
if (nxtnum+tempblock.numofelment<MERGENUM) {
f1.close(),f2.close(),f3.close(),f4.close();
f5.close();
mergeblock(tempheadoffsey, nxtblock(tempheadoffsey));
return;}
}
f1.close(),f2.close(),f3.close(),f4.close();
f5.close();
}
vector<int> findelement(const string key_name) {
f1.open(filename,ios::in | ios::out|ios::binary);
//cout<<"f1 check"<<f1.is_open()<<endl;
f2.open(filename,ios::in | ios::out|ios::binary);
f3.open(filename,ios::in | ios::out|ios::binary);
f4.open(filename,ios::in | ios::out|ios::binary);
// cout<<f2.is_open()<<endl;
vector<int> return_ans;
return_ans.clear();
f1.seekg(16);
f2.seekg(0,ios::end);
//cout<<f1.tellg()<<"*****"<<endl;
//cout<<f1.tellg()<<" "<<f2.tellg()<<"in find element"<<endl;
if (f1.tellg()>f2.tellg()) {
f1.close(),f2.close(),f3.close(),f4.close();
return return_ans; }
char temp[100];
f1.read(reinterpret_cast<char*>(&temp),100);
//cout<<temp<<" "<<key_name<<" the keyname and the head tempdebug in find element"<<endl;//todo
if (strcmp(temp,key_name.c_str())>0) {
f1.close(),f2.close(),f3.close(),f4.close();
return return_ans;}
// if (strcmp(temp,key_name.c_str())==0) {
// f1.seekg(-104,ios::cur);
// int ans;
// f1.read(reinterpret_cast<char*>(&ans),4);
// return_ans.push_back(ans);
// f1.close(),f2.close(),f3.close(),f4.close();
// return return_ans;
// }
int headoffset=0;
int controloffset=0;
while (strcmp(temp,key_name.c_str())<0)
{
//if (controloffset!=0) cout<<"++++++++"<<temp<<" "<<key_name<<endl;
f1.seekg(controloffset);
int temp1;
f1.read(reinterpret_cast<char*>(&temp1),4);
headoffset=temp1;
//headoffset=nxtblock(headoffset);
if (headoffset==-1) break;
controloffset=headoffset;
f3.seekg(headoffset+16);
f3.read(reinterpret_cast<char*>(&temp),100);
//cout<<temp<<" "<<key_name<<"*******************"<<endl;
// cout<<" f1.fail:"<<f1.fail()<<" f2.fail:"<<f3.fail()<<endl;
}
int num11;
if (headoffset!=-1){
f3.seekg(controloffset+4);
f3.read(reinterpret_cast<char*>(&controloffset),4);
}
if (controloffset==-1){controloffset=0;}
f3.seekg(controloffset);
//cout<<controloffset<<" findelement controloffset"<<endl;
block tempblock;
//cout<<controloffset<<"&&&&& control offset in findelement"<<endl;
f3.read(reinterpret_cast<char*>(&tempblock),sizeof (block));
element tempelement;
strcpy(tempelement.key,key_name.c_str());
//cout<<tempelement.key<<" in find ele temp.key"<<endl;
//cout<<key_name<<"findelement"<<endl;
// cout<<tempblock.array[0].key<<" 8888888"<<tempblock.array[1].key<<"debug in findelement"<<endl;
//cout<<tempblock.numofelment<<"debug in tempblock.num in findelement"<<endl;
int location=lower_bound(tempblock.array,tempblock.array+tempblock.numofelment,tempelement)-tempblock.array;//todo
int nowlocation=location;
//cout<<location<<"location debug in findnode"<<endl;
//above is right
while (nowlocation!=(tempblock.numofelment)&&key_name==tempblock.array[nowlocation].key){
// cout<<tempblock.array[nowlocation].offset<<" &&&&&&&&&&&& find element"<<endl;
return_ans.push_back(tempblock.array[nowlocation].offset);
nowlocation++;
}
if (nowlocation!=(tempblock.numofelment)){
f1.close(),f2.close(),f3.close(),f4.close();
return return_ans;
} else//below may have bug
{
int the_beginofblock_offset;
the_beginofblock_offset=nxtblock(controloffset);
while (the_beginofblock_offset!=-1){
f4.seekg(the_beginofblock_offset);
f4.read(reinterpret_cast<char*>(&tempblock),sizeof (block));
strcpy(tempelement.key,key_name.c_str());
location=lower_bound(tempblock.array,tempblock.array+tempblock.numofelment,tempelement)-tempblock.array;
nowlocation=location;
while (nowlocation!=tempblock.numofelment&&key_name==tempblock.array[nowlocation].key){
return_ans.push_back(tempblock.array[nowlocation].offset);
nowlocation++;
}
if (nowlocation!=tempblock.numofelment) break;
the_beginofblock_offset=nxtblock(the_beginofblock_offset);
}
}
f1.close(),f2.close(),f3.close(),f4.close();
return return_ans;
}
void deleteblock(int offset){//may be useless
f1.open(filename,ios::in | ios::out|ios::binary);
f2.open(filename,ios::in | ios::out|ios::binary);
f3.open(filename,ios::in | ios::out|ios::binary);
f4.open(filename,ios::in | ios::out|ios::binary);
f1.seekg(offset);
int pre,nxt;
f1.read(reinterpret_cast<char*>(&nxt),4);
f1.read(reinterpret_cast<char*>(&pre),4);
if (pre==-1){
block tempblock;
f4.seekg(0);
f4.write(reinterpret_cast<char*>(&tempblock),sizeof (block));
} else{
f2.seekg(pre);
f2.write(reinterpret_cast<char*>(&nxt),4);
}
if (nxt!=-1){
f3.seekg(nxt+4);
f3.write(reinterpret_cast<char*>(&pre),4);
}
f1.close(),f2.close(),f3.close(),f4.close();
}
vector<int> return_all_offset(){
vector<int >return_ans;
f1.open(filename,ios::in | ios::out|ios::binary);
f2.open(filename,ios::in | ios::out|ios::binary);
f3.open(filename,ios::in | ios::out|ios::binary);
f4.open(filename,ios::in | ios::out|ios::binary);
//cout<<"DEBUG"<<endl;
//debug_print_the_filenode();
f1.seekg(0);
int nxt;
f1.read(reinterpret_cast<char*>(&nxt),4);
//cout<<nxt<<"debug in the nxt output"<<endl;
f2.seekg(0);
block tempblock;
f2.read(reinterpret_cast<char*>(&tempblock),sizeof (tempblock));
//cout<<tempblock.numofelment<<"blocklist debug the numofelement"<<endl;
for (int i = 0; i <tempblock.numofelment ; ++i) {
//cout<<tempblock.array[i].key<<"___________*************____________"<<tempblock.array[i].offset<<endl;
return_ans.push_back(tempblock.array[i].offset);
}
while (nxt!=-1)
{
f3.seekg(nxt);
f3.read(reinterpret_cast<char*>(&tempblock),sizeof (tempblock));
for (int i = 0; i <tempblock.numofelment ; ++i) {
return_ans.push_back(tempblock.array[i].offset);
}
nxt=nxtblock(nxt);
}
f1.close(),f2.close(),f3.close(),f4.close();
return return_ans;
}//用于show操作无参数的情况
void debug(){
cout<<"____________________________________________________"<<endl;
f1.open(filename,ios::in | ios::out|ios::binary);
f2.open(filename,ios::in | ios::out|ios::binary);
f3.open(filename,ios::in | ios::out|ios::binary);
f4.open(filename,ios::in | ios::out|ios::binary);
cout<<"DEBUG"<<endl;
//debug_print_the_filenode();
f1.seekg(0);
int nxt;
f1.read(reinterpret_cast<char*>(&nxt),4);
cout<<nxt<<"debug in the nxt output"<<endl;
f2.seekg(0);
block tempblock;
f2.read(reinterpret_cast<char*>(&tempblock),sizeof (tempblock));
cout<<tempblock.numofelment<<"blocklist debug the numofelement"<<endl;
for (int i = 0; i <tempblock.numofelment ; ++i) {
cout<<tempblock.array[i].key<<"___________*************____________"<<tempblock.array[i].offset<<endl;
}
while (nxt!=-1)
{
f3.seekg(nxt);
f3.read(reinterpret_cast<char*>(&tempblock),sizeof (tempblock));
for (int i = 0; i <tempblock.numofelment ; ++i) {
cout<<tempblock.array[i].key<<"&&&&& ---------********** &&&&"<<tempblock.array[i].offset<<endl;
}
nxt=nxtblock(nxt);
}
f1.close(),f2.close(),f3.close(),f4.close();
cout<<"the end of the blocklist debug"<<endl;
cout<<"_________________________________________________"<<endl;
}
};
#endif //CODE_UNROLLEDLINKEDLIST_H