1use super::*;
2use crate::into_range::IntoRange;
3use crate::{Idx, IdxInc, assert, debug_assert};
4impl<'a, R: Shape, T, Rs: Stride, RowRange: IntoRange<IdxInc<R>, Len<R>: 'a>>
5 ColIndex<RowRange> for ColRef<'a, T, R, Rs>
6{
7 type Target = ColRef<'a, T, RowRange::Len<R>, Rs>;
8
9 #[track_caller]
10 #[inline]
11 fn get(this: Self, row: RowRange) -> Self::Target {
12 let row = row.into_range(R::start(), this.nrows().end());
13 assert!(all(row.start <= row.end, row.end <= this.nrows()));
14 let nrows = unsafe {
15 RowRange::Len::<R>::new_unbound(
16 row.end.unbound() - row.start.unbound(),
17 )
18 };
19 this.subrows(row.start, nrows)
20 }
21
22 #[track_caller]
23 #[inline]
24 unsafe fn get_unchecked(this: Self, row: RowRange) -> Self::Target {
25 let row = row.into_range(R::start(), this.nrows().end());
26 debug_assert!(all(row.start <= row.end, row.end <= this.nrows(),));
27 let nrows = unsafe {
28 RowRange::Len::<R>::new_unbound(
29 row.end.unbound() - row.start.unbound(),
30 )
31 };
32 this.subrows(row.start, nrows)
33 }
34}
35impl<'a, R: Shape, T, Rs: Stride, RowRange: IntoRange<IdxInc<R>, Len<R>: 'a>>
36 ColIndex<RowRange> for ColMut<'a, T, R, Rs>
37{
38 type Target = ColMut<'a, T, RowRange::Len<R>, Rs>;
39
40 #[track_caller]
41 #[inline]
42 fn get(this: Self, row: RowRange) -> Self::Target {
43 let row = row.into_range(R::start(), this.nrows().end());
44 assert!(all(row.start <= row.end, row.end <= this.nrows()));
45 let nrows = unsafe {
46 RowRange::Len::<R>::new_unbound(
47 row.end.unbound() - row.start.unbound(),
48 )
49 };
50 this.subrows_mut(row.start, nrows)
51 }
52
53 #[track_caller]
54 #[inline]
55 unsafe fn get_unchecked(this: Self, row: RowRange) -> Self::Target {
56 let row = row.into_range(R::start(), this.nrows().end());
57 debug_assert!(all(row.start <= row.end, row.end <= this.nrows(),));
58 let nrows = unsafe {
59 RowRange::Len::<R>::new_unbound(
60 row.end.unbound() - row.start.unbound(),
61 )
62 };
63 this.subrows_mut(row.start, nrows)
64 }
65}
66macro_rules! idx_impl {
67 ($R:ty $(, $tt:tt)?) => {
68 impl <'a $(, $tt)?, T, Rs : Stride > ColIndex < Idx <$R >> for ColRef <'a, T, $R,
69 Rs > { type Target = &'a T; #[track_caller] #[inline] fn get(this : Self, row :
70 Idx <$R >) -> Self::Target { this.at(row) } #[track_caller] #[inline] unsafe fn
71 get_unchecked(this : Self, row : Idx <$R >) -> Self::Target { this
72 .at_unchecked(row) } } impl <'a $(, $tt)?, T, Rs : Stride > ColIndex < Idx <$R >>
73 for ColMut <'a, T, $R, Rs > { type Target = &'a mut T; #[track_caller] #[inline]
74 fn get(this : Self, row : Idx <$R >) -> Self::Target { this.at_mut(row) }
75 #[track_caller] #[inline] unsafe fn get_unchecked(this : Self, row : Idx <$R >)
76 -> Self::Target { this.at_mut_unchecked(row) } }
77 };
78}
79idx_impl!(usize);
80idx_impl!(Dim <'N >, 'N);