From 3a0c73cd1cd68373ea3a0784cab08a95e40fd6e2 Mon Sep 17 00:00:00 2001 From: mljoshi Date: Sun, 16 Jun 2024 21:14:18 -0700 Subject: [PATCH] add hashtable statistics, fix hashtable remove call --- main.cpp | 115 +++++++++++++++++++++++++++++++------------------------ 1 file changed, 65 insertions(+), 50 deletions(-) diff --git a/main.cpp b/main.cpp index 314c065..f960fc7 100644 --- a/main.cpp +++ b/main.cpp @@ -35,9 +35,10 @@ using std::cin, std::cout, std::string, std::vector; -void processInput(char command, HashTable& table, BinarySearchTree& tree, Stack& stack); +void processInput(char command, HashTable &table, BinarySearchTree &tree, Stack &stack); -int main() { +int main() +{ // Print help table for commands printHelp(); @@ -46,14 +47,18 @@ int main() { Stack 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 &hashTable, BinarySearchTree &tree, Stack< void undoDelete(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack); -void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTree, Stack &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 &cpuTable, BinarySearchTree &cpuTree, Stack &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 &hashTable, BinarySearchTree &tree) { +void insertCPU(HashTable &hashTable, BinarySearchTree &tree) +{ string cpuId; int releaseYear, coreCount; string architecture; @@ -133,26 +144,30 @@ void insertCPU(HashTable &hashTable, BinarySearchTree &tree) { tree.insert(cpuId); } -void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) { +void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack &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 &hashTable, BinarySearchTree &tree, Stack &undoStack) { - if (undoStack.isEmpty()) { +void undoDelete(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) +{ + if (undoStack.isEmpty()) + { cout << "No deletions to undo.\n"; return; }