Skip to content

Commit d923542

Browse files
SirJosh3917SirJosh3917
authored andcommitted
make separate thread do sorting
1 parent f2b8667 commit d923542

File tree

6 files changed

+98
-14
lines changed

6 files changed

+98
-14
lines changed

include/SortingVisualizer/Collection.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ using Decision = std::variant<Comparison, Swap>;
3232
class Collection
3333
{
3434
private:
35+
std::vector<uint64_t> initialValues;
3536
std::vector<uint64_t> values;
3637
std::vector<Decision> decisions;
3738

@@ -45,6 +46,7 @@ class Collection
4546
Order compare(std::size_t leftIdx, std::size_t rightIdx);
4647
void swap(std::size_t leftIdx, std::size_t rightIdx);
4748
void randomize();
49+
const std::vector<Decision> &getDecisions() const;
4850

4951
std::vector<uint64_t> contents() const;
5052
};

include/SortingVisualizer/Display.hpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,20 @@
44
#include <SFML/Graphics/RectangleShape.hpp>
55
#include <SFML/Graphics/RenderTarget.hpp>
66
#include <SortingVisualizer/Collection.hpp>
7+
#include <memory>
78
#include <stdint.h>
89

910
class Display
1011
{
1112
private:
1213
sf::RenderTarget &renderTarget;
13-
const Collection &collection;
14+
std::shared_ptr<std::vector<uint64_t>> bars;
1415
const uint64_t maxBarValue;
1516

1617
public:
1718
Display(sf::RenderTarget &renderTarget, Collection &collection);
1819
void draw();
20+
void setBars(std::vector<uint64_t> bars);
1921
};
2022

2123
#endif

src/SortingVisualizer/Algorithms/BubbleSort.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,25 @@
33

44
const void bubbleSort(Collection &collection)
55
{
6+
// auto swapped = false;
7+
// auto last = collection.length() - 1;
8+
9+
// do
10+
// {
11+
// swapped = false;
12+
13+
// for (auto i = 0; i < last; i++)
14+
// {
15+
// if (collection.compare(i, i + 1) == Order::GREATER_THAN)
16+
// {
17+
// collection.swap(i, i + 1);
18+
// swapped = true;
19+
// }
20+
// }
21+
22+
// last -= 1;
23+
// } while (swapped);
24+
625
for (auto i = 0; i < collection.length(); i++)
726
{
827
for (auto j = i; j < collection.length(); j++)

src/SortingVisualizer/Collection.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ std::vector<uint64_t> Collection::contents() const { return this->values; }
4545

4646
void Collection::randomize() { std::random_shuffle(this->values.begin(), this->values.end()); }
4747

48+
const std::vector<Decision> &Collection::getDecisions() const { return this->decisions; }
49+
4850
// std::size_t length() ;
4951
// void doParallel(std::initializer_list<std::function<void(Collection)>> parallelActions) ;
5052

src/SortingVisualizer/Display.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,19 @@
33
#include <SortingVisualizer/Display.hpp>
44
#include <algorithm>
55
#include <iostream>
6+
#include <memory>
67
#include <stdint.h>
78

89
Display::Display(sf::RenderTarget &renderTarget, Collection &collection)
9-
: renderTarget(renderTarget), collection(collection), maxBarValue(collection.max())
10+
: renderTarget(renderTarget), bars(std::make_shared<std::vector<uint64_t>>(collection.contents())), maxBarValue(collection.max())
1011
{
1112
}
1213

1314
void Display::draw()
1415
{
16+
auto bars = *this->bars.get();
1517
auto size = this->renderTarget.getSize();
16-
auto barCount = (float)this->collection.length();
18+
auto barCount = (float)bars.size();
1719

1820
// when we draw the bars, we want each bar to have one pixel of spacing inbetween each other,
1921
// and one pixel of spacing at the start and the end
@@ -22,7 +24,7 @@ void Display::draw()
2224
auto pixelsOfBoxes = size.x - pixelsOfSpacing;
2325
auto barWidth = pixelsOfBoxes / barCount;
2426

25-
for (auto i = 0; auto bar : this->collection.contents())
27+
for (auto i = 0; auto bar : bars)
2628
{
2729
auto barX = spacing + i * (barWidth + spacing);
2830
auto barY = (float)bar / (float)maxBarValue * size.y;
@@ -36,4 +38,6 @@ void Display::draw()
3638

3739
++i;
3840
}
39-
}
41+
}
42+
43+
void Display::setBars(std::vector<uint64_t> bars) { *this->bars.get() = bars; }

src/SortingVisualizer/main.cpp

Lines changed: 64 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,39 +2,94 @@
22
#include <SortingVisualizer/Algorithms/BubbleSort.hpp>
33
#include <SortingVisualizer/Collection.hpp>
44
#include <SortingVisualizer/Display.hpp>
5+
#include <chrono>
56
#include <iostream>
7+
#include <memory>
8+
#include <thread>
9+
#include <tuple>
10+
11+
void sorter(std::tuple<Collection *, Display *> container)
12+
{
13+
auto collectionPtr = std::get<0>(container);
14+
auto displayPtr = std::get<1>(container);
15+
16+
if (collectionPtr == nullptr) throw "didn't expect nullptr for collection";
17+
if (displayPtr == nullptr) throw "didn't expect nullptr for display";
18+
19+
auto collection = *collectionPtr;
20+
auto display = *displayPtr;
21+
22+
auto bars = collection.contents();
23+
bubbleSort(collection);
24+
25+
for (auto &decision : collection.getDecisions())
26+
{
27+
if (std::holds_alternative<Swap>(decision))
28+
{
29+
auto swap = std::get<Swap>(decision);
30+
std::swap(bars[swap.leftIdx], bars[swap.rightIdx]);
31+
display.setBars(bars);
32+
}
33+
34+
std::this_thread::sleep_for(std::chrono::milliseconds(16));
35+
}
36+
}
637

738
int main()
839
{
40+
// seed RNG
41+
std::srand(std::time(0));
42+
943
sf::ContextSettings settings;
10-
settings.antialiasingLevel = 16;
44+
settings.antialiasingLevel = 4;
45+
settings.majorVersion = 3;
1146

12-
sf::RenderWindow window(sf::VideoMode(800, 600), "Sorting Visualizer", sf::Style::Default, settings);
13-
window.setActive(false);
47+
auto window = sf::RenderWindow(sf::VideoMode(800, 600), "Sorting Visualizer", sf::Style::Default, settings);
1448
window.setVerticalSyncEnabled(true);
49+
window.setFramerateLimit(60);
1550

16-
Collection collection(10);
17-
Display display(window, collection);
51+
auto collection = Collection(100);
52+
collection.randomize();
53+
54+
auto display = Display(window, collection);
55+
56+
auto dirty = true;
57+
58+
sf::Thread sortAlgo(&sorter, std::make_tuple<Collection *, Display *>(&collection, &display));
59+
sortAlgo.launch();
1860

1961
while (window.isOpen())
2062
{
2163
sf::Event event;
2264
while (window.pollEvent(event))
2365
{
24-
if (event.type == sf::Event::Closed) window.close();
2566
if (event.type == sf::Event::Resized)
2667
{
68+
[[likely]]
2769
// SFML views are useful in games, but for me, i'd prefer if i was dealing with everything
2870
// as if it were regular window coordinates
2971
window.setView(sf::View(sf::FloatRect(0.0f, 0.0f, event.size.width, event.size.height)));
72+
73+
// we modified the view, we want to guarantee a re-render
74+
dirty = true;
75+
}
76+
else if (event.type == sf::Event::LostFocus)
77+
{
78+
window.setFramerateLimit(12);
79+
}
80+
else if (event.type == sf::Event::GainedFocus)
81+
{
82+
window.setFramerateLimit(60);
83+
}
84+
else if (event.type == sf::Event::Closed)
85+
{
86+
[[unlikely]] window.close();
87+
exit(0);
3088
}
3189
}
3290

3391
window.clear();
34-
35-
collection.randomize();
3692
display.draw();
37-
3893
window.display();
3994
}
4095

0 commit comments

Comments
 (0)