-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
248 lines (204 loc) · 6.21 KB
/
main.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
/* * * * * * * * *
* Main program:
* reads command line options, runs a hash table interpreter
*
* created for COMP20007 Design of Algorithms - Assignment 2, 2017
* by Matt Farrugia <[email protected]>
*
* Modified by
* 14/05/17
* Ben Tomlin
* SN: 834198
*
* Summary of modification
* Added code to support csv printing operations
* Need silencer on for bash code to work as intended
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <getopt.h>
#include "inthash.h"
#include "hashtbl.h"
// Need the silencer for bash code to work all proper goodly
#define SILENT 0 // Silences insert and lookup operation
#define DEFAULT_SIZE 4
// command line options
typedef struct options {
TableType type;
int initial_size;
} Options;
Options get_options(int argc, char** argv);
// interpreter commands
#define INSERT 'i'
#define LOOKUP 'l'
#define PRINT 'p'
#define STATS 's'
#define STATS_HEAD 'n'
#define STATS_ROW 'd'
#define HELP 'h'
#define QUIT 'q'
#define MAX_LINE_LEN 80
int get_command(char *operation, int64 *key);
// main program
void run_interpreter(HashTable *table);
int main(int argc, char **argv) {
// get command line options (to determine table type, size, etc.)
Options options = get_options(argc, argv);
// create hashtable (of given type)
HashTable *table = new_hash_table(options.type, options.initial_size);
// start the interpreter loop
run_interpreter(table);
// done!
free_hash_table(table);
return 0;
}
// print out the valid operations
void print_operations() {
printf(" %c number: insert 'number' into table\n", INSERT);
printf(" %c number: lookup is 'number' in table\n", LOOKUP);
printf(" %c: print table\n", PRINT);
printf(" %c: print stats\n", STATS);
printf(" %c: print csv stats header\n", STATS_HEAD);
printf(" %c: print csv stats\n", STATS_ROW);
printf(" %c: quit\n", QUIT);
}
// run the interpreter, reading and performing commands until 'quit'
void run_interpreter(HashTable *table) {
// print a prompt at the beginning
if(!SILENT) {printf("enter a command (h for help):\n");}
char op;
int64 key;
// then loop, getting and executing commands, until 'quit'
while (true) {
// read a command, storing results in op and key variables
int argc = get_command(&op, &key);
if (argc < 1) {
continue; // no valid command entered, get another
}
// execute the command
switch (op) {
case INSERT:
if (argc < 2) {
// insert commands must have an argument
printf("syntax: %c number\n", INSERT);
} else {
// perform the insertion
if (hash_table_insert(table, key)) {
if (!SILENT) {printf("%llu inserted\n", key);}
} else {
if (!SILENT) {printf("%llu already in table\n", key);}
}
}
break;
case LOOKUP:
if (argc < 2) {
// lookup commands must have an argument
printf("syntax: %c number\n", LOOKUP);
} else {
// perform the lookup
if (hash_table_lookup(table, key)) {
if (!SILENT) {printf("%llu found\n", key);}
} else {
if (!SILENT) {printf("%llu not found\n", key);}
}
}
break;
case PRINT:
// perform the print table
hash_table_print(table);
break;
case STATS:
// perform the print stats
hash_table_stats(table);
break;
case STATS_HEAD:
// perform the print csv stat header
hash_table_csv_stats_header(table);
break;
case STATS_ROW:
// perform the print csv format stats
hash_table_csv_stats(table);
break;
default:
// display error
printf("unknown operation '%c'\n", op);
// fall through!
case HELP:
// list available options
printf("available operations:\n");
print_operations();
break;
case QUIT:
// leave the interpreter loop
if (!SILENT) {printf("exiting\n");}
return;
}
}
}
// reads a line from stdin, parses it into an operation character and possibly
// a long long uinteger argument. store results in *operation and *key, resp.
//
// returns the number of tokens successfully read (e.g. 0 for none,
// 1 for operation only, 2 for both operation and integer)
int get_command(char *operation, int64 *key) {
// read a line from stdin, up to MAX_LINE_LENGTH, into character buffer
char line[MAX_LINE_LEN];
fgets(line, MAX_LINE_LEN, stdin);
line[strlen(line)-1] = '\0'; // strip trailing newline
// attempt to parse the line string into *operation and *key
int argc = sscanf(line, "%c %llu", operation, key);
// note: since llu is unsigned, a command like 'i -1' will overflow,
// resulting in *key = 18446744073709551615 (2^64-1). this is a feature.
// return the number of variables successfully read, as required
return argc;
}
// scans command line arguments for program options,
// prints usage info and exits if commands are missing or otherwise invalid
Options get_options(int argc, char** argv) {
// create the Options structure with defaults
Options options = { .type = NOTYPE, .initial_size = DEFAULT_SIZE };
// use C's built-in getopt function to scan inputs by flag
char option;
while ((option = getopt(argc, argv, "t:s:")) != EOF){
switch (option){
case 't': // set hash table type
options.type = strtotype(optarg);
break;
case 's': // set hash table size
options.initial_size = atoi(optarg);
break;
default:
break;
}
}
// validation and printing error / usage messages
bool valid = true;
// check part validity
if(options.type == NOTYPE){
fprintf(stderr,
"please specify which table type to use, using the -t flag:\n");
fprintf(stderr, " -t linear: linear hash table\n");
fprintf(stderr, " -t xtndbl1: 1-key extendible hash table\n");
fprintf(stderr, " -t 1 or cuckoo: cuckoo hash table (part 1)\n");
fprintf(stderr,
" -t 2 or xtnbdln: n-key extendible hash table (part 2)\n");
fprintf(stderr, " -t 3 or xuckoo: extendible cuckoo table (part 3)\n");
fprintf(stderr, " -t xuckoon: xuckoon table (bonus part 4)\n");
valid = false;
}
// validate table size
if(options.initial_size <= 0) {
fprintf(stderr,
"please specify initial table size (>0) using the -s flag\n");
valid = false;
}
// check overall validity before continuing
if(!valid){
exit(EXIT_FAILURE);
}
return options;
}