faer/row/
rowown.rs

1use super::*;
2use crate::{ContiguousFwd, Idx, IdxInc, TryReserveError};
3
4/// see [`super::Row`]
5#[derive(Clone)]
6pub struct Own<T, Cols: Shape = usize> {
7	pub(crate) trans: Col<T, Cols>,
8}
9
10impl<T, Cols: Shape> Row<T, Cols> {
11	/// returns a new row with dimension `nrows`, filled with the provided function
12	#[inline]
13	pub fn from_fn(nrows: Cols, f: impl FnMut(Idx<Cols>) -> T) -> Self {
14		Self {
15			0: Own {
16				trans: Col::from_fn(nrows, f),
17			},
18		}
19	}
20
21	/// returns a new row with dimension `nrows`, filled with zeros
22	#[inline]
23	pub fn zeros(ncols: Cols) -> Self
24	where
25		T: ComplexField,
26	{
27		Self {
28			0: Own { trans: Col::zeros(ncols) },
29		}
30	}
31
32	/// returns a new row with dimension `nrows`, filled with ones
33	#[inline]
34	pub fn ones(ncols: Cols) -> Self
35	where
36		T: ComplexField,
37	{
38		Self {
39			0: Own { trans: Col::ones(ncols) },
40		}
41	}
42
43	/// returns a new row with dimension `nrows`, filled with `value`
44	#[inline]
45	pub fn full(ncols: Cols, value: T) -> Self
46	where
47		T: Clone,
48	{
49		Self {
50			0: Own {
51				trans: Col::full(ncols, value),
52			},
53		}
54	}
55
56	/// reserves the minimum capacity for `col_capacity` columns without reallocating, or returns an
57	/// error in case of failure. does nothing if the capacity is already sufficient
58	#[inline]
59	pub fn try_reserve(&mut self, new_row_capacity: usize) -> Result<(), TryReserveError> {
60		self.0.trans.try_reserve(new_row_capacity)
61	}
62
63	/// reserves the minimum capacity for `col_capacity` columns without reallocating. does nothing
64	/// if the capacity is already sufficient
65	#[track_caller]
66	pub fn reserve(&mut self, new_row_capacity: usize) {
67		self.0.trans.reserve(new_row_capacity)
68	}
69
70	/// resizes the row in-place so that the new dimension is `new_ncols`.
71	/// new elements are created with the given function `f`, so that elements at index `j`
72	/// are created by calling `f(j)`
73	#[inline]
74	pub fn resize_with(&mut self, new_nrows: Cols, f: impl FnMut(Idx<Cols>) -> T) {
75		self.0.trans.resize_with(new_nrows, f);
76	}
77
78	/// truncates the row so that its new dimensions are `new_ncols`.  
79	/// the new dimension must be smaller than or equal to the current dimension
80	///
81	/// # panics
82	/// the function panics if any of the following conditions are violated:
83	/// - `new_ncols > self.ncols()`
84	#[inline]
85	pub fn truncate(&mut self, new_nrows: Cols) {
86		self.0.trans.truncate(new_nrows);
87	}
88
89	/// see [`RowRef::as_col_shape`]
90	#[inline]
91	pub fn into_col_shape<V: Shape>(self, nrows: V) -> Row<T, V> {
92		Row {
93			0: Own {
94				trans: self.0.trans.into_row_shape(nrows),
95			},
96		}
97	}
98
99	/// see [`RowRef::as_diagonal`]
100	#[inline]
101	pub fn into_diagonal(self) -> Diag<T, Cols> {
102		Diag {
103			0: crate::diag::Own { inner: self.0.trans },
104		}
105	}
106
107	/// see [`RowRef::transpose`]
108	#[inline]
109	pub fn into_transpose(self) -> Col<T, Cols> {
110		self.0.trans
111	}
112}
113
114impl<T, Cols: Shape> Row<T, Cols> {
115	/// returns the number of rows of the row (always 1)
116	#[inline]
117	pub fn nrows(&self) -> usize {
118		self.0.trans.ncols()
119	}
120
121	/// returns the number of columns of the row
122	#[inline]
123	pub fn ncols(&self) -> Cols {
124		self.0.trans.nrows()
125	}
126}
127
128impl<T: core::fmt::Debug, Cols: Shape> core::fmt::Debug for Own<T, Cols> {
129	fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
130		self.rb().fmt(f)
131	}
132}
133
134impl<T, Cols: Shape> Row<T, Cols> {
135	#[inline(always)]
136	/// see [`RowRef::as_ptr`]
137	pub fn as_ptr(&self) -> *const T {
138		self.rb().as_ptr()
139	}
140
141	#[inline(always)]
142	/// see [`RowRef::shape`]
143	pub fn shape(&self) -> (usize, Cols) {
144		self.rb().shape()
145	}
146
147	#[inline(always)]
148	/// see [`RowRef::col_stride`]
149	pub fn col_stride(&self) -> isize {
150		self.rb().col_stride()
151	}
152
153	#[inline(always)]
154	/// see [`RowRef::ptr_at`]
155	pub fn ptr_at(&self, col: IdxInc<Cols>) -> *const T {
156		self.rb().ptr_at(col)
157	}
158
159	#[inline(always)]
160	#[track_caller]
161	/// see [`RowRef::ptr_inbounds_at`]
162	pub unsafe fn ptr_inbounds_at(&self, col: Idx<Cols>) -> *const T {
163		self.rb().ptr_inbounds_at(col)
164	}
165
166	#[inline]
167	#[track_caller]
168	/// see [`RowRef::split_at_col`]
169	pub fn split_at_col(&self, col: IdxInc<Cols>) -> (RowRef<'_, T, usize>, RowRef<'_, T, usize>) {
170		self.rb().split_at_col(col)
171	}
172
173	#[inline(always)]
174	/// see [`RowRef::transpose`]
175	pub fn transpose(&self) -> ColRef<'_, T, Cols> {
176		self.rb().transpose()
177	}
178
179	#[inline(always)]
180	/// see [`RowRef::conjugate`]
181	pub fn conjugate(&self) -> RowRef<'_, T::Conj, Cols>
182	where
183		T: Conjugate,
184	{
185		self.rb().conjugate()
186	}
187
188	#[inline(always)]
189	/// see [`RowRef::canonical`]
190	pub fn canonical(&self) -> RowRef<'_, T::Canonical, Cols>
191	where
192		T: Conjugate,
193	{
194		self.rb().canonical()
195	}
196
197	#[inline(always)]
198	/// see [`RowRef::adjoint`]
199	pub fn adjoint(&self) -> ColRef<'_, T::Conj, Cols>
200	where
201		T: Conjugate,
202	{
203		self.rb().adjoint()
204	}
205
206	#[track_caller]
207	#[inline(always)]
208	/// see [`RowRef::get`]
209	pub fn get<ColRange>(&self, col: ColRange) -> <RowRef<'_, T, Cols> as RowIndex<ColRange>>::Target
210	where
211		for<'a> RowRef<'a, T, Cols>: RowIndex<ColRange>,
212	{
213		<RowRef<'_, T, Cols> as RowIndex<ColRange>>::get(self.rb(), col)
214	}
215
216	#[track_caller]
217	#[inline(always)]
218	/// see [`RowRef::get_unchecked`]
219	pub unsafe fn get_unchecked<ColRange>(&self, col: ColRange) -> <RowRef<'_, T, Cols> as RowIndex<ColRange>>::Target
220	where
221		for<'a> RowRef<'a, T, Cols>: RowIndex<ColRange>,
222	{
223		unsafe { <RowRef<'_, T, Cols> as RowIndex<ColRange>>::get_unchecked(self.rb(), col) }
224	}
225
226	#[inline]
227	/// see [`RowRef::reverse_cols`]
228	pub fn reverse_cols(&self) -> RowRef<'_, T, Cols> {
229		self.rb().reverse_cols()
230	}
231
232	#[inline]
233	/// see [`RowRef::subcols`]
234	pub fn subcols<V: Shape>(&self, col_start: IdxInc<Cols>, ncols: V) -> RowRef<'_, T, V> {
235		self.rb().subcols(col_start, ncols)
236	}
237
238	#[inline]
239	/// see [`RowRef::as_col_shape`]
240	pub fn as_col_shape<V: Shape>(&self, ncols: V) -> RowRef<'_, T, V> {
241		self.rb().as_col_shape(ncols)
242	}
243
244	#[inline]
245	/// see [`RowRef::as_dyn_cols`]
246	pub fn as_dyn_cols(&self) -> RowRef<'_, T, usize> {
247		self.rb().as_dyn_cols()
248	}
249
250	#[inline]
251	/// see [`RowRef::as_dyn_stride`]
252	pub fn as_dyn_stride(&self) -> RowRef<'_, T, Cols, isize> {
253		self.rb().as_dyn_stride()
254	}
255
256	#[inline]
257	/// see [`RowRef::iter`]
258	pub fn iter(&self) -> impl '_ + ExactSizeIterator + DoubleEndedIterator<Item = &'_ T> {
259		self.rb().iter()
260	}
261
262	#[inline]
263	#[cfg(feature = "rayon")]
264	/// see [`RowRef::par_iter`]
265	pub fn par_iter(&self) -> impl '_ + rayon::iter::IndexedParallelIterator<Item = &'_ T>
266	where
267		T: Sync,
268	{
269		self.rb().par_iter()
270	}
271
272	#[inline]
273	/// see [`RowRef::try_as_row_major`]
274	pub fn try_as_row_major(&self) -> Option<RowRef<'_, T, Cols, ContiguousFwd>> {
275		self.rb().try_as_row_major()
276	}
277
278	#[inline]
279	/// see [`RowRef::as_diagonal`]
280	pub fn as_diagonal(&self) -> DiagRef<'_, T, Cols> {
281		self.rb().as_diagonal()
282	}
283
284	#[inline(always)]
285	/// see [`RowRef::const_cast`]
286	pub unsafe fn const_cast(&self) -> RowMut<'_, T, Cols> {
287		self.rb().const_cast()
288	}
289
290	#[inline]
291	/// see [`RowRef::as_mat`]
292	pub fn as_mat(&self) -> MatRef<'_, T, usize, Cols, isize> {
293		self.rb().as_mat()
294	}
295
296	#[inline]
297	/// see [`RowRef::as_mat`]
298	pub fn as_mat_mut(&mut self) -> MatMut<'_, T, usize, Cols, isize> {
299		self.rb_mut().as_mat_mut()
300	}
301}
302
303impl<T, Cols: Shape> Row<T, Cols> {
304	#[inline(always)]
305	/// see [`RowMut::as_ptr_mut`]
306	pub fn as_ptr_mut(&mut self) -> *mut T {
307		self.rb_mut().as_ptr_mut()
308	}
309
310	#[inline(always)]
311	/// see [`RowMut::ptr_at_mut`]
312	pub fn ptr_at_mut(&mut self, col: IdxInc<Cols>) -> *mut T {
313		self.rb_mut().ptr_at_mut(col)
314	}
315
316	#[inline(always)]
317	#[track_caller]
318	/// see [`RowMut::ptr_inbounds_at_mut`]
319	pub unsafe fn ptr_inbounds_at_mut(&mut self, col: Idx<Cols>) -> *mut T {
320		self.rb_mut().ptr_inbounds_at_mut(col)
321	}
322
323	#[inline]
324	#[track_caller]
325	/// see [`RowMut::split_at_col_mut`]
326	pub fn split_at_col_mut(&mut self, col: IdxInc<Cols>) -> (RowMut<'_, T, usize>, RowMut<'_, T, usize>) {
327		self.rb_mut().split_at_col_mut(col)
328	}
329
330	#[inline(always)]
331	/// see [`RowMut::transpose_mut`]
332	pub fn transpose_mut(&mut self) -> ColMut<'_, T, Cols> {
333		self.rb_mut().transpose_mut()
334	}
335
336	#[inline(always)]
337	/// see [`RowMut::conjugate_mut`]
338	pub fn conjugate_mut(&mut self) -> RowMut<'_, T::Conj, Cols>
339	where
340		T: Conjugate,
341	{
342		self.rb_mut().conjugate_mut()
343	}
344
345	#[inline(always)]
346	/// see [`RowMut::canonical_mut`]
347	pub fn canonical_mut(&mut self) -> RowMut<'_, T::Canonical, Cols>
348	where
349		T: Conjugate,
350	{
351		self.rb_mut().canonical_mut()
352	}
353
354	#[inline(always)]
355	/// see [`RowMut::adjoint_mut`]
356	pub fn adjoint_mut(&mut self) -> ColMut<'_, T::Conj, Cols>
357	where
358		T: Conjugate,
359	{
360		self.rb_mut().adjoint_mut()
361	}
362
363	#[track_caller]
364	#[inline(always)]
365	/// see [`RowMut::get_mut`]
366	pub fn get_mut<ColRange>(&mut self, col: ColRange) -> <RowMut<'_, T, Cols> as RowIndex<ColRange>>::Target
367	where
368		for<'a> RowMut<'a, T, Cols>: RowIndex<ColRange>,
369	{
370		<RowMut<'_, T, Cols> as RowIndex<ColRange>>::get(self.rb_mut(), col)
371	}
372
373	#[track_caller]
374	#[inline(always)]
375	/// see [`RowMut::get_mut_unchecked`]
376	pub unsafe fn get_mut_unchecked<ColRange>(&mut self, col: ColRange) -> <RowMut<'_, T, Cols> as RowIndex<ColRange>>::Target
377	where
378		for<'a> RowMut<'a, T, Cols>: RowIndex<ColRange>,
379	{
380		unsafe { <RowMut<'_, T, Cols> as RowIndex<ColRange>>::get_unchecked(self.rb_mut(), col) }
381	}
382
383	#[inline]
384	/// see [`RowMut::reverse_cols_mut`]
385	pub fn reverse_cols_mut(&mut self) -> RowMut<'_, T, Cols> {
386		self.rb_mut().reverse_cols_mut()
387	}
388
389	#[inline]
390	/// see [`RowMut::subcols_mut`]
391	pub fn subcols_mut<V: Shape>(&mut self, col_start: IdxInc<Cols>, ncols: V) -> RowMut<'_, T, V> {
392		self.rb_mut().subcols_mut(col_start, ncols)
393	}
394
395	#[inline]
396	/// see [`RowMut::as_col_shape_mut`]
397	pub fn as_col_shape_mut<V: Shape>(&mut self, ncols: V) -> RowMut<'_, T, V> {
398		self.rb_mut().as_col_shape_mut(ncols)
399	}
400
401	#[inline]
402	/// see [`RowMut::as_dyn_cols_mut`]
403	pub fn as_dyn_cols_mut(&mut self) -> RowMut<'_, T, usize> {
404		self.rb_mut().as_dyn_cols_mut()
405	}
406
407	#[inline]
408	/// see [`RowMut::as_dyn_stride_mut`]
409	pub fn as_dyn_stride_mut(&mut self) -> RowMut<'_, T, Cols, isize> {
410		self.rb_mut().as_dyn_stride_mut()
411	}
412
413	#[inline]
414	/// see [`RowMut::iter_mut`]
415	pub fn iter_mut(&mut self) -> impl '_ + ExactSizeIterator + DoubleEndedIterator<Item = &'_ mut T> {
416		self.rb_mut().iter_mut()
417	}
418
419	#[inline]
420	#[cfg(feature = "rayon")]
421	/// see [`RowMut::par_iter_mut`]
422	pub fn par_iter_mut(&mut self) -> impl '_ + rayon::iter::IndexedParallelIterator<Item = &'_ mut T>
423	where
424		T: Send,
425	{
426		self.rb_mut().par_iter_mut()
427	}
428
429	#[inline]
430	/// see [`RowMut::try_as_row_major_mut`]
431	pub fn try_as_row_major_mut(&mut self) -> Option<RowMut<'_, T, Cols, ContiguousFwd>> {
432		self.rb_mut().try_as_row_major_mut()
433	}
434
435	#[inline]
436	/// see [`RowMut::as_diagonal_mut`]
437	pub fn as_diagonal_mut(&mut self) -> DiagMut<'_, T, Cols> {
438		self.rb_mut().as_diagonal_mut()
439	}
440}
441
442impl<'short, T, Cols: Shape> Reborrow<'short> for Own<T, Cols> {
443	type Target = Ref<'short, T, Cols>;
444
445	#[inline]
446	fn rb(&'short self) -> Self::Target {
447		Ref { trans: self.trans.rb() }
448	}
449}
450impl<'short, T, Cols: Shape> ReborrowMut<'short> for Own<T, Cols> {
451	type Target = Mut<'short, T, Cols>;
452
453	#[inline]
454	fn rb_mut(&'short mut self) -> Self::Target {
455		Mut { trans: self.trans.rb_mut() }
456	}
457}
458
459impl<T, Cols: Shape> Row<T, Cols>
460where
461	T: RealField,
462{
463	/// Returns the maximum element in the row, or `None` if the row is empty
464	pub fn max(&self) -> Option<T> {
465		self.as_dyn_cols().as_dyn_stride().internal_max()
466	}
467
468	/// Returns the minimum element in the row, or `None` if the row is empty
469	pub fn min(&self) -> Option<T> {
470		self.as_dyn_cols().as_dyn_stride().internal_min()
471	}
472}
473
474impl<T> FromIterator<T> for Row<T> {
475	fn from_iter<I>(iter: I) -> Self
476	where
477		I: IntoIterator<Item = T>,
478	{
479		Row {
480			0: Own {
481				trans: Col::from_iter_imp(iter.into_iter()),
482			},
483		}
484	}
485}
486
487#[cfg(test)]
488mod tests {
489	use crate::Row;
490
491	#[test]
492	fn test_row_min() {
493		let row: Row<f64> = Row::from_fn(5, |x| (x + 1) as f64);
494		assert_eq!(row.min(), Some(1.0));
495
496		let empty: Row<f64> = Row::from_fn(0, |_| 0.0);
497		assert_eq!(empty.min(), None);
498	}
499
500	#[test]
501	fn test_row_max() {
502		let row: Row<f64> = Row::from_fn(5, |x| (x + 1) as f64);
503		assert_eq!(row.max(), Some(5.0));
504
505		let empty: Row<f64> = Row::from_fn(0, |_| 0.0);
506		assert_eq!(empty.max(), None);
507	}
508
509	#[test]
510	fn test_from_iter() {
511		let row: Row<i32> = (0..10).collect();
512		assert_eq!(row.ncols(), 10);
513		assert_eq!(row[0], 0);
514		assert_eq!(row[9], 9);
515	}
516}