use core::marker::PhantomData;
use crate::enums::Layout;
use crate::error::{Error, Result};
#[derive(Debug, Clone, Copy)]
pub struct MatrixRef<'a, T> {
data: *const T,
rows: usize,
cols: usize,
leading_dim: usize,
layout: Layout,
_marker: PhantomData<&'a [T]>,
}
#[derive(Debug)]
pub struct MatrixMut<'a, T> {
data: *mut T,
rows: usize,
cols: usize,
leading_dim: usize,
layout: Layout,
_marker: PhantomData<&'a mut [T]>,
}
unsafe impl<T: Sync> Send for MatrixRef<'_, T> {}
unsafe impl<T: Sync> Sync for MatrixRef<'_, T> {}
unsafe impl<T: Send> Send for MatrixMut<'_, T> {}
unsafe impl<T: Sync> Sync for MatrixMut<'_, T> {}
impl<'a, T> MatrixRef<'a, T> {
pub fn new(data: &'a [T], rows: usize, cols: usize, layout: Layout) -> Result<Self> {
let lda = match layout {
Layout::RowMajor => cols,
Layout::ColMajor => rows,
};
Self::with_lda(data, rows, cols, layout, lda)
}
pub fn with_lda(
data: &'a [T],
rows: usize,
cols: usize,
layout: Layout,
leading_dim: usize,
) -> Result<Self> {
check_lda(rows, cols, layout, leading_dim, data.len())?;
Ok(Self {
data: data.as_ptr(),
rows,
cols,
leading_dim,
layout,
_marker: PhantomData,
})
}
#[inline]
#[must_use]
pub fn rows(&self) -> usize {
self.rows
}
#[inline]
#[must_use]
pub fn cols(&self) -> usize {
self.cols
}
#[inline]
#[must_use]
pub fn leading_dim(&self) -> usize {
self.leading_dim
}
#[inline]
#[must_use]
pub fn layout(&self) -> Layout {
self.layout
}
#[inline]
#[must_use]
pub fn as_ptr(&self) -> *const T {
self.data
}
}
impl<'a, T> MatrixMut<'a, T> {
pub fn new(data: &'a mut [T], rows: usize, cols: usize, layout: Layout) -> Result<Self> {
let lda = match layout {
Layout::RowMajor => cols,
Layout::ColMajor => rows,
};
Self::with_lda(data, rows, cols, layout, lda)
}
pub fn with_lda(
data: &'a mut [T],
rows: usize,
cols: usize,
layout: Layout,
leading_dim: usize,
) -> Result<Self> {
check_lda(rows, cols, layout, leading_dim, data.len())?;
Ok(Self {
data: data.as_mut_ptr(),
rows,
cols,
leading_dim,
layout,
_marker: PhantomData,
})
}
#[inline]
#[must_use]
pub fn rows(&self) -> usize {
self.rows
}
#[inline]
#[must_use]
pub fn cols(&self) -> usize {
self.cols
}
#[inline]
#[must_use]
pub fn leading_dim(&self) -> usize {
self.leading_dim
}
#[inline]
#[must_use]
pub fn layout(&self) -> Layout {
self.layout
}
#[inline]
#[must_use]
pub fn as_ptr(&self) -> *const T {
self.data
}
#[inline]
#[must_use]
pub fn as_mut_ptr(&mut self) -> *mut T {
self.data
}
#[must_use]
pub fn as_ref(&self) -> MatrixRef<'_, T> {
MatrixRef {
data: self.data,
rows: self.rows,
cols: self.cols,
leading_dim: self.leading_dim,
layout: self.layout,
_marker: PhantomData,
}
}
}
fn check_lda(
rows: usize,
cols: usize,
layout: Layout,
leading_dim: usize,
buf_len: usize,
) -> Result<()> {
let (contiguous, strided) = match layout {
Layout::RowMajor => (cols, rows),
Layout::ColMajor => (rows, cols),
};
if leading_dim < contiguous {
return Err(Error::InvalidArgument(
"leading dimension is smaller than the contiguous dimension",
));
}
if rows == 0 || cols == 0 {
return Ok(());
}
let needed = (strided - 1)
.checked_mul(leading_dim)
.and_then(|v| v.checked_add(contiguous))
.ok_or(Error::DimensionOverflow)?;
if buf_len < needed {
return Err(Error::InvalidArgument(
"backing slice is too small for the declared rows × cols × leading_dim",
));
}
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn natural_leading_dim_row_major() {
let buf = vec![0.0_f32; 12];
let m = MatrixRef::new(&buf, 3, 4, Layout::RowMajor).unwrap();
assert_eq!(m.leading_dim(), 4);
assert_eq!(m.rows(), 3);
assert_eq!(m.cols(), 4);
}
#[test]
fn natural_leading_dim_col_major() {
let buf = vec![0.0_f32; 12];
let m = MatrixRef::new(&buf, 3, 4, Layout::ColMajor).unwrap();
assert_eq!(m.leading_dim(), 3);
}
#[test]
fn small_buffer_rejected() {
let buf = vec![0.0_f32; 11];
assert!(MatrixRef::new(&buf, 3, 4, Layout::RowMajor).is_err());
}
#[test]
fn lda_smaller_than_contiguous_rejected() {
let buf = vec![0.0_f32; 12];
assert!(
MatrixRef::with_lda(&buf, 3, 4, Layout::RowMajor, 3).is_err(),
"lda < cols in row-major must be rejected"
);
}
#[test]
fn padded_lda_accepted() {
let buf = vec![0.0_f32; 30]; let m = MatrixRef::with_lda(&buf, 3, 4, Layout::RowMajor, 10).unwrap();
assert_eq!(m.leading_dim(), 10);
}
}