This commit is contained in:
Rolf Martin Glomsrud 2022-11-07 03:36:26 +01:00
parent 8bd2b12103
commit b7b3c5fdd7
4 changed files with 41 additions and 23 deletions

View file

@ -8,6 +8,13 @@ use std::thread::yield_now;
pub struct Algorithm{ pub struct Algorithm{
name:String, name:String,
} }
#[derive(PartialEq)]
pub enum Algo{
InsertSort,
StalinSort,
BubbleSort,
BogoSort,
}
impl Algorithm { impl Algorithm {
@ -15,18 +22,19 @@ impl Algorithm {
Algorithm{name:"Test".to_owned()} Algorithm{name:"Test".to_owned()}
} }
pub fn insertSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{ 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(){
let mut j = index; let mut j = index;
while j>0 && list.lessThan(j, j-1){ while j>0 && list.lessThan(j, j-1){
//yield list.clone();
yield list.swap(j, j-1); yield list.swap(j, j-1);
//yield list.clone();
j = j-1; j = j-1;
} }
} }
} }
} }
pub fn stalinSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{ pub fn stalinSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{

View file

@ -13,11 +13,11 @@ pub struct Bar {
impl Bar{ impl Bar{
pub fn new(position:i32) -> Self{ pub fn new(width:f32, height: f32,position:i32) -> Self{
Bar{ Bar{
width: 10.0, width: width,
height: position as f32 *10.0, height: height,
position, position,
color:Color::from_rgba(rand::gen_range(0, 255),rand::gen_range(0, 254),rand::gen_range(0, 255),255), color:Color::from_rgba(rand::gen_range(0, 255),rand::gen_range(0, 254),rand::gen_range(0, 255),255),
} }

View file

@ -20,35 +20,35 @@ pub struct GuiVec{
pub reads:i32, pub reads:i32,
pub writes:i32, pub writes:i32,
pub comps:i32, pub comps:i32,
screen_height:f32,
screen_width:f32
} }
impl GuiVec{ impl GuiVec{
pub fn new(length:i32) -> Self { pub fn new(screen_width:f32, screen_height:f32,length:i32) -> Self {
let barWidth = (screen_width/((length) as f32)) - 1_f32;
let barHeightStep = (screen_height/((length) as f32));
let mut list:Vec<Bar> = vec!(); let mut list:Vec<Bar> = vec!();
for i in 1..length+1 { for i in 1..length+1 {
list.push(Bar::new(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} GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new(), 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()-(200.+ (bar.position as f32 * 10.0)), bar.width, bar.height, 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, bar.color);
count += 1; count += 1;
} }
} }
pub fn resize(&mut self, length:i32){ pub fn resize(&mut self, length:i32){
self.list = GuiVec::new(length).list; self.list = GuiVec::new(self.screen_width, self.screen_height,length).list;
} }
pub fn len(&self) -> usize{ pub fn len(&self) -> usize{
self.list.len() self.list.len()
} }
pub async fn push(&mut self){
self.list.push(Bar::new(self.initialSize as i32));
self.initialSize += 1;
}
pub fn pop(&mut self) -> Bar { pub fn pop(&mut self) -> Bar {
self.list.pop().unwrap() self.list.pop().unwrap()

View file

@ -11,25 +11,27 @@ use crate::BarPlugin::Bar;
use crate::GuiHookVec::GuiVec; use crate::GuiHookVec::GuiVec;
use std::ops::{Generator, GeneratorState}; use std::ops::{Generator, GeneratorState};
use macroquad::ui::root_ui;
use crate::Algorithm::Algo;
const BAR_WIDTH:f32 = 10.0; const BAR_WIDTH:f32 = 10.0;
#[macroquad::main("BeepSort")] #[macroquad::main("BeepSort")]
async fn main() { async fn main() {
let mut gui_vec = GuiVec::new(5); let mut gui_vec = GuiVec::new(screen_width(), screen_height(), 200);
gui_vec.randomize(); gui_vec.randomize();
let mut lasttime:f64 = 0.; let mut lasttime:f64 = 0.;
let mut holder = gui_vec.clone(); let mut holder = gui_vec.clone();
let mut generator = Algorithm::Algorithm::bogoSort(&mut gui_vec); let mut generator = Algorithm::Algorithm::bubbleSort(&mut gui_vec);
let mut finished = false; let mut finished = false;
let timeout = 0.000001; let timeout = 0.000001;
let mut paused = false;
loop { loop {
clear_background(WHITE); clear_background(WHITE);
if get_time()-lasttime > timeout && !finished{ if get_time()-lasttime > timeout && !finished && !paused{
match Pin::new(& mut generator).resume(()){ match Pin::new(& mut generator).resume(()){
GeneratorState::Yielded(x) => { GeneratorState::Yielded(x) => {
holder = x.clone(); holder = x.clone();
}, },
GeneratorState::Complete(x) => { GeneratorState::Complete(x) => {
@ -38,11 +40,19 @@ async fn main() {
}; };
lasttime = get_time(); lasttime = get_time();
} }
draw_text(format!("Read: {}", holder.reads).as_str(), screen_width()*0.1, screen_height()-100.0, 20.0, BLACK);
draw_text(format!("Write: {}", holder.writes).as_str(), screen_width()*0.1, screen_height()-80.0, 20.0, BLACK);
draw_text(format!("Comparisons: {}", holder.comps).as_str(), screen_width()*0.1, screen_height()-60.0, 20.0, BLACK);
draw_text(format!("FPS: {}", get_fps()).as_str(), screen_width()*0.1, screen_height()-40., 20.0, BLACK);
holder.draw(); 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;
}
}
next_frame().await next_frame().await
} }
} }