11 use crate::euclidean::*; |
11 use crate::euclidean::*; |
12 use crate::norms::*; |
12 use crate::norms::*; |
13 use crate::linops::{AXPY, Mapping, Linear}; |
13 use crate::linops::{AXPY, Mapping, Linear}; |
14 use crate::instance::{Instance, BasicDecomposition}; |
14 use crate::instance::{Instance, BasicDecomposition}; |
15 use crate::mapping::Space; |
15 use crate::mapping::Space; |
16 use serde::ser::{Serialize, Serializer, SerializeSeq}; |
16 use serde::{Serialize, Deserialize}; |
17 |
17 |
18 |
18 |
19 /// A container type for (short) `N`-dimensional vectors of element type `F`. |
19 /// A container type for (short) `N`-dimensional vectors of element type `F`. |
20 /// |
20 /// |
21 /// Supports basic operations of an [`Euclidean`] space, several [`Norm`]s, and |
21 /// Supports basic operations of an [`Euclidean`] space, several [`Norm`]s, and |
22 /// fused [`AXPY`] operations, among others. |
22 /// fused [`AXPY`] operations, among others. |
23 #[derive(Copy,Clone,Debug,PartialEq,Eq)] |
23 #[derive(Copy,Clone,Debug,PartialEq,Eq,Serialize,Deserialize)] |
|
24 #[serde(bound( |
|
25 serialize = "[F; N] : Serialize", |
|
26 deserialize = "[F; N] : for<'a> Deserialize<'a>" |
|
27 ))] |
24 pub struct Loc<F, const N : usize>( |
28 pub struct Loc<F, const N : usize>( |
25 /// An array of the elements of the vector |
29 /// An array of the elements of the vector |
26 pub [F; N] |
30 pub [F; N] |
27 ); |
31 ); |
28 |
32 |
34 for e in self.iter() { |
38 for e in self.iter() { |
35 write!(f, "{comma}{e}")?; |
39 write!(f, "{comma}{e}")?; |
36 comma = ", "; |
40 comma = ", "; |
37 } |
41 } |
38 write!(f, "]") |
42 write!(f, "]") |
39 } |
|
40 } |
|
41 |
|
42 // Need to manually implement as [F; N] serialisation is provided only for some N. |
|
43 impl<F, const N : usize> Serialize for Loc<F, N> |
|
44 where |
|
45 F: Serialize, |
|
46 { |
|
47 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
|
48 where |
|
49 S: Serializer, |
|
50 { |
|
51 let mut seq = serializer.serialize_seq(Some(N))?; |
|
52 for e in self.iter() { |
|
53 seq.serialize_element(e)?; |
|
54 } |
|
55 seq.end() |
|
56 } |
43 } |
57 } |
44 } |
58 |
45 |
59 impl<F, const N : usize> Loc<F, N> { |
46 impl<F, const N : usize> Loc<F, N> { |
60 /// Creates a new `Loc` vector from an array. |
47 /// Creates a new `Loc` vector from an array. |