use ndarray::{Array1, ArrayView1};
use std::ops::Deref;
use std::sync::Arc;
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct SignedWeightsView<'a>(ArrayView1<'a, f64>);
impl<'a> SignedWeightsView<'a> {
#[inline]
pub fn new(view: ArrayView1<'a, f64>) -> Self {
Self(view)
}
#[inline]
pub fn from_array(array: &'a Array1<f64>) -> Self {
Self(array.view())
}
#[inline]
pub fn from_slice(slice: &'a [f64]) -> Self {
Self(ArrayView1::from(slice))
}
#[inline]
pub fn view(&self) -> ArrayView1<'a, f64> {
self.0
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn as_slice(&self) -> Option<&[f64]> {
self.0.as_slice()
}
#[inline]
pub fn as_psd(self) -> Option<PsdWeightsView<'a>> {
if self.0.iter().all(|&w| w >= 0.0) {
Some(PsdWeightsView(self.0))
} else {
None
}
}
}
#[derive(Copy, Clone)]
#[repr(transparent)]
pub struct PsdWeightsView<'a>(ArrayView1<'a, f64>);
impl<'a> PsdWeightsView<'a> {
#[inline]
pub fn try_new(view: ArrayView1<'a, f64>) -> Result<Self, String> {
if view.iter().all(|&w| w >= 0.0) {
Ok(Self(view))
} else {
Err("PsdWeights::try_new: weights must be nonneg (use SignedWeightsView for observed-Hessian assembly)".to_string())
}
}
#[inline]
pub fn try_from_array(array: &'a Array1<f64>) -> Result<Self, String> {
Self::try_new(array.view())
}
#[inline]
pub fn from_view_unchecked(view: ArrayView1<'a, f64>) -> Self {
Self(view)
}
#[inline]
pub fn as_signed(self) -> SignedWeightsView<'a> {
SignedWeightsView(self.0)
}
#[inline]
pub fn view(&self) -> ArrayView1<'a, f64> {
self.0
}
#[inline]
pub fn len(&self) -> usize {
self.0.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
#[inline]
pub fn as_slice(&self) -> Option<&[f64]> {
self.0.as_slice()
}
}
#[derive(Clone)]
#[repr(transparent)]
pub struct SignedWeightsArc(Arc<Array1<f64>>);
impl SignedWeightsArc {
#[inline]
pub fn from_arc(arc: Arc<Array1<f64>>) -> Self {
Self(arc)
}
#[inline]
pub fn from_array(array: Array1<f64>) -> Self {
Self(Arc::new(array))
}
#[inline]
pub fn view_signed(&self) -> SignedWeightsView<'_> {
SignedWeightsView::from_array(self.0.as_ref())
}
#[inline]
pub fn as_arc(&self) -> &Arc<Array1<f64>> {
&self.0
}
}
impl Deref for SignedWeightsArc {
type Target = Array1<f64>;
#[inline]
fn deref(&self) -> &Array1<f64> {
self.0.as_ref()
}
}
impl AsRef<Array1<f64>> for SignedWeightsArc {
#[inline]
fn as_ref(&self) -> &Array1<f64> {
self.0.as_ref()
}
}