-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathIndexfileUtilities.cpp
More file actions
101 lines (75 loc) · 2.54 KB
/
IndexfileUtilities.cpp
File metadata and controls
101 lines (75 loc) · 2.54 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
class Node;
#include <fstream>
#include <utility>
using namespace std;
#include "Rectangle.cpp"
#include "Node.cpp"
#include "Record.cpp"
#ifndef INDEXFILEUTILITIES_CPP
#define INDEXFILEUTILITIES_CPP
class IndexfileUtilities {
public:
int nextId;
IndexfileUtilities(){
nextId = 1;
}
int newBlockIdWithEmptyNode(Rectangle &boundingBox, bool isLeafNode) {
vector<pair<int, Rectangle>> rec(50);
return newBlockID(boundingBox, isLeafNode, rec, false);
}
int newBlockID(Rectangle &boundingBox, bool isLeafNode, vector<pair<int, Rectangle>> &rec, bool shouldInit){
Node newNode(boundingBox, isLeafNode, rec, -1, nextId, shouldInit);
newNode.modifiedNode();
return nextId++;
}
void modifiedBlockId(Node aNode, int id){
ofstream myfile;
myfile.open ("indexfile.dat", ios::out | ios::binary);
int blockStart = (id - 1) * (int)sizeof(Node);
myfile.seekp(blockStart, ios::beg);
myfile.write((char *) &aNode, sizeof(Node));
myfile.close();
}
Node getNodeByBlockId(int id) {
return getNodeByBlockIdHelper(id);
}
Record getRecordFromDatafile(int entityNumber){
Record output;
int blockNum = entityNumber >> 27;
int recordNum = (entityNumber << 5) >> 5;
ifstream myFile;
myFile.open("datafile.dat",ios::out | ios::binary );
int index = (32770 * (blockNum - 1)) + sizeof(Record)*(recordNum-1);
myFile.seekg(index, myFile.beg);
myFile.read((char *)&output, sizeof(Record));
myFile.close();
return output;
}
void updateBounds(Node *node, Rectangle newBoundingBox){
node->boundingBox = newBoundingBox;
int ndId = node->blockId;
int prnId = node->parentId;
if(prnId != -1){
Node parentNode = this->getNodeByBlockId(prnId);
for(int i=0;i<parentNode.capacity;i++){
if(parentNode.rectangles[i].first == ndId){
parentNode.rectangles[i].second = newBoundingBox;
modifiedBlockId(parentNode, prnId);
}
}
}
}
private:
Node getNodeByBlockIdHelper(int id) {
ifstream myFile;
myFile.open ("indexfile.dat", ios::out | ios::binary);
int blockStart = (id - 1) * (int)sizeof(Node);
myFile.seekg(blockStart, ios::beg);
Node *node;
node = new Node();
myFile.read ((char*) node, sizeof (Node));
myFile.close();
return *node;
}
};
#endif