Skip to main content

faer/row/
mod.rs

1use crate::Shape;
2use crate::internal_prelude::*;
3use crate::utils::bound::{One, Zero};
4/// represents a type that can be used to slice a row, such as an index or a
5/// range of indices
6pub trait RowIndex<ColRange> {
7	/// sliced view type
8	type Target;
9	/// slice `this` using `col`
10	fn get(this: Self, col: ColRange) -> Self::Target;
11	/// slice `this` using `col` without bound checks
12	unsafe fn get_unchecked(this: Self, col: ColRange) -> Self::Target;
13}
14mod row_index;
15pub(crate) mod rowmut;
16pub(crate) mod rowown;
17pub(crate) mod rowref;
18use mat::AsMat;
19pub use rowmut::Mut;
20pub use rowown::Own;
21pub use rowref::Ref;
22/// immutable view over a row vector, similar to an immutable reference to a
23/// strided [prim@slice]
24///
25/// # note
26///
27/// unlike a slice, the data pointed to by `RowRef<'_, T>` is allowed to be
28/// partially or fully uninitialized under certain conditions. in this case,
29/// care must be taken to not perform any operations that read the uninitialized
30/// values, or form references to them, either directly or indirectly through
31/// any of the numerical library routines, unless it is explicitly permitted
32pub type RowRef<'a, T, Cols = usize, CStride = isize> =
33	generic::Row<Ref<'a, T, Cols, CStride>>;
34/// mutable view over a row vector, similar to a mutable reference to a strided
35/// [prim@slice]
36///
37/// # note
38///
39/// unlike a slice, the data pointed to by `RowMut<'_, T>` is allowed to be
40/// partially or fully uninitialized under certain conditions. in this case,
41/// care must be taken to not perform any operations that read the uninitialized
42/// values, or form references to them, either directly or indirectly through
43/// any of the numerical library routines, unless it is explicitly permitted
44pub type RowMut<'a, T, Cols = usize, CStride = isize> =
45	generic::Row<Mut<'a, T, Cols, CStride>>;
46/// heap allocated resizable row vector.
47///
48/// # note
49///
50/// the memory layout of `Row` is guaranteed to be row-major, meaning that it
51/// has a column stride of `1`.
52pub type Row<T, Cols = usize> = generic::Row<Own<T, Cols>>;
53
54#[doc(hidden)]
55/// generic `Row` wrapper
56pub mod generic {
57	use crate::{Idx, Shape, Stride};
58	use core::fmt::Debug;
59	use core::ops::{Index, IndexMut};
60	use reborrow::*;
61	/// generic `Row` wrapper
62	#[derive(Copy, Clone)]
63	#[repr(transparent)]
64	pub struct Row<Inner>(pub Inner);
65	impl<Inner: Debug> Debug for Row<Inner> {
66		#[inline(always)]
67		fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
68			self.0.fmt(f)
69		}
70	}
71	impl<Inner> Row<Inner> {
72		/// wrap by reference
73		#[inline(always)]
74		pub fn from_inner_ref(inner: &Inner) -> &Self {
75			unsafe { &*(inner as *const Inner as *const Self) }
76		}
77
78		/// wrap by mutable reference
79		#[inline(always)]
80		pub fn from_inner_mut(inner: &mut Inner) -> &mut Self {
81			unsafe { &mut *(inner as *mut Inner as *mut Self) }
82		}
83	}
84	impl<Inner> core::ops::Deref for Row<Inner> {
85		type Target = Inner;
86
87		#[inline(always)]
88		fn deref(&self) -> &Self::Target {
89			&self.0
90		}
91	}
92	impl<Inner> core::ops::DerefMut for Row<Inner> {
93		#[inline(always)]
94		fn deref_mut(&mut self) -> &mut Self::Target {
95			&mut self.0
96		}
97	}
98	impl<'short, Inner: Reborrow<'short>> Reborrow<'short> for Row<Inner> {
99		type Target = Row<Inner::Target>;
100
101		#[inline(always)]
102		fn rb(&'short self) -> Self::Target {
103			Row(self.0.rb())
104		}
105	}
106	impl<'short, Inner: ReborrowMut<'short>> ReborrowMut<'short> for Row<Inner> {
107		type Target = Row<Inner::Target>;
108
109		#[inline(always)]
110		fn rb_mut(&'short mut self) -> Self::Target {
111			Row(self.0.rb_mut())
112		}
113	}
114	impl<Inner: IntoConst> IntoConst for Row<Inner> {
115		type Target = Row<Inner::Target>;
116
117		#[inline(always)]
118		fn into_const(self) -> Self::Target {
119			Row(self.0.into_const())
120		}
121	}
122	impl<
123		T,
124		Cols: Shape,
125		CStride: Stride,
126		Inner: for<'short> Reborrow<
127				'short,
128				Target = super::Ref<'short, T, Cols, CStride>,
129			>,
130	> Index<Idx<Cols>> for Row<Inner>
131	{
132		type Output = T;
133
134		#[inline]
135		#[track_caller]
136		fn index(&self, col: Idx<Cols>) -> &Self::Output {
137			self.rb().at(col)
138		}
139	}
140	impl<
141		T,
142		Cols: Shape,
143		CStride: Stride,
144		Inner: for<'short> Reborrow<
145				'short,
146				Target = super::Ref<'short, T, Cols, CStride>,
147			> + for<'short> ReborrowMut<
148				'short,
149				Target = super::Mut<'short, T, Cols, CStride>,
150			>,
151	> IndexMut<Idx<Cols>> for Row<Inner>
152	{
153		#[inline]
154		#[track_caller]
155		fn index_mut(&mut self, col: Idx<Cols>) -> &mut Self::Output {
156			self.rb_mut().at_mut(col)
157		}
158	}
159}
160/// trait for types that can be converted to a row view
161pub trait AsRowMut: AsRowRef {
162	/// returns a view over `self`
163	fn as_row_mut(&mut self) -> RowMut<'_, Self::T, Self::Cols>;
164}
165/// trait for types that can be converted to a row view
166pub trait AsRowRef: AsMatRef<Rows = One> {
167	/// returns a view over `self`
168	fn as_row_ref(&self) -> RowRef<'_, Self::T, Self::Cols>;
169}
170impl<M: AsMatRef<Rows = One>> AsRowRef for M {
171	#[inline]
172	fn as_row_ref(&self) -> RowRef<'_, Self::T, Self::Cols> {
173		self.as_mat_ref().row(Zero)
174	}
175}
176impl<M: AsMatMut<Rows = One>> AsRowMut for M {
177	#[inline]
178	fn as_row_mut(&mut self) -> RowMut<'_, Self::T, Self::Cols> {
179		self.as_mat_mut().row_mut(Zero)
180	}
181}
182impl<T, Cols: Shape, Rs: Stride> AsMatRef for RowRef<'_, T, Cols, Rs> {
183	type Cols = Cols;
184	type Owned = Row<T, Cols>;
185	type Rows = One;
186	type T = T;
187
188	#[inline]
189	fn as_mat_ref(&self) -> MatRef<'_, Self::T, One, Self::Cols> {
190		self.as_dyn_stride().as_mat().as_row_shape(One)
191	}
192}
193impl<T, Cols: Shape, Rs: Stride> AsMatRef for RowMut<'_, T, Cols, Rs> {
194	type Cols = Cols;
195	type Owned = Row<T, Cols>;
196	type Rows = One;
197	type T = T;
198
199	#[inline]
200	fn as_mat_ref(&self) -> MatRef<'_, Self::T, One, Self::Cols> {
201		self.rb().as_dyn_stride().as_mat().as_row_shape(One)
202	}
203}
204impl<T, Cols: Shape> AsMatRef for Row<T, Cols> {
205	type Cols = Cols;
206	type Owned = Row<T, Cols>;
207	type Rows = One;
208	type T = T;
209
210	#[inline]
211	fn as_mat_ref(&self) -> MatRef<'_, Self::T, One, Self::Cols> {
212		self.as_dyn_stride().as_mat().as_row_shape(One)
213	}
214}
215impl<T, Cols: Shape, Rs: Stride> AsMatMut for RowMut<'_, T, Cols, Rs> {
216	#[inline]
217	fn as_mat_mut(&mut self) -> MatMut<'_, Self::T, One, Self::Cols> {
218		self.rb_mut()
219			.as_dyn_stride_mut()
220			.as_mat_mut()
221			.as_row_shape_mut(One)
222	}
223}
224impl<T, Cols: Shape> AsMatMut for Row<T, Cols> {
225	#[inline]
226	fn as_mat_mut(&mut self) -> MatMut<'_, Self::T, One, Self::Cols> {
227		self.as_dyn_stride_mut().as_mat_mut().as_row_shape_mut(One)
228	}
229}
230impl<T, Cols: Shape> AsMat<T> for Row<T, Cols> {
231	#[inline]
232	fn zeros(_: One, cols: Cols) -> Self
233	where
234		T: ComplexField,
235	{
236		Row::zeros(cols)
237	}
238
239	#[track_caller]
240	#[inline]
241	fn truncate(&mut self, _: One, cols: Self::Cols) {
242		self.truncate(cols)
243	}
244}