Skip to content

Instantly share code, notes, and snippets.

@bvssvni
Last active August 29, 2015 14:16
Show Gist options
  • Save bvssvni/e9333849873e830ef6a4 to your computer and use it in GitHub Desktop.
Save bvssvni/e9333849873e830ef6a4 to your computer and use it in GitHub Desktop.
impl<'a> ToIndex<usize, &'a [usize]> for Permutation<Data> {
fn to_index(&self, dim: usize, pos: &'a [usize]) -> usize {
let mut index = 0;
let mut count = self.count(dim);
for (i, &x) in pos.iter().enumerate() {
count /= dim - i;
let lower = pos[..i].iter().fold(0, |a, &y|
if y < x { a + 1 } else { a });
index += count * (x - lower);
}
index
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment