From cd2e1470ca9033ec913291139e16289328f78b8c Mon Sep 17 00:00:00 2001 From: Iurii Tatishchev Date: Fri, 21 Jun 2024 21:12:57 -0700 Subject: [PATCH] finalize documentation --- BinaryTreeNode.h | 2 -- CPU.h | 34 ++++++++++++++++++++++++++++++- HashNode.h | 26 +++++++++++++++--------- HashTable.h | 25 +++++++++++++++++++++++ fio.cpp | 25 +++++++++++++++++++++++ fio.h | 37 ++++++++++++++++++++++++++++++---- main.cpp | 36 +++++++++++++++++++++++++++------ utils.cpp | 10 ---------- utils.h | 52 ++++++++++++++++++++++++++++++++++++++++++++++++ 9 files changed, 215 insertions(+), 32 deletions(-) diff --git a/BinaryTreeNode.h b/BinaryTreeNode.h index e5e582d..5c9c5c7 100644 --- a/BinaryTreeNode.h +++ b/BinaryTreeNode.h @@ -21,8 +21,6 @@ #ifndef INC_08_TEAM_PROJECT_BINARYTREENODE_H #define INC_08_TEAM_PROJECT_BINARYTREENODE_H -#include - template class BinaryTreeNode { private: diff --git a/CPU.h b/CPU.h index 266a9b2..1e9e5d5 100644 --- a/CPU.h +++ b/CPU.h @@ -1,3 +1,8 @@ +// CPU class +// +// Written by: Kevin Galvan Serrano +// Modified by: Iurii Tatishchev + #ifndef INC_08_TEAM_PROJECT_CPU_H #define INC_08_TEAM_PROJECT_CPU_H @@ -72,19 +77,46 @@ public: }; +/* + * Display the CPU object as a table. + * + * Written by: Iurii Tatishchev + */ void display(const CPU &cpu); +/* + * Display the CPU object as part of an indented tree. + * + * Written by: Tuhin Mondal + */ void iDisplay(const std::string &cpuId, int level); +/* + * Display the CPU object as a table row. + * + * Written by: Iurii Tatishchev + */ void rowDisplay(const CPU &cpu, const std::vector &widths); +/* + * Overload the << operator to display the CPU object. + * + * Written by: Iurii Tatishchev + */ std::ostream &operator<<(std::ostream &os, const CPU &cpu); /*~*~*~* - Hash function: takes the key and returns the index in the hash table + Hash function: takes the key and returns the index in the hash table. + + Written by: Joshiro Lawrence *~**/ int key_to_index(const CPU &key, int size); +/*~*~*~* + Converts the CPU object to a string. + + Written by: Iurii Tatishchev + *~**/ std::string to_string(const CPU &cpu); #endif // INC_08_TEAM_PROJECT_CPU_H diff --git a/HashNode.h b/HashNode.h index d9a696c..ca35b79 100644 --- a/HashNode.h +++ b/HashNode.h @@ -1,9 +1,13 @@ +/* +Joshiro Lawrence - Unit 3 (Hash Table) +Wrote all functions in this file. +*/ + #ifndef INC_08_TEAM_PROJECT_HASHNODE_H #define INC_08_TEAM_PROJECT_HASHNODE_H -template -class HashNode -{ +template +class HashNode { private: T item; int occupied; // 1 -> occupied, 0 -> empty from start, -1 -> empty after removal @@ -11,31 +15,35 @@ private: public: // constructors - HashNode() - { + HashNode() { occupied = 0; numCollisions = 0; } - HashNode(T anItem) - { + + HashNode(T anItem) { item = anItem; occupied = 1; numCollisions = 0; } - HashNode(T anItem, int ocp, int nCol) - { + + HashNode(T anItem, int ocp, int nCol) { item = anItem; occupied = ocp; numCollisions = nCol; } + // setters void setItem(const T &anItem) { item = anItem; } + void setOccupied(int ocp) { occupied = ocp; } + void setNumCollisions(int nCol) { numCollisions = nCol; } // getters T getItem() const { return item; } + int getOccupied() const { return occupied; } + int getNumCollisions() const { return numCollisions; } }; diff --git a/HashTable.h b/HashTable.h index 9b38745..ed66a99 100644 --- a/HashTable.h +++ b/HashTable.h @@ -1,3 +1,10 @@ +/* +Joshiro Lawrence - Unit 3 (Hash Table) +Wrote all functions in this file except for _reHash, writeToFile, and the code block of insert that deals with rehashing + +The purpose of the hash table in this project is to store the CPU objects. This allows for quick insertion, search, and removal into the hash table. Specifically, the hash table allows for quick searching in this program, to tell if a CPU object exists in the database already. +*/ + #ifndef INC_08_TEAM_PROJECT_HASHTABLE_H #define INC_08_TEAM_PROJECT_HASHTABLE_H @@ -173,6 +180,15 @@ int HashTable::search(T &itemOut, const T &key, int h(const T &key, int size) return -1; } +/* + * Name: reHash + * Written By: Kevin Cremin + * Modified By: + * Purpose: Increases the size of the Hash Table's array + * Input: Hash function + * Output: N/A + * Procedure: Creates a larger array, and then traverses the old array, rehashing each item into the new one. + */ template void HashTable::_reHash(int h(const T &key, int size)) { @@ -207,6 +223,15 @@ void HashTable::_reHash(int h(const T &key, int size)) { hashSize = nHashSize; } +/* + * Name: writeToFile + * Written By: Kevin Cremin + * Modified By: + * Purpose: Outputs each item in the hash table into a file. + * Input: Name of the file, function + * Output: N/A + * Procedure: Checks if a bucket is in use, and outputs it into the file. + */ template void writeToFile(const HashTable &hashTable, const string &filename, string visit(const T &)) { ofstream outputFile(filename); diff --git a/fio.cpp b/fio.cpp index 5de2b5f..73cb386 100644 --- a/fio.cpp +++ b/fio.cpp @@ -1,3 +1,10 @@ +// Unit 5: File I/O +// - Determine hash size based on the number of records in the file +// - Read data from the input file and insert them into the hash table and BST +// +// Written by: Kevin Cremin +// Modified by: Iurii Tatishchev + #include #include "fio.h" @@ -143,6 +150,15 @@ void insertCPU(BinarySearchTree &bst, HashTable &hash) { hash.insert(aCPU, key_to_index); } +/* + * Name: isInteger + * Written By: Kevin Cremin + * Modified By: + * Purpose: Determine if input is an integer + * Input: String to test + * Output: Bool of whether input was an integer + * Procedure: Checks that all characters are digits. + */ bool isInteger(const string &str) { for (int i = 0; i < str.length(); i++) { if (!(isdigit(str[i]))) { @@ -153,6 +169,15 @@ bool isInteger(const string &str) { return true; } +/* + * Name: isDouble + * Written By: Kevin Cremin + * Modified By: + * Purpose: Determine if input is a double + * Input: String to test + * Output: Bool of whether input was an double + * Procedure: Checks that all characters are digits and that there is at most one period. + */ bool isDouble(const string &str) { int chance = 0; diff --git a/fio.h b/fio.h index e084433..6549e1c 100644 --- a/fio.h +++ b/fio.h @@ -1,8 +1,8 @@ // Unit 5: File I/O // - Determine hash size based on the number of records in the file // - Read data from the input file and insert them into the hash table and BST -// - Save to file (in hash table sequence) -// - Re-hashing +// - Save to file (in hash table sequence) (in HashTable.h) +// - Re-hashing (in HashTable.h) // // Written by: Kevin Cremin @@ -18,10 +18,39 @@ using std::string; -int findHashSize(const string& filename); +/* + * Name: findHashSize + * Written By: Kevin Cremin + * Modified By: Iurii Tatishchev + * Purpose: Find the the necessary initial size of the hash table + * Input: Name of file + * Output: Appropriate size of hash array based on how many items are in the fileName + * Procedure: Multiplies number of items in file by two, then finds next prime number + */ +int findHashSize(const string &filename); -void insertFile(const string& filename, BinarySearchTree &bst, HashTable &hash); +/* + * Name: insertFile + * Written By: Kevin Cremin + * Modified By: Iurii Tatishchev + * Purpose: Input all items in the file into the program. + * Input: Name of file, the Binary Search Tree, and the Hash Table + * Output: N/A + * Procedure: Goes through each item in the file and inputs them into the table and tree, + * verifying that there are no duplicate keys. + */ +void insertFile(const string &filename, BinarySearchTree &bst, HashTable &hash); +/* + * Name: insertCPU + * Written By: Kevin Cremin + * Modified By: + * Purpose: Input an individual CPU from the user + * Input: The Binary Search Tree and the Hash Table + * Output: N/A + * Procedure: Requests information from the user, making sure that it is not a duplicate, + * and that all inputs are the correct data type. + */ void insertCPU(BinarySearchTree &bst, HashTable &hash); #endif //INC_08_TEAM_PROJECT_FIO_H diff --git a/main.cpp b/main.cpp index 8d40c2a..201a2bf 100644 --- a/main.cpp +++ b/main.cpp @@ -24,6 +24,11 @@ // The output file name does not have to be the same as the input file name, // but the file format must be the same so that it can be read back into the program. +// Unit 1: Main +// - Menu and input processing +// +// Written by: Iurii Tatishchev + #include #include #include "utils.h" @@ -40,6 +45,9 @@ using namespace std; const string DEFAULT_FILE = "out.txt"; +/* + * Takes a command character and calls the appropriate function to process the command. + */ void processInput(char command, HashTable &table, BinarySearchTree &tree, UndoManager &undoManager, DisplayManager &displayManager, SearchManager &searchManager); @@ -68,7 +76,9 @@ int main() { cout << "Enter an option (H - for help): "; cin >> command; command = toupper(command, std::locale()); - // Temporary try catch block to handle unimplemented commands + // Temporary try catch block to handle unimplemented commands. + // Actually, this catches exceptions like stoi (integer input too large) + // so I think leaving it is a good idea. try { processInput(command, cpuTable, cpuTree, undoManager, displayManager, searchManager); } @@ -77,18 +87,31 @@ int main() { } } // Quit command received - writeToFile(cpuTable, DEFAULT_FILE, to_string); cout << "Exiting program...\n"; return 0; } +/* + * Input a new record into the table and tree. + */ void handleInsert(HashTable &hashTable, BinarySearchTree &tree); +/* + * Input data from a file into the table and tree. + */ void handleFileInput(HashTable &hashTable, BinarySearchTree &tree); +/* + * Delete a record from the table and tree. + * Also adds the deleted record to the undo stack. + */ void deleteCPU(HashTable &hashTable, BinarySearchTree &tree, UndoManager &undoManager); +/* + * Write data to a file. + * Also clears the undo stack. + */ void handleFileOutput(HashTable &hashTable, UndoManager &undoManager); void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTree, @@ -121,9 +144,9 @@ void processInput(char command, HashTable &cpuTable, BinarySearchTree &cpuTable, BinarySearchTree &hashTable, BinarySearchTree &tree, UndoMa CPU cpu(cpuId, 0, 0, "", 0.0); CPU cpuFound; - if (hashTable.search(cpuFound, cpu, key_to_index) == -1) { + if (hashTable.search(cpuFound, cpu, key_to_index) == -1) { cout << "CPU ID \"" << cpuId << "\" not found.\n"; return; } diff --git a/utils.cpp b/utils.cpp index 3baea30..841de19 100644 --- a/utils.cpp +++ b/utils.cpp @@ -58,16 +58,6 @@ void printTableFooter(const vector &widths) { cout << "\b┘\n"; } -/* - * Print ascii/unicode table with given column widths and data. For example: - * ┌────────────────────────────────────────┬──────────┬────────────────────────┬────────────────┐ - * | Col1 | Col2 | Col3 | Numeric Column | - * +========================================+==========+========================+================+ - * | Value 1 | Value 2 | 123 | 10.0 | - * | Separate | cols | with a tab or 4 spaces | -2,027.1 | - * | This is a row with only one cell | | | | - * └────────────────────────────────────────┴──────────┴────────────────────────┴────────────────┘ - */ void printTable(const vector &widths, const vector> &data) { printTableHeader(widths, data[0]); for (int i = 1; i < data.size(); i++) { diff --git a/utils.h b/utils.h index c64e04b..9e74c07 100644 --- a/utils.h +++ b/utils.h @@ -1,3 +1,6 @@ +// Contains utility functions mostly for printing tables and formatting. +// Written by: Iurii Tatishchev + #ifndef INC_08_TEAM_PROJECT_UTILS_H #define INC_08_TEAM_PROJECT_UTILS_H @@ -6,22 +9,71 @@ using namespace std; +/* + * Print a single row of a table, given the widths of each column and the data for the row. + * + * Example output: + * | Value 1 | Value 2 | 123 | 10.0 | + */ void printRow(const vector &widths, const vector &row); +/* + * Print the header of a table, given the widths of each column and the headers for each column. + * Unicode characters are used to draw the table. + * + * Example output: + * ┌────────────────────────────────────────┬──────────┬────────────────────────┬────────────────┐ + * | Col1 | Col2 | Col3 | Numeric Column | + * +========================================+==========+========================+================+ + */ void printTableHeader(const vector &widths, const vector &headers); +/* + * Print the footer of a table, given the widths of each column. + * + * Example output: + * └────────────────────────────────────────┴──────────┴────────────────────────┴────────────────┘ + */ void printTableFooter(const vector &widths); +/* + * Print ascii/unicode table with given column widths and data. For example: + * ┌────────────────────────────────────────┬──────────┬────────────────────────┬────────────────┐ + * | Col1 | Col2 | Col3 | Numeric Column | + * +========================================+==========+========================+================+ + * | Value 1 | Value 2 | 123 | 10.0 | + * | Separate | cols | with a tab or 4 spaces | -2,027.1 | + * | This is a row with only one cell | | | | + * └────────────────────────────────────────┴──────────┴────────────────────────┴────────────────┘ + */ void printTable(const vector &widths, const vector> &data); +/* + * Print a help table with all available commands and their descriptions. + */ void printHelp(); +/* + * Print a table with the team members and their roles. + */ void printTeam(); +/* + * Find the next prime number after n. + * + * Written by: Kevin Cremin + * Modified by: Iurii Tatishchev + */ int findNextPrime(int n); +/* + * Add bold control characters around a string for terminal output. + */ string boldStr(const string &str); +/* + * Pad a string with spaces on both sides to center it in a string of a given width. + */ string centeredStr(const string &str, int width); #endif //INC_08_TEAM_PROJECT_UTILS_H