-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path5th version
More file actions
487 lines (468 loc) · 13.8 KB
/
5th version
File metadata and controls
487 lines (468 loc) · 13.8 KB
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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
string charToString(char arr[],int length)
{
string stringToReturn="";
for (int i = 0; i < length; ++i) {
stringToReturn+=arr[i];
}
return stringToReturn;
}
string fillTheSpace(int value,string space,int length)
{
string stringWithSpaces= to_string(value);
while(stringWithSpaces.length()<length)
{
string spaceChar = space;
stringWithSpaces=spaceChar.append(stringWithSpaces);
}
return stringWithSpaces;
}
int headerEmp(){
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::in);
string hEmp = "";
char hE;
for (int i = 0; i < 4; ++i) {
sequenceSet>>hE;
hEmp+=hE;
}
if (hEmp=="00-1"){
return -1;
}
int firstEmpRecord = stoi(hEmp);
return firstEmpRecord;
}
int headerNonEmpty(){
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::in);
sequenceSet.seekg(4);
string hEmp = "";
char hE;
for (int i = 0; i < 4; ++i) {
sequenceSet>>hE;
hEmp+=hE;
}
if (hEmp=="00-1"){
return -1;
}
int firstEmpRecord = stoi(hEmp);
return firstEmpRecord;
}
struct Record{
int key;
int value;
};
void createBlocks(int numOfBlocks, int numOfRecords){
ofstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::out);
int maxVal = 0000;
int numOfBlock = 2;
string full = "";
sequenceSet << setfill ('0') <<setw (4);
sequenceSet << 1 ;
sequenceSet << setfill ('0') <<setw (4);
sequenceSet << -1 ;
for (int i = 0; i < numOfRecords*8 ; ++i) {
full+="@";
}
for(int i=0 ; i < numOfBlocks-1 ; i++){
sequenceSet << setfill ('0') <<setw (4);
sequenceSet << numOfBlock ;
numOfBlock++;
sequenceSet << setfill ('0') <<setw (4);
sequenceSet << maxVal ;
sequenceSet<<full;
}
sequenceSet << setfill ('0') <<setw (4);
sequenceSet << -1 ;
sequenceSet << setfill ('0') <<setw (4);
sequenceSet << maxVal ;
sequenceSet<<full;
}
int getNext(int recordNo, int numOfRecords)
{
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt", ios::in);
int z =(8 + (recordNo - 1) * (numOfRecords+1) * 8);
sequenceSet.seekg(8 + (recordNo - 1) * (numOfRecords+1) * 8);
char x ;
string y;
for (int i = 0; i < 4; ++i) {
sequenceSet >> x;
y +=x;
}
sequenceSet.close();
if(y=="00-1")
return -1;
return stoi(y);
int c;
}
void setNext(int recordNo,int next, int numOfRecords)
{
fstream sequenceSet;
sequenceSet.open("sequenceSet.txt", ios::in|ios::out);
sequenceSet.seekg(8 + (recordNo - 1) * (numOfRecords+1) * 8);
sequenceSet << fillTheSpace(next,"0",4);
sequenceSet.close();
}
int getMax(int recordNo, int numOfRecords)
{
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt", ios::in);
int pos=12 + (recordNo - 1) * (numOfRecords+1) * 8;
sequenceSet.seekg(pos);
char x ;
string y="";
for (int i = 0; i < 4; ++i) {
sequenceSet >> x;
y+=x;
}
sequenceSet.close();
if(y=="00-1")
return -1;
return stoi(y);
}
int turnRRnToByte(int rrn,int numOfRecords){
int x=(8 + (rrn-1)*8*(numOfRecords+1));
return x ;
}
int searchBlock(int key, int numOfRecords){
//insertion search
//return RRn of block that val in.
//search by key
int rrnOfBlock;//RRn
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt", ios::in);
int curBlock,maxVal,nextBlock;
curBlock= headerNonEmpty();
while(true){
maxVal= getMax(curBlock,numOfRecords);
nextBlock= getNext(curBlock,numOfRecords);
if (key <= maxVal || nextBlock == -1){
return curBlock;
}
curBlock=nextBlock;
}
}
string loadBlock(int posOfBegBlock, int numOfRecords){
string block="";
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::in);
sequenceSet.seekg(posOfBegBlock);
char pt;
for (int i = 0; i < (numOfRecords+1)*8; ++i) {
sequenceSet>>pt;
block+=pt;
}
return block;
}//take block from file to string
void returnBlock(string newBlock,int posOfBegBlock){
fstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::out|ios::in);
sequenceSet.seekp(posOfBegBlock);
sequenceSet<<newBlock;
sequenceSet.close();
}//return new block to file
string sortBlock(string unSortedBlock){
int numEmptySpaces=0;
string block="";
for (int i = 0; i < unSortedBlock.length(); ++i) {
if (unSortedBlock[i]!='@'){
block+=unSortedBlock[i];
}
else
{
numEmptySpaces++;
}
}
if(8+numEmptySpaces==unSortedBlock.length())
{//if all the block is empty
unSortedBlock[4]='0';
unSortedBlock[5]='0';
unSortedBlock[6]='0';
unSortedBlock[7]='0';
return unSortedBlock;
}
vector<pair<int,int>>rec;
string ke="";
string va="";
int kk;
int vv;
for (int i = 8; i < block.length()-4; ++i) {
ke+=block[i];
va+=block[i + 4];
if ( i%4 == 3 && i!=0){
i+=4;
kk = stoi(ke);
vv = stoi(va);
rec.push_back({kk,vv});
ke="";
va="";
}
}
sort(rec.begin(), rec.end());
int maxK=rec[rec.size() - 1].first;
string sortedBlock="";
for (int i = 0; i < 4; ++i) {
sortedBlock+=block[i];
}
string maxValue= to_string(maxK);
while(maxValue.length()<4)
{
string zero="0";
maxValue=zero.append(maxValue);
}
sortedBlock+=maxValue;
for (int i = 0; i < rec.size(); ++i) {
string keyy= to_string(rec[i].first);
while(keyy.length()<4)
{
string zero="0";
keyy=zero.append(keyy);
}
string value= to_string(rec[i].second);
sortedBlock+=keyy;
while(value.length()<4)
{
string zero="0";
value=zero.append(value);
}
sortedBlock+=value;
//cout<<rec[i].first<<" "<<rec[i].second<<"\n";
}
for (int i = 0; i < numEmptySpaces; ++i) {
sortedBlock.append("@");
}
return sortedBlock;
}
void editMax(int recordNum,int numOfRecords)
{
int blockPosition=turnRRnToByte(recordNum,numOfRecords);
string block= loadBlock(blockPosition,numOfRecords);
block = sortBlock(block);
returnBlock(block,blockPosition);
}
void insert(string block,int key, int val,int blockNum,int numOfRecords){
//val*4 key*4
//4"@@@@key"4"val"
string value= to_string(val);
while(value.length()<4)
{
string zero="0";
value=zero.append(value);
}
string curKey= to_string(key);
while(curKey.length()<4)
{
string zero="0";
curKey=zero.append(curKey);
}
for (int i = 0; i < block.length(); ++i) {
if (block[i]=='@'){
for (int j = 0; j < 4; ++j) {
block[j+i]=curKey[j];
}
i+=4;
for (int j = 0; j < 4; ++j) {
block[j+i]=value[j];
}
break;
}
}
returnBlock(sortBlock(block), turnRRnToByte(blockNum, numOfRecords));
}
void splitBlock(int blockNum,string block,int numOfRecords){
block = sortBlock(block);
int firstEmptyBlock=headerEmp();
int secondEmptyBlock= getNext(firstEmptyBlock,numOfRecords);
string firstBlock="00000000";
string secondBlock="00000000";
for (int i = 8; i < block.length(); ++i) {
if(i<(numOfRecords)*8)
{
firstBlock+=block[i];
}
else
{
secondBlock+=block[i];
}
}
while(firstBlock.length()<8*(numOfRecords+1))
{
firstBlock.append("@");
}
while(secondBlock.length()<8*(numOfRecords+1))
{
secondBlock.append("@");
}
firstBlock=sortBlock(firstBlock);
secondBlock= sortBlock(secondBlock);
int next1=getNext(blockNum,numOfRecords);
returnBlock(firstBlock, turnRRnToByte(blockNum,numOfRecords));
returnBlock(secondBlock, turnRRnToByte(firstEmptyBlock,numOfRecords));
fstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::out|ios::in);
sequenceSet<<fillTheSpace(secondEmptyBlock,"0",4);
setNext(firstEmptyBlock,next1 ,numOfRecords);
setNext(blockNum,firstEmptyBlock,numOfRecords);
}
void insertRecord(int key,int value,int numOfRecords){
//search rrn of block
//load rnn
//insert
//sortBlock
//return
// next block Done if split
//
int blockNum;//rrn
fstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::out|ios::in);
if (headerNonEmpty() == -1){
blockNum=1;
sequenceSet<<"0002";
sequenceSet<<"0001";
sequenceSet<<"00-1";
}
else{
blockNum= searchBlock(key, numOfRecords);
}
int blockByteOff;
blockByteOff = turnRRnToByte(blockNum,numOfRecords);
string block = loadBlock(blockByteOff,numOfRecords);
if (block[block.length()-1]!='@'){
//overflow
block.append(fillTheSpace(key,"0",4));
block.append(fillTheSpace(value,"0",4));
splitBlock(blockNum,block,numOfRecords);
}
else{
insert(block,key,value,blockNum, numOfRecords);
}
}
int recordByteSearch(int key, int numOfRecords){
//insertion search
//return RRn of block that val in.
//search by key
int rrnOfBlock;//RRn
ifstream sequenceSet;
sequenceSet.open("sequenceSet.txt", ios::in);
int curBlock,maxVal,nextBlock;
curBlock= headerNonEmpty();
while(true){
maxVal= getMax(curBlock,numOfRecords);
nextBlock= getNext(curBlock,numOfRecords);
if (key <= maxVal || nextBlock == -1){//if the record exists it will be here
sequenceSet.seekg(turnRRnToByte(curBlock,numOfRecords)+8,ios::cur);
for (int i = 0; i < numOfRecords; ++i) {
char reededKey[4];
sequenceSet.read(reededKey,4);
if(reededKey[0]=='@')
{
sequenceSet.seekg(4,ios::cur);
continue;
}
if(stoi(charToString(reededKey,4)) == key)
{
return turnRRnToByte(curBlock,numOfRecords)+8*(i+1);
}
sequenceSet.seekg(4,ios::cur);
}
return -1;
}
curBlock=nextBlock;
}
}
int getNumberOfrecords(int blocknum,int numOfRecords)
{
string block= loadBlock(turnRRnToByte(blocknum,numOfRecords),numOfRecords);
int numNonEmptyRecords=0;
for (int i = 0; i < block.size(); ++i) {
if(block[i]=='@')
numNonEmptyRecords++;
}
numNonEmptyRecords/=8;
numNonEmptyRecords=numOfRecords-numNonEmptyRecords;
return numNonEmptyRecords;
}
string popMin(int blocknum,int numOfRecords)
{
string block= loadBlock(turnRRnToByte(blocknum,numOfRecords),numOfRecords);
string minRecord="";
for (int i = 8; i < 16; ++i) {
minRecord+=block[i];
block[i]='@';
}
block= sortBlock(block);
returnBlock(block, turnRRnToByte(blocknum,numOfRecords));
return minRecord;
}
void deleteKey(int key, int numOfRecords){
int recordByte= recordByteSearch(key,numOfRecords);
if(recordByte==-1)
return;
int currentBlockRRn;
currentBlockRRn =searchBlock( key, numOfRecords);
fstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::out|ios::in);
sequenceSet.seekp(recordByte,ios::cur);
sequenceSet<<"@@@@@@@@";
sequenceSet.close();
editMax(currentBlockRRn,numOfRecords);
string block = loadBlock(turnRRnToByte(currentBlockRRn,numOfRecords),numOfRecords);
int minNumOfRecords =((numOfRecords % 2 + numOfRecords) / 2);
if(block [minNumOfRecords * 8] == '@')
{//underflow!
int nextBlock = getNext(currentBlockRRn,numOfRecords);
int recordNumInNextBlock = getNumberOfrecords(nextBlock,numOfRecords);
string recordToShare;
if(recordNumInNextBlock>minNumOfRecords)
{//redistribute with the right
cout<<"first case";
recordToShare= popMin(nextBlock,numOfRecords);
block.erase(block.length()-8);
block.append(recordToShare);
block= sortBlock(block);
returnBlock(block, turnRRnToByte(currentBlockRRn,numOfRecords));
}
else
{//merge
cout<<"merge";
block.erase(block.length()-(recordNumInNextBlock*8));
for (int i = 0; i < recordNumInNextBlock; ++i) {
block.append(popMin(nextBlock,numOfRecords));
}
block=sortBlock(block);
returnBlock(block, turnRRnToByte(currentBlockRRn,numOfRecords));
setNext(currentBlockRRn, getNext(nextBlock,numOfRecords),numOfRecords);
setNext(nextBlock,headerEmp(),numOfRecords);
sequenceSet.open("sequenceSet.txt",ios::out|ios::in);
sequenceSet<<fillTheSpace(nextBlock,"0",4);
sequenceSet.close();
}
}
return;
}
int main() {
int numberOFRecords= 3;
createBlocks(3,numberOFRecords);
insertRecord(1111,2222,numberOFRecords);
insertRecord(4444,0000,numberOFRecords);
insertRecord(3333,0000,numberOFRecords);
insertRecord(5555,6666,numberOFRecords);
deleteKey(3333,numberOFRecords);
// deleteKey(7777,numberOFRecords);
//cout<<loadRecord(9,2);
//returnBlock("#0010001@@@1@@@5@@@@@@@@",9);
//addRecord(3333,5555);
//cout<<headerEmp()<<" "<<headerNonEmpty();
//cout<<searchBlock(2,2,2);
//cout<< getNext(2,2)<<" " << getMax(2,2);
//cout<<searchBlock(3,2);
//sortBlock("0001000600010006000300080004000900020007");
//sortBlock("77778888111122225555666633334444@@@@@@@@");
// insertRecord(3,7,2);
return 0;
}
// close file in all functions