diff --git a/OutputProcessor.cpp b/OutputProcessor.cpp index 67d7f4d..49d8358 100644 --- a/OutputProcessor.cpp +++ b/OutputProcessor.cpp @@ -1,14 +1,77 @@ #include "OutputProcessor.h" +#include +#include #include +#include #include #include #include +#include #include +#include +#include #include #include +bool checkSorted(const std::vector &vector) { + for (size_t i = 0; i < vector.size() - 1; i++) { + if (vector.at(i) > vector.at(i + 1)) { + return false; + } + } + return true; +} + +void bozosortAlignedVectors(std::vector &vector1, + std::vector &vector2) { + auto threadCount = std::thread::hardware_concurrency(); + if (threadCount == 0) + threadCount = 8; + + std::atomic shouldAbort(false); + std::vector threads{}; + for (unsigned int i = 0; i < threadCount; i++) { + std::thread t( + [vector1, vector2, &shouldAbort, + i](std::vector* vector1Original, std::vector* vector2Original) mutable { + std::mt19937 twister(std::chrono::steady_clock::now() + .time_since_epoch() + .count() + + i); + std::uniform_int_distribution dist(0, + vector1.size() - 1); + do { + if (shouldAbort) return; + size_t first = dist(twister); + size_t second = dist(twister); + + std::string temp = vector1.at(first); + vector1.at(first) = vector1.at(second); + vector1.at(second) = temp; + + // Also swap elements in the aligned vector. If I store + // where everything moved to maybe it could be faster? + unsigned int temp2 = vector2.at(first); + vector2.at(first) = vector2.at(second); + vector2.at(second) = temp2; + } while (!checkSorted(vector1)); + + *vector1Original = vector1; + *vector2Original = vector2; + shouldAbort = true; + }, + &vector1, &vector2); + + threads.push_back(std::move(t)); + } + + for (unsigned int i = 0; i < threadCount; i++) { + threads.at(i).join(); + } +} + OutputProcessor::OutputProcessor() { _fileOut = std::ofstream(); _allWords = std::vector(); @@ -77,6 +140,9 @@ void OutputProcessor::analyzeWords(std::vector allWords, // Increment total word count _totalWordCount++; } + + // :3 + bozosortAlignedVectors(_uniqueWords, _wordCounts); } bool OutputProcessor::openStream() {