Skip to main content

faer/col/
mod.rs

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