split data into sortsession struct with vectors

This commit is contained in:
2026-08-08 14:10:48 +02:00
parent d2d9ef6c70
commit e6e170a42a
2 changed files with 42 additions and 18 deletions
+1 -1
View File
@@ -27,7 +27,7 @@ struct SortView {
}; };
class SteppedBsort: Steppable { class SteppedBsort: public Steppable {
public: public:
SortInput &data; SortInput &data;
size_t stepcount = 0; size_t stepcount = 0;
+41 -17
View File
@@ -3,6 +3,7 @@
#include <cstddef> #include <cstddef>
#include <cstdio> #include <cstdio>
#include <cstdlib> #include <cstdlib>
#include <iostream>
#include <memory> #include <memory>
#include <sstream> #include <sstream>
#include <string> #include <string>
@@ -18,15 +19,17 @@
#include <SFML/Window.hpp> #include <SFML/Window.hpp>
#include <SFML/Graphics.hpp> #include <SFML/Graphics.hpp>
#include "steppable.hpp"
#include "timer.hpp" #include "timer.hpp"
#include "sorters.hpp" #include "sorters.hpp"
using namespace std::chrono; using namespace std::chrono;
using namespace std; using namespace std;
bool displaynumbers(vector<int> input) { bool displaySortInput(SortInput input) {
for (size_t i = 0; i < input.size(); ++i) { cout << "name: " + input.name + " values: ";
printf("array:%d ", input[i]); for (size_t i = 0; i < input.values.size(); ++i) {
printf("%d | ",input.values[i]);
} }
printf("\n"); printf("\n");
return false; return false;
@@ -45,21 +48,35 @@ vector<int> dataprovider(){
return vector<int> {9,8,1,2,7,3,6,4,5}; return vector<int> {9,8,1,2,7,3,6,4,5};
} }
void setupsorttimers(TimerManager &mytimermanager, SteppedBsort &mysession){ void setupsorttimers(TimerManager &mytimermanager, unique_ptr<Steppable> &mysession){
unique_ptr<Timer> mytimer = make_unique<Timer>( unique_ptr<Timer> mytimer = make_unique<Timer>(
"sort1", 0, "sort1", 0,
[&mysession](){return mysession.step();}, [&mysession](){return mysession->step();},
milliseconds(250)); milliseconds(250));
mytimermanager.add_timer(std::move(mytimer)); mytimermanager.add_timer(std::move(mytimer));
} }
//list of inputs, manually assign them to sorts
struct SortSession {
vector<SortInput> &inputs;
unique_ptr<Steppable> sorts;
TimerManager SortTimer;
SortSession(vector<SortInput> &inputs): inputs(inputs), sorts(make_unique<SteppedBsort>(inputs[0])) {
setupsorttimers(SortTimer, sorts);
}
void update_sorts(){
SortTimer.update_timers();
}
};
struct App{ struct App{
sf::RenderWindow mywindow; sf::RenderWindow mywindow;
sf::Font myfont; sf::Font myfont;
sf::Text text; sf::Text text;
TimerManager mytimermanager; vector<SortInput> inputs;
SortInput input; SortSession mysortsession;
SteppedBsort mystb;
sf::Font load_font(const string &filename) { sf::Font load_font(const string &filename) {
sf::Font font; sf::Font font;
@@ -70,19 +87,17 @@ struct App{
return font; return font;
} }
App(SortInput input): App(vector<SortInput> inputs):
mywindow(sf::VideoMode({800,600}), "mywindow"), mywindow(sf::VideoMode({800,600}), "mywindow"),
myfont(load_font("assets/NS-R.ttf")), myfont(load_font("assets/NS-R.ttf")),
text(myfont), text(myfont),
input(std::move(input)), inputs(std::move(inputs)),
mystb(this->input) mysortsession(this->inputs)
{ {
text.setFont(myfont); text.setFont(myfont);
text.setString("Hello world"); text.setString("Hello world");
text.setCharacterSize(24); text.setCharacterSize(24);
text.setFillColor(sf::Color::Red); text.setFillColor(sf::Color::Red);
setupsorttimers(mytimermanager, mystb);
} }
void run(){ void run(){
@@ -95,11 +110,14 @@ struct App{
mywindow.clear(sf::Color::Black); mywindow.clear(sf::Color::Black);
mywindow.draw(text); mywindow.draw(text);
text.setString(arraytostr(input)); text.setString(arraytostr(inputs[0]));
mytimermanager.update_timers(); mysortsession.update_sorts();
displaynumbers(input.values); for (SortInput input : inputs) {
displaySortInput(input);
}
mywindow.display(); mywindow.display();
this_thread::sleep_for(milliseconds(250)); this_thread::sleep_for(milliseconds(250));
@@ -108,7 +126,13 @@ struct App{
}; };
int main() { int main() {
vector<SortInput> inputs;
SortInput myinput("arr1", dataprovider()); SortInput myinput("arr1", dataprovider());
App myapp(myinput); SortInput myinput2("arr2", dataprovider());
inputs.push_back(myinput);
inputs.push_back(myinput2);
App myapp(inputs);
myapp.run(); myapp.run();
} }