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

View file

@ -16,7 +16,7 @@ pub struct GuiVec{
list: Vec<Bar>, list: Vec<Bar>,
initialSize:usize, initialSize:usize,
pub lastTime:f64, pub lastTime:f64,
algo:Algorithm,
pub reads:i32, pub reads:i32,
pub writes:i32, pub writes:i32,
pub comps:i32, pub comps:i32,
@ -32,13 +32,13 @@ impl GuiVec{
for i in 1..length+1 { for i in 1..length+1 {
list.push(Bar::new(barWidth, barHeightStep*(i as f32) ,i)); 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){ pub fn draw(&self){
let mut count = 0; let mut count = 0;
for bar in &self.list{ 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; 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 BarPlugin;
mod GuiHookVec; mod GuiHookVec;
mod Algorithm; mod Algorithm;
mod State;
use std::pin::Pin; use std::pin::Pin;
@ -10,23 +11,29 @@ use macroquad::prelude::scene::clear;
use crate::BarPlugin::Bar; use crate::BarPlugin::Bar;
use crate::GuiHookVec::GuiVec; use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState}; use std::ops::{Deref, Generator, GeneratorState};
use macroquad::ui::root_ui; use macroquad::ui::root_ui;
use crate::Algorithm::Algo; use crate::State::GeneratorEnum;
const BAR_WIDTH:f32 = 10.0;
#[macroquad::main("BeepSort")] #[macroquad::main("BeepSort")]
async fn main() { 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 lasttime:f64 = 0.;
let mut holder = gui_vec.clone(); let mut holder = GuiVec::new(screen_width(), screen_height(), length);
let mut generator = Algorithm::Algorithm::bubbleSort(&mut gui_vec);
let mut finished = false; let mut finished = false;
let timeout = 0.000001; let timeout = 0.0;
let mut paused = false; let mut paused = false;
let mut reset = false;
let mut algoChoise = GeneratorEnum::InsertSort;
loop{ loop{
let mut generator = Algorithm::Algorithm::insertSort(length);
while !reset {
clear_background(WHITE); clear_background(WHITE);
if get_time()-lasttime > timeout && !finished && !paused{ if get_time()-lasttime > timeout && !finished && !paused{
match Pin::new(& mut generator).resume(()){ match Pin::new(& mut generator).resume(()){
@ -41,10 +48,10 @@ async fn main() {
lasttime = get_time(); lasttime = get_time();
} }
holder.draw(); holder.draw();
draw_text(format!("Read: {}", holder.reads).as_str(), screen_width()*0.01, 0.0, 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, 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, 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, 60., 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 root_ui().button(Vec2::new(screen_width()*0.01, 70.), "Pause"){
if paused { if paused {
@ -53,8 +60,18 @@ async fn main() {
paused = true; 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;
}
} }