use crate::utils::gt_by;
use std::{cmp::Ordering, iter::from_generator};
pub fn insertion_sort<T>(v: &[T]) -> impl Iterator<Item = Vec<T>>
where
T: PartialOrd + Clone,
{
insertion_sort_by(v, |a, b| a.partial_cmp(b))
}
pub fn insertion_sort_by<T, F>(v: &[T], compare: F) -> impl Iterator<Item = Vec<T>>
where
F: Fn(&T, &T) -> Option<Ordering> + Copy,
T: Clone,
{
let mut state = v.to_vec();
from_generator(move || {
yield state.to_vec();
for i in 1..state.len() {
let mut j = i;
while j > 0 && gt_by(&state[j - 1], &state[j], compare) {
state.swap(j - 1, j);
yield state.to_vec();
j -= 1;
}
}
})
}