Skip to content

Commit

Permalink
adjust operation indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
semihbkgr committed Feb 10, 2024
1 parent d950c19 commit 9ad6aaf
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 15 deletions.
30 changes: 15 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,18 +136,18 @@ impl AlgorithmUI {
result.push('\n');
}
let mut text = Text::raw(result);
match operation {
match operation.adjusted() {
Operation::Compare(a, b) => {
for line in text.lines.iter_mut() {
let line_content = line.spans[0].content.clone();
let line_chars = line_content.chars().into_iter().collect::<Vec<char>>();
let pre = Span::raw(line_chars[..*a].iter().collect::<String>());
let a_span = Span::raw(line_chars[*a..*a + 1].iter().collect::<String>())
let pre = Span::raw(line_chars[..a].iter().collect::<String>());
let a_span = Span::raw(line_chars[a..a + 1].iter().collect::<String>())
.fg(Color::LightCyan);
let mid = Span::raw(line_chars[*a + 1..*b].iter().collect::<String>());
let b_span = Span::raw(line_chars[*b..*b + 1].iter().collect::<String>())
let mid = Span::raw(line_chars[a + 1..b].iter().collect::<String>());
let b_span = Span::raw(line_chars[b..b + 1].iter().collect::<String>())
.fg(Color::LightCyan);
let last = Span::raw(line_chars[*b + 1..].iter().collect::<String>());
let last = Span::raw(line_chars[b + 1..].iter().collect::<String>());

line.spans = vec![pre, a_span, mid, b_span, last];
}
Expand All @@ -156,13 +156,13 @@ impl AlgorithmUI {
for line in text.lines.iter_mut() {
let line_content = line.spans[0].content.clone();
let line_chars = line_content.chars().into_iter().collect::<Vec<char>>();
let pre = Span::raw(line_chars[..*a].iter().collect::<String>());
let a_span = Span::raw(line_chars[*a..*a + 1].iter().collect::<String>())
let pre = Span::raw(line_chars[..a].iter().collect::<String>());
let a_span = Span::raw(line_chars[a..a + 1].iter().collect::<String>())
.fg(Color::LightGreen);
let mid = Span::raw(line_chars[*a + 1..*b].iter().collect::<String>());
let b_span = Span::raw(line_chars[*b..*b + 1].iter().collect::<String>())
let mid = Span::raw(line_chars[a + 1..b].iter().collect::<String>());
let b_span = Span::raw(line_chars[b..b + 1].iter().collect::<String>())
.fg(Color::LightGreen);
let last = Span::raw(line_chars[*b + 1..].iter().collect::<String>());
let last = Span::raw(line_chars[b + 1..].iter().collect::<String>());

line.spans = vec![pre, a_span, mid, b_span, last];
}
Expand All @@ -171,10 +171,10 @@ impl AlgorithmUI {
for line in text.lines.iter_mut() {
let line_content = line.spans[0].content.clone();
let line_chars = line_content.chars().into_iter().collect::<Vec<char>>();
let pre = Span::raw(line_chars[..*i].iter().collect::<String>());
let span = Span::raw(line_chars[*i..*i + 1].iter().collect::<String>())
let pre = Span::raw(line_chars[..i].iter().collect::<String>());
let span = Span::raw(line_chars[i..i + 1].iter().collect::<String>())
.fg(Color::LightYellow);
let last = Span::raw(line_chars[*i + 1..].iter().collect::<String>());
let last = Span::raw(line_chars[i + 1..].iter().collect::<String>());

line.spans = vec![pre, span, last];
}
Expand Down Expand Up @@ -378,7 +378,7 @@ fn ui(frame: &mut Frame, app: &mut App) {

if !algorithm.auto_next {
let (step, operation) = algorithm.status.step_info();
let info = format!("step: {}\n{}", step, operation);
let info = format!("step: {}\n{}", step, operation.adjusted());
let text_info = Text::from(info);
let paragraph_info = Paragraph::new(text_info).alignment(Alignment::Left);
let next_area = next_area_vertical(area, 2, 1);
Expand Down
5 changes: 5 additions & 0 deletions src/sorting/bubble_sort.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,18 @@ pub const NAME: &str = "bubble sort";
pub fn sort(nums: &mut [i32], ctx: &dyn AlgorithmContext) {
let len = nums.len();
for i in 0..len {
let mut swapped = false;
for j in 0..len - i - 1 {
ctx.next(Compare(j, j + 1), nums.to_vec());
if nums[j] > nums[j + 1] {
nums.swap(j, j + 1);
ctx.next(Swap(j, j + 1), nums.to_vec());
swapped = true;
}
}
if !swapped {
break;
}
}
ctx.next(Noop(), nums.to_vec());
}
Expand Down
35 changes: 35 additions & 0 deletions src/sorting/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
use std::fmt::Display;

pub mod bubble_sort;
pub mod comb_sort;
pub mod heap_sort;
pub mod insertion_sort;
pub mod merge_sort;
pub mod quick_sort;
pub mod selection_sort;
pub mod shell_sort;

pub trait AlgorithmContext {
fn next(&self, operation: Operation, nums: Vec<i32>);
Expand All @@ -17,6 +21,29 @@ pub enum Operation {
Insert(usize),
}

impl Operation {
pub fn adjusted(&self) -> Self {
return match *self {
Self::Compare(a, b) => {
if a > b {
Self::Compare(b, a)
} else {
Self::Compare(a, b)
}
}
Self::Swap(a, b) => {
if a > b {
Self::Swap(b, a)
} else {
Self::Swap(a, b)
}
}
Self::Insert(i) => return Self::Insert(i),
Self::Noop() => return Self::Noop(),
};
}
}

impl Display for Operation {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Expand All @@ -40,6 +67,10 @@ pub fn get_algorithms() -> Vec<&'static str> {
selection_sort::NAME,
insertion_sort::NAME,
merge_sort::NAME,
shell_sort::NAME,
heap_sort::NAME,
quick_sort::NAME,
comb_sort::NAME,
];
}

Expand All @@ -49,6 +80,10 @@ pub fn get_algorithm_func<'a>(s: &str) -> impl FnOnce(&mut [i32], &dyn Algorithm
selection_sort::NAME => selection_sort::sort,
insertion_sort::NAME => insertion_sort::sort,
merge_sort::NAME => merge_sort::sort,
shell_sort::NAME => shell_sort::sort,
heap_sort::NAME => heap_sort::sort,
quick_sort::NAME => quick_sort::sort,
comb_sort::NAME => comb_sort::sort,
_ => panic!("algorithm not found"),
}
}
Expand Down

0 comments on commit 9ad6aaf

Please sign in to comment.