use core::cmp::min;
pub fn rotate_slice<T>(source: &[T], frame: usize, step: usize, width: usize) -> (&[T], &[T]) {
let animation_index = frame / step;
let animation_index = animation_index % source.len();
if animation_index + width < source.len() {
(&source[animation_index..animation_index + width], &[])
} else {
let remaining_width = min(source.len(), width - (source.len() - animation_index));
(&source[animation_index..], &source[..remaining_width])
}
}
#[cfg(test)]
mod test {
use crate::algos::slice::rotating::rotate_slice;
use std::string::String;
#[test]
fn test_rotate_slice() {
let input = "text rotate".as_bytes();
let (first, second) = rotate_slice(&input, 0, 2, 4);
assert_eq!(first, "text".as_bytes());
assert_eq!(second, &[]);
let (first, second) = rotate_slice(&input, 1, 2, 4);
assert_eq!(first, "text".as_bytes());
assert_eq!(second, &[]);
let (first, second) = rotate_slice(&input, 2, 2, 4);
assert_eq!(first, "ext ".as_bytes());
assert_eq!(second, &[]);
let (first, second) = rotate_slice(&input, 14, 2, 4);
assert_eq!(first, "tate".as_bytes());
assert_eq!(second, &[]);
let (first, second) = rotate_slice(&input, 16, 2, 4);
assert_eq!(first, "ate".as_bytes());
assert_eq!(second, "t".as_bytes());
let (first, second) = rotate_slice(&input, 18, 2, 4);
assert_eq!(first, "te".as_bytes());
assert_eq!(second, "te".as_bytes());
let (first, second) = rotate_slice(&input, 20, 2, 4);
assert_eq!(first, "e".as_bytes());
assert_eq!(second, "tex".as_bytes());
let (first, second) = rotate_slice(&input, 22, 2, 4);
assert_eq!(first, "text".as_bytes());
assert_eq!(second, "".as_bytes());
}
#[test]
fn input_shorter_than_width() {
let input = "This is 20 chars....".as_bytes();
assert_eq!(input.len(), 20);
let (first, second) = rotate_slice(input, 960, 30, 29);
assert_eq!(String::from_utf8_lossy(first), "hars....");
assert_eq!(String::from_utf8_lossy(second), "This is 20 chars....");
}
}