Hide MatrixMarketScalar implementation details for now
The existing MatrixMarketScalar is relatively closely tied to the way oru parser is implemented. I've moved these internals into an internal trait and sealed the public trait. This makes it less flexible for users for now, but gives us more freedom to change internals in the future.
This commit is contained in:
parent
93f3d60005
commit
e3d1119bff
|
@ -4,7 +4,6 @@
|
||||||
use crate::coo::CooMatrix;
|
use crate::coo::CooMatrix;
|
||||||
use crate::SparseFormatError;
|
use crate::SparseFormatError;
|
||||||
use crate::SparseFormatErrorKind;
|
use crate::SparseFormatErrorKind;
|
||||||
use nalgebra::base::Scalar;
|
|
||||||
use nalgebra::Complex;
|
use nalgebra::Complex;
|
||||||
use pest::iterators::Pairs;
|
use pest::iterators::Pairs;
|
||||||
use pest::Parser;
|
use pest::Parser;
|
||||||
|
@ -486,25 +485,38 @@ fn typecode_precheck(tc: &Typecode) -> Result<(), MatrixMarketError> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Scalar types supported by the matrix market parser.
|
/// Scalar types supported by the matrix market parser.
|
||||||
pub trait MatrixMarketScalar: Scalar {
|
mod internal {
|
||||||
/// When the matrix is an integer matrix, it will convert a [i128] number to this type.
|
use crate::io::MatrixMarketError;
|
||||||
fn from_i128(i: i128) -> Result<Self, MatrixMarketError>;
|
use na::{Complex, Scalar};
|
||||||
/// When matrix is a Real matrix, it will convert a [f64] number to this type.
|
|
||||||
fn from_f64(f: f64) -> Result<Self, MatrixMarketError>;
|
pub trait SupportedMatrixMarketScalar: Scalar {
|
||||||
/// When matrix is a Complx matrix, it will convert a [Complex<f64>] number to this type.
|
/// When the matrix is an integer matrix, it will convert a [i128] number to this type.
|
||||||
fn from_c64(c: Complex<f64>) -> Result<Self, MatrixMarketError>;
|
fn from_i128(i: i128) -> Result<Self, MatrixMarketError>;
|
||||||
/// When matrix is a Pattern matrix, it will convert a unit type [unit] to this type.
|
/// When matrix is a Real matrix, it will convert a [f64] number to this type.
|
||||||
fn from_pattern(p: ()) -> Result<Self, MatrixMarketError>;
|
fn from_f64(f: f64) -> Result<Self, MatrixMarketError>;
|
||||||
/// When matrix is a Skew-symmetric matrix, it will convert itself to its negative.
|
/// When matrix is a Complx matrix, it will convert a [Complex<f64>] number to this type.
|
||||||
fn negative(self) -> Result<Self, MatrixMarketError>;
|
fn from_c64(c: Complex<f64>) -> Result<Self, MatrixMarketError>;
|
||||||
/// When matrix is a Hermitian matrix, it will convert itself to its conjugate.
|
/// When matrix is a Pattern matrix, it will convert a unit type [unit] to this type.
|
||||||
fn conjugate(self) -> Result<Self, MatrixMarketError>;
|
fn from_pattern(p: ()) -> Result<Self, MatrixMarketError>;
|
||||||
|
/// When matrix is a Skew-symmetric matrix, it will convert itself to its negative.
|
||||||
|
fn negative(self) -> Result<Self, MatrixMarketError>;
|
||||||
|
/// When matrix is a Hermitian matrix, it will convert itself to its conjugate.
|
||||||
|
fn conjugate(self) -> Result<Self, MatrixMarketError>;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// A marker trait for supported matrix market scalars.
|
||||||
|
///
|
||||||
|
/// This is a sealed trait; it cannot be implemented by external crates. This is done in order to prevent leaking
|
||||||
|
/// some of the implementation details we currently rely on. We may relax this restriction in the future.
|
||||||
|
pub trait MatrixMarketScalar: internal::SupportedMatrixMarketScalar {}
|
||||||
|
|
||||||
/// Implement MatrixMarketScalar for primitive integer types.
|
/// Implement MatrixMarketScalar for primitive integer types.
|
||||||
macro_rules! mm_int_impl {
|
macro_rules! mm_int_impl {
|
||||||
($T:ty) => {
|
($T:ty) => {
|
||||||
impl MatrixMarketScalar for $T {
|
impl MatrixMarketScalar for $T {}
|
||||||
|
|
||||||
|
impl internal::SupportedMatrixMarketScalar for $T {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_i128(i: i128) -> Result<Self, MatrixMarketError> {
|
fn from_i128(i: i128) -> Result<Self, MatrixMarketError> {
|
||||||
Ok(Self::try_from(i)?)
|
Ok(Self::try_from(i)?)
|
||||||
|
@ -547,7 +559,9 @@ macro_rules! mm_int_impl {
|
||||||
/// Implement MatrixMarketScalar for primitive real types.
|
/// Implement MatrixMarketScalar for primitive real types.
|
||||||
macro_rules! mm_real_impl {
|
macro_rules! mm_real_impl {
|
||||||
($T:ty) => {
|
($T:ty) => {
|
||||||
impl MatrixMarketScalar for $T {
|
impl MatrixMarketScalar for $T {}
|
||||||
|
|
||||||
|
impl internal::SupportedMatrixMarketScalar for $T {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_i128(_i: i128) -> Result<Self, MatrixMarketError> {
|
fn from_i128(_i: i128) -> Result<Self, MatrixMarketError> {
|
||||||
Err(MatrixMarketError::from_kind_and_message(
|
Err(MatrixMarketError::from_kind_and_message(
|
||||||
|
@ -591,7 +605,9 @@ macro_rules! mm_real_impl {
|
||||||
/// Implement MatrixMarketScalar for primitive complex types.
|
/// Implement MatrixMarketScalar for primitive complex types.
|
||||||
macro_rules! mm_complex_impl {
|
macro_rules! mm_complex_impl {
|
||||||
($T:ty) => {
|
($T:ty) => {
|
||||||
impl MatrixMarketScalar for Complex<$T> {
|
impl MatrixMarketScalar for Complex<$T> {}
|
||||||
|
|
||||||
|
impl internal::SupportedMatrixMarketScalar for Complex<$T> {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_i128(_i: i128) -> Result<Self, MatrixMarketError> {
|
fn from_i128(_i: i128) -> Result<Self, MatrixMarketError> {
|
||||||
Err(MatrixMarketError::from_kind_and_message(
|
Err(MatrixMarketError::from_kind_and_message(
|
||||||
|
@ -634,7 +650,9 @@ macro_rules! mm_complex_impl {
|
||||||
/// Implement MatrixMarketScalar for primitive unit types.
|
/// Implement MatrixMarketScalar for primitive unit types.
|
||||||
macro_rules! mm_pattern_impl {
|
macro_rules! mm_pattern_impl {
|
||||||
($T:ty) => {
|
($T:ty) => {
|
||||||
impl MatrixMarketScalar for $T {
|
impl MatrixMarketScalar for $T {}
|
||||||
|
|
||||||
|
impl internal::SupportedMatrixMarketScalar for $T {
|
||||||
#[inline]
|
#[inline]
|
||||||
fn from_i128(_i: i128) -> Result<Self, MatrixMarketError> {
|
fn from_i128(_i: i128) -> Result<Self, MatrixMarketError> {
|
||||||
Err(MatrixMarketError::from_kind_and_message(
|
Err(MatrixMarketError::from_kind_and_message(
|
||||||
|
|
Loading…
Reference in New Issue