1use crate::Shape;
2use crate::internal_prelude::*;
3use crate::utils::bound::{One, Zero};
4
5pub trait RowIndex<ColRange> {
7 type Target;
9
10 fn get(this: Self, col: ColRange) -> Self::Target;
12 unsafe fn get_unchecked(this: Self, col: ColRange) -> Self::Target;
14}
15
16mod row_index;
17
18pub(crate) mod rowmut;
19pub(crate) mod rowown;
20pub(crate) mod rowref;
21
22use mat::AsMat;
23pub use rowmut::Mut;
24pub use rowown::Own;
25pub use rowref::Ref;
26
27pub type RowRef<'a, T, Cols = usize, CStride = isize> = generic::Row<Ref<'a, T, Cols, CStride>>;
37
38pub type RowMut<'a, T, Cols = usize, CStride = isize> = generic::Row<Mut<'a, T, Cols, CStride>>;
48
49pub type Row<T, Cols = usize> = generic::Row<Own<T, Cols>>;
56
57pub mod generic {
59 use crate::{Idx, Shape, Stride};
60 use core::fmt::Debug;
61 use core::ops::{Index, IndexMut};
62 use reborrow::*;
63
64 #[derive(Copy, Clone)]
66 #[repr(transparent)]
67 pub struct Row<Inner>(pub Inner);
68
69 impl<Inner: Debug> Debug for Row<Inner> {
70 #[inline(always)]
71 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
72 self.0.fmt(f)
73 }
74 }
75
76 impl<Inner> Row<Inner> {
77 #[inline(always)]
79 pub fn from_inner_ref(inner: &Inner) -> &Self {
80 unsafe { &*(inner as *const Inner as *const Self) }
81 }
82
83 #[inline(always)]
85 pub fn from_inner_mut(inner: &mut Inner) -> &mut Self {
86 unsafe { &mut *(inner as *mut Inner as *mut Self) }
87 }
88 }
89
90 impl<Inner> core::ops::Deref for Row<Inner> {
91 type Target = Inner;
92
93 #[inline(always)]
94 fn deref(&self) -> &Self::Target {
95 &self.0
96 }
97 }
98
99 impl<Inner> core::ops::DerefMut for Row<Inner> {
100 #[inline(always)]
101 fn deref_mut(&mut self) -> &mut Self::Target {
102 &mut self.0
103 }
104 }
105
106 impl<'short, Inner: Reborrow<'short>> Reborrow<'short> for Row<Inner> {
107 type Target = Row<Inner::Target>;
108
109 #[inline(always)]
110 fn rb(&'short self) -> Self::Target {
111 Row(self.0.rb())
112 }
113 }
114
115 impl<'short, Inner: ReborrowMut<'short>> ReborrowMut<'short> for Row<Inner> {
116 type Target = Row<Inner::Target>;
117
118 #[inline(always)]
119 fn rb_mut(&'short mut self) -> Self::Target {
120 Row(self.0.rb_mut())
121 }
122 }
123
124 impl<Inner: IntoConst> IntoConst for Row<Inner> {
125 type Target = Row<Inner::Target>;
126
127 #[inline(always)]
128 fn into_const(self) -> Self::Target {
129 Row(self.0.into_const())
130 }
131 }
132
133 impl<T, Cols: Shape, CStride: Stride, Inner: for<'short> Reborrow<'short, Target = super::Ref<'short, T, Cols, CStride>>> Index<Idx<Cols>>
134 for Row<Inner>
135 {
136 type Output = T;
137
138 #[inline]
139 #[track_caller]
140 fn index(&self, col: Idx<Cols>) -> &Self::Output {
141 self.rb().at(col)
142 }
143 }
144
145 impl<
146 T,
147 Cols: Shape,
148 CStride: Stride,
149 Inner: for<'short> Reborrow<'short, Target = super::Ref<'short, T, Cols, CStride>>
150 + for<'short> ReborrowMut<'short, Target = super::Mut<'short, T, Cols, CStride>>,
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
161pub trait AsRowMut: AsRowRef {
163 fn as_row_mut(&mut self) -> RowMut<'_, Self::T, Self::Cols>;
165}
166pub trait AsRowRef: AsMatRef<Rows = One> {
168 fn as_row_ref(&self) -> RowRef<'_, Self::T, Self::Cols>;
170}
171
172impl<M: AsMatRef<Rows = One>> AsRowRef for M {
173 #[inline]
174 fn as_row_ref(&self) -> RowRef<'_, Self::T, Self::Cols> {
175 self.as_mat_ref().row(Zero)
176 }
177}
178
179impl<M: AsMatMut<Rows = One>> AsRowMut for M {
180 #[inline]
181 fn as_row_mut(&mut self) -> RowMut<'_, Self::T, Self::Cols> {
182 self.as_mat_mut().row_mut(Zero)
183 }
184}
185
186impl<T, Cols: Shape, Rs: Stride> AsMatRef for RowRef<'_, T, Cols, Rs> {
187 type Cols = Cols;
188 type Owned = Row<T, Cols>;
189 type Rows = One;
190 type T = T;
191
192 #[inline]
193 fn as_mat_ref(&self) -> MatRef<Self::T, One, Self::Cols> {
194 self.as_dyn_stride().as_mat().as_row_shape(One)
195 }
196}
197
198impl<T, Cols: Shape, Rs: Stride> AsMatRef for RowMut<'_, T, Cols, Rs> {
199 type Cols = Cols;
200 type Owned = Row<T, Cols>;
201 type Rows = One;
202 type T = T;
203
204 #[inline]
205 fn as_mat_ref(&self) -> MatRef<Self::T, One, Self::Cols> {
206 self.rb().as_dyn_stride().as_mat().as_row_shape(One)
207 }
208}
209
210impl<T, Cols: Shape> AsMatRef for Row<T, Cols> {
211 type Cols = Cols;
212 type Owned = Row<T, Cols>;
213 type Rows = One;
214 type T = T;
215
216 #[inline]
217 fn as_mat_ref(&self) -> MatRef<Self::T, One, Self::Cols> {
218 self.as_dyn_stride().as_mat().as_row_shape(One)
219 }
220}
221
222impl<T, Cols: Shape, Rs: Stride> AsMatMut for RowMut<'_, T, Cols, Rs> {
223 #[inline]
224 fn as_mat_mut(&mut self) -> MatMut<Self::T, One, Self::Cols> {
225 self.rb_mut().as_dyn_stride_mut().as_mat_mut().as_row_shape_mut(One)
226 }
227}
228
229impl<T, Cols: Shape> AsMatMut for Row<T, Cols> {
230 #[inline]
231 fn as_mat_mut(&mut self) -> MatMut<Self::T, One, Self::Cols> {
232 self.as_dyn_stride_mut().as_mat_mut().as_row_shape_mut(One)
233 }
234}
235
236impl<T, Cols: Shape> AsMat<T> for Row<T, Cols> {
237 #[inline]
238 fn zeros(_: One, cols: Cols) -> Self
239 where
240 T: ComplexField,
241 {
242 Row::zeros(cols)
243 }
244
245 #[track_caller]
246 #[inline]
247 fn truncate(&mut self, _: One, cols: Self::Cols) {
248 self.truncate(cols)
249 }
250}