1use super::*;
2use crate::{ContiguousFwd, Idx, IdxInc, TryReserveError};
3
4#[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 #[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 #[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 #[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 #[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 #[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 #[track_caller]
66 pub fn reserve(&mut self, new_row_capacity: usize) {
67 self.0.trans.reserve(new_row_capacity)
68 }
69
70 #[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 #[inline]
85 pub fn truncate(&mut self, new_nrows: Cols) {
86 self.0.trans.truncate(new_nrows);
87 }
88
89 #[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 #[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 #[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 #[inline]
117 pub fn nrows(&self) -> usize {
118 self.0.trans.ncols()
119 }
120
121 #[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 pub fn as_ptr(&self) -> *const T {
138 self.rb().as_ptr()
139 }
140
141 #[inline(always)]
142 pub fn shape(&self) -> (usize, Cols) {
144 self.rb().shape()
145 }
146
147 #[inline(always)]
148 pub fn col_stride(&self) -> isize {
150 self.rb().col_stride()
151 }
152
153 #[inline(always)]
154 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 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 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 pub fn transpose(&self) -> ColRef<'_, T, Cols> {
176 self.rb().transpose()
177 }
178
179 #[inline(always)]
180 pub fn conjugate(&self) -> RowRef<'_, T::Conj, Cols>
182 where
183 T: Conjugate,
184 {
185 self.rb().conjugate()
186 }
187
188 #[inline(always)]
189 pub fn canonical(&self) -> RowRef<'_, T::Canonical, Cols>
191 where
192 T: Conjugate,
193 {
194 self.rb().canonical()
195 }
196
197 #[inline(always)]
198 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 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 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 pub fn reverse_cols(&self) -> RowRef<'_, T, Cols> {
229 self.rb().reverse_cols()
230 }
231
232 #[inline]
233 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 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 pub fn as_dyn_cols(&self) -> RowRef<'_, T, usize> {
247 self.rb().as_dyn_cols()
248 }
249
250 #[inline]
251 pub fn as_dyn_stride(&self) -> RowRef<'_, T, Cols, isize> {
253 self.rb().as_dyn_stride()
254 }
255
256 #[inline]
257 pub fn iter(&self) -> impl '_ + ExactSizeIterator + DoubleEndedIterator<Item = &'_ T> {
259 self.rb().iter()
260 }
261
262 #[inline]
263 #[cfg(feature = "rayon")]
264 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 pub fn try_as_row_major(&self) -> Option<RowRef<'_, T, Cols, ContiguousFwd>> {
275 self.rb().try_as_row_major()
276 }
277
278 #[inline]
279 pub fn as_diagonal(&self) -> DiagRef<'_, T, Cols> {
281 self.rb().as_diagonal()
282 }
283
284 #[inline(always)]
285 pub unsafe fn const_cast(&self) -> RowMut<'_, T, Cols> {
287 self.rb().const_cast()
288 }
289
290 #[inline]
291 pub fn as_mat(&self) -> MatRef<'_, T, usize, Cols, isize> {
293 self.rb().as_mat()
294 }
295
296 #[inline]
297 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 pub fn as_ptr_mut(&mut self) -> *mut T {
307 self.rb_mut().as_ptr_mut()
308 }
309
310 #[inline(always)]
311 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 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 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 pub fn transpose_mut(&mut self) -> ColMut<'_, T, Cols> {
333 self.rb_mut().transpose_mut()
334 }
335
336 #[inline(always)]
337 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 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 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 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 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 pub fn reverse_cols_mut(&mut self) -> RowMut<'_, T, Cols> {
386 self.rb_mut().reverse_cols_mut()
387 }
388
389 #[inline]
390 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 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 pub fn as_dyn_cols_mut(&mut self) -> RowMut<'_, T, usize> {
404 self.rb_mut().as_dyn_cols_mut()
405 }
406
407 #[inline]
408 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 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 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 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 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 pub fn max(&self) -> Option<T> {
465 self.as_dyn_cols().as_dyn_stride().internal_max()
466 }
467
468 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}