use crate::Shape;
use crate::internal_prelude::*;
use crate::utils::bound::{One, Zero};
pub trait RowIndex<ColRange> {
type Target;
fn get(this: Self, col: ColRange) -> Self::Target;
unsafe fn get_unchecked(this: Self, col: ColRange) -> Self::Target;
}
mod row_index;
pub(crate) mod rowmut;
pub(crate) mod rowown;
pub(crate) mod rowref;
use mat::AsMat;
pub use rowmut::Mut;
pub use rowown::Own;
pub use rowref::Ref;
pub type RowRef<'a, T, Cols = usize, CStride = isize> =
generic::Row<Ref<'a, T, Cols, CStride>>;
pub type RowMut<'a, T, Cols = usize, CStride = isize> =
generic::Row<Mut<'a, T, Cols, CStride>>;
pub type Row<T, Cols = usize> = generic::Row<Own<T, Cols>>;
#[doc(hidden)]
pub mod generic {
use crate::{Idx, Shape, Stride};
use core::fmt::Debug;
use core::ops::{Index, IndexMut};
use reborrow::*;
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct Row<Inner>(pub Inner);
impl<Inner: Debug> Debug for Row<Inner> {
#[inline(always)]
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.0.fmt(f)
}
}
impl<Inner> Row<Inner> {
#[inline(always)]
pub fn from_inner_ref(inner: &Inner) -> &Self {
unsafe { &*(inner as *const Inner as *const Self) }
}
#[inline(always)]
pub fn from_inner_mut(inner: &mut Inner) -> &mut Self {
unsafe { &mut *(inner as *mut Inner as *mut Self) }
}
}
impl<Inner> core::ops::Deref for Row<Inner> {
type Target = Inner;
#[inline(always)]
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl<Inner> core::ops::DerefMut for Row<Inner> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl<'short, Inner: Reborrow<'short>> Reborrow<'short> for Row<Inner> {
type Target = Row<Inner::Target>;
#[inline(always)]
fn rb(&'short self) -> Self::Target {
Row(self.0.rb())
}
}
impl<'short, Inner: ReborrowMut<'short>> ReborrowMut<'short> for Row<Inner> {
type Target = Row<Inner::Target>;
#[inline(always)]
fn rb_mut(&'short mut self) -> Self::Target {
Row(self.0.rb_mut())
}
}
impl<Inner: IntoConst> IntoConst for Row<Inner> {
type Target = Row<Inner::Target>;
#[inline(always)]
fn into_const(self) -> Self::Target {
Row(self.0.into_const())
}
}
impl<
T,
Cols: Shape,
CStride: Stride,
Inner: for<'short> Reborrow<
'short,
Target = super::Ref<'short, T, Cols, CStride>,
>,
> Index<Idx<Cols>> for Row<Inner>
{
type Output = T;
#[inline]
#[track_caller]
fn index(&self, col: Idx<Cols>) -> &Self::Output {
self.rb().at(col)
}
}
impl<
T,
Cols: Shape,
CStride: Stride,
Inner: for<'short> Reborrow<
'short,
Target = super::Ref<'short, T, Cols, CStride>,
> + for<'short> ReborrowMut<
'short,
Target = super::Mut<'short, T, Cols, CStride>,
>,
> IndexMut<Idx<Cols>> for Row<Inner>
{
#[inline]
#[track_caller]
fn index_mut(&mut self, col: Idx<Cols>) -> &mut Self::Output {
self.rb_mut().at_mut(col)
}
}
}
pub trait AsRowMut: AsRowRef {
fn as_row_mut(&mut self) -> RowMut<'_, Self::T, Self::Cols>;
}
pub trait AsRowRef: AsMatRef<Rows = One> {
fn as_row_ref(&self) -> RowRef<'_, Self::T, Self::Cols>;
}
impl<M: AsMatRef<Rows = One>> AsRowRef for M {
#[inline]
fn as_row_ref(&self) -> RowRef<'_, Self::T, Self::Cols> {
self.as_mat_ref().row(Zero)
}
}
impl<M: AsMatMut<Rows = One>> AsRowMut for M {
#[inline]
fn as_row_mut(&mut self) -> RowMut<'_, Self::T, Self::Cols> {
self.as_mat_mut().row_mut(Zero)
}
}
impl<T, Cols: Shape, Rs: Stride> AsMatRef for RowRef<'_, T, Cols, Rs> {
type Cols = Cols;
type Owned = Row<T, Cols>;
type Rows = One;
type T = T;
#[inline]
fn as_mat_ref(&self) -> MatRef<'_, Self::T, One, Self::Cols> {
self.as_dyn_stride().as_mat().as_row_shape(One)
}
}
impl<T, Cols: Shape, Rs: Stride> AsMatRef for RowMut<'_, T, Cols, Rs> {
type Cols = Cols;
type Owned = Row<T, Cols>;
type Rows = One;
type T = T;
#[inline]
fn as_mat_ref(&self) -> MatRef<'_, Self::T, One, Self::Cols> {
self.rb().as_dyn_stride().as_mat().as_row_shape(One)
}
}
impl<T, Cols: Shape> AsMatRef for Row<T, Cols> {
type Cols = Cols;
type Owned = Row<T, Cols>;
type Rows = One;
type T = T;
#[inline]
fn as_mat_ref(&self) -> MatRef<'_, Self::T, One, Self::Cols> {
self.as_dyn_stride().as_mat().as_row_shape(One)
}
}
impl<T, Cols: Shape, Rs: Stride> AsMatMut for RowMut<'_, T, Cols, Rs> {
#[inline]
fn as_mat_mut(&mut self) -> MatMut<'_, Self::T, One, Self::Cols> {
self.rb_mut()
.as_dyn_stride_mut()
.as_mat_mut()
.as_row_shape_mut(One)
}
}
impl<T, Cols: Shape> AsMatMut for Row<T, Cols> {
#[inline]
fn as_mat_mut(&mut self) -> MatMut<'_, Self::T, One, Self::Cols> {
self.as_dyn_stride_mut().as_mat_mut().as_row_shape_mut(One)
}
}
impl<T, Cols: Shape> AsMat<T> for Row<T, Cols> {
#[inline]
fn zeros(_: One, cols: Cols) -> Self
where
T: ComplexField,
{
Row::zeros(cols)
}
#[track_caller]
#[inline]
fn truncate(&mut self, _: One, cols: Self::Cols) {
self.truncate(cols)
}
}