From 643ae4a77f0a8146987f36256bd8cf108e17edea Mon Sep 17 00:00:00 2001 From: polsevev Date: Mon, 7 Nov 2022 22:38:37 +0100 Subject: [PATCH] Still not able to freely choose generator --- src/Algorithm.rs | 33 +++++++++--------- src/GuiHookVec.rs | 6 ++-- src/State.rs | 13 ++++++++ src/main.rs | 85 ++++++++++++++++++++++++++++------------------- 4 files changed, 83 insertions(+), 54 deletions(-) create mode 100644 src/State.rs diff --git a/src/Algorithm.rs b/src/Algorithm.rs index 99d9af4..c37c871 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -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 +'a{ + pub fn insertSort(length:i32) -> impl Generator{ + 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 +'a{ + pub fn stalinSort(length:i32) -> impl Generator{ + 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 +'a { + pub fn bubbleSort(length:i32) -> impl Generator{ + 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 +'a{ + pub fn bogoSort(length:i32) -> impl Generator{ + let mut list = GuiVec::new(screen_width(), screen_height(), length); + list.randomize(); move || { loop{ yield list.clone(); diff --git a/src/GuiHookVec.rs b/src/GuiHookVec.rs index ae73b27..07a09f8 100644 --- a/src/GuiHookVec.rs +++ b/src/GuiHookVec.rs @@ -16,7 +16,7 @@ pub struct GuiVec{ list: Vec, 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; } } diff --git a/src/State.rs b/src/State.rs new file mode 100644 index 0000000..5d96939 --- /dev/null +++ b/src/State.rs @@ -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, +} + diff --git a/src/main.rs b/src/main.rs index 301f99c..dab92a4 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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,51 +11,67 @@ 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 { - clear_background(WHITE); - if get_time()-lasttime > timeout && !finished && !paused{ - match Pin::new(& mut generator).resume(()){ - GeneratorState::Yielded(x) => { + let mut reset = false; - holder = x.clone(); - }, - GeneratorState::Complete(x) => { - finished = true; - } - }; - 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); - if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "Pause"){ - if paused { - paused = false; - }else{ - paused = true; + 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(()){ + GeneratorState::Yielded(x) => { + + holder = x.clone(); + }, + GeneratorState::Complete(x) => { + finished = true; + } + }; + lasttime = get_time(); } + holder.draw(); + 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 { + paused = false; + }else{ + paused = true; + } + } + if root_ui().button(Vec2::new(screen_width()*0.01, 95.), "Reset!"){ + reset = true; + } + next_frame().await } - next_frame().await + algoChoise = GeneratorEnum::StalinSort; + reset = false; + finished = false; } + + + }