I don't really know
This commit is contained in:
parent
d57068af75
commit
c70ebbe4bd
4 changed files with 59 additions and 17 deletions
|
@ -7,6 +7,8 @@ use std::rc::Rc;
|
||||||
use std::thread::yield_now;
|
use std::thread::yield_now;
|
||||||
use macroquad::prelude::screen_width;
|
use macroquad::prelude::screen_width;
|
||||||
use macroquad::window::screen_height;
|
use macroquad::window::screen_height;
|
||||||
|
use std::collections::BinaryHeap;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Algorithm{
|
pub struct Algorithm{
|
||||||
|
@ -28,12 +30,10 @@ impl Algorithm{
|
||||||
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.swap(j, j - 1);
|
yield list.swap(j, j - 1);
|
||||||
j = j-1;
|
j -= 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
pub fn stalinSort(length:i32) -> impl Generator<Yield=GuiVec, Return=()>{
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
|
@ -12,7 +12,6 @@ pub struct Bar {
|
||||||
|
|
||||||
impl Bar{
|
impl Bar{
|
||||||
pub fn new(position:i32, hsl_color:f32) -> Self{
|
pub fn new(position:i32, hsl_color:f32) -> Self{
|
||||||
println!("{}", hsl_color);
|
|
||||||
Bar{
|
Bar{
|
||||||
position,
|
position,
|
||||||
color: color::hsl_to_rgb((hsl_color as f32) , 1.0, 0.5),
|
color: color::hsl_to_rgb((hsl_color as f32) , 1.0, 0.5),
|
||||||
|
|
19
src/State.rs
19
src/State.rs
|
@ -1,11 +1,12 @@
|
||||||
use std::ops::{Generator, GeneratorState};
|
use std::ops::{Generator, GeneratorState};
|
||||||
use std::pin::Pin;
|
use std::pin::Pin;
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
|
|
||||||
use macroquad::color::{BLACK, WHITE};
|
use macroquad::color::{BLACK, WHITE};
|
||||||
use macroquad::math::Vec2;
|
use macroquad::math::Vec2;
|
||||||
use macroquad::prelude::{clear_background, draw_text, get_fps, get_time, next_frame, screen_width};
|
use macroquad::prelude::{clear_background, draw_text, get_fps, get_time, next_frame, screen_width};
|
||||||
use macroquad::time::get_frame_time;
|
use macroquad::time::get_frame_time;
|
||||||
use macroquad::ui::root_ui;
|
use macroquad::ui::{root_ui, hash};
|
||||||
use macroquad::window::screen_height;
|
use macroquad::window::screen_height;
|
||||||
use crate::Algorithm::Algorithm;
|
use crate::Algorithm::Algorithm;
|
||||||
use crate::GuiHookVec::GuiVec;
|
use crate::GuiHookVec::GuiVec;
|
||||||
|
@ -20,10 +21,14 @@ impl State{
|
||||||
let mut ret = false;
|
let mut ret = false;
|
||||||
let mut lasttime:f64 = 0.;
|
let mut lasttime:f64 = 0.;
|
||||||
let mut holder = GuiVec::new(screen_width(), screen_height(), length);
|
let mut holder = GuiVec::new(screen_width(), screen_height(), length);
|
||||||
let mut counter = 0;
|
let mut speed = 1;
|
||||||
let mut speed = 200;
|
let mut speedText = "1".to_owned();
|
||||||
loop{
|
loop{
|
||||||
clear_background(WHITE);
|
clear_background(WHITE);
|
||||||
|
speed = match speedText.parse::<i32>(){
|
||||||
|
Ok(a) => a,
|
||||||
|
Err(error)=> {1}
|
||||||
|
};
|
||||||
for _ in 0..speed{
|
for _ in 0..speed{
|
||||||
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(()){
|
||||||
|
@ -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!("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!("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);
|
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 root_ui().button(Vec2::new(screen_width()*0.01, 90.), "Pause"){
|
||||||
if paused {
|
if paused {
|
||||||
|
@ -54,9 +62,10 @@ impl State{
|
||||||
if root_ui().button(Vec2::new(screen_width()*0.01, 110.), "Return"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 110.), "Return"){
|
||||||
ret = true;
|
ret = true;
|
||||||
}
|
}
|
||||||
|
if ret{
|
||||||
|
break;
|
||||||
|
}
|
||||||
next_frame().await
|
next_frame().await
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
25
src/main.rs
25
src/main.rs
|
@ -13,18 +13,24 @@ use crate::GuiHookVec::GuiVec;
|
||||||
|
|
||||||
use std::ops::{Deref, Generator, GeneratorState};
|
use std::ops::{Deref, Generator, GeneratorState};
|
||||||
use std::process::id;
|
use std::process::id;
|
||||||
|
use macroquad::hash;
|
||||||
use macroquad::ui::root_ui;
|
use macroquad::ui::root_ui;
|
||||||
use crate::Algorithm::AlgoEnum;
|
use crate::Algorithm::AlgoEnum;
|
||||||
|
|
||||||
|
|
||||||
#[macroquad::main("BeepSort")]
|
#[macroquad::main("BeepSort")]
|
||||||
async fn main() {
|
async fn main() {
|
||||||
let mut length = 100;
|
let mut length = 500;
|
||||||
|
|
||||||
let mut delay = 0.;
|
let mut delay = 0.;
|
||||||
|
let mut delayText = "0.1".to_owned();
|
||||||
loop{
|
loop{
|
||||||
clear_background(WHITE);
|
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("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);
|
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"){
|
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"){
|
if root_ui().button(Vec2::new(screen_width()*0.84, 50.), "-1"){
|
||||||
length -= 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"){
|
if root_ui().button(Vec2::new(screen_width()*0.01, 70.), "InsertSort"){
|
||||||
State::State::runInsertSort(delay,length, Algorithm::Algorithm::insertSort(length)).await;
|
State::State::runInsertSort(delay,length, Algorithm::Algorithm::insertSort(length)).await;
|
||||||
}
|
}
|
||||||
if root_ui().button(Vec2::new(screen_width()*0.01, 100.), "BogoSort"){
|
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"){
|
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"){
|
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"){
|
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
|
next_frame().await
|
||||||
|
|
Loading…
Reference in a new issue