-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassn_1.c
267 lines (221 loc) · 7.1 KB
/
assn_1.c
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
#include<stdio.h>
#include<string.h>
#include<sys/time.h>
#include<sys/stat.h>
int bSearch(int s, int begin, int end, int K[]);
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("Invalid number of arguments!\nCommand Line Format: assn_1 search-mode keyfile-name seekfile-name\nExiting.\n");
return 1;
}
char* searchMode = argv[1];
char* keyfile = argv[2];
char* seekfile = argv[3];
FILE *fs, *fk; /* Seek and Key file stream */
int i, j;
struct timeval start, end, exec_tm;
/* Open seek.db file */
if ((fs = fopen( seekfile, "rb" )) == NULL) {
printf("Error in opening key file. Exiting.\n");
return 1;
}
/* Find the size of seek.db file to get number of integers in the file*/
int seekCount = 0;
size_t seekSize;
fseek(fs, 0L, SEEK_END);
seekSize = ftell(fs);
seekCount = seekSize/sizeof(int);
(void) fseek(fs, 0L, SEEK_SET);
/*
if ((fk = fopen( keyfile, "rb" )) == NULL) {
printf("Error in opening key file. Exiting.\n");
return 1;
}
int keyCount = 0;
size_t keySize;
fseek(fk, 0L, SEEK_END);
keySize = ftell(fk);
keyCount = keySize/sizeof(int);
(void) fseek(fk, 0L, SEEK_SET); */
int S[seekCount];
/* Create a third array hit of same size as S */
int hit[seekCount];
/* Read seek.db into an appropriately-sized integer array S */
for(i = 0; i < seekCount; i++) {
fread( &S[i], sizeof( int ), 1, fs );
}
(void) fseek(fs, 0L, SEEK_SET);
/* Find the size of key.db file to get number of integers in the file */
int keyCount = 0;
size_t keySize;
struct stat st;
stat(keyfile, &st);
keySize = st.st_size;
keyCount = keySize/sizeof(int);
/* In-Memory Sequential Search*/
if (strcmp(searchMode, "--mem-lin") == 0) {
int K[keyCount];
/* Start recording time */
gettimeofday( &start, NULL);
/* Open and read key.db into an appropriately-sized integer array K */
if ((fk = fopen( keyfile, "rb" )) == NULL) {
printf("Error in opening key file. Exiting.\n");
return 1;
}
for(i = 0; i < keyCount; i++) {
fread( &K[i], sizeof( int ), 1, fk );
}
/* For each S[i], search K sequentially from beginning to end for a matching key value.
If S[i] is found in K, set hit[i]=1. If S[i] is not found in K, set hit[i]=0 */
for(i = 0; i < seekCount; i++) {
hit[i] = 0;
for(j = 0; j < keyCount; j++){
if (S[i] == K[j]){
hit[i] = 1;
break;
}
}
}
/* End recording time */
gettimeofday( &end, NULL);
/* Calculate time difference and store in exec_tm */
long timeDiff = ( end.tv_sec - start.tv_sec) * 1000000 + ( end.tv_usec - start.tv_usec);
/*double timeDiff = ( end.tv_sec - start.tv_sec) + ( (end.tv_usec - start.tv_usec)/1000000.0 ); */
exec_tm.tv_sec = timeDiff / 1000000;
exec_tm.tv_usec = timeDiff % 1000000;
}
/* In-Memory Binary Search*/
else if (strcmp(searchMode, "--mem-bin") == 0) {
int K[keyCount];
/* Start recording time */
gettimeofday( &start, NULL);
/* Open and read key.db into an appropriately-sized integer array K */
if ((fk = fopen( keyfile, "rb" )) == NULL) {
printf("Error in opening key file. Exiting.\n");
return 1;
}
for(i = 0; i < keyCount; i++) {
fread( &K[i], sizeof( int ), 1, fk );
}
/* For each S[i], use a binary search on K to find a matching key value.
If S[i] is found in K, set hit[i]=1. If S[i] is not found, set hit[i]=0*/
for(i = 0; i < seekCount; i++) {
hit[i] = bSearch(S[i], 0, keyCount - 1, K);
}
/* End recording time */
gettimeofday( &end, NULL);
/* Calculate time difference and store in exec_tm */
long timeDiff = ( end.tv_sec - start.tv_sec) * 1000000 + ( end.tv_usec - start.tv_usec);
exec_tm.tv_sec = timeDiff / 1000000;
exec_tm.tv_usec = timeDiff % 1000000;
}
/* On-Disk Sequential Search*/
else if (strcmp(searchMode, "--disk-lin") == 0) {
int k;
/* Open key.db for reading */
if ((fk = fopen( keyfile, "rb" )) == NULL) {
printf("Error in opening key file. Exiting.\n");
return 1;
}
/* Start recording time */
gettimeofday( &start, NULL);
/* For each S[i], search key.db sequentially from beginning to end for a matching key value.
If S[i] is found in key.db, set hit[i]=1. If S[i] is not found in key.db, set hit[i]=0. */
for(i = 0; i < seekCount; i++) {
hit[i] = 0;
for(j = 0; j < keyCount; j++){
fread( &k, sizeof( int ), 1, fk);
if (S[i] == k){
hit[i] = 1;
break;
}
fseek( fk, (j + 1) * sizeof( int ), SEEK_SET );
}
(void) fseek(fk, 0, SEEK_SET);
clearerr(fk);
}
/* End recording time */
gettimeofday( &end, NULL);
/* Calculate time difference and store in exec_tm */
long timeDiff = ( end.tv_sec - start.tv_sec) * 1000000 + ( end.tv_usec - start.tv_usec);
exec_tm.tv_sec = timeDiff / 1000000;
exec_tm.tv_usec = timeDiff % 1000000;
}
/* On-Disk Binary Search*/
else if (strcmp(searchMode, "--disk-bin") == 0) {
int k, mid, startIndex, endIndex;
/* Open key.db for reading */
if ((fk = fopen( keyfile, "rb" )) == NULL) {
printf("Error in opening key file. Exiting.\n");
return 1;
}
/* Start recording time */
gettimeofday( &start, NULL);
/* For each S[i], use a binary search on key.db to find a matching key value.
If S[i] is found in key.db, set hit[i]=1. If S[i] is not found in key.db, set hit[i]=0.*/
for(i = 0; i < seekCount; i++) {
startIndex = 0;
endIndex = keyCount - 1;
hit[i] = 0;
for(; startIndex <= endIndex; ){
mid = (startIndex + endIndex) / 2;
fseek( fk, mid * sizeof( int ), SEEK_SET );
fread( &k, sizeof( int ), 1, fk);
if (k == S[i]){
hit[i] = 1;
break;
}
else if (k < S[i])
startIndex = mid + 1;
else if (k > S[i])
endIndex = mid - 1;
}
(void) fseek(fk, 0, SEEK_SET);
clearerr(fk);
}
/* End recording time */
gettimeofday( &end, NULL);
/* Calculate time difference and store in exec_tm */
long timeDiff = ( end.tv_sec - start.tv_sec) * 1000000 + ( end.tv_usec - start.tv_usec);
exec_tm.tv_sec = timeDiff / 1000000;
exec_tm.tv_usec = timeDiff % 1000000;
}
else {
printf("Invalid search-mode. Exiting.\n");
return 1;
}
/* Writing results for each key in S[i], and the total time needed to perform all searching */
for(i = 0; i < seekCount; i++) {
if ( hit[i] == 1 ) {
printf( "%12d: Yes\n", S[i] );
}
else if ( hit[i] == 0 ) {
printf( "%12d: No\n", S[i] );
}
}
printf( "Time: %ld.%06ld\n", exec_tm.tv_sec, exec_tm.tv_usec );
/* Closing both file descriptors and exiting */
fclose(fs);
fclose(fk);
return 0;
}
/* Function for performing Binary Search recursively */
int bSearch(int s, int begin, int end, int K[]) {
int mid;
if (begin < end) {
mid = (begin + end) / 2;
if (s == K[mid])
return 1;
else if (s > K[mid])
return bSearch(s, mid + 1, end, K);
else if (s < K[mid])
return bSearch(s, begin, mid, K);
}
else if (begin == end) {
if (s == K[begin])
return 1;
else
return 0;
}
return 0;
}