1use super::{cache::Cache, cached_vec::CachedVec};
2use crate::{CardD1, Dim, FunVec, IntoIdx, NVec, VariableCardD2, VariableCardD3, D1, D2, D3, D4};
3
4impl<T, V, C> NVec<D1, T> for CachedVec<D1, T, V, C>
7where
8 V: NVec<D1, T>,
9 C: Cache<<D1 as Dim>::Idx, T>,
10 T: Copy,
11{
12 #[inline(always)]
13 fn at(&self, idx: impl IntoIdx<D1>) -> T {
14 *unsafe { self.entry_or_insert_with(idx) }
15 }
16
17 fn child(&self, _: <D1 as Dim>::ChildIdx) -> impl NVec<<D1 as Dim>::PrevDim, T> {
18 self
19 }
20
21 fn all(&self) -> impl Iterator<Item = T> {
22 (0..self.num_children()).map(|i| self.at(i))
23 }
24}
25
26impl<T, V, C> NVec<D2, T> for CachedVec<D2, T, V, C>
29where
30 V: NVec<D2, T>,
31 C: Cache<<D2 as Dim>::Idx, T>,
32 T: Copy,
33{
34 #[inline(always)]
35 fn at(&self, idx: impl IntoIdx<D2>) -> T {
36 *unsafe { self.entry_or_insert_with(idx) }
37 }
38
39 fn child(&self, i: <D2 as Dim>::ChildIdx) -> impl NVec<<D2 as Dim>::PrevDim, T> {
40 let fun = move |idx: <<D2 as Dim>::PrevDim as Dim>::Idx| {
41 let idx = D2::left_join_from_lower_dim(i, idx);
42 self.at(idx)
43 };
44 FunVec::new(fun, CardD1::from(self.vec.card([i])))
45 }
46
47 fn all(&self) -> impl Iterator<Item = T> {
48 (0..self.num_children())
49 .flat_map(move |i| (0..self.card([i])).map(move |j| self.at([i, j])))
50 }
51}
52
53impl<T, V, C> NVec<D3, T> for CachedVec<D3, T, V, C>
56where
57 V: NVec<D3, T>,
58 C: Cache<<D3 as Dim>::Idx, T>,
59 T: Copy,
60{
61 #[inline(always)]
62 fn at(&self, idx: impl IntoIdx<D3>) -> T {
63 *unsafe { self.entry_or_insert_with(idx) }
64 }
65
66 fn child(&self, i: <D3 as Dim>::ChildIdx) -> impl NVec<<D3 as Dim>::PrevDim, T> {
67 let fun = move |idx: <<D3 as Dim>::PrevDim as Dim>::Idx| {
68 let idx = D3::left_join_from_lower_dim(i, idx);
69 self.at(idx)
70 };
71
72 let card = FunVec::new(
73 move |[j]| self.vec.card([i, j]),
74 CardD1::from(self.card([i])),
75 );
76
77 FunVec::new(fun, VariableCardD2::from(card))
78 }
79
80 fn all(&self) -> impl Iterator<Item = T> {
81 (0..self.num_children()).flat_map(move |i| {
82 (0..self.card([i]))
83 .flat_map(move |j| (0..self.card([i, j])).map(move |k| self.at([i, j, k])))
84 })
85 }
86}
87
88impl<T, V, C> NVec<D4, T> for CachedVec<D4, T, V, C>
91where
92 V: NVec<D4, T>,
93 C: Cache<<D4 as Dim>::Idx, T>,
94 T: Copy,
95{
96 #[inline(always)]
97 fn at(&self, idx: impl IntoIdx<D4>) -> T {
98 *unsafe { self.entry_or_insert_with(idx) }
99 }
100
101 fn child(&self, i: <D4 as Dim>::ChildIdx) -> impl NVec<<D4 as Dim>::PrevDim, T> {
102 let fun = move |idx: <<D4 as Dim>::PrevDim as Dim>::Idx| {
103 let idx = D4::left_join_from_lower_dim(i, idx);
104 self.at(idx)
105 };
106
107 let card1 = FunVec::new(
108 move |[j]| self.vec.card([i, j]),
109 CardD1::from(self.card([i])),
110 );
111
112 let card = FunVec::new(
113 move |[j, k]| self.vec.card([i, j, k]),
114 VariableCardD2::from(card1),
115 );
116
117 FunVec::new(fun, VariableCardD3::from(card))
118 }
119
120 fn all(&self) -> impl Iterator<Item = T> {
121 (0..self.num_children()).flat_map(move |i| {
122 (0..self.card([i])).flat_map(move |j| {
123 (0..self.card([i, j])).flat_map(move |k| {
124 (0..self.card([i, j, k])).map(move |l| self.at([i, j, k, l]))
125 })
126 })
127 })
128 }
129}