#[cfg(not(feature = "std"))]
use alloc::vec::Vec;
use core::cmp::Ordering;
use num_traits::Float;
pub struct SortedData<T> {
pub x: Vec<T>,
pub y: Vec<T>,
pub indices: Vec<usize>,
}
#[inline]
pub fn sort_by_x<T: Float>(x: &[T], y: &[T]) -> SortedData<T> {
let n = x.len();
let is_sorted = x.windows(2).all(|w| w[0] <= w[1]);
if is_sorted {
return SortedData {
x: x.to_vec(),
y: y.to_vec(),
indices: (0..n).collect(),
};
}
let mut pairs: Vec<(T, usize)> = x.iter().enumerate().map(|(i, &xi)| (xi, i)).collect();
pairs.sort_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(Ordering::Equal));
SortedData {
x: pairs.iter().map(|p| p.0).collect(),
y: pairs.iter().map(|p| y[p.1]).collect(),
indices: pairs.iter().map(|p| p.1).collect(),
}
}
#[inline]
pub fn unsort<T: Float>(sorted_values: &[T], indices: &[usize]) -> Vec<T> {
let n = indices.len();
let mut result = vec![T::zero(); n];
for (sorted_idx, &orig_idx) in indices.iter().enumerate() {
result[orig_idx] = sorted_values[sorted_idx];
}
result
}