fixed, add yield ahead of any change in the list to update the UI
This commit is contained in:
parent
5203cf1359
commit
fb91932372
7 changed files with 80 additions and 85 deletions
62
Cargo.lock
generated
62
Cargo.lock
generated
|
@ -6,8 +6,8 @@ version = 3
|
|||
name = "BeepSortMacroQuad"
|
||||
version = "0.1.0"
|
||||
dependencies = [
|
||||
"beep",
|
||||
"macroquad",
|
||||
"tokio",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -51,16 +51,6 @@ version = "1.1.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "beep"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "add99ab8e6fa29e525696f04be01c6e18815f5d799e026a06c8b09af8301bd5a"
|
||||
dependencies = [
|
||||
"lazy_static",
|
||||
"nix",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "1.2.1"
|
||||
|
@ -85,12 +75,6 @@ version = "1.4.3"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
|
||||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.0.74"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "581f5dba903aac52ea3feb5ec4810848460ee833876f1f9b0fdeab1f19091574"
|
||||
|
||||
[[package]]
|
||||
name = "cfg-if"
|
||||
version = "1.0.0"
|
||||
|
@ -193,12 +177,6 @@ dependencies = [
|
|||
"png",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "lazy_static"
|
||||
version = "1.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
|
||||
|
||||
[[package]]
|
||||
name = "lewton"
|
||||
version = "0.9.4"
|
||||
|
@ -253,15 +231,6 @@ version = "2.0.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "60302e4db3a61da70c0cb7991976248362f30319e88850c487b9b95bbf059e00"
|
||||
|
||||
[[package]]
|
||||
name = "memoffset"
|
||||
version = "0.6.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5aa361d4faea93603064a027415f07bd8e1d5c88c9fbf68bf56a285428fd79ce"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "miniquad"
|
||||
version = "0.3.14"
|
||||
|
@ -298,19 +267,6 @@ version = "0.2.2"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e1bcdd74c20ad5d95aacd60ef9ba40fdf77f767051040541df557b7a9b2a2121"
|
||||
|
||||
[[package]]
|
||||
name = "nix"
|
||||
version = "0.20.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f5e06129fb611568ef4e868c14b326274959aa70ff7776e9d55323531c374945"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"cc",
|
||||
"cfg-if",
|
||||
"libc",
|
||||
"memoffset",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "num-integer"
|
||||
version = "0.1.45"
|
||||
|
@ -365,6 +321,12 @@ version = "1.16.0"
|
|||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "86f0b0d4bf799edbc74508c1e8bf170ff5f41238e5f8225603ca7caaae2b7860"
|
||||
|
||||
[[package]]
|
||||
name = "pin-project-lite"
|
||||
version = "0.2.9"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e0a7ae3ac2f1173085d398531c705756c94a4c56843785df85a60c1a0afac116"
|
||||
|
||||
[[package]]
|
||||
name = "png"
|
||||
version = "0.17.7"
|
||||
|
@ -414,6 +376,16 @@ dependencies = [
|
|||
"maybe-uninit",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "tokio"
|
||||
version = "1.21.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a9e03c497dc955702ba729190dc4aac6f2a0ce97f913e5b1b5912fc5039d9099"
|
||||
dependencies = [
|
||||
"autocfg",
|
||||
"pin-project-lite",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "ttf-parser"
|
||||
version = "0.15.2"
|
||||
|
|
|
@ -7,6 +7,6 @@ edition = "2021"
|
|||
|
||||
[dependencies]
|
||||
macroquad = "0.3.25"
|
||||
beep = "0.3.0"
|
||||
tokio = "1.21.2"
|
||||
[toolchain]
|
||||
channel = "nightly"
|
||||
|
|
|
@ -4,7 +4,6 @@ use crate::GuiHookVec::GuiVec;
|
|||
use std::ops::{Generator, GeneratorState};
|
||||
use std::rc::Rc;
|
||||
use std::thread::yield_now;
|
||||
use beep::beep;
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Algorithm{
|
||||
name:String,
|
||||
|
@ -16,19 +15,37 @@ impl Algorithm {
|
|||
Algorithm{name:"Test".to_owned()}
|
||||
}
|
||||
|
||||
pub fn sort<'a>(&'a self, 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 ||{
|
||||
yield list.clone();
|
||||
|
||||
for index in 0..list.clone().len(){
|
||||
for index in 0..list.len(){
|
||||
let mut j = index;
|
||||
while j>0 && list.get(j-1).position > list.get(j).position{
|
||||
list.swap(j, j-1);
|
||||
yield list.clone();
|
||||
while j>0 && list.lessThan(j, j-1){
|
||||
//yield list.clone();
|
||||
yield list.swap(j, j-1);
|
||||
//yield list.clone();
|
||||
j = j-1;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
pub fn stalinSort<'a>(list: &'a mut GuiVec) -> impl Generator<Yield=GuiVec, Return=()> +'a{
|
||||
move ||{
|
||||
yield list.clone();
|
||||
let mut cur = 1;
|
||||
loop{
|
||||
if cur == list.len() {
|
||||
break;
|
||||
}
|
||||
yield list.clone();
|
||||
if list.lessThan(cur, cur-1){
|
||||
list.delete(cur)
|
||||
}else{
|
||||
cur += 1;
|
||||
}
|
||||
yield list.clone();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,19 +1,25 @@
|
|||
use macroquad::color::Color;
|
||||
use macroquad::color_u8;
|
||||
use macroquad::rand;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Bar {
|
||||
pub width:f32,
|
||||
pub height:f32,
|
||||
pub position:i32
|
||||
pub position:i32,
|
||||
pub color:Color
|
||||
}
|
||||
|
||||
|
||||
|
||||
impl Bar{
|
||||
pub fn new(position:i32) -> Self{
|
||||
|
||||
Bar{
|
||||
width: 10.0,
|
||||
height: position as f32 *10.0,
|
||||
position
|
||||
position,
|
||||
color:Color::from_rgba(rand::gen_range(0, 255),rand::gen_range(0, 254),rand::gen_range(0, 255),255),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -16,7 +16,10 @@ pub struct GuiVec{
|
|||
list: Vec<Bar>,
|
||||
initialSize:usize,
|
||||
pub lastTime:f64,
|
||||
algo:Algorithm
|
||||
algo:Algorithm,
|
||||
pub reads:i32,
|
||||
pub writes:i32,
|
||||
pub comps:i32,
|
||||
}
|
||||
|
||||
impl GuiVec{
|
||||
|
@ -25,13 +28,13 @@ impl GuiVec{
|
|||
for i in 1..length+1 {
|
||||
list.push(Bar::new(i));
|
||||
}
|
||||
GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new()}
|
||||
GuiVec{list, initialSize:length as usize, lastTime: 0.0 , algo:Algorithm::new(), reads:0, writes:0, comps:0}
|
||||
}
|
||||
|
||||
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()-(200.+ (bar.position as f32 * 10.0)), bar.width, bar.height, BROWN);
|
||||
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);
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
|
@ -39,7 +42,7 @@ impl GuiVec{
|
|||
pub fn resize(&mut self, length:i32){
|
||||
self.list = GuiVec::new(length).list;
|
||||
}
|
||||
pub fn len(self) -> usize{
|
||||
pub fn len(&self) -> usize{
|
||||
self.list.len()
|
||||
}
|
||||
pub async fn push(&mut self){
|
||||
|
@ -55,13 +58,17 @@ impl GuiVec{
|
|||
self.list.insert(index, element)
|
||||
}
|
||||
|
||||
pub async fn delete(&mut self, index:usize){
|
||||
pub fn delete(&mut self, index:usize){
|
||||
self.writes += 1;
|
||||
self.list.remove(index);
|
||||
self.initialSize -= 1;
|
||||
}
|
||||
|
||||
pub fn swap(&mut self, index1:usize, index2:usize){
|
||||
pub fn swap(&mut self, index1:usize, index2:usize) -> GuiVec{
|
||||
self.writes += 2;
|
||||
self.reads += 2;
|
||||
self.list.swap(index1, index2);
|
||||
self.clone()
|
||||
}
|
||||
pub fn randomize(&mut self){
|
||||
self.list.shuffle();
|
||||
|
@ -71,8 +78,13 @@ impl GuiVec{
|
|||
self.list.iter()
|
||||
}
|
||||
|
||||
pub fn get(&self, i:usize)-> &Bar{
|
||||
pub fn get(&mut self, i:usize)-> &Bar{
|
||||
self.reads += 1;
|
||||
self.list.get(i).unwrap()
|
||||
}
|
||||
pub fn lessThan(&mut self, a:usize, b:usize) -> bool{
|
||||
self.comps += 1;
|
||||
return self.get(a).position < self.get(b).position
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,14 +0,0 @@
|
|||
use std::cell::RefCell;
|
||||
use std::ops::{Generator, GeneratorState};
|
||||
use std::pin::Pin;
|
||||
use std::rc::Rc;
|
||||
use crate::Algorithm::Algorithm;
|
||||
use crate::GuiHookVec::GuiVec;
|
||||
|
||||
pub struct State{
|
||||
|
||||
}
|
||||
|
||||
impl State{
|
||||
|
||||
}
|
20
src/main.rs
20
src/main.rs
|
@ -2,7 +2,6 @@
|
|||
mod BarPlugin;
|
||||
mod GuiHookVec;
|
||||
mod Algorithm;
|
||||
mod StateMover;
|
||||
|
||||
|
||||
use std::pin::Pin;
|
||||
|
@ -10,24 +9,25 @@ use macroquad::prelude::*;
|
|||
use macroquad::prelude::scene::clear;
|
||||
use crate::BarPlugin::Bar;
|
||||
use crate::GuiHookVec::GuiVec;
|
||||
use crate::StateMover::State;
|
||||
|
||||
use std::ops::{Generator, GeneratorState};
|
||||
|
||||
|
||||
const BAR_WIDTH:f32 = 10.0;
|
||||
#[macroquad::main("BeepSort")]
|
||||
async fn main() {
|
||||
let mut gui_vec = GuiVec::new(30);
|
||||
let mut gui_vec = GuiVec::new(50);
|
||||
gui_vec.randomize();
|
||||
let mut lasttime:f64 = 0.;
|
||||
let mut holder = gui_vec.clone();
|
||||
let mut algo = Algorithm::Algorithm::new();
|
||||
let mut generator = algo.sort(&mut gui_vec);
|
||||
let mut generator = Algorithm::Algorithm::insertSort(&mut gui_vec);
|
||||
let mut finished = false;
|
||||
|
||||
let timeout = 0.000001;
|
||||
|
||||
loop {
|
||||
if get_time()-lasttime > 0.005 && !finished{
|
||||
|
||||
clear_background(WHITE);
|
||||
if get_time()-lasttime > timeout && !finished{
|
||||
match Pin::new(& mut generator).resume(()){
|
||||
GeneratorState::Yielded(x) => {
|
||||
holder = x.clone();
|
||||
|
@ -38,8 +38,10 @@ async fn main() {
|
|||
};
|
||||
lasttime = get_time();
|
||||
}
|
||||
|
||||
//clear_background(WHITE);
|
||||
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();
|
||||
next_frame().await
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue