add hashtable statistics, fix hashtable remove call

This commit is contained in:
mljoshi 2024-06-16 21:14:18 -07:00
parent 6bf9b6c9ee
commit 3a0c73cd1c

115
main.cpp
View File

@ -35,9 +35,10 @@
using std::cin, std::cout, std::string, std::vector;
void processInput(char command, HashTable<CPU>& table, BinarySearchTree<string>& tree, Stack<CPU>& stack);
void processInput(char command, HashTable<CPU> &table, BinarySearchTree<string> &tree, Stack<CPU> &stack);
int main() {
int main()
{
// Print help table for commands
printHelp();
@ -46,14 +47,18 @@ int main() {
Stack<CPU> undoStack;
char command = ' ';
while (command != 'Q') {
while (command != 'Q')
{
cout << "Enter an option (H - for help): ";
cin >> command;
command = toupper(command, std::locale());
// Temporary try catch block to handle unimplemented commands
try {
try
{
processInput(command, cpuTable, cpuTree, undoStack);
} catch (std::logic_error& e) {
}
catch (std::logic_error &e)
{
cout << e.what() << '\n';
}
}
@ -70,49 +75,55 @@ void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<
void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack);
void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<string> &cpuTree, Stack<CPU> &undoStack) {
switch (command) {
case 'H':
printHelp();
break;
case 'I': // Insert a new record
insertCPU(cpuTable, cpuTree);
break;
case 'F': // File input: add data from a file
throw std::logic_error("Not yet implemented: File input: add data from a file");
break;
case 'D': // Delete one record
deleteCPU(cpuTable, cpuTree, undoStack);
break;
case 'U': // Undo delete
undoDelete(cpuTable, cpuTree, undoStack);
break;
case 'L': // List all CPUs sorted by primary key
throw std::logic_error("Not yet implemented: List all CPUs sorted by primary key");
break;
case 'S': // Search for a CPU by the primary key
throw std::logic_error("Not yet implemented: Search for a CPU by the primary key");
break;
case 'W': // Write data to a file
throw std::logic_error("Not yet implemented: Write data to a file");
break;
case 'T': // Hashtable statistics
throw std::logic_error("Not yet implemented: Hashtable statistics");
break;
case 'P': // Print indented tree
throw std::logic_error("Not yet implemented: Print indented tree");
break;
case 'Z': // Display names of team members
printTeam();
break;
case 'Q': // Quit
break;
default:
cout << "Invalid command. Press 'H' to view available commands.\n";
void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<string> &cpuTree, Stack<CPU> &undoStack)
{
switch (command)
{
case 'H':
printHelp();
break;
case 'I': // Insert a new record
insertCPU(cpuTable, cpuTree);
break;
case 'F': // File input: add data from a file
throw std::logic_error("Not yet implemented: File input: add data from a file");
break;
case 'D': // Delete one record
deleteCPU(cpuTable, cpuTree, undoStack);
break;
case 'U': // Undo delete
undoDelete(cpuTable, cpuTree, undoStack);
break;
case 'L': // List all CPUs sorted by primary key
throw std::logic_error("Not yet implemented: List all CPUs sorted by primary key");
break;
case 'S': // Search for a CPU by the primary key
throw std::logic_error("Not yet implemented: Search for a CPU by the primary key");
break;
case 'W': // Write data to a file
throw std::logic_error("Not yet implemented: Write data to a file");
break;
case 'T': // Hashtable statistics
// throw std::logic_error("Not yet implemented: Hashtable statistics");
cout << "Load factor: " << cpuTable.getLoadFactor() << std::endl;
cout << "Total number of collisions: " << cpuTable.getTotalCollisions() << std::endl;
cout << "Longest collision path: " << cpuTable.getMaxCollisions() << std::endl;
break;
case 'P': // Print indented tree
throw std::logic_error("Not yet implemented: Print indented tree");
break;
case 'Z': // Display names of team members
printTeam();
break;
case 'Q': // Quit
break;
default:
cout << "Invalid command. Press 'H' to view available commands.\n";
}
}
void insertCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree) {
void insertCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree)
{
string cpuId;
int releaseYear, coreCount;
string architecture;
@ -133,26 +144,30 @@ void insertCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree) {
tree.insert(cpuId);
}
void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack) {
void deleteCPU(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack)
{
string cpuId;
cout << "Enter CPU ID to delete: ";
cin >> cpuId;
CPU cpu(cpuId, 0, 0, "", 0.0);
CPU cpuFound;
while (!hashTable.search(cpuFound, cpu, key_to_index)) {
while (!hashTable.search(cpuFound, cpu, key_to_index))
{
cout << "CPU ID not found. Enter a valid CPU ID: ";
cin >> cpuId;
cpu = CPU(cpuId, 0, 0, "", 0.0);
}
hashTable.remove(cpuFound, cpu, key_to_index);
undoStack.push(cpuFound);
hashTable.remove(cpu, key_to_index);
tree.remove(cpuId);
cout << "CPU ID \"" << cpuId << "\" deleted.\n";
}
void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack) {
if (undoStack.isEmpty()) {
void undoDelete(HashTable<CPU> &hashTable, BinarySearchTree<string> &tree, Stack<CPU> &undoStack)
{
if (undoStack.isEmpty())
{
cout << "No deletions to undo.\n";
return;
}