-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinearsearch.cpp
159 lines (142 loc) · 4.08 KB
/
linearsearch.cpp
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
#include<iostream>
#include "file_manager.h"
#include "errors.h"
#include<cstring>
#include <bits/stdc++.h>
using namespace std;
/* ALGORITHM
Input : A file accessible through the File Manager API containing the sorted integers, and num,
the integer to search
Output: An output file accessible through the File Manager API in which result is to be stored.
Store (Page number, offset) containing the integer if present, else (-1, -1) in the output file
curr = first Page Number
while curr ≤ lastP ageN umber do
Read page curr into the buffer
if num is present in curr then
output (curr, offset at which num is present)
return
end
else
increment curr by 1
end
end
output(-1, -1)
*/
int main(int argc,char* argv[]) {
char* input_file = argv[1];
char* output_file = argv[3];
FileManager fm;
FileHandler fh_input = fm.OpenFile(input_file);
int num = 1; // later add a while loop on query_search.txt, and also handle execptions (optional)
FileHandler fh_output = fm.CreateFile(output_file);
PageHandler first = fh_input.FirstPage();
int firstPageNum = first.GetPageNum();
fh_input.UnpinPage(firstPageNum);
PageHandler last = fh_input.LastPage();
int lastPageNum = last.GetPageNum();
fh_input.UnpinPage(lastPageNum);
int total_first = firstPageNum;
int total_last = lastPageNum;
PageHandler cur = fh_input.FirstPage();
char* data;
int value,count;
// also need to enter (-1,-1) and INT_MIN if output_count<PAGE_CONTENT_SIZE
PageHandler cur_output = fh_output.NewPage();
char* data_output;
data_output = cur_output.GetData();
int output_count=0;
string query_line;
string token;
ifstream query_file(argv[2]);
// fm.PrintBuffer();
while(getline(query_file,query_line)){
vector<string> queries;
stringstream query(query_line);
while(getline(query,token,' ')){
queries.push_back(token.c_str());
}
num = atoi(queries[1].c_str());
if(queries[0].compare("SEARCH")!=0){
continue;
}
while(true){
// fm.PrintBuffer();
data = cur.GetData();
count=0;
while(count<PAGE_CONTENT_SIZE){
memcpy(&value,&data[count],sizeof(int));
if(value==INT_MIN){ // empty entry reached
break;
}
if(value==num){
if(output_count==PAGE_CONTENT_SIZE){
fh_output.UnpinPage(cur_output.GetPageNum());
fh_output.FlushPages();
cur_output = fh_output.NewPage();
output_count=0;
data_output = cur_output.GetData();
}
// page number
value = cur.GetPageNum();
memcpy(&data_output[output_count],&value,sizeof(int));
output_count+=4;
if(output_count==PAGE_CONTENT_SIZE){
fh_output.UnpinPage(cur_output.GetPageNum());
fh_output.FlushPages();
cur_output = fh_output.NewPage();
output_count=0;
data_output = cur_output.GetData();
}
// offset
value = count/4;
memcpy(&data_output[output_count],&value,sizeof(int));
output_count+=4;
}
count+=4;
}
fh_input.UnpinPage(cur.GetPageNum());
if(cur.GetPageNum()==total_last){
// all pages have been processed
break;
}
else{
cur=fh_input.NextPage(cur.GetPageNum());
count=0;
}
}
// Insert -1,-1
if(output_count==PAGE_CONTENT_SIZE){
fh_output.UnpinPage(cur_output.GetPageNum());
fh_output.FlushPages();
cur_output = fh_output.NewPage();
output_count=0;
data_output = cur_output.GetData();
}
// -1
value = -1;
memcpy(&data_output[output_count],&value,sizeof(int));
output_count+=4;
if(output_count==PAGE_CONTENT_SIZE){
fh_output.UnpinPage(cur_output.GetPageNum());
fh_output.FlushPages();
cur_output = fh_output.NewPage();
output_count=0;
data_output = cur_output.GetData();
}
memcpy(&data_output[output_count],&value,sizeof(int));
output_count+=4;
// re-initialized cur for next query
cur = fh_input.FirstPage();
}
// Now fill empty entries by INT_MIN
value=INT_MIN;
while(output_count<PAGE_CONTENT_SIZE){
memcpy(&data_output[output_count],&value,sizeof(int));
output_count+=4;
}
fh_output.UnpinPage(cur_output.GetPageNum());
fh_output.FlushPages();
// Now close both files
fm.CloseFile(fh_input);
fm.CloseFile(fh_output);
}