1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#[inline(always)]
pub fn offset(n: usize, i: usize, off: isize) -> usize {
(i + (off % n as isize + n as isize) as usize) % n
}
#[inline(always)]
pub fn previous(n: usize, i: usize) -> usize {
offset(n, i, -1)
}
#[inline(always)]
pub fn next(n: usize, i: usize) -> usize {
offset(n, i, 1)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_offset() {
assert_eq!(offset(3, 0, -1), 2);
assert_eq!(offset(3, 1, -1), 0);
assert_eq!(offset(3, 2, -1), 1);
assert_eq!(offset(3, 3, -1), 2);
assert_eq!(offset(3, 0, 1), 1);
assert_eq!(offset(3, 1, 1), 2);
assert_eq!(offset(3, 2, 1), 0);
assert_eq!(offset(3, 3, 1), 1);
}
}