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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use crate::{funvec_ref::FunVecRef, funvec_val::FunVec, index::IntoIndex};
use std::collections::{BTreeMap, HashMap};

const DIM: usize = 2;
const LOW_DIM: usize = DIM - 1;

// val
impl<T: Clone + Copy, V1: FunVec<LOW_DIM, T>> FunVec<DIM, T> for Vec<V1> {
    #[inline(always)]
    fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T> {
        let [i, j] = index.into_index();
        self.get(i).and_then(|x| x.at([j]))
    }
}
impl<const N: usize, T: Clone + Copy, V1: FunVec<LOW_DIM, T>> FunVec<DIM, T> for [V1; N] {
    #[inline(always)]
    fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T> {
        let [i, j] = index.into_index();
        self.get(i).and_then(|x| x.at([j]))
    }
}

impl<T: Clone + Copy, V1: FunVec<LOW_DIM, T>> FunVec<DIM, T> for HashMap<usize, V1> {
    #[inline(always)]
    fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T> {
        let [i, j] = index.into_index();
        self.get(&i).and_then(|x| x.at([j]))
    }
}
impl<T: Clone + Copy, V1: FunVec<LOW_DIM, T>> FunVec<DIM, T> for BTreeMap<usize, V1> {
    #[inline(always)]
    fn at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<T> {
        let [i, j] = index.into_index();
        self.get(&i).and_then(|x| x.at([j]))
    }
}

// ref
impl<T, V1: FunVecRef<LOW_DIM, T>> FunVecRef<DIM, T> for Vec<V1> {
    #[inline(always)]
    fn ref_at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<&T> {
        let [i, j] = index.into_index();
        self.get(i).and_then(|x| x.ref_at([j]))
    }
}
impl<const N: usize, T, V1: FunVecRef<LOW_DIM, T>> FunVecRef<DIM, T> for [V1; N] {
    #[inline(always)]
    fn ref_at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<&T> {
        let [i, j] = index.into_index();
        self.get(i).and_then(|x| x.ref_at([j]))
    }
}

impl<T, V1: FunVecRef<LOW_DIM, T>> FunVecRef<DIM, T> for HashMap<usize, V1> {
    #[inline(always)]
    fn ref_at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<&T> {
        let [i, j] = index.into_index();
        self.get(&i).and_then(|x| x.ref_at([j]))
    }
}
impl<T, V1: FunVecRef<LOW_DIM, T>> FunVecRef<DIM, T> for BTreeMap<usize, V1> {
    #[inline(always)]
    fn ref_at<Idx: IntoIndex<DIM>>(&self, index: Idx) -> Option<&T> {
        let [i, j] = index.into_index();
        self.get(&i).and_then(|x| x.ref_at([j]))
    }
}