1use super::*;
2use crate::internal_prelude::*;
3use crate::into_range::IntoRange;
4use crate::{Idx, IdxInc, assert, debug_assert};
5impl<
6 'a,
7 R: Shape,
8 C: Shape,
9 T,
10 Rs: Stride,
11 Cs: Stride,
12 RowRange: IntoRange<IdxInc<R>, Len<R>: 'a>,
13 ColRange: IntoRange<IdxInc<C>, Len<C>: 'a>,
14> MatIndex<RowRange, ColRange> for MatRef<'a, T, R, C, Rs, Cs>
15{
16 type Target = MatRef<'a, T, RowRange::Len<R>, ColRange::Len<C>, Rs, Cs>;
17
18 #[track_caller]
19 #[inline]
20 fn get(this: Self, row: RowRange, col: ColRange) -> Self::Target {
21 let row = row.into_range(R::start(), this.nrows().end());
22 let col = col.into_range(C::start(), this.ncols().end());
23 assert!(all(
24 row.start <= row.end,
25 row.end <= this.nrows(),
26 col.start <= col.end,
27 col.end <= this.ncols(),
28 ));
29 let nrows = unsafe {
30 RowRange::Len::<R>::new_unbound(
31 row.end.unbound() - row.start.unbound(),
32 )
33 };
34 let ncols = unsafe {
35 ColRange::Len::<C>::new_unbound(
36 col.end.unbound() - col.start.unbound(),
37 )
38 };
39 this.submatrix(row.start, col.start, nrows, ncols)
40 }
41
42 #[track_caller]
43 #[inline]
44 unsafe fn get_unchecked(
45 this: Self,
46 row: RowRange,
47 col: ColRange,
48 ) -> Self::Target {
49 let row = row.into_range(R::start(), this.nrows().end());
50 let col = col.into_range(C::start(), this.ncols().end());
51 debug_assert!(all(
52 row.start <= row.end,
53 row.end <= this.nrows(),
54 col.start <= col.end,
55 col.end <= this.ncols(),
56 ));
57 let nrows = unsafe {
58 RowRange::Len::<R>::new_unbound(
59 row.end.unbound() - row.start.unbound(),
60 )
61 };
62 let ncols = unsafe {
63 ColRange::Len::<C>::new_unbound(
64 col.end.unbound() - col.start.unbound(),
65 )
66 };
67 this.submatrix(row.start, col.start, nrows, ncols)
68 }
69}
70impl<
71 'a,
72 R: Shape,
73 C: Shape,
74 T,
75 Rs: Stride,
76 Cs: Stride,
77 RowRange: IntoRange<IdxInc<R>, Len<R>: 'a>,
78 ColRange: IntoRange<IdxInc<C>, Len<C>: 'a>,
79> MatIndex<RowRange, ColRange> for MatMut<'a, T, R, C, Rs, Cs>
80{
81 type Target = MatMut<'a, T, RowRange::Len<R>, ColRange::Len<C>, Rs, Cs>;
82
83 #[track_caller]
84 #[inline]
85 fn get(this: Self, row: RowRange, col: ColRange) -> Self::Target {
86 let row = row.into_range(R::start(), this.nrows().end());
87 let col = col.into_range(C::start(), this.ncols().end());
88 assert!(all(
89 row.start <= row.end,
90 row.end <= this.nrows(),
91 col.start <= col.end,
92 col.end <= this.ncols(),
93 ));
94 let nrows = unsafe {
95 RowRange::Len::<R>::new_unbound(
96 row.end.unbound() - row.start.unbound(),
97 )
98 };
99 let ncols = unsafe {
100 ColRange::Len::<C>::new_unbound(
101 col.end.unbound() - col.start.unbound(),
102 )
103 };
104 this.submatrix_mut(row.start, col.start, nrows, ncols)
105 }
106
107 #[track_caller]
108 #[inline]
109 unsafe fn get_unchecked(
110 this: Self,
111 row: RowRange,
112 col: ColRange,
113 ) -> Self::Target {
114 let row = row.into_range(R::start(), this.nrows().end());
115 let col = col.into_range(C::start(), this.ncols().end());
116 debug_assert!(all(
117 row.start <= row.end,
118 row.end <= this.nrows(),
119 col.start <= col.end,
120 col.end <= this.ncols(),
121 ));
122 let nrows = unsafe {
123 RowRange::Len::<R>::new_unbound(
124 row.end.unbound() - row.start.unbound(),
125 )
126 };
127 let ncols = unsafe {
128 ColRange::Len::<C>::new_unbound(
129 col.end.unbound() - col.start.unbound(),
130 )
131 };
132 this.submatrix_mut(row.start, col.start, nrows, ncols)
133 }
134}
135macro_rules! row_impl {
136 ($R:ty $(,$tt:tt)?) => {
137 impl <'a $(, $tt)?, C : Shape, T, Rs : Stride, Cs : Stride, ColRange : IntoRange
138 < IdxInc < C >, Len < C >: 'a >> MatIndex < Idx <$R >, ColRange > for MatRef <'a,
139 T, $R, C, Rs, Cs > { type Target = RowRef <'a, T, ColRange::Len < C >, Cs >;
140 #[track_caller] #[inline] fn get(this : Self, row : Idx <$R >, col : ColRange) ->
141 Self::Target { let col = col.into_range(C::start(), this.ncols().end());
142 assert!(all(row < this.nrows(), col.start <= col.end, col.end <= this.ncols(),));
143 let ncols = unsafe { ColRange::Len::< C >::new_unbound(col.end.unbound() - col
144 .start.unbound()) }; this.subcols(col.start, ncols).row(row) } #[track_caller]
145 #[inline] unsafe fn get_unchecked(this : Self, row : Idx <$R >, col : ColRange)
146 -> Self::Target { let col = col.into_range(C::start(), this.ncols().end());
147 debug_assert!(all(row < this.nrows(), col.start <= col.end, col.end <= this
148 .ncols(),)); let ncols = unsafe { ColRange::Len::< C >::new_unbound(col.end
149 .unbound() - col.start.unbound()) }; this.subcols(col.start, ncols).row(row) } }
150 impl <'a $(, $tt)?, C : Shape, T, Rs : Stride, Cs : Stride, ColRange : IntoRange
151 < IdxInc < C >, Len < C >: 'a >> MatIndex < Idx <$R >, ColRange > for MatMut <'a,
152 T, $R, C, Rs, Cs > { type Target = RowMut <'a, T, ColRange::Len < C >, Cs >;
153 #[track_caller] #[inline] fn get(this : Self, row : Idx <$R >, col : ColRange) ->
154 Self::Target { let col = col.into_range(C::start(), this.ncols().end());
155 assert!(all(row < this.nrows(), col.start <= col.end, col.end <= this.ncols(),));
156 let ncols = unsafe { ColRange::Len::< C >::new_unbound(col.end.unbound() - col
157 .start.unbound()) }; this.subcols_mut(col.start, ncols).row_mut(row) }
158 #[track_caller] #[inline] unsafe fn get_unchecked(this : Self, row : Idx <$R >,
159 col : ColRange) -> Self::Target { let col = col.into_range(C::start(), this
160 .ncols().end()); debug_assert!(all(row < this.nrows(), col.start <= col.end, col
161 .end <= this.ncols(),)); let ncols = unsafe { ColRange::Len::< C
162 >::new_unbound(col.end.unbound() - col.start.unbound()) }; this.subcols_mut(col
163 .start, ncols).row_mut(row) } }
164 };
165}
166macro_rules! col_impl {
167 ($C:ty $(,$tt:tt)?) => {
168 impl <'a $(, $tt)?, R : Shape, T, Rs : Stride, Cs : Stride, RowRange : IntoRange
169 < IdxInc < R >, Len < R >: 'a >> MatIndex < RowRange, Idx <$C >> for MatRef <'a,
170 T, R, $C, Rs, Cs > { type Target = ColRef <'a, T, RowRange::Len < R >, Rs >;
171 #[track_caller] #[inline] fn get(this : Self, row : RowRange, col : Idx <$C >) ->
172 Self::Target { let row = row.into_range(R::start(), this.nrows().end());
173 assert!(all(col < this.ncols(), row.start <= row.end, row.end <= this.nrows(),));
174 let nrows = unsafe { RowRange::Len::< R >::new_unbound(row.end.unbound() - row
175 .start.unbound()) }; this.subrows(row.start, nrows).col(col) } #[track_caller]
176 #[inline] unsafe fn get_unchecked(this : Self, row : RowRange, col : Idx <$C >)
177 -> Self::Target { let row = row.into_range(R::start(), this.nrows().end());
178 assert!(all(col < this.ncols(), row.start <= row.end, row.end <= this.nrows(),));
179 let nrows = unsafe { RowRange::Len::< R >::new_unbound(row.end.unbound() - row
180 .start.unbound()) }; this.subrows(row.start, nrows).col(col) } } impl <'a $(,
181 $tt)?, R : Shape, T, Rs : Stride, Cs : Stride, RowRange : IntoRange < IdxInc < R
182 >, Len < R >: 'a >> MatIndex < RowRange, Idx <$C >> for MatMut <'a, T, R, $C, Rs,
183 Cs > { type Target = ColMut <'a, T, RowRange::Len < R >, Rs >; #[track_caller]
184 #[inline] fn get(this : Self, row : RowRange, col : Idx <$C >) -> Self::Target {
185 let row = row.into_range(R::start(), this.nrows().end()); assert!(all(col < this
186 .ncols(), row.start <= row.end, row.end <= this.nrows(),)); let nrows = unsafe {
187 RowRange::Len::< R >::new_unbound(row.end.unbound() - row.start.unbound()) };
188 this.subrows_mut(row.start, nrows).col_mut(col) } #[track_caller] #[inline]
189 unsafe fn get_unchecked(this : Self, row : RowRange, col : Idx <$C >) ->
190 Self::Target { let row = row.into_range(R::start(), this.nrows().end());
191 assert!(all(col < this.ncols(), row.start <= row.end, row.end <= this.nrows(),));
192 let nrows = unsafe { RowRange::Len::< R >::new_unbound(row.end.unbound() - row
193 .start.unbound()) }; this.subrows_mut(row.start, nrows).col_mut(col) } }
194 };
195}
196macro_rules! idx_impl {
197 (($R:ty $(,$Rtt:tt)?), ($C:ty $(,$Ctt:tt)?)) => {
198 impl <'a $(, $Rtt)? $(, $Ctt)?, T, Rs : Stride, Cs : Stride > MatIndex < Idx <$R
199 >, Idx <$C >> for MatRef <'a, T, $R, $C, Rs, Cs > { type Target = &'a T;
200 #[track_caller] #[inline] fn get(this : Self, row : Idx <$R >, col : Idx <$C >)
201 -> Self::Target { this.at(row, col) } #[track_caller] #[inline] unsafe fn
202 get_unchecked(this : Self, row : Idx <$R >, col : Idx <$C >) -> Self::Target {
203 this.at_unchecked(row, col) } } impl <'a $(, $Rtt)? $(, $Ctt)?, T, Rs : Stride,
204 Cs : Stride > MatIndex < Idx <$R >, Idx <$C >> for MatMut <'a, T, $R, $C, Rs, Cs
205 > { type Target = &'a mut T; #[track_caller] #[inline] fn get(this : Self, row :
206 Idx <$R >, col : Idx <$C >) -> Self::Target { this.at_mut(row, col) }
207 #[track_caller] #[inline] unsafe fn get_unchecked(this : Self, row : Idx <$R >,
208 col : Idx <$C >) -> Self::Target { this.at_mut_unchecked(row, col) } }
209 };
210}
211row_impl!(usize);
212row_impl!(Dim <'N >, 'N);
213col_impl!(usize);
214col_impl!(Dim <'N >, 'N);
215idx_impl!((usize), (usize));
216idx_impl!((usize), (Dim <'N >, 'N));
217idx_impl!((Dim <'M >, 'M), (usize));
218idx_impl!((Dim <'M >, 'M), (Dim <'N >, 'N));