use std::{iter::Rev, ops::RangeInclusive}; pub fn split_from(min: T, max: T, x: T) -> Vec> where T: num::Integer + Copy, RangeInclusive: Iterator + DoubleEndedIterator, Rev>: Iterator, { if min > x || x > max { return Vec::new(); } let mut output: Vec> = Vec::with_capacity(2); if x > min + T::one() { let x_lower = x - T::one(); output.push((min..=x_lower).rev().collect()); } if x + T::one() < max { output.push(((x + T::one())..=max).collect()); } output } pub fn offsets(i: usize, j: usize, range: usize) -> Vec<(usize, usize)> { let mut output = Vec::new(); for i_o in [-(range as i16), 0, range as i16] { let new_i = i as i16 + i_o; if 0 > new_i { continue; } for j_o in [-(range as i16), 0, range as i16] { let new_j = j as i16 + j_o; if 0 > new_j { continue; } output.push((new_i as usize, new_j as usize)); } } output } #[cfg(test)] mod test { use super::*; #[test] fn split_test() { assert_eq!(split_from(0, 6, 2), vec![vec![1, 0], vec![3, 4, 5, 6]]); assert_eq!(split_from(0, 6, 0), vec![vec![1, 2, 3, 4, 5, 6]]); assert_eq!(split_from(0, 6, 6), vec![vec![5, 4, 3, 2, 1, 0]]); // test out-of-bounds and also generics assert_eq!(split_from::(-1, 4, 10), Vec::>::new()); } }