faer/col/
col_index.rs

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