-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcmdgen.c
More file actions
97 lines (78 loc) · 2.44 KB
/
Copy pathcmdgen.c
File metadata and controls
97 lines (78 loc) · 2.44 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
/* * * * * * * * *
* Utility program that generates random input and lookup commands for
* the hash table interpreter program
*
* usage:
* make cmdgen
* ./cmdgen ninserts nlookups > commandfilename
* ninserts: number of insert commands to generate
* nlookups: number of lookup commands to generate
* commandfilename: name of file to store commands in
*
* created for COMP20007 Design of Algorithms - Assignment 2, 2017
* by Shreyash Patodia and Matt Farrugia
*
* modifications by ...
* 14/05/17
* Ben Tomlin
* SN: 834198
* btomlin@student.unimelb.edu.au
*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "inthash.h"
/*************************************************************************/
void printusageexit(char *exe) {
/* Print usage information: */
fprintf(stderr, "usage: %s ninserts nlookups > commandfilename\n", exe);
fprintf(stderr, " ninserts: number of insert commands to generate\n");
fprintf(stderr, " nlookups: number of lookup commands to generate\n");
fprintf(stderr, " no-print: just print commands\n");
fprintf(stderr, " commandfilename: name of file to store commands in\n");
/* and exit, as promised :) */
exit(1);
}
/*************************************************************************/
int main(int argc, char **argv) {
int i;
/* Get command line arguments. */
if (argc < 3) {
printusageexit(argv[0]);
}
int ninserts = atoi(argv[1]);
int nlookups = atoi(argv[2]);
int NO_PRINT = atoi(argv[3]);
/* Seed the random number generator. */
srand(time(NULL));
/* Decide on some random numbers for insertion. */
int max = 100 * ninserts + 1;
int64 *inserts = malloc(sizeof (int64) * ninserts);
for (i = 0; i < ninserts; i++) {
inserts[i] = rand() % max;
}
/* Print insertion commands for these numbers. */
for (i = 0; i < ninserts; i++) {
printf("i %llu\n", inserts[i]);
}
/* Print lookup commands. Some will succeed, others will fail. */
for (i = 0; i < nlookups; i++) {
/* Flip a coin to decide whether to use an existing key or a new one. */
int64 lookup;
if (rand() % 2) {
/* Use a random existing key */
lookup = inserts[rand() % ninserts];
} else {
/* Generate a new random key */
lookup = rand() % max;
}
printf("l %llu\n", lookup);
}
/* Finish with commands to print the table, print statistics, and quit. */
if(!NO_PRINT) {
printf("p\n");
printf("s\n");
}
printf("q\n");
return 0;
}