permutation/lib.rs
1pub trait Permutation {
2 fn inv(&self) -> Vec<usize>;
3}
4
5impl Permutation for [usize] {
6 fn inv(&self) -> Vec<usize> {
7 let n = self.len();
8 let mut res = vec![0; n];
9 for i in 0..n {
10 res[self[i]] = i;
11 }
12 res
13 }
14}
15
16#[test]
17fn sanity_check() {
18 let a = [1, 5, 2, 3, 6, 0, 4];
19 assert_eq!(a.inv(), [5, 0, 2, 3, 6, 1, 4]);
20}