I don't really know

This commit is contained in:
Rolf Martin Glomsrud 2022-11-08 16:31:16 +01:00
parent d57068af75
commit c70ebbe4bd
4 changed files with 59 additions and 17 deletions

View file

@ -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<Yield=GuiVec, Return=()>{
@ -121,4 +121,27 @@ impl Algorithm{
}
pub fn binaryHeap(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
let mut list = GuiVec::new(screen_width(), screen_height(), length);
let mut indexMap:HashMap<i32, usize> = HashMap::new();
let mut binHeap:BinaryHeap<i32> = 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;
}
}
}
}

View file

@ -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),

View file

@ -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::<i32>(){
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
}
}
}

View file

@ -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::<f64>(){
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