use camino::Utf8PathBuf;
use super::super::super::cycle::support::path_eq;
#[cfg(kani)]
use super::super::super::cycle::support::first_byte_cmp;
#[cfg(not(kani))]
use super::super::super::cycle::support::path_cmp;
pub(super) fn insertion_sort_by<T, F>(values: &mut [T], cmp: F)
where
F: Fn(&T, &T) -> std::cmp::Ordering,
{
let mut index = 1;
while index < values.len() {
let mut sorted_index = index;
while sorted_index > 0 {
let swap = values
.get(sorted_index)
.zip(values.get(sorted_index - 1))
.is_some_and(|(cur, prev)| cmp(cur, prev) == std::cmp::Ordering::Less);
if !swap {
break;
}
values.swap(sorted_index, sorted_index - 1);
sorted_index -= 1;
}
index += 1;
}
}
pub(super) fn sort_strings(values: &mut [String]) {
insertion_sort_by(values, |a, b| string_cmp(a, b));
}
#[cfg(not(kani))]
fn string_cmp(left: &str, right: &str) -> std::cmp::Ordering {
left.cmp(right)
}
#[cfg(kani)]
fn string_cmp(left: &str, right: &str) -> std::cmp::Ordering {
first_byte_cmp(left, right)
}
#[cfg(not(kani))]
pub(super) fn sort_paths(paths: &mut [Utf8PathBuf]) {
insertion_sort_by(paths, |left, right| {
path_cmp(left.as_path(), right.as_path())
});
}
#[cfg(kani)]
pub(super) fn sort_paths(_paths: &mut [Utf8PathBuf]) {}
pub(super) fn has_seen_output(seen: &[&Utf8PathBuf], output: &Utf8PathBuf) -> bool {
let mut index = 0;
while index < seen.len() {
if let Some(candidate) = seen.get(index)
&& path_eq(candidate.as_path(), output.as_path())
{
return true;
}
index += 1;
}
false
}