Still not able to freely choose generator

This commit is contained in:
Rolf Martin Glomsrud 2022-11-07 22:38:37 +01:00
parent b7b3c5fdd7
commit 643ae4a77f
4 changed files with 83 additions and 54 deletions

View file

@ -4,26 +4,19 @@ use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState};
use std::rc::Rc;
use std::thread::yield_now;
use macroquad::prelude::screen_width;
use macroquad::window::screen_height;
#[derive(Debug, Clone)]
pub struct Algorithm{
name:String,
}
#[derive(PartialEq)]
pub enum Algo{
InsertSort,
StalinSort,
BubbleSort,
BogoSort,
}
impl Algorithm {
pub fn new() -> Algorithm{
Algorithm{name:"Test".to_owned()}
}
pub fn insertSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
pub fn insertSort(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
let mut list = GuiVec::new(screen_width(), screen_height(), length);
list.randomize();
move ||{
yield list.clone();
for index in 0..list.len(){
@ -37,7 +30,9 @@ impl Algorithm {
}
}
pub fn stalinSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
pub fn stalinSort(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
let mut list = GuiVec::new(screen_width(), screen_height(), length);
list.randomize();
move ||{
yield list.clone();
let mut cur = 1;
@ -56,7 +51,9 @@ impl Algorithm {
}
}
pub fn bubbleSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a {
pub fn bubbleSort(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
let mut list = GuiVec::new(screen_width(), screen_height(), length);
list.randomize();
move || {
let n = list.len();
for i in 0..n {
@ -69,7 +66,9 @@ impl Algorithm {
}
}
pub fn bogoSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
pub fn bogoSort(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
let mut list = GuiVec::new(screen_width(), screen_height(), length);
list.randomize();
move || {
loop{
yield list.clone();

View file

@ -16,7 +16,7 @@ pub struct GuiVec{
list: Vec<Bar>,
initialSize:usize,
pub lastTime:f64,
algo:Algorithm,
pub reads:i32,
pub writes:i32,
pub comps:i32,
@ -32,13 +32,13 @@ impl GuiVec{
for i in 1..length+1 {
list.push(Bar::new(barWidth, barHeightStep*(i as f32) ,i));
}
GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new(), reads:0, writes:0, comps:0, screen_height, screen_width}
GuiVec{list, initialSize:length as usize, lastTime: 0.0 , reads:0, writes:0, comps:0, screen_height, screen_width}
}
pub fn draw(&self){
let mut count = 0;
for bar in &self.list{
draw_rectangle(screen_width() * ((count as f32)/(self.initialSize as f32)), (screen_height()-bar.height)-50., screen_width()/((self.len()) as f32), (screen_height()/((self.len()) as f32))*bar.position as f32, bar.color);
draw_rectangle(screen_width() * ((count as f32)/(self.initialSize as f32)), (screen_height()-bar.height)-50., screen_width()/((self.len()) as f32), (screen_height()/((self.len()) as f32))*bar.position as f32 * 4., bar.color);
count += 1;
}
}

13
src/State.rs Normal file
View file

@ -0,0 +1,13 @@
use std::ops::Generator;
use macroquad::prelude::screen_width;
use macroquad::window::screen_height;
use crate::Algorithm::Algorithm;
use crate::GuiHookVec::GuiVec;
#[derive(PartialEq)]
pub enum GeneratorEnum{
InsertSort,
BogoSort,
StalinSort,
BubbleSort,
}

View file

@ -1,7 +1,8 @@
#![feature(generators, generator_trait)]
#![feature(generators, generator_trait, type_alias_impl_trait)]
mod BarPlugin;
mod GuiHookVec;
mod Algorithm;
mod State;
use std::pin::Pin;
@ -10,23 +11,29 @@ use macroquad::prelude::scene::clear;
use crate::BarPlugin::Bar;
use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState};
use std::ops::{Deref, Generator, GeneratorState};
use macroquad::ui::root_ui;
use crate::Algorithm::Algo;
use crate::State::GeneratorEnum;
const BAR_WIDTH:f32 = 10.0;
#[macroquad::main("BeepSort")]
async fn main() {
let mut gui_vec = GuiVec::new(screen_width(), screen_height(), 200);
gui_vec.randomize();
let mut length = 100;
let mut lasttime:f64 = 0.;
let mut holder = gui_vec.clone();
let mut generator = Algorithm::Algorithm::bubbleSort(&mut gui_vec);
let mut holder = GuiVec::new(screen_width(), screen_height(), length);
let mut finished = false;
let timeout = 0.000001;
let timeout = 0.0;
let mut paused = false;
loop {
let mut reset = false;
let mut algoChoise = GeneratorEnum::InsertSort;
loop{
let mut generator = Algorithm::Algorithm::insertSort(length);
while !reset {
clear_background(WHITE);
if get_time()-lasttime > timeout && !finished && !paused{
match Pin::new(& mut generator).resume(()){
@ -41,10 +48,10 @@ async fn main() {
lasttime = get_time();
}
holder.draw();
draw_text(format!("Read: {}", holder.reads).as_str(), screen_width()*0.01, 0.0, 20.0, BLACK);
draw_text(format!("Write: {}", holder.writes).as_str(), screen_width()*0.01, 20.0, 20.0, BLACK);
draw_text(format!("Comparisons: {}", holder.comps).as_str(), screen_width()*0.01, 40.0, 20.0, BLACK);
draw_text(format!("FPS: {}", get_fps()).as_str(), screen_width()*0.01, 60., 20.0, BLACK);
draw_text(format!("Read: {}", holder.reads).as_str(), screen_width()*0.01, 20.0, 20.0, BLACK);
draw_text(format!("Write: {}", holder.writes).as_str(), screen_width()*0.01, 40.0, 20.0, BLACK);
draw_text(format!("Comparisons: {}", holder.comps).as_str(), screen_width()*0.01, 60.0, 20.0, BLACK);
draw_text(format!("FPS: {}", get_fps()).as_str(), screen_width()*0.01, 80., 20.0, BLACK);
if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "Pause"){
if paused {
@ -53,8 +60,18 @@ async fn main() {
paused = true;
}
}
if root_ui().button(Vec2::new(screen_width()*0.01, 95.), "Reset!"){
reset = true;
}
next_frame().await
}
algoChoise = GeneratorEnum::StalinSort;
reset = false;
finished = false;
}
}