pub fn find_fitting_subslice<A>(options: &[A], selection: usize, height: usize) -> (&[A], usize) {
if options.len() <= height {
return (options, 0);
}
let half_height = height / 2;
let mut start = if selection > half_height {
selection - half_height
} else {
0
};
let end = if start + height > options.len() {
options.len()
} else {
start + height
};
if end - start < height {
start = end.saturating_sub(height);
}
(&options[start..end], start)
}
#[cfg(test)]
mod tests {
use crate::algos::slice::subslice::find_fitting_subslice;
#[test]
pub fn test_find_subslice() {
let data = [1, 2, 3, 4, 5, 6, 7, 8, 9];
assert_eq!(
find_fitting_subslice(&data, 0, 5),
(&[1, 2, 3, 4, 5] as &[i32], 0)
);
assert_eq!(
find_fitting_subslice(&data, 4, 5),
(&[3, 4, 5, 6, 7] as &[i32], 2)
);
assert_eq!(
find_fitting_subslice(&data, 7, 5),
(&[5, 6, 7, 8, 9] as &[i32], 4)
);
assert_eq!(
find_fitting_subslice(&data, 2, 4),
(&[1, 2, 3, 4] as &[i32], 0)
);
assert_eq!(
find_fitting_subslice(&[0, 1, 2], 2, 20),
(&[0, 1, 2] as &[i32], 0)
);
}
}