Skip to content

Commit

Permalink
Statistics are now safe variables
Browse files Browse the repository at this point in the history
And added forgotten description for bitonic
  • Loading branch information
CosminPerRam committed Jun 19, 2022
1 parent 082441a commit cdc2572
Show file tree
Hide file tree
Showing 11 changed files with 38 additions and 27 deletions.
8 changes: 5 additions & 3 deletions include/SortingStatistics.h
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
#pragma once

#include <atomic>

struct SortingStatistics
{
float sortTimeMs = 0.f;
unsigned long long comparisons = 0, reads = 0, writes = 0, steps = 0;
std::atomic<float> sortTimeMs = 0.f;
std::atomic<unsigned long long> comparisons = 0, reads = 0, writes = 0, steps = 0;

unsigned cursorPosition = 0, cursorValue = 0;
std::atomic<unsigned> cursorPosition = 0, cursorValue = 0;

void reset();

Expand Down
6 changes: 4 additions & 2 deletions src/BitonicSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ void BitonicSort::bitonicMerge(int low, int cnt, int dir)
std::swap(numbers[i], numbers[i + k]);

DO_PUT_CURSOR_AT(i + k);
DO_PROGRESSIVE_CHECKSTEP;
stats.addStep();
}
else
DO_PUT_CURSOR_AT(i);

DO_CHECKSTEP;
}

bitonicMerge(low, k, dir);
Expand Down Expand Up @@ -47,5 +49,5 @@ void BitonicSort::sorter() {
}

const char* BitonicSort::getDescription() {
return "aaa";
return "Bitonic sort is a comparison-based sorting algorithm that can be run in parallel. It focuses on converting a random sequence of numbers into a bitonic sequence, one that monotonically increases, then decreases. Not running paralleled.";
}
4 changes: 3 additions & 1 deletion src/BubbleSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,10 @@ void BubbleSort::sorter() {
std::swap(numbers[j], numbers[j + 1]);

stats.addSwaps();
DO_PROGRESSIVE_CHECKSTEP;
stats.addStep();
}

DO_CHECKSTEP;
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/CocktailSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ void CocktailSort::sorter() {

while (swapped)
{
DO_PROGRESSIVE_CHECKSTEP;

swapped = false;

for (int i = start; i < end; ++i)
Expand All @@ -21,9 +23,9 @@ void CocktailSort::sorter() {
std::swap(numbers[i], numbers[i + 1]);
stats.addSwaps();
swapped = true;

DO_PROGRESSIVE_CHECKSTEP;
}

DO_CHECKSTEP;
}

if (!swapped)
Expand All @@ -41,9 +43,9 @@ void CocktailSort::sorter() {
std::swap(numbers[i], numbers[i + 1]);
stats.addSwaps();
swapped = true;

DO_PROGRESSIVE_CHECKSTEP;
}

DO_CHECKSTEP;
}

++start;
Expand Down
4 changes: 3 additions & 1 deletion src/CombSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,12 @@ void CombSort::sorter() {
swapped = true;

DO_PUT_CURSOR_AT(i);
DO_PROGRESSIVE_CHECKSTEP;
stats.addStep();
}
else
DO_PUT_CURSOR_AT_WITH_BACKWARDS(i + 1);

DO_CHECKSTEP;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/HeapSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void HeapSort::sorter() {
std::swap(numbers[0], numbers[i]);

DO_PUT_CURSOR_AT(i);
DO_CHECKSTEP;
DO_PROGRESSIVE_CHECKSTEP;

heapify(i, 0);
}
Expand Down
3 changes: 2 additions & 1 deletion src/InsertionSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ void InsertionSort::sorter() {
stats.addAssigments();
numbers[j + 1] = numbers[j];
DO_PUT_CURSOR_AT(j + 1);
DO_PROGRESSIVE_CHECKSTEP;
DO_CHECKSTEP;
j = j - 1;
}
stats.addAssigments();
numbers[j + 1] = key;
DO_PROGRESSIVE_CHECKSTEP;
}

DO_FINISHED;
Expand Down
22 changes: 11 additions & 11 deletions src/Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,11 @@ void Interface::draw(sf::RenderWindow& window) {
ImGui::EndDisabled(); ImGui::SameLine();

static bool noDelay = false;
static float oldDelay = 0.f;
static float oldDelay = 0.f, delay = Manager::delayMs;

ImGui::BeginDisabled(noDelay);
ImGui::Text("Delay"); ImGui::SameLine();
ImGui::PushItemWidth(128);
float delay = Manager::delayMs;
if (ImGui::SliderFloat("##Delay", &delay, 1.f, 500.f, "%.2f ms", ImGuiSliderFlags_Logarithmic))
Manager::delayMs = delay;
ImGui::PopItemWidth();
Expand Down Expand Up @@ -211,23 +210,23 @@ void Interface::draw(sf::RenderWindow& window) {

if (ImGui::BeginTable("table", 6, ImGuiTableFlags_BordersInnerV)) {
ImGui::TableNextColumn();
ImGui::Text("Comparisons: %llu", currentData.comparisons);
ImGui::Text("Comparisons: %llu", currentData.comparisons.load());

ImGui::TableNextColumn();
ImGui::Text("Reads: %llu", currentData.reads);
ImGui::Text("Reads: %llu", currentData.reads.load());

ImGui::TableNextColumn();
ImGui::Text("Writes: %llu", currentData.writes);
ImGui::Text("Writes: %llu", currentData.writes.load());

ImGui::TableNextColumn();
ImGui::Text("Steps: %llu", currentData.steps);
ImGui::Text("Steps: %llu", currentData.steps.load());

ImGui::TableNextColumn();
ImGui::Text("Visual time: %.2f s", Manager::visualTime.asSeconds());

ImGui::TableNextColumn();
ImGui::Text("Real time: %.f ms", currentData.sortTimeMs); ImGui::SameLine();
Custom::HelpMarker("(An approximation, in actual\nreal time use its faster)");
ImGui::Text("Real time: %.f ms", currentData.sortTimeMs.load()); ImGui::SameLine();
Custom::HelpMarker("(This is an approximation, in real use its faster)");

ImGui::EndTable();
}
Expand Down Expand Up @@ -262,15 +261,16 @@ void Interface::draw(sf::RenderWindow& window) {

if (Settings::PLOT_CURSOR_SHOW) {
ImPlot::PushStyleColor(ImPlotCol_Fill, { 255, 0, 0, 255 });

unsigned cursorPos = currentData.cursorPosition, cursorVal = currentData.cursorValue;

if (Settings::PLOT_CURSOR_DOT) {
ImPlot::SetNextMarkerStyle(ImPlotMarker_Circle);
ImPlot::PushStyleVar(ImPlotStyleVar_LineWeight, Settings::CURSOR_LINE_WIDTH);
ImPlot::PlotLine("##Cursor", &currentData.cursorPosition, &currentData.cursorValue, 1);
ImPlot::PlotLine("##Cursor", &cursorPos, &cursorVal, 1);
ImPlot::PopStyleVar();
}
else
ImPlot::PlotBars("##Cursor", &currentData.cursorPosition, &currentData.cursorValue, 1, Settings::CURSOR_LINE_WIDTH);
ImPlot::PlotBars("##Cursor", &cursorPos, &cursorVal, 1, Settings::CURSOR_LINE_WIDTH);

ImPlot::PopStyleColor();
}
Expand Down
3 changes: 2 additions & 1 deletion src/QuickSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,11 @@ void QuickSort::sorter()
std::swap(numbers[i], numbers[j]);

stats.addSwaps();
DO_PROGRESSIVE_CHECKSTEP;
stats.addStep();
}

DO_PUT_CURSOR_AT(j);
DO_CHECKSTEP;
}
std::swap(numbers[i + 1], numbers[h]);

Expand Down
1 change: 0 additions & 1 deletion src/SelectionSort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ void SelectionSort::sorter() {
if (numbers[j] < numbers[min_idx]) {
min_idx = j;
DO_PUT_CURSOR_AT(j);
DO_CHECKSTEP;
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/SortingAlgorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ SortingAlgorithm::stepState SortingAlgorithm::checkStep() {
return stepState::PAUSED;
}
//else
stats.sortTimeMs += theClock.getElapsedTime().asSeconds() * 1000;
stats.sortTimeMs = stats.sortTimeMs + theClock.getElapsedTime().asSeconds() * 1000;

sf::sleep(sf::microseconds(Manager::delayMs * 1000));
theClock.restart();
Expand Down

0 comments on commit cdc2572

Please sign in to comment.