From c6243e2314a3fb06b902e4f2e3ea64b75b928423 Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Mon, 17 Jun 2024 22:07:55 -0700 Subject: [PATCH] fix template classes apparently you can't have template stuff in .cpp files after all, lesson learned. --- BinarySearchTree.cpp | 63 -------------------------------------------- BinarySearchTree.h | 62 +++++++++++++++++++++++++++++++++++++++++++ BinaryTree.cpp | 56 --------------------------------------- BinaryTree.h | 57 +++++++++++++++++++++++++++++++++++++-- CMakeLists.txt | 8 +++--- CPU.cpp | 17 ++++++++++++ CPU.h | 15 +++-------- DisplayManager.cpp | 29 -------------------- DisplayManager.h | 42 ++++++++++++++++++++++++++--- SearchManager.cpp | 27 ------------------- SearchManager.h | 29 +++++++++++++++++--- Stack.cpp | 1 - StackNode.cpp | 1 - UndoManager.cpp | 37 -------------------------- UndoManager.h | 55 +++++++++++++++++++++++++++++++++----- main.cpp | 50 +++++++++++++++++------------------ 16 files changed, 279 insertions(+), 270 deletions(-) delete mode 100644 BinarySearchTree.cpp delete mode 100644 BinaryTree.cpp create mode 100644 CPU.cpp delete mode 100644 DisplayManager.cpp delete mode 100644 SearchManager.cpp delete mode 100644 Stack.cpp delete mode 100644 StackNode.cpp delete mode 100644 UndoManager.cpp diff --git a/BinarySearchTree.cpp b/BinarySearchTree.cpp deleted file mode 100644 index 38542fe..0000000 --- a/BinarySearchTree.cpp +++ /dev/null @@ -1,63 +0,0 @@ -#include "BinarySearchTree.h" - -template -void BinarySearchTree::insert(const T& newEntry) -{ - BinaryTreeNode* newNodePtr = new BinaryTreeNode(newEntry); - this->rootPtr = _insert(this->rootPtr, newNodePtr); - this->count++; -} - -template -BinaryTreeNode* BinarySearchTree::_insert(BinaryTreeNode* nodePtr, BinaryTreeNode* newNodePtr) { - BinaryTreeNode* pWalk = nodePtr, * parent = nullptr; - - if (!nodePtr) - { - nodePtr = newNodePtr; - return nodePtr; - } else { - while (pWalk) - { - parent = pWalk; - if (pWalk->getItem() > newNodePtr->getItem()) - pWalk = pWalk->getLeftPtr(); - else - pWalk = pWalk->getRightPtr(); - } - if (parent->getItem() > newNodePtr->getItem()) - parent->setLeftPtr(newNodePtr); - else - parent->setRightPtr(newNodePtr); - } - - return nodePtr; -} - -template -bool BinarySearchTree::search(const T& target, T& returnedItem) const -{ - BinarySearchTree* temp = nullptr; - - temp = _search(this->rootPtr, target); - if (temp) { - returnedItem = temp->getItem(); - return true; - } - - return false; -} - -template -BinaryTreeNode* BinarySearchTree::_search(BinaryTreeNode* nodePtr, const T& target) const -{ - BinaryTreeNode* found = nullptr; - - if (nodePtr) { - if (target.getCode() == nodePtr->getItem().getCode()) found = nodePtr; - else if (target.getCode() < nodePtr->getItem().getCode()) found = _search(nodePtr->getLeftPtr(), target); - else found = _search(nodePtr->getRightPtr(), target); - } - - return found; -} \ No newline at end of file diff --git a/BinarySearchTree.h b/BinarySearchTree.h index db00249..e4a7940 100644 --- a/BinarySearchTree.h +++ b/BinarySearchTree.h @@ -20,4 +20,66 @@ public: bool search(const T& target, T& returnedItem) const; }; +template +void BinarySearchTree::insert(const T& newEntry) +{ + BinaryTreeNode* newNodePtr = new BinaryTreeNode(newEntry); + this->root = _insert(this->root, newNodePtr); + this->size++; +} + +template +BinaryTreeNode* BinarySearchTree::_insert(BinaryTreeNode* nodePtr, BinaryTreeNode* newNodePtr) { + BinaryTreeNode* pWalk = nodePtr, * parent = nullptr; + + if (!nodePtr) + { + nodePtr = newNodePtr; + return nodePtr; + } else { + while (pWalk) + { + parent = pWalk; + if (pWalk->getItem() > newNodePtr->getItem()) + pWalk = pWalk->getLeftPtr(); + else + pWalk = pWalk->getRightPtr(); + } + if (parent->getItem() > newNodePtr->getItem()) + parent->setLeftPtr(newNodePtr); + else + parent->setRightPtr(newNodePtr); + } + + return nodePtr; +} + +template +bool BinarySearchTree::search(const T& target, T& returnedItem) const +{ + BinaryTreeNode* temp = nullptr; + + temp = _search(this->root, target); + if (temp) { + returnedItem = temp->getItem(); + return true; + } + + return false; +} + +template +BinaryTreeNode* BinarySearchTree::_search(BinaryTreeNode* nodePtr, const T& target) const +{ + BinaryTreeNode* found = nullptr; + + if (nodePtr) { + if (target == nodePtr->getItem()) found = nodePtr; + else if (target < nodePtr->getItem()) found = _search(nodePtr->getLeftPtr(), target); + else found = _search(nodePtr->getRightPtr(), target); + } + + return found; +} + #endif //INC_08_TEAM_PROJECT_BINARYSEARCHTREE_H diff --git a/BinaryTree.cpp b/BinaryTree.cpp deleted file mode 100644 index 18df1c1..0000000 --- a/BinaryTree.cpp +++ /dev/null @@ -1,56 +0,0 @@ -#include "BinaryTree.h" - -template -void BinaryTree::destroyTree(BinaryTreeNode* nodePtr) -{ - if (nodePtr) // != NULL - { - destroyTree(nodePtr->getLeftPtr()); - destroyTree(nodePtr->getRightPtr()); - delete nodePtr; - } -} - -template -void BinaryTree::_preorder(void visit(const T&), BinaryTreeNode* nodePtr) const -{ - if (nodePtr) { - T item = nodePtr->getItem(); - visit(item); - _preorder(visit, nodePtr->getLeftPtr()); - _preorder(visit, nodePtr->getRightPtr()); - } -} - -template -void BinaryTree::_inorder(void visit(const T&), BinaryTreeNode* nodePtr) const -{ - if (nodePtr) // != NULL - { - T item = nodePtr->getItem(); - _inorder(visit, nodePtr->getLeftPtr()); - visit(item); - _inorder(visit, nodePtr->getRightPtr()); - } -} - -template -void BinaryTree::_postorder(void visit(const T&), BinaryTreeNode* nodePtr) const -{ - if (nodePtr) { - T item = nodePtr->getItem(); - _postorder(visit, nodePtr->getLeftPtr()); - _postorder(visit, nodePtr->getRightPtr()); - visit(item); - } -} - -template -void BinaryTree::_printindented(void visit(const T&, int), BinaryTreeNode* nodePtr) const { - if (nodePtr) { - T item = nodePtr->getItem(); - _printInnerNodes(visit, nodePtr->getLeftPtr()); - if (nodePtr->getLeftPtr() || nodePtr->getRightPtr()) visit(item); - _printInnerNodes(visit, nodePtr->getRightPtr()); - } -} \ No newline at end of file diff --git a/BinaryTree.h b/BinaryTree.h index 1dbe773..f00d4a5 100644 --- a/BinaryTree.h +++ b/BinaryTree.h @@ -13,8 +13,6 @@ public: BinaryTree() : root(nullptr), size(0) {}; - BinaryTree(const BinaryTree& tree) { } - ~BinaryTree() { destroyTree(root); }; [[nodiscard]] bool isEmpty() const { return size == 0; }; @@ -51,4 +49,59 @@ private: }; +template +void BinaryTree::destroyTree(BinaryTreeNode* nodePtr) +{ + if (nodePtr) // != NULL + { + destroyTree(nodePtr->getLeftPtr()); + destroyTree(nodePtr->getRightPtr()); + delete nodePtr; + } +} + +template +void BinaryTree::_preorder(void visit(const T&), BinaryTreeNode* nodePtr) const +{ + if (nodePtr) { + T item = nodePtr->getItem(); + visit(item); + _preorder(visit, nodePtr->getLeftPtr()); + _preorder(visit, nodePtr->getRightPtr()); + } +} + +template +void BinaryTree::_inorder(void visit(const T&), BinaryTreeNode* nodePtr) const +{ + if (nodePtr) // != NULL + { + T item = nodePtr->getItem(); + _inorder(visit, nodePtr->getLeftPtr()); + visit(item); + _inorder(visit, nodePtr->getRightPtr()); + } +} + +template +void BinaryTree::_postorder(void visit(const T&), BinaryTreeNode* nodePtr) const +{ + if (nodePtr) { + T item = nodePtr->getItem(); + _postorder(visit, nodePtr->getLeftPtr()); + _postorder(visit, nodePtr->getRightPtr()); + visit(item); + } +} + +template +void BinaryTree::_printindented(void visit(const T&, int), BinaryTreeNode* nodePtr) const { + if (nodePtr) { + T item = nodePtr->getItem(); + _printInnerNodes(visit, nodePtr->getLeftPtr()); + if (nodePtr->getLeftPtr() || nodePtr->getRightPtr()) visit(item); + _printInnerNodes(visit, nodePtr->getRightPtr()); + } +} + #endif //INC_08_TEAM_PROJECT_BINARYTREE_H diff --git a/CMakeLists.txt b/CMakeLists.txt index 6b86449..1579af2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -7,17 +7,17 @@ add_executable(08_team_project main.cpp utils.h utils.cpp BinaryTreeNode.h - BinaryTree.cpp BinaryTree.h - BinarySearchTree.cpp BinarySearchTree.h HashNode.h HashTable.h CPU.h - Stack.cpp + CPU.cpp Stack.h - StackNode.cpp StackNode.h fio.h fio.cpp + DisplayManager.h + UndoManager.h + SearchManager.h ) diff --git a/CPU.cpp b/CPU.cpp new file mode 100644 index 0000000..4618648 --- /dev/null +++ b/CPU.cpp @@ -0,0 +1,17 @@ +#include +#include "CPU.h" + +using std::string, std::ostream, std::endl; + +ostream &operator<<(ostream &os, const CPU &cpu) { + os << "CPU ID: " << cpu.cpuId << std::endl; + return os; +} + +int key_to_index(const CPU &key, int size) { + std::string k = key.getCpuId(); + int sum = 0; + for (int i = 0; k[i]; i++) + sum += k[i]; + return sum % size; +} diff --git a/CPU.h b/CPU.h index 51f4924..847572c 100644 --- a/CPU.h +++ b/CPU.h @@ -59,20 +59,13 @@ public: friend void iDisplay(const CPU &cpu, int level); - friend std::ostream &operator<<(std::ostream &os, const CPU &cpu) { - os << "CPU ID: " << cpu.cpuId << std::endl; - return os; - } + friend std::ostream &operator<<(std::ostream &os, const CPU &cpu); - friend int key_to_index(const CPU &key, int size) { - std::string k = key.getCpuId(); - int sum = 0; - for (int i = 0; k[i]; i++) - sum += k[i]; - return sum % size; - }; + friend int key_to_index(const CPU &key, int size) ; }; +std::ostream &operator<<(std::ostream &os, const CPU &cpu); + /*~*~*~* Hash function: takes the key and returns the index in the hash table *~**/ diff --git a/DisplayManager.cpp b/DisplayManager.cpp deleted file mode 100644 index 29bed2c..0000000 --- a/DisplayManager.cpp +++ /dev/null @@ -1,29 +0,0 @@ -#include "DisplayManager.h" -#include - -DisplayManager::DisplayManager(HashTable* ht, BinarySearchTree* bst) { - this->hashTable = ht; - this->bst = bst; -} - -DisplayManager::~DisplayManager() { - // No dynamic memory allocation, so no cleanup needed -} - -void DisplayManager::displayAll() const { - std::cout << "All CPUs in the database:" << std::endl; - - // Iterate over the HashTable and display each CPU - for (int i = 0; i < hashTable->getSize(); i++) { - if (hashTable->getItem(i) != nullptr) { - std::cout << *(hashTable->getItem(i)) << std::endl; - } - } -} - -void DisplayManager::displayTree() const { - std::cout << "CPU Binary Search Tree:" << std::endl; - - // Call the BST's inorder traversal method to display the tree - bst->inorderTraversal(); -} \ No newline at end of file diff --git a/DisplayManager.h b/DisplayManager.h index 83a1b51..2321f0f 100644 --- a/DisplayManager.h +++ b/DisplayManager.h @@ -8,15 +8,49 @@ template class DisplayManager { private: - HashTable* hashTable; - BinarySearchTree* bst; + HashTable hashTable; + BinarySearchTree bst; public: - DisplayManager(HashTable* ht, BinarySearchTree* bst); + DisplayManager(HashTable& ht, BinarySearchTree& bst); ~DisplayManager(); void displayAll() const; void displayTree() const; }; -#endif \ No newline at end of file +template +DisplayManager::DisplayManager(HashTable &ht, BinarySearchTree &bst) { + this->hashTable = ht; + this->bst = bst; +} + +template +DisplayManager::~DisplayManager() { + // No dynamic memory allocation, so no cleanup needed +} + +template +void DisplayManager::displayAll() const { + std::cout << "All CPUs in the database:" << std::endl; + + // Iterate over the HashTable and display each CPU + for (int i = 0; i < hashTable.getSize(); i++) { + if (hashTable.getItem(i) != nullptr) { + std::cout << *(hashTable.getItem(i)) << std::endl; + } + } +} + +template +void DisplayManager::displayTree() const { + std::cout << "CPU Binary Search Tree:" << std::endl; + + // Call the BST's inorder traversal method to display the tree + // TODO: Use a proper display function + bst.inOrder([](const std::string& key) { + std::cout << key << std::endl; + }); +} + +#endif diff --git a/SearchManager.cpp b/SearchManager.cpp deleted file mode 100644 index 8e642c3..0000000 --- a/SearchManager.cpp +++ /dev/null @@ -1,27 +0,0 @@ -#include "SearchManager.h" -#include -#include - -SearchManager::SearchManager(HashTable* ht) { - this->hashTable = ht; -} - -SearchManager::~SearchManager() { - // No dynamic memory allocation, so no cleanup needed -} - -void SearchManager::searchCPU() const { - std::string cpuID; - std::cout << "Enter the CPU ID to search: "; - std::cin >> cpuID; - - // Search for the CPU in the HashTable - CPU* foundCPU = hashTable->search(cpuID); - - if (foundCPU != nullptr) { - std::cout << "CPU found:" << std::endl; - std::cout << *foundCPU << std::endl; - } else { - std::cout << "CPU not found." << std::endl; - } -} \ No newline at end of file diff --git a/SearchManager.h b/SearchManager.h index 2198acf..d3d8ed1 100644 --- a/SearchManager.h +++ b/SearchManager.h @@ -7,13 +7,34 @@ template class SearchManager { private: - HashTable* hashTable; + HashTable hashTable; public: - SearchManager(HashTable* ht); - ~SearchManager(); + SearchManager(HashTable &ht); void searchCPU() const; }; -#endif \ No newline at end of file +template +SearchManager::SearchManager(HashTable &ht) { + this->hashTable = ht; +} + +template +void SearchManager::searchCPU() const { + std::string cpuID; + std::cout << "Enter the CPU ID to search: "; + std::cin >> cpuID; + + // Search for the CPU in the HashTable + CPU *foundCPU = hashTable.search(cpuID); + + if (foundCPU != nullptr) { + std::cout << "CPU found:" << std::endl; + std::cout << *foundCPU << std::endl; + } else { + std::cout << "CPU not found." << std::endl; + } +} + +#endif diff --git a/Stack.cpp b/Stack.cpp deleted file mode 100644 index 25cd86c..0000000 --- a/Stack.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "Stack.h" diff --git a/StackNode.cpp b/StackNode.cpp deleted file mode 100644 index a0bb713..0000000 --- a/StackNode.cpp +++ /dev/null @@ -1 +0,0 @@ -#include "StackNode.h" diff --git a/UndoManager.cpp b/UndoManager.cpp deleted file mode 100644 index 218c2bb..0000000 --- a/UndoManager.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include "UndoManager.h" -#include - -UndoManager::UndoManager(HashTable* ht, BinarySearchTree* bst) { - this->hashTable = ht; - this->bst = bst; - this->undoStack = new Stack(); -} - -UndoManager::~UndoManager() { - delete undoStack; -} - -void UndoManager::addToUndoStack(const CPU& cpu) { - undoStack->push(cpu); -} - -void UndoManager::undoDelete() { - if (!undoStack->isEmpty()) { - CPU lastDeleted = undoStack->pop(); - - // Reinsert the CPU into the HashTable and BST - hashTable->insert(lastDeleted); - bst->insert(lastDeleted); - - std::cout << "Undo successful. CPU reinserted:" << std::endl; - std::cout << lastDeleted << std::endl; - } else { - std::cout << "No deletions to undo." << std::endl; - } -} - -void UndoManager::clearUndoStack() { - while (!undoStack->isEmpty()) { - undoStack->pop(); - } -} \ No newline at end of file diff --git a/UndoManager.h b/UndoManager.h index 6239cf3..2b4ee22 100644 --- a/UndoManager.h +++ b/UndoManager.h @@ -9,17 +9,60 @@ template class UndoManager { private: - HashTable* hashTable; - BinarySearchTree* bst; - Stack* undoStack; + HashTable hashTable; + BinarySearchTree bst; + Stack *undoStack; public: - UndoManager(HashTable* ht, BinarySearchTree* bst); + UndoManager(HashTable &ht, BinarySearchTree &bst); + ~UndoManager(); - void addToUndoStack(const CPU& cpu); + void addToUndoStack(const T &cpu); + void undoDelete(); + void clearUndoStack(); }; -#endif \ No newline at end of file +template +UndoManager::UndoManager(HashTable &ht, BinarySearchTree &bst) { + this->hashTable = ht; + this->bst = bst; + this->undoStack = new Stack(); +} + +template +UndoManager::~UndoManager() { + delete undoStack; +} + +template +void UndoManager::addToUndoStack(const T &cpu) { + undoStack->push(cpu); +} + +template +void UndoManager::undoDelete() { + if (!undoStack->isEmpty()) { + T lastDeleted = undoStack->pop(); + + // Reinsert the CPU into the HashTable and BST + hashTable.insert(lastDeleted, key_to_index); + bst.insert(lastDeleted.getCpuId()); + + std::cout << "Undo successful. CPU reinserted:" << std::endl; + std::cout << lastDeleted << std::endl; + } else { + std::cout << "No deletions to undo." << std::endl; + } +} + +template +void UndoManager::clearUndoStack() { + while (!undoStack->isEmpty()) { + undoStack->pop(); + } +} + +#endif diff --git a/main.cpp b/main.cpp index ff12eb9..308c2e4 100644 --- a/main.cpp +++ b/main.cpp @@ -33,10 +33,15 @@ #include "BinarySearchTree.h" #include "Stack.h" #include "fio.h" +#include "UndoManager.h" +#include "DisplayManager.h" +#include "SearchManager.h" 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, + UndoManager &undoManager, DisplayManager &displayManager, + SearchManager &searchManager); int main() { // Print help table for commands @@ -44,7 +49,11 @@ int main() { HashTable cpuTable; BinarySearchTree cpuTree; - Stack undoStack; + // Stack undoStack; + + DisplayManager displayManager(cpuTable, cpuTree); + SearchManager searchManager(cpuTable); + UndoManager undoManager(cpuTable, cpuTree); char command = ' '; while (command != 'Q') { @@ -53,7 +62,7 @@ int main() { command = toupper(command, std::locale()); // Temporary try catch block to handle unimplemented commands try { - processInput(command, cpuTable, cpuTree, undoStack); + processInput(command, cpuTable, cpuTree, undoManager, displayManager, searchManager); } catch (std::logic_error &e) { cout << e.what() << '\n'; @@ -68,13 +77,15 @@ int main() { void handleInsert(HashTable &hashTable, BinarySearchTree &tree); -void handleFileInput(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack); +void handleFileInput(HashTable &hashTable, BinarySearchTree &tree); -void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack); +void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, UndoManager &undoManager); -void undoDelete(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack); +// void undoDelete(HashTable &hashTable, BinarySearchTree &tree, UndoManager &undoManager); -void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTree, Stack &undoStack) { +void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTree, + UndoManager &undoManager, DisplayManager &displayManager, + SearchManager &searchManager) { switch (command) { case 'H': printHelp(); @@ -83,13 +94,14 @@ void processInput(char command, HashTable &cpuTable, BinarySearchTree &hashTable, BinarySearchTree &tree) { insertCPU(tree, hashTable); } -void handleFileInput(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) { +void handleFileInput(HashTable &hashTable, BinarySearchTree &tree) { string filename; cout << "Enter filename: "; cin >> filename; int hashSize = findHashSize(filename); hashTable = HashTable(hashSize); tree = BinarySearchTree(); - undoStack = Stack(); insertFile(filename, tree, hashTable); cout << "Data from file \"" << filename << "\" added.\n"; } -void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) { +void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, UndoManager &undoManager) { string cpuId; cout << "Enter CPU ID to delete: "; cin >> cpuId; @@ -147,19 +158,8 @@ void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, Stack< cpu = CPU(cpuId, 0, 0, "", 0.0); } hashTable.remove(cpuFound, cpu, key_to_index); - undoStack.push(cpuFound); + undoManager.addToUndoStack(cpuFound); tree.remove(cpuId); cout << "CPU ID \"" << cpuId << "\" deleted.\n"; } - -void undoDelete(HashTable &hashTable, BinarySearchTree &tree, Stack &undoStack) { - if (undoStack.isEmpty()) { - cout << "No deletions to undo.\n"; - return; - } - CPU cpu = undoStack.pop(); - hashTable.insert(cpu, key_to_index); - tree.insert(cpu.getCpuId()); - cout << "Undo deletion of CPU ID \"" << cpu.getCpuId() << "\".\n"; -}