#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Mapping<const C: usize>(pub [usize; C]);
impl<const C: usize> Mapping<C> {
#[inline]
pub fn new(indices: [usize; C]) -> Self {
Mapping(indices)
}
#[inline]
pub fn get(&self, index: usize) -> usize {
self.0[index]
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_mapping_new_and_get() {
let indices = [2, 0, 1];
let mapping = Mapping::<3>::new(indices);
assert_eq!(mapping.get(0), 2);
assert_eq!(mapping.get(1), 0);
assert_eq!(mapping.get(2), 1);
}
#[test]
fn test_mapping_equality() {
let a = Mapping::<3>::new([1, 2, 0]);
let b = Mapping::<3>::new([1, 2, 0]);
let c = Mapping::<3>::new([0, 1, 2]);
assert_eq!(a, b);
assert_ne!(a, c);
}
#[test]
#[should_panic]
fn test_mapping_get_out_of_bounds() {
let mapping = Mapping::<2>::new([0, 1]);
mapping.get(2);
}
}