forked from M-Labs/nalgebra
Unify separate (de)serialization helper structs by using Cow<'a, [T]>
This commit is contained in:
parent
3be81be2e3
commit
a8fa7f71c0
@ -1,6 +1,7 @@
|
||||
//! An implementation of the COO sparse matrix format.
|
||||
|
||||
use crate::SparseFormatError;
|
||||
use std::borrow::Cow;
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
@ -278,19 +279,19 @@ impl<T> CooMatrix<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Serialize)]
|
||||
struct CooMatrixSerializationData<'a, T> {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CooMatrixSerializationData<'a, T: Clone> {
|
||||
nrows: usize,
|
||||
ncols: usize,
|
||||
row_indices: &'a [usize],
|
||||
col_indices: &'a [usize],
|
||||
values: &'a [T],
|
||||
row_indices: Cow<'a, [usize]>,
|
||||
col_indices: Cow<'a, [usize]>,
|
||||
values: Cow<'a, [T]>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<T> Serialize for CooMatrix<T>
|
||||
where
|
||||
T: Serialize,
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -299,42 +300,31 @@ where
|
||||
CooMatrixSerializationData {
|
||||
nrows: self.nrows(),
|
||||
ncols: self.ncols(),
|
||||
row_indices: self.row_indices(),
|
||||
col_indices: self.col_indices(),
|
||||
values: self.values(),
|
||||
row_indices: self.row_indices().into(),
|
||||
col_indices: self.col_indices().into(),
|
||||
values: self.values().into(),
|
||||
}
|
||||
.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Deserialize)]
|
||||
struct CooMatrixDeserializationData<T> {
|
||||
nrows: usize,
|
||||
ncols: usize,
|
||||
row_indices: Vec<usize>,
|
||||
col_indices: Vec<usize>,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<'de, T> Deserialize<'de> for CooMatrix<T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
T: Deserialize<'de> + Clone,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<CooMatrix<T>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let de = CooMatrixDeserializationData::deserialize(deserializer)?;
|
||||
let de = CooMatrixSerializationData::deserialize(deserializer)?;
|
||||
CooMatrix::try_from_triplets(
|
||||
de.nrows,
|
||||
de.ncols,
|
||||
de.row_indices,
|
||||
de.col_indices,
|
||||
de.values,
|
||||
de.row_indices.into(),
|
||||
de.col_indices.into(),
|
||||
de.values.into(),
|
||||
)
|
||||
.map(|m| m.into())
|
||||
.map_err(|e| de::Error::custom(e))
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix};
|
||||
use crate::csr::CsrMatrix;
|
||||
use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter};
|
||||
use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind};
|
||||
use std::borrow::Cow;
|
||||
|
||||
use nalgebra::Scalar;
|
||||
use num_traits::One;
|
||||
@ -523,19 +524,19 @@ impl<T> CscMatrix<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Serialize)]
|
||||
struct CscMatrixSerializationData<'a, T> {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CscMatrixSerializationData<'a, T: Clone> {
|
||||
nrows: usize,
|
||||
ncols: usize,
|
||||
col_offsets: &'a [usize],
|
||||
row_indices: &'a [usize],
|
||||
values: &'a [T],
|
||||
col_offsets: Cow<'a, [usize]>,
|
||||
row_indices: Cow<'a, [usize]>,
|
||||
values: Cow<'a, [T]>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<T> Serialize for CscMatrix<T>
|
||||
where
|
||||
T: Serialize,
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -544,42 +545,31 @@ where
|
||||
CscMatrixSerializationData {
|
||||
nrows: self.nrows(),
|
||||
ncols: self.ncols(),
|
||||
col_offsets: self.col_offsets(),
|
||||
row_indices: self.row_indices(),
|
||||
values: self.values(),
|
||||
col_offsets: Cow::Borrowed(self.col_offsets()),
|
||||
row_indices: Cow::Borrowed(self.row_indices()),
|
||||
values: Cow::Borrowed(self.values()),
|
||||
}
|
||||
.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Deserialize)]
|
||||
struct CscMatrixDeserializationData<T> {
|
||||
nrows: usize,
|
||||
ncols: usize,
|
||||
col_offsets: Vec<usize>,
|
||||
row_indices: Vec<usize>,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<'de, T> Deserialize<'de> for CscMatrix<T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
T: Deserialize<'de> + Clone,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<CscMatrix<T>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let de = CscMatrixDeserializationData::deserialize(deserializer)?;
|
||||
let de = CscMatrixSerializationData::deserialize(deserializer)?;
|
||||
CscMatrix::try_from_csc_data(
|
||||
de.nrows,
|
||||
de.ncols,
|
||||
de.col_offsets,
|
||||
de.row_indices,
|
||||
de.values,
|
||||
de.col_offsets.into(),
|
||||
de.row_indices.into(),
|
||||
de.values.into(),
|
||||
)
|
||||
.map(|m| m.into())
|
||||
.map_err(|e| de::Error::custom(e))
|
||||
}
|
||||
}
|
||||
|
@ -6,6 +6,7 @@ use crate::cs::{CsLane, CsLaneIter, CsLaneIterMut, CsLaneMut, CsMatrix};
|
||||
use crate::csc::CscMatrix;
|
||||
use crate::pattern::{SparsityPattern, SparsityPatternFormatError, SparsityPatternIter};
|
||||
use crate::{SparseEntry, SparseEntryMut, SparseFormatError, SparseFormatErrorKind};
|
||||
use std::borrow::Cow;
|
||||
|
||||
use nalgebra::Scalar;
|
||||
use num_traits::One;
|
||||
@ -594,19 +595,19 @@ impl<T> CsrMatrix<T> {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Serialize)]
|
||||
struct CsrMatrixSerializationData<'a, T> {
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct CsrMatrixSerializationData<'a, T: Clone> {
|
||||
nrows: usize,
|
||||
ncols: usize,
|
||||
row_offsets: &'a [usize],
|
||||
col_indices: &'a [usize],
|
||||
values: &'a [T],
|
||||
row_offsets: Cow<'a, [usize]>,
|
||||
col_indices: Cow<'a, [usize]>,
|
||||
values: Cow<'a, [T]>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<T> Serialize for CsrMatrix<T>
|
||||
where
|
||||
T: Serialize,
|
||||
T: Serialize + Clone,
|
||||
{
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
@ -615,42 +616,31 @@ where
|
||||
CsrMatrixSerializationData {
|
||||
nrows: self.nrows(),
|
||||
ncols: self.ncols(),
|
||||
row_offsets: self.row_offsets(),
|
||||
col_indices: self.col_indices(),
|
||||
values: self.values(),
|
||||
row_offsets: Cow::Borrowed(self.row_offsets()),
|
||||
col_indices: Cow::Borrowed(self.col_indices()),
|
||||
values: Cow::Borrowed(self.values()),
|
||||
}
|
||||
.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Deserialize)]
|
||||
struct CsrMatrixDeserializationData<T> {
|
||||
nrows: usize,
|
||||
ncols: usize,
|
||||
row_offsets: Vec<usize>,
|
||||
col_indices: Vec<usize>,
|
||||
values: Vec<T>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<'de, T> Deserialize<'de> for CsrMatrix<T>
|
||||
where
|
||||
T: Deserialize<'de>,
|
||||
T: Deserialize<'de> + Clone,
|
||||
{
|
||||
fn deserialize<D>(deserializer: D) -> Result<CsrMatrix<T>, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let de = CsrMatrixDeserializationData::deserialize(deserializer)?;
|
||||
let de = CsrMatrixSerializationData::deserialize(deserializer)?;
|
||||
CsrMatrix::try_from_csr_data(
|
||||
de.nrows,
|
||||
de.ncols,
|
||||
de.row_offsets,
|
||||
de.col_indices,
|
||||
de.values,
|
||||
de.row_offsets.into(),
|
||||
de.col_indices.into(),
|
||||
de.values.into(),
|
||||
)
|
||||
.map(|m| m.into())
|
||||
.map_err(|e| de::Error::custom(e))
|
||||
}
|
||||
}
|
||||
|
@ -1,6 +1,7 @@
|
||||
//! Sparsity patterns for CSR and CSC matrices.
|
||||
use crate::cs::transpose_cs;
|
||||
use crate::SparseFormatError;
|
||||
use std::borrow::Cow;
|
||||
use std::error::Error;
|
||||
use std::fmt;
|
||||
|
||||
@ -293,12 +294,12 @@ pub enum SparsityPatternFormatError {
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Serialize)]
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct SparsityPatternSerializationData<'a> {
|
||||
major_dim: usize,
|
||||
minor_dim: usize,
|
||||
major_offsets: &'a [usize],
|
||||
minor_indices: &'a [usize],
|
||||
major_offsets: Cow<'a, [usize]>,
|
||||
minor_indices: Cow<'a, [usize]>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
@ -310,36 +311,26 @@ impl Serialize for SparsityPattern {
|
||||
SparsityPatternSerializationData {
|
||||
major_dim: self.major_dim(),
|
||||
minor_dim: self.minor_dim(),
|
||||
major_offsets: self.major_offsets(),
|
||||
minor_indices: self.minor_indices(),
|
||||
major_offsets: Cow::Borrowed(self.major_offsets()),
|
||||
minor_indices: Cow::Borrowed(self.minor_indices()),
|
||||
}
|
||||
.serialize(serializer)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
#[derive(Deserialize)]
|
||||
struct SparsityPatternDeserializationData {
|
||||
major_dim: usize,
|
||||
minor_dim: usize,
|
||||
major_offsets: Vec<usize>,
|
||||
minor_indices: Vec<usize>,
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde-serialize")]
|
||||
impl<'de> Deserialize<'de> for SparsityPattern {
|
||||
fn deserialize<D>(deserializer: D) -> Result<SparsityPattern, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let de = SparsityPatternDeserializationData::deserialize(deserializer)?;
|
||||
let de = SparsityPatternSerializationData::deserialize(deserializer)?;
|
||||
SparsityPattern::try_from_offsets_and_indices(
|
||||
de.major_dim,
|
||||
de.minor_dim,
|
||||
de.major_offsets,
|
||||
de.minor_indices,
|
||||
de.major_offsets.into(),
|
||||
de.minor_indices.into(),
|
||||
)
|
||||
.map(|m| m.into())
|
||||
.map_err(|e| de::Error::custom(e))
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user