use super::*;
pub struct Ref<'a, T, Dim = usize, Stride = isize> {
pub(crate) inner: ColRef<'a, T, Dim, Stride>,
}
impl<T: core::fmt::Debug, Dim: Shape, S: Stride> core::fmt::Debug
for Ref<'_, T, Dim, S>
{
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
self.inner.fmt(f)
}
}
impl<T, Dim: Copy, Stride: Copy> Copy for Ref<'_, T, Dim, Stride> {}
impl<T, Dim: Copy, Stride: Copy> Clone for Ref<'_, T, Dim, Stride> {
#[inline]
fn clone(&self) -> Self {
*self
}
}
impl<'short, T, Dim: Copy, Stride: Copy> Reborrow<'short>
for Ref<'_, T, Dim, Stride>
{
type Target = Ref<'short, T, Dim, Stride>;
#[inline]
fn rb(&'short self) -> Self::Target {
*self
}
}
impl<'short, T, Dim: Copy, Stride: Copy> ReborrowMut<'short>
for Ref<'_, T, Dim, Stride>
{
type Target = Ref<'short, T, Dim, Stride>;
#[inline]
fn rb_mut(&'short mut self) -> Self::Target {
*self
}
}
impl<'a, T, Dim: Copy, Stride: Copy> IntoConst for Ref<'a, T, Dim, Stride> {
type Target = Ref<'a, T, Dim, Stride>;
#[inline]
fn into_const(self) -> Self::Target {
self
}
}
impl<'a, T> DiagRef<'a, T> {
#[inline]
pub fn from_ref(value: &'a T) -> Self {
unsafe { DiagRef::from_raw_parts(value as *const T, 1, 1) }
}
#[inline]
pub fn from_slice(slice: &'a [T]) -> Self {
let len = slice.len();
unsafe { Self::from_raw_parts(slice.as_ptr(), len, 1) }
}
}
impl<'a, T, Dim: Shape, Stride: crate::Stride> DiagRef<'a, T, Dim, Stride> {
#[inline(always)]
#[track_caller]
pub const unsafe fn from_raw_parts(
ptr: *const T,
dim: Dim,
stride: Stride,
) -> Self {
Self {
0: Ref {
inner: ColRef::from_raw_parts(ptr, dim, stride),
},
}
}
#[inline(always)]
pub fn stride(&self) -> Stride {
self.inner.row_stride()
}
#[inline]
pub fn map<U>(&self, f: impl FnMut(&T) -> U) -> Diag<U, Dim> {
let mut f = f;
zip!(self).map(|unzip!(x)| f(x))
}
#[inline]
pub fn for_each(&self, f: impl FnMut(&T)) {
let mut f = f;
zip!(self).for_each(|unzip!(x)| f(x))
}
#[inline(always)]
pub fn column_vector(self) -> ColRef<'a, T, Dim, Stride> {
self.inner
}
#[inline]
pub fn as_ref(&self) -> DiagRef<'_, T, Dim, Stride> {
*self
}
#[inline]
#[track_caller]
pub fn as_shape<D: Shape>(self, len: D) -> DiagRef<'a, T, D, Stride> {
DiagRef {
0: Ref {
inner: self.inner.as_row_shape(len),
},
}
}
#[inline]
pub fn as_dyn(self) -> DiagRef<'a, T, usize, Stride> {
DiagRef {
0: Ref {
inner: self.inner.as_dyn_rows(),
},
}
}
#[inline]
pub fn as_dyn_stride(self) -> DiagRef<'a, T, Dim> {
DiagRef {
0: Ref {
inner: self.inner.as_dyn_stride(),
},
}
}
#[inline]
pub fn conjugate(self) -> DiagRef<'a, T::Conj, Dim, Stride>
where
T: Conjugate,
{
DiagRef {
0: Ref {
inner: self.inner.conjugate(),
},
}
}
#[inline]
pub fn canonical(self) -> DiagRef<'a, T::Canonical, Dim, Stride>
where
T: Conjugate,
{
DiagRef {
0: Ref {
inner: self.inner.canonical(),
},
}
}
#[inline]
pub fn dim(&self) -> Dim {
self.inner.nrows()
}
}
impl<
T,
Dim: Shape,
Stride: crate::Stride,
Inner: for<'short> Reborrow<'short, Target = Ref<'short, T, Dim, Stride>>,
> generic::Diag<Inner>
{
#[inline]
pub fn is_all_finite(&self) -> bool
where
T: Conjugate,
{
self.rb().column_vector().is_all_finite()
}
#[inline]
pub fn has_nan(&self) -> bool
where
T: Conjugate,
{
self.rb().column_vector().has_nan()
}
}