-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path3rd version
More file actions
372 lines (358 loc) · 9.75 KB
/
3rd version
File metadata and controls
372 lines (358 loc) · 9.75 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
#include <iostream>
#include <bits/stdc++.h>
using namespace std;
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;
}//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++;
}
}
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 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);
}
}
/*
void deleteKey(int key, int numOfRecords){
int currentBlock;
currentBlock =searchBlock( key, numOfRecords);
fstream sequenceSet;
sequenceSet.open("sequenceSet.txt",ios::out|ios::in);
sequenceSet.seekp(turnRRnToByte(currentBlock, numOfRecords),ios::beg);
sequenceSet.seekp(key,ios::cur);
char f[4];
sequenceSet.read(f,4);
if(f=="****"){
cout<<" Ivalue Already deleted.."<<endl;
}else{
long long pos=sequenceSet.tellp();
sequenceSet.seekp(pos+4);
sequenceSet.write("****",4);
cout<<"Deleted Successfully.."<<endl;
}
sequenceSet.close();
}
*/
int main() {
createBlocks(3,2);
insertRecord(1111,2222,2);
insertRecord(3333,4444,2);
insertRecord(5555,6666,2);
//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