Major refactor of files

This commit is contained in:
2023-03-05 03:05:41 -06:00
parent f3dabfdb65
commit 0fbc5b8bea
10 changed files with 535 additions and 392 deletions
+129
View File
@@ -0,0 +1,129 @@
#include "basic_sorts.hpp"
#include <string>
#include <vector>
namespace basic_sorts
{
// Reimplementation of Insertion Sort to be standalone
void InsertionSort(std::vector<std::string> *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<std::string> *newWordList)
{
_MergeSort(newWordList, 0, newWordList->size() - 1);
return;
}
void _MergeSort(std::vector<std::string> *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<std::string> *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<std::string> *newWordList)
{
_HeapSort(newWordList);
return;
}
void _HeapSort(std::vector<std::string> *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<std::string> *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);
}
}
+4 -4
View File
@@ -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;
}
+197
View File
@@ -0,0 +1,197 @@
#include "sort_controller.hpp"
#include "basic_sorts.hpp"
#include "trees.hpp"
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// 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;
}
-334
View File
@@ -1,334 +0,0 @@
#include "sorts.h"
#include <chrono>
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
// 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<std::string> *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<std::string> *newWordList)
{
__MergeSort__(newWordList, 0, newWordList->size() - 1);
return;
}
void __MergeSort__(std::vector<std::string> *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<std::string> *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<std::string> *newWordList)
{
__HeapSort__(newWordList);
return;
}
void __HeapSort__(std::vector<std::string> *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<std::string> *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);
}
}
+81
View File
@@ -0,0 +1,81 @@
#include "trees.hpp"
#include <string>
#include <vector>
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)
{
;
}
}