diff --git a/Makefile b/Makefile index 1d29d69..b253a16 100644 --- a/Makefile +++ b/Makefile @@ -5,7 +5,9 @@ main: compile link compile: g++ $(INC) $(STD) -c -o build/main.o src/main.cpp - g++ $(INC) $(STD) -c -o build/sorts.o src/sorts.cpp + g++ $(INC) $(STD) -c -o build/sort_controller.o src/sort_controller.cpp + g++ $(INC) $(STD) -c -o build/basic_sorts.o src/basic_sorts.cpp + g++ $(INC) $(STD) -c -o build/trees.o src/trees.cpp link: g++ -o bin/main.out build/*.o diff --git a/include/basic_sorts.hpp b/include/basic_sorts.hpp new file mode 100644 index 0000000..1aee411 --- /dev/null +++ b/include/basic_sorts.hpp @@ -0,0 +1,26 @@ +#ifndef BASIC_SORTS_HPP +#define BASIC_SORTS_HPP + +#include +#include + +// Different basic sorting algorithm implementations +namespace basic_sorts +{ + // Performs Insertion Sort on given word list (vector of strings) + void InsertionSort(std::vector *newWordList); + + // Performs Merge Sort on given word list (vector of strings) + void MergeSort(std::vector *newWordList); + void _MergeSort(std::vector *newWordList, int p, int r); + void _Merge(std::vector *newWordList, int p, int q, int r); + + // Performs Heap Sort on given word list (vector of strings) + void HeapSort(std::vector *newWordList); + void _HeapSort(std::vector *newWordList); + void _Heapify(std::vector *newWordList, int i, int heapSize); + int _HEAPSORT_LEFT(int i); + int _HEAPSORT_RIGHT(int i); +} + +#endif \ No newline at end of file diff --git a/include/sort_controller.hpp b/include/sort_controller.hpp new file mode 100644 index 0000000..2bce6ad --- /dev/null +++ b/include/sort_controller.hpp @@ -0,0 +1,34 @@ +#ifndef SORT_CONTROLLER_HPP +#define SORT_CONTROLLER_HPP + +#include +#include +#include + +enum SortType {INSERTION = 0, MERGE, HEAP}; + +class SortController +{ +public: + SortController(); + void CheckArguments(int argc, char* arguments[]); + void ReadWordFile(void); + void RunBenchmarks(void); +protected: + ; +private: + std::string filename; + SortType currentType; + std::chrono::duration sortTime; + std::vector newWordList; + std::vector originalWordList; + int lineCount; + bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven; + void Benchmarking(void); + void BenchmarkingAll(void); + void OutputResult(void); + void EchoSortTime(std::string outputFilename); + void WriteOutputToFile(std::string outputFilename); +}; + +#endif \ No newline at end of file diff --git a/include/sorts.h b/include/sorts.h deleted file mode 100644 index 8448bb4..0000000 --- a/include/sorts.h +++ /dev/null @@ -1,53 +0,0 @@ -#ifndef SORTS_H -#define SORTS_H - -#include -#include -#include - -enum SortType {INSERTION = 0, MERGE, HEAP}; - -class Sorter -{ -private: - std::string filename; - int lineCount; - std::vector originalWordList; - std::chrono::duration sortTime; -public: - Sorter(); - std::vector newWordList; - bool defaultFile, defaultOnly, fileGiven, allLists, sortGiven; - SortType currentType; - std::string GetFilename(void); - void SetFilename(std::string newName); - void SetWordList(void); - void SortAll(void); - void RunSorts(void); - void __RunSorts__(void); - void OutputResult(void); - void PrintSortTime(std::string outputFilename); - void PrintToFile(std::string outputFilename); -}; - -void CheckArguments(int argc, char* arguments[], Sorter* sortObj); - -namespace ImplementedSort -{ - // Performs Insertion Sort on given word list (vector of strings) - void InsertionSort(std::vector *newWordList); - - // Performs Merge Sort on given word list (vector of strings) - void MergeSort(std::vector *newWordList); - void __MergeSort__(std::vector *newWordList, int p, int r); - void __Merge__(std::vector *newWordList, int p, int q, int r); - - // Performs Heap Sort on given word list (vector of strings) - void HeapSort(std::vector *newWordList); - void __HeapSort__(std::vector *newWordList); - void __Heapify__(std::vector *newWordList, int i, int heapSize); - int __HEAPSORT_LEFT__(int i); - int __HEAPSORT_RIGHT__(int i); -} - -#endif diff --git a/include/trees.hpp b/include/trees.hpp new file mode 100644 index 0000000..cd22ad4 --- /dev/null +++ b/include/trees.hpp @@ -0,0 +1,61 @@ +#ifndef TREES_HPP +#define TREES_HPP + +#include +#include + +// Namespace for different implementations of trees +namespace tree_implementation +{ + // Base Tree class + class Tree + { + public: + Tree(void); + void Insert(void); + void Search(void); + void InOrderTreeTraversal(void); + protected: + virtual void PrintParentKey(std::string key) = 0; + virtual void PrintLeftChild(std::string key) = 0; + virtual void PrintRightChild(std::string key) = 0; + virtual void PrintPathToRoot(std::string key) = 0; + private: + ; + }; + + // Binary Search Tree operations + // TODO: Implement BST + class BinarySearchTree : public Tree + { + public: + void PrintParentKey(std::string key); + void PrintLeftChild(std::string key); + void PrintRightChild(std::string key); + void PrintPathToRoot(std::string key); + protected: + ; + private: + ; + }; + + // Red-Black Tree operations + // TODO: Implement Red-black tree + class RedBlackTree : public Tree + { + public: + void PrintParentKey(std::string key); + void PrintLeftChild(std::string key); + void PrintRightChild(std::string key); + void PrintPathToRoot(std::string key); + void PrintColor(std::string key); + void PrintParentColor(std::string key); + void PrintUncleColor(std::string key); + protected: + ; + private: + ; + }; +} + +#endif \ No newline at end of file diff --git a/src/basic_sorts.cpp b/src/basic_sorts.cpp new file mode 100644 index 0000000..384d742 --- /dev/null +++ b/src/basic_sorts.cpp @@ -0,0 +1,129 @@ +#include "basic_sorts.hpp" +#include +#include + +namespace basic_sorts +{ + // Reimplementation of Insertion Sort to be standalone + void InsertionSort(std::vector *newWordList) + { + int i; + std::string key; + for (int j = 1; j < newWordList->size(); j++) + { + key = newWordList->at(j); + i = j - 1; + while ((i >= 0) && (newWordList->at(i) > key)) + { + newWordList->at(i + 1) = newWordList->at(i); + i = i - 1; + } + newWordList->at(i + 1) = key; + } + } + + // Reimplementation of Merge Sort to be standalone + void MergeSort(std::vector *newWordList) + { + _MergeSort(newWordList, 0, newWordList->size() - 1); + return; + } + + void _MergeSort(std::vector *newWordList, int p, int r) + { + if (p < r) + { + int q = (p + r) / 2; + _MergeSort(newWordList, p, q); + _MergeSort(newWordList, q+1, r); + _Merge(newWordList, p, q, r); + } + return; + } + + void _Merge(std::vector *newWordList, int p, int q, int r) + { + int n1 = q - p + 1; + int n2 = r - q; + const int leftTmpSize = n1 + 1; + const int rightTmpSize = n2 + 1; + std::string leftTmp[leftTmpSize]; + std::string rightTmp[rightTmpSize]; + for (int i = 0; i < n1; i++) + leftTmp[i] = newWordList->at(p + i); + for (int i = 0; i < n2; i++) + rightTmp[i] = newWordList->at(q + i + 1); + leftTmp[n1] = "ZZZZZ"; + rightTmp[n2] = "ZZZZZ"; + int i = 0; + int j = 0; + for (int k = p; k <= r; k++) + { + if (leftTmp[i] <= rightTmp[j]) + { + newWordList->at(k) = leftTmp[i]; + i++; + } else { + newWordList->at(k) = rightTmp[j]; + j++; + } + } + + } + + // Reimplementation of Heap Sort to be standalone + void HeapSort(std::vector *newWordList) + { + _HeapSort(newWordList); + return; + } + + void _HeapSort(std::vector *newWordList) + { + std::string tempStr; + int heapSize; + // Build Max Heap + heapSize = newWordList->size(); + for (int i = ((newWordList->size() / 2) - 1); i >= 0; i--) + _Heapify(newWordList, i, heapSize); + // Heap Sort + for (int i = (newWordList->size() - 1); i >= 0; i--) + { + tempStr = newWordList->at(i); + newWordList->at(i) = newWordList->at(0); + newWordList->at(0) = tempStr; + heapSize = i; + _Heapify(newWordList, 0, heapSize); + } + return; + } + + void _Heapify(std::vector *newWordList, int i, int heapSize) + { + std::string tempStr; + int largest = i; + int leftIndex = _HEAPSORT_LEFT(i); + int rightIndex = _HEAPSORT_RIGHT(i); + if ((leftIndex < heapSize) && (newWordList->at(leftIndex) > newWordList->at(largest))) + largest = leftIndex; + if ((rightIndex < heapSize) && (newWordList->at(rightIndex) > newWordList->at(largest))) + largest = rightIndex; + if (largest != i) + { + tempStr = newWordList->at(i); + newWordList->at(i) = newWordList->at(largest); + newWordList->at(largest) = tempStr; + _Heapify(newWordList, largest, heapSize); + } + } + + int _HEAPSORT_LEFT(int i) + { + return ((2 * i) + 1); + } + + int _HEAPSORT_RIGHT(int i) + { + return ((2 * i) + 2); + } +} \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index d73fb73..ccb8fb2 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,9 +1,9 @@ -#include "sorts.h" +#include "sort_controller.hpp" int main(int argc, char* argv[]) { - Sorter newSort; - CheckArguments(argc, argv, &newSort); - newSort.RunSorts(); + SortController benchmark; + benchmark.CheckArguments(argc, argv); + benchmark.RunBenchmarks(); return 0; } diff --git a/src/sort_controller.cpp b/src/sort_controller.cpp new file mode 100644 index 0000000..059db1e --- /dev/null +++ b/src/sort_controller.cpp @@ -0,0 +1,197 @@ +#include "sort_controller.hpp" +#include "basic_sorts.hpp" +#include "trees.hpp" +#include +#include +#include +#include +#include + +// Initialization function for SortController class +SortController::SortController() +{ + lineCount = 0; + currentType = INSERTION; + defaultFile = 0; + defaultOnly = 1; + fileGiven = 0; + allLists = 0; + sortGiven = 0; +} + +// Sets word list found in file into vector +void SortController::ReadWordFile(void) +{ + std::string bufferStr; + std::ifstream file(this->filename); + if (!file.is_open()) + { + std::cout << "Failed opening file: " << this->filename << '\n'; + exit(1); + } + while (getline(file, bufferStr)) + { + originalWordList.push_back(bufferStr); + lineCount++; + } + return; +} + +// Main function for calling all sort categories +void SortController::RunBenchmarks(void) +{ + if (defaultOnly || fileGiven) + { + ReadWordFile(); + Benchmarking(); + } + if (allLists) + BenchmarkingAll(); + return; +} + +// Sorts all default files if allLists is set +void SortController::BenchmarkingAll(void) +{ + int fileCount = 10; + std::string newFilename; + for (int i = 1; i <= fileCount; i++) + { + lineCount = 0; + newFilename = "test/PERM/perm"; + newFilename += std::to_string(15*i); + newFilename += "K.txt"; + filename = newFilename; + ReadWordFile(); + Benchmarking(); + originalWordList.clear(); + } +} + +// Function for starting sort functions +void SortController::Benchmarking(void) +{ + if (!sortGiven) + currentType = INSERTION; + if (currentType == INSERTION) + { + newWordList = originalWordList; + auto start = std::chrono::system_clock::now(); + basic_sorts::InsertionSort(&newWordList); + auto end = std::chrono::system_clock::now(); + sortTime = end - start; + OutputResult(); + } + if (!sortGiven) + currentType = MERGE; + if (currentType == MERGE) + { + newWordList = originalWordList; + auto start = std::chrono::system_clock::now(); + basic_sorts::MergeSort(&newWordList); + auto end = std::chrono::system_clock::now(); + sortTime = end - start; + OutputResult(); + } + if (!sortGiven) + currentType = HEAP; + if (currentType == HEAP) + { + newWordList = originalWordList; + auto start = std::chrono::system_clock::now(); + basic_sorts::HeapSort(&newWordList); + auto end = std::chrono::system_clock::now(); + sortTime = end - start; + OutputResult(); + } +} + +// Main function for printing results +void SortController::OutputResult(void) +{ + switch(currentType) + { + case INSERTION: + EchoSortTime("IS"); + WriteOutputToFile("IS"); + break; + case MERGE: + EchoSortTime("MS"); + WriteOutputToFile("MS"); + break; + case HEAP: + EchoSortTime("HS"); + WriteOutputToFile("HS"); + break; + default: + break; + } +} + +// Prints sort time to the screen +void SortController::EchoSortTime(std::string outputFilename) +{ + std::string sortSizeString = std::to_string(lineCount / 1000); + std::cout << outputFilename << sortSizeString; + std::cout << " took " << sortTime.count() << " s" << std::endl; + return; +} + +// Prints result of sort operation to file +void SortController::WriteOutputToFile(std::string outputFilename) +{ + std::string sortSizeString = std::to_string(lineCount / 1000); + std::string outputPath = "test/OUTPUT/"; + outputPath += outputFilename + sortSizeString + "K.txt"; + std::ofstream file(outputPath); + if (!file.is_open()) + { + std::cout << "Failed opening file\n"; + exit(1); + } + for (unsigned int i = 0; i < lineCount; i++) + file << newWordList[i] << '\n'; + file.close(); +} + +// Checks for command line arguments +void SortController::CheckArguments(int argc, char* arguments[]) +{ + std::string tempStr; + for (int i = 0; i < argc; i++) + { + tempStr = arguments[i]; + if ((tempStr == "-a") || (tempStr == "--all")) + { + defaultOnly = 0; + allLists = 1; + } + if ((tempStr == "-f") || (tempStr == "--filename")) + { + filename = arguments[i + 1]; + defaultOnly = 0; + fileGiven = 1; + } + if ((tempStr == "-d") || (tempStr == "--default")) + { + filename = "test/PERM/perm15K.txt"; + defaultFile = 1; + } + if ((tempStr == "-s") || (tempStr == "--sort-type")) + { + sortGiven = 1; + tempStr = arguments[i + 1]; + if (tempStr == "insertion") + currentType = INSERTION; + if (tempStr == "merge") + currentType = MERGE; + if (tempStr == "heap") + currentType = HEAP; + if (tempStr == "all") + sortGiven = 0; + } + } + if (defaultOnly) + filename = "test/PERM/perm15K.txt"; + return; +} \ No newline at end of file diff --git a/src/sorts.cpp b/src/sorts.cpp deleted file mode 100644 index 1fc759d..0000000 --- a/src/sorts.cpp +++ /dev/null @@ -1,334 +0,0 @@ -#include "sorts.h" -#include -#include -#include -#include -#include - -// Initialization function for Sorter class -Sorter::Sorter() -{ - lineCount = 0; - currentType = INSERTION; - defaultFile = 0; - defaultOnly = 1; - fileGiven = 0; - allLists = 0; - sortGiven = 0; -} - -// Sets the current filename -void Sorter::SetFilename(std::string newName) -{ - this->filename = newName; -} - -// Returns the current filename -std::string Sorter::GetFilename(void) -{ - return this->filename; -} - -// Sets word list found in file into vector -void Sorter::SetWordList(void) -{ - std::string bufferStr; - std::ifstream file(this->filename); - if (!file.is_open()) - { - std::cout << "Failed opening file: " << this->filename << '\n'; - exit(1); - } - while (getline(file, bufferStr)) - { - originalWordList.push_back(bufferStr); - lineCount++; - } - return; -} - -// Main function for calling all sort categories -void Sorter::RunSorts(void) -{ - if (defaultOnly || fileGiven) - { - SetWordList(); - __RunSorts__(); - } - if (allLists) - SortAll(); - return; -} - -// Sorts all default files if allLists is set -void Sorter::SortAll(void) -{ - int fileCount = 10; - std::string newFilename; - for (int i = 1; i <= fileCount; i++) - { - lineCount = 0; - newFilename = "test/PERM/perm"; - newFilename += std::to_string(15*i); - newFilename += "K.txt"; - SetFilename(newFilename); - SetWordList(); - __RunSorts__(); - originalWordList.clear(); - } -} - -// Function for starting sort functions -void Sorter::__RunSorts__(void) -{ - if (!sortGiven) - currentType = INSERTION; - if (currentType == INSERTION) - { - newWordList = originalWordList; - auto start = std::chrono::system_clock::now(); - ImplementedSort::InsertionSort(&newWordList); - auto end = std::chrono::system_clock::now(); - sortTime = end - start; - OutputResult(); - } - if (!sortGiven) - currentType = MERGE; - if (currentType == MERGE) - { - newWordList = originalWordList; - auto start = std::chrono::system_clock::now(); - ImplementedSort::MergeSort(&newWordList); - auto end = std::chrono::system_clock::now(); - sortTime = end - start; - OutputResult(); - } - if (!sortGiven) - currentType = HEAP; - if (currentType == HEAP) - { - newWordList = originalWordList; - auto start = std::chrono::system_clock::now(); - ImplementedSort::HeapSort(&newWordList); - auto end = std::chrono::system_clock::now(); - sortTime = end - start; - OutputResult(); - } -} - -// Main function for printing results -void Sorter::OutputResult(void) -{ - switch(currentType) - { - case INSERTION: - PrintSortTime("IS"); - PrintToFile("IS"); - break; - case MERGE: - PrintSortTime("MS"); - PrintToFile("MS"); - break; - case HEAP: - PrintSortTime("HS"); - PrintToFile("HS"); - break; - default: - break; - } -} - -// Prints sort time to the screen -void Sorter::PrintSortTime(std::string outputFilename) -{ - std::string sortSizeString = std::to_string(lineCount / 1000); - std::cout << outputFilename << sortSizeString; - std::cout << " took " << sortTime.count() << " s" << std::endl; - return; -} - -// Prints result of sort operation to file -void Sorter::PrintToFile(std::string outputFilename) -{ - std::string sortSizeString = std::to_string(lineCount / 1000); - std::string outputPath = "test/OUTPUT/"; - outputPath += outputFilename + sortSizeString + "K.txt"; - std::ofstream file(outputPath); - if (!file.is_open()) - { - std::cout << "Failed opening file\n"; - exit(1); - } - for (unsigned int i = 0; i < lineCount; i++) - file << newWordList[i] << '\n'; - file.close(); -} - -// Checks for command line arguments -void CheckArguments(int argc, char* arguments[], Sorter* sortObj) -{ - std::string tempStr; - for (int i = 0; i < argc; i++) - { - tempStr = arguments[i]; - if ((tempStr == "-a") || (tempStr == "--all")) - { - sortObj->defaultOnly = 0; - sortObj->allLists = 1; - - } - if ((tempStr == "-f") || (tempStr == "--filename")) - { - sortObj->SetFilename(arguments[i + 1]); - sortObj->defaultOnly = 0; - sortObj->fileGiven = 1; - } - if ((tempStr == "-d") || (tempStr == "--default")) - { - sortObj->SetFilename("test/PERM/perm15K.txt"); - sortObj->defaultFile = 1; - } - if ((tempStr == "-s") || (tempStr == "--sort-type")) - { - sortObj->sortGiven = 1; - tempStr = arguments[i + 1]; - if (tempStr == "insertion") - sortObj->currentType = INSERTION; - if (tempStr == "merge") - sortObj->currentType = MERGE; - if (tempStr == "heap") - sortObj->currentType = HEAP; - if (tempStr == "all") - sortObj->sortGiven = 0; - } - } - if (sortObj->defaultOnly) - sortObj->SetFilename("test/PERM/perm15K.txt"); - return; -} - -namespace ImplementedSort -{ - // Reimplementation of Insertion Sort to be standalone - void InsertionSort(std::vector *newWordList) - { - int i; - std::string key; - for (int j = 1; j < newWordList->size(); j++) - { - key = newWordList->at(j); - i = j - 1; - while ((i >= 0) && (newWordList->at(i) > key)) - { - newWordList->at(i + 1) = newWordList->at(i); - i = i - 1; - } - newWordList->at(i + 1) = key; - } - } - - // Reimplementation of Merge Sort to be standalone - void MergeSort(std::vector *newWordList) - { - __MergeSort__(newWordList, 0, newWordList->size() - 1); - return; - } - - void __MergeSort__(std::vector *newWordList, int p, int r) - { - if (p < r) - { - int q = (p + r) / 2; - __MergeSort__(newWordList, p, q); - __MergeSort__(newWordList, q+1, r); - __Merge__(newWordList, p, q, r); - } - return; - } - - void __Merge__(std::vector *newWordList, int p, int q, int r) - { - int n1 = q - p + 1; - int n2 = r - q; - const int leftTmpSize = n1 + 1; - const int rightTmpSize = n2 + 1; - std::string leftTmp[leftTmpSize]; - std::string rightTmp[rightTmpSize]; - for (int i = 0; i < n1; i++) - leftTmp[i] = newWordList->at(p + i); - for (int i = 0; i < n2; i++) - rightTmp[i] = newWordList->at(q + i + 1); - leftTmp[n1] = "ZZZZZ"; - rightTmp[n2] = "ZZZZZ"; - int i = 0; - int j = 0; - for (int k = p; k <= r; k++) - { - if (leftTmp[i] <= rightTmp[j]) - { - newWordList->at(k) = leftTmp[i]; - i++; - } else { - newWordList->at(k) = rightTmp[j]; - j++; - } - } - - } - - // Reimplementation of Heap Sort to be standalone - void HeapSort(std::vector *newWordList) - { - __HeapSort__(newWordList); - return; - } - - void __HeapSort__(std::vector *newWordList) - { - std::string tempStr; - int heapSize; - // Build Max Heap - heapSize = newWordList->size(); - for (int i = ((newWordList->size() / 2) - 1); i >= 0; i--) - __Heapify__(newWordList, i, heapSize); - // Heap Sort - for (int i = (newWordList->size() - 1); i >= 0; i--) - { - tempStr = newWordList->at(i); - newWordList->at(i) = newWordList->at(0); - newWordList->at(0) = tempStr; - heapSize = i; - __Heapify__(newWordList, 0, heapSize); - } - return; - } - - void __Heapify__(std::vector *newWordList, int i, int heapSize) - { - std::string tempStr; - int largest = i; - int leftIndex = __HEAPSORT_LEFT__(i); - int rightIndex = __HEAPSORT_RIGHT__(i); - if ((leftIndex < heapSize) && (newWordList->at(leftIndex) > newWordList->at(largest))) - largest = leftIndex; - if ((rightIndex < heapSize) && (newWordList->at(rightIndex) > newWordList->at(largest))) - largest = rightIndex; - if (largest != i) - { - tempStr = newWordList->at(i); - newWordList->at(i) = newWordList->at(largest); - newWordList->at(largest) = tempStr; - __Heapify__(newWordList, largest, heapSize); - } - } - - int __HEAPSORT_LEFT__(int i) - { - return ((2 * i) + 1); - } - - int __HEAPSORT_RIGHT__(int i) - { - return ((2 * i) + 2); - } -} \ No newline at end of file diff --git a/src/trees.cpp b/src/trees.cpp new file mode 100644 index 0000000..7b2f837 --- /dev/null +++ b/src/trees.cpp @@ -0,0 +1,81 @@ +#include "trees.hpp" +#include +#include + +namespace tree_implementation +{ + Tree::Tree(void) + { + ; + } + + void Tree::Insert(void) + { + ; + } + + void Tree::Search(void) + { + ; + } + + void Tree::InOrderTreeTraversal(void) + { + ; + } + + void BinarySearchTree::PrintParentKey(std::string key) + { + ; + } + + void BinarySearchTree::PrintLeftChild(std::string key) + { + ; + } + + void BinarySearchTree::PrintRightChild(std::string key) + { + ; + } + + void BinarySearchTree::PrintPathToRoot(std::string key) + { + ; + } + + void RedBlackTree::PrintParentKey(std::string key) + { + ; + } + + void RedBlackTree::PrintLeftChild(std::string key) + { + ; + } + + void RedBlackTree::PrintRightChild(std::string key) + { + ; + } + + void RedBlackTree::PrintPathToRoot(std::string key) + { + ; + } + + void RedBlackTree::PrintColor(std::string key) + { + ; + } + + void RedBlackTree::PrintParentColor(std::string key) + { + ; + } + + void RedBlackTree::PrintUncleColor(std::string key) + { + ; + } +} \ No newline at end of file