From c70ebbe4bdf1c1cbb5c14663933b0b9ad8f92c66 Mon Sep 17 00:00:00 2001 From: polsevev Date: Tue, 8 Nov 2022 16:31:16 +0100 Subject: [PATCH] I don't really know --- src/Algorithm.rs | 31 +++++++++++++++++++++++++++---- src/BarPlugin.rs | 1 - src/State.rs | 19 ++++++++++++++----- src/main.rs | 25 ++++++++++++++++++------- 4 files changed, 59 insertions(+), 17 deletions(-) diff --git a/src/Algorithm.rs b/src/Algorithm.rs index b15f244..2dcdab9 100644 --- a/src/Algorithm.rs +++ b/src/Algorithm.rs @@ -7,6 +7,8 @@ use std::rc::Rc; use std::thread::yield_now; use macroquad::prelude::screen_width; use macroquad::window::screen_height; +use std::collections::BinaryHeap; +use std::collections::HashMap; #[derive(Debug, Clone)] pub struct Algorithm{ @@ -27,13 +29,11 @@ impl Algorithm{ for index in 0..list.len(){ let mut j = index; while j>0 && list.lessThan(j, j-1){ - yield list.swap(j, j-1); - j = j-1; + yield list.swap(j, j - 1); + j -= 1; } } - - } } pub fn stalinSort(length:i32) -> impl Generator{ @@ -121,4 +121,27 @@ impl Algorithm{ } + pub fn binaryHeap(length:i32) -> impl Generator{ + let mut list = GuiVec::new(screen_width(), screen_height(), length); + let mut indexMap:HashMap = HashMap::new(); + let mut binHeap:BinaryHeap = BinaryHeap::new(); + list.randomize(); + move || { + let mut ind = 0; + for bar in list.elements(){ + binHeap.push(bar.position); + indexMap.insert(bar.position, ind); + ind += 1; + } + for i in (0..list.len()).rev(){ + let bar = binHeap.pop().unwrap(); + let barIndex = *indexMap.get(&bar).unwrap(); + let clone = list.swap(i, barIndex); + let temp = list.get(barIndex).position; + indexMap.insert(temp, barIndex); + yield clone; + } + } + } + } \ No newline at end of file diff --git a/src/BarPlugin.rs b/src/BarPlugin.rs index a33162c..b6b0109 100644 --- a/src/BarPlugin.rs +++ b/src/BarPlugin.rs @@ -12,7 +12,6 @@ pub struct Bar { impl Bar{ pub fn new(position:i32, hsl_color:f32) -> Self{ - println!("{}", hsl_color); Bar{ position, color: color::hsl_to_rgb((hsl_color as f32) , 1.0, 0.5), diff --git a/src/State.rs b/src/State.rs index ff4bdcb..3cce16f 100644 --- a/src/State.rs +++ b/src/State.rs @@ -1,11 +1,12 @@ use std::ops::{Generator, GeneratorState}; use std::pin::Pin; use std::time::Instant; + use macroquad::color::{BLACK, WHITE}; use macroquad::math::Vec2; use macroquad::prelude::{clear_background, draw_text, get_fps, get_time, next_frame, screen_width}; use macroquad::time::get_frame_time; -use macroquad::ui::root_ui; +use macroquad::ui::{root_ui, hash}; use macroquad::window::screen_height; use crate::Algorithm::Algorithm; use crate::GuiHookVec::GuiVec; @@ -20,10 +21,14 @@ impl State{ let mut ret = false; let mut lasttime:f64 = 0.; let mut holder = GuiVec::new(screen_width(), screen_height(), length); - let mut counter = 0; - let mut speed = 200; + let mut speed = 1; + let mut speedText = "1".to_owned(); loop{ clear_background(WHITE); + speed = match speedText.parse::(){ + Ok(a) => a, + Err(error)=> {1} + }; for _ in 0..speed{ if get_time()-lasttime > timeout && !finished && !paused{ match Pin::new(& mut generator).resume(()){ @@ -43,6 +48,9 @@ impl State{ 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); + root_ui().window(hash!(), Vec2::new(screen_width()*0.1, 10.), Vec2::new(200., 25.), |ui|{ + ui.input_text(hash!(), "Speed", &mut speedText); + }); if root_ui().button(Vec2::new(screen_width()*0.01, 90.), "Pause"){ if paused { @@ -54,9 +62,10 @@ impl State{ if root_ui().button(Vec2::new(screen_width()*0.01, 110.), "Return"){ ret = true; } - + if ret{ + break; + } next_frame().await - } } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 26d728c..5166abd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,18 +13,24 @@ use crate::GuiHookVec::GuiVec; use std::ops::{Deref, Generator, GeneratorState}; use std::process::id; +use macroquad::hash; use macroquad::ui::root_ui; use crate::Algorithm::AlgoEnum; #[macroquad::main("BeepSort")] async fn main() { - let mut length = 100; + let mut length = 500; let mut delay = 0.; - + let mut delayText = "0.1".to_owned(); loop{ clear_background(WHITE); + + delay = match delayText.parse::(){ + Ok(a) => a/1000., + Err(error)=> {0.1} + }; draw_text("Sorting!", screen_width()*0.3, screen_height()*0.5, 100.0, GREEN); draw_text(format!("Length: {}", length.to_string()).as_str(), screen_width()*0.83, 30., 20.0, BLACK); if root_ui().button(Vec2::new(screen_width()*0.9, 50.), "+10"){ @@ -39,22 +45,27 @@ async fn main() { if root_ui().button(Vec2::new(screen_width()*0.84, 50.), "-1"){ length -= 1; } - + root_ui().window(hash!(), Vec2::new(screen_width()*0.1, 10.), Vec2::new(200., 25.), |ui|{ + ui.input_text(hash!(), "Delay (ms)", &mut delayText); + }); if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "InsertSort"){ State::State::runInsertSort(delay,length, Algorithm::Algorithm::insertSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "BogoSort"){ - State::State::runInsertSort(delay,length, Algorithm::Algorithm::bogoSort(length)).await; + //State::State::runInsertSort(delay,length, Algorithm::Algorithm::bogoSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 130.), "BubbleSort"){ - State::State::runInsertSort(delay, length, Algorithm::Algorithm::bubbleSort(length)).await; + //State::State::runInsertSort(delay, length, Algorithm::Algorithm::bubbleSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 160.), "StalinSort"){ - State::State::runInsertSort(delay, length, Algorithm::Algorithm::stalinSort(length)).await; + //State::State::runInsertSort(delay, length, Algorithm::Algorithm::stalinSort(length)).await; } if root_ui().button(Vec2::new(screen_width()*0.01, 190.), "CoctailShaker"){ - State::State::runInsertSort(delay, length, Algorithm::Algorithm::cocktailShaker(length)).await; + //State::State::runInsertSort(delay, length, Algorithm::Algorithm::cocktailShaker(length)).await; + } + if root_ui().button(Vec2::new(screen_width()*0.01, 220.), "HeapSort!"){ + //State::State::runInsertSort(delay, length, Algorithm::Algorithm::binaryHeap(length)).await; } next_frame().await