faer/row/
row_index.rs

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