visualization of bsort, manager now responsible for updates and time
split timeractions from timers into seperate header integrated the experiments into an improved displaynumbers, which includes the vector as well as bars above them outsourced the calculation of delta time to the manager to clean up main()
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
#include "timer.hpp"
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <clocale>
|
#include <clocale>
|
||||||
#include <cstdio>
|
#include <cstdio>
|
||||||
@@ -24,16 +25,18 @@ void myupdatedisplay(const timer &timer_input) {
|
|||||||
"test: █");
|
"test: █");
|
||||||
}
|
}
|
||||||
|
|
||||||
void createbalk(const timer &timer_input, cchar_t &input_char) {
|
|
||||||
int ticnt = timer_input.triggercounter;
|
|
||||||
mvwvline_set(stdscr, LINES - ticnt, ticnt, &input_char, ticnt);
|
|
||||||
}
|
|
||||||
|
|
||||||
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
std::vector<int> mynumbers = {1, 2, 3, 4, 5, 6, 7, 8, 9};
|
||||||
|
std::vector<int> myothernumbers = {1, 4, 9, 8, 6, 2, 3, 5, 7};
|
||||||
|
|
||||||
void displaynumbers(int myy, std::vector<int> &input_numbers) {
|
void displaynumbers(int startx, int starty, std::vector<int> &input_numbers, cchar_t &input_char) {
|
||||||
for (size_t i = 0; i < input_numbers.size(); i++) {
|
for (size_t i = 0; i < input_numbers.size(); i++) {
|
||||||
mvprintw(myy, 10 + i, "%d,", input_numbers[i]);
|
/**
|
||||||
|
I make a horizontal line of i chars, which are supposed to represent mynumbers
|
||||||
|
above that, I want a line of bars.
|
||||||
|
_all_ bars stretch from LINES-1 to LINES-1-numbervalue
|
||||||
|
**/
|
||||||
|
mvprintw(LINES -1 - starty, i + startx, "%d,", input_numbers[i]);
|
||||||
|
mvwvline_set(stdscr, LINES-1-input_numbers[i]-starty, i+startx, &input_char, input_numbers[i]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -54,19 +57,27 @@ int main() {
|
|||||||
myinitialize();
|
myinitialize();
|
||||||
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
cchar_t my_c_char = {.attr= A_NORMAL, .chars={L'█'}, .ext_color = 0};
|
||||||
|
|
||||||
|
//create actions, then put them in timers, then put them into the timermanager
|
||||||
|
|
||||||
auto myaction = std::make_unique<swapaction>(mynumbers);
|
auto myaction = std::make_unique<swapaction>(mynumbers);
|
||||||
auto clocktimer = std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
|
auto clocktimer =
|
||||||
|
std::make_unique<timer>("secondsprinter", 1, 10, std::move(myaction));
|
||||||
|
|
||||||
|
auto myothersort = std::make_unique<sortaction>(myothernumbers);
|
||||||
|
auto sorttimer =
|
||||||
|
std::make_unique<timer>("sorttimer", 0.1, std::move(myothersort));
|
||||||
|
|
||||||
timermanager mytimermanager;
|
timermanager mytimermanager;
|
||||||
mytimermanager.addtimer(std::move(clocktimer));
|
mytimermanager.addtimer(std::move(clocktimer));
|
||||||
displaynumbers(2, mynumbers);
|
mytimermanager.addtimer(std::move(sorttimer));
|
||||||
|
|
||||||
// calculate delta between current and previous time
|
|
||||||
while (true) {
|
while (true) {
|
||||||
erase();
|
erase();
|
||||||
|
|
||||||
mytimermanager.updatetimers();
|
mytimermanager.updatetimers();
|
||||||
|
|
||||||
displaynumbers(3, mynumbers);
|
displaynumbers(0,0,mynumbers, my_c_char);
|
||||||
|
displaynumbers(10, 10, myothernumbers, my_c_char);
|
||||||
|
|
||||||
refresh();
|
refresh();
|
||||||
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
std::this_thread::sleep_for(std::chrono::milliseconds(500));
|
||||||
|
|||||||
+12
-32
@@ -11,14 +11,14 @@
|
|||||||
// infinite timer
|
// infinite timer
|
||||||
timer::timer(std::string name_input, size_t interval_input,
|
timer::timer(std::string name_input, size_t interval_input,
|
||||||
std::unique_ptr<timeraction> action_input)
|
std::unique_ptr<timeraction> action_input)
|
||||||
: time_interval(interval_input), name(name_input), is_infinite(true),
|
: time_interval(interval_input), name(name_input), is_infinite(true),
|
||||||
action(std::move(action_input)) {}
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
// limited timer
|
// limited timer
|
||||||
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
timer::timer(std::string name, size_t interval_input, int triggeramount,
|
||||||
std::unique_ptr<timeraction> action_input)
|
std::unique_ptr<timeraction> action_input)
|
||||||
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
: triggeramount(triggeramount), time_interval(interval_input), name(name),
|
||||||
action(std::move(action_input)) {}
|
action(std::move(action_input)) {}
|
||||||
|
|
||||||
// callbackfunc once enough time has accumulated
|
// callbackfunc once enough time has accumulated
|
||||||
void timer::updatetime(double DeltaTime) {
|
void timer::updatetime(double DeltaTime) {
|
||||||
@@ -40,14 +40,18 @@ void timermanager::addtimer(std::unique_ptr<timer> input_timer) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void timermanager::updatetimers() {
|
void timermanager::updatetimers() {
|
||||||
|
//get dtime
|
||||||
|
auto currenttime = std::chrono::steady_clock::now();
|
||||||
|
std::chrono::duration<double> myDeltaTime = currenttime - prevtime;
|
||||||
|
double dtime = myDeltaTime.count();
|
||||||
|
|
||||||
|
//update timers with dtime
|
||||||
for (auto &mytimer : timers) {
|
for (auto &mytimer : timers) {
|
||||||
auto currenttime = std::chrono::steady_clock::now();
|
|
||||||
std::chrono::duration<double> myDeltaTime = currenttime - prevtime;
|
|
||||||
double dtime = myDeltaTime.count();
|
|
||||||
mytimer->updatetime(dtime);
|
mytimer->updatetime(dtime);
|
||||||
prevtime = currenttime;
|
|
||||||
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
mvprintw(12, 0, "updated timer %s", mytimer->name.c_str());
|
||||||
}
|
}
|
||||||
|
//reset for next tick
|
||||||
|
prevtime = currenttime;
|
||||||
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
timers.erase(std::remove_if(timers.begin(), timers.end(),
|
||||||
[](auto &timer) { return timer->is_expired; }),
|
[](auto &timer) { return timer->is_expired; }),
|
||||||
timers.end());
|
timers.end());
|
||||||
@@ -57,27 +61,3 @@ void timermanager::updatetimers() {
|
|||||||
|
|
||||||
timermanager::timermanager(): prevtime(std::chrono::steady_clock::now()){}
|
timermanager::timermanager(): prevtime(std::chrono::steady_clock::now()){}
|
||||||
|
|
||||||
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
|
||||||
|
|
||||||
void swapaction::update(const timer &input_timer){
|
|
||||||
if (!iscomplete) {
|
|
||||||
int temp = input_vector[it_i];
|
|
||||||
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
|
||||||
input_vector[input_vector.size() - 1 - it_i] = temp;
|
|
||||||
it_i++;
|
|
||||||
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
printaction::printaction(std::vector<balkjes> &input_vector): input_vector(input_vector) {}
|
|
||||||
|
|
||||||
void printaction::update(const timer &input_timer) {
|
|
||||||
if(!iscomplete) {
|
|
||||||
// for each it_i add a balkje then eventually print it
|
|
||||||
if (it_i>=10) {
|
|
||||||
iscomplete=true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|||||||
@@ -62,25 +62,6 @@ public:
|
|||||||
void updatetimers();
|
void updatetimers();
|
||||||
};
|
};
|
||||||
|
|
||||||
class swapaction : public timeraction {
|
|
||||||
public:
|
|
||||||
size_t it_i = 0;
|
|
||||||
bool iscomplete = false;
|
|
||||||
std::vector<int> &input_vector;
|
|
||||||
swapaction(std::vector<int> &input_vector);
|
|
||||||
void update(const timer &t);
|
|
||||||
};
|
|
||||||
|
|
||||||
class printaction : public timeraction {
|
|
||||||
public:
|
|
||||||
size_t it_i = 0;
|
|
||||||
bool iscomplete = false;
|
|
||||||
std::vector<balkjes> &input_vector;
|
|
||||||
printaction(std::vector<balkjes> &input_vector);
|
|
||||||
void update(const timer &t);
|
|
||||||
};
|
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,53 @@
|
|||||||
|
#include <vector>
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include "timeractions.hpp"
|
||||||
|
#include <algorithm>
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
swapaction::swapaction(std::vector<int> &input_vector): input_vector(input_vector){}
|
||||||
|
|
||||||
|
void swapaction::update(const timer &input_timer){
|
||||||
|
if (!iscomplete) {
|
||||||
|
int temp = input_vector[it_i];
|
||||||
|
input_vector[it_i] = input_vector[input_vector.size() -1 - it_i];
|
||||||
|
input_vector[input_vector.size() - 1 - it_i] = temp;
|
||||||
|
it_i++;
|
||||||
|
if(it_i>=input_vector.size() / 2){iscomplete=true;}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
sortaction::sortaction(std::vector<int> &input_vector): input_vector(input_vector) {}
|
||||||
|
|
||||||
|
//sorts the included array using bsort and external iterators rather than for loops
|
||||||
|
void sortaction::update(const timer &input_timer) {
|
||||||
|
if (!iscomplete) {
|
||||||
|
/**
|
||||||
|
we take a number, it_i loop
|
||||||
|
compare it with its neighbour, it_j loop
|
||||||
|
if it's larger swap it
|
||||||
|
keep going until it_i == length - sorted numbers
|
||||||
|
once that is true, reset it_j and advance it_i
|
||||||
|
**/
|
||||||
|
if (input_vector[it_j]>input_vector[it_j+1]) {
|
||||||
|
// int temp = input_vector[it_j + 1];
|
||||||
|
// input_vector[it_j + 1] = input_vector[it_j];
|
||||||
|
// input_vector[it_j] = temp;
|
||||||
|
std::swap(input_vector[it_j], input_vector[it_j+1]);
|
||||||
|
}
|
||||||
|
it_j++;
|
||||||
|
|
||||||
|
if (it_j >= input_vector.size() - 1 - it_i) {
|
||||||
|
it_j = 0;
|
||||||
|
it_i++;
|
||||||
|
}
|
||||||
|
if (it_i>=input_vector.size()) {
|
||||||
|
iscomplete=true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,32 @@
|
|||||||
|
#ifndef TIMERACTIONS_HPP
|
||||||
|
#define TIMERACTIONS_HPP
|
||||||
|
|
||||||
|
#include "timer.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <cstddef>
|
||||||
|
#include <functional>
|
||||||
|
#include <memory>
|
||||||
|
#include <ncurses.h>
|
||||||
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
|
class swapaction : public timeraction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
bool iscomplete = false;
|
||||||
|
std::vector<int> &input_vector;
|
||||||
|
swapaction(std::vector<int> &input_vector);
|
||||||
|
void update(const timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
class sortaction : public timeraction {
|
||||||
|
public:
|
||||||
|
size_t it_i = 0;
|
||||||
|
size_t it_j = 0;
|
||||||
|
bool iscomplete = false;
|
||||||
|
std::vector<int> &input_vector;
|
||||||
|
sortaction(std::vector<int> &input_vector);
|
||||||
|
void update(const timer &t);
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
Reference in New Issue
Block a user