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

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,8 +75,10 @@ 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) {
void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<string> &cpuTree, Stack<CPU> &undoStack)
{
switch (command)
{
case 'H':
printHelp();
break;
@ -97,7 +104,10 @@ void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<strin
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");
// 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");
@ -112,7 +122,8 @@ void processInput(char command, HashTable<CPU> &cpuTable, BinarySearchTree<strin
}
}
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;
}