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
|
main(int argc, char **argv)
{
string s, s2, key;
string digits = "0123456789abcdef";
int i;
HashTable *h;
int ts;
ifstream f;
if (argc != 5 && argc != 4) usage("");
ts = atoi(argv[1]);
if (ts <= 0) usage("Bad Table Size");
s = argv[2];
if (s != "Last7" && s != "XOR") usage("Bad function");
s = argv[3];
if (s != "Linear" && s != "Double") usage("Bad resolution");
if (argc == 5) {
f.open(argv[4]);
if (f.fail()) { perror(argv[4]); exit(1); }
}
h = new HashTable(ts, argv[2], argv[3]);
if (argc == 5) {
while (getline(f, s)) {
for (i = 0; digits.find(s[i]) != string::npos; i++) ;
if (i == 0 || i == s.size() || s[i] != ' ') {
cerr << "Bad line: " << s << endl;
exit(1);
}
key = s.substr(0, i);
for (; s[i] == ' '; i++) ;
if (i == s.size()) { cerr << "Bad line: " << s << endl; exit(1); }
s2 = s.substr(i);
if (h->Find(key) != "") {
fprintf(stderr, "Key %s inserted twice\n", key.c_str());
exit(1);
}
h->Add_Hash(key, s2);
}
f.close();
}
while (cin >> s) {
if (s == "PRINT") {
h->Print();
} else if (s == "TPROBES") {
printf("%d\n", h->Total_Probes());
} else if (s == "ADD") {
if (cin >> s >> s2) {
if (h->Find(s) != "") {
printf("Key %s is already in the table\n", s.c_str());
} else {
h->Add_Hash(s, s2);
}
}
} else if (s == "FIND") {
if (cin >> s) {
s2 = h->Find(s);
if (s2 == "") {
printf("Didn't find %s\n", s.c_str());
} else {
printf("%s %s\n", s.c_str(), s2.c_str());
}
}
} else {
cerr << "Commands are PRINT, FIND key, ADD key val, and TPROBES\n";
}
}
}
|