src/sets/cube.rs

branch
dev
changeset 86
d5b0e496b72f
parent 55
7b2ee3e84c5f
equal deleted inserted replaced
76:99ad55974e62 86:d5b0e496b72f
14 # use alg_tools::types::float; 14 # use alg_tools::types::float;
15 let cube : Cube<float, 2> = [[0.0, 1.0], [-1.0, 1.0]].into(); 15 let cube : Cube<float, 2> = [[0.0, 1.0], [-1.0, 1.0]].into();
16 ``` 16 ```
17 */ 17 */
18 18
19 use serde::ser::{Serialize, Serializer, SerializeTupleStruct}; 19 use serde::{Serialize, Deserialize};
20 use crate::types::*; 20 use crate::types::*;
21 use crate::loc::Loc; 21 use crate::loc::Loc;
22 use crate::sets::SetOrd; 22 use crate::sets::SetOrd;
23 use crate::maputil::{ 23 use crate::maputil::{
24 FixedLength, 24 FixedLength,
28 map2, 28 map2,
29 }; 29 };
30 30
31 /// A multi-dimensional cube $∏_{i=1}^N [a_i, b_i)$ with the starting and ending points 31 /// A multi-dimensional cube $∏_{i=1}^N [a_i, b_i)$ with the starting and ending points
32 /// along $a_i$ and $b_i$ along each dimension of type `U`. 32 /// along $a_i$ and $b_i$ along each dimension of type `U`.
33 #[derive(Copy, Clone, Debug, Eq, PartialEq)] 33 #[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
34 pub struct Cube<U : Num, const N : usize>(pub(super) [[U; 2]; N]); 34 #[serde(bound(
35 35 serialize = "[[U; 2]; N] : Serialize",
36 // Need to manually implement as [F; N] serialisation is provided only for some N. 36 deserialize = "[[U; 2]; N] : for<'a> Deserialize<'a>",
37 impl<F : Num + Serialize, const N : usize> Serialize for Cube<F, N> 37 ))]
38 where 38 pub struct Cube<U : Num, const N : usize>(
39 F: Serialize, 39 pub(super) [[U; 2]; N]
40 { 40 );
41 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
42 where
43 S: Serializer,
44 {
45 let mut ts = serializer.serialize_tuple_struct("Cube", N)?;
46 for e in self.0.iter() {
47 ts.serialize_field(e)?;
48 }
49 ts.end()
50 }
51 }
52 41
53 impl<A : Num, const N : usize> FixedLength<N> for Cube<A,N> { 42 impl<A : Num, const N : usize> FixedLength<N> for Cube<A,N> {
54 type Iter = std::array::IntoIter<[A; 2], N>; 43 type Iter = std::array::IntoIter<[A; 2], N>;
55 type Elem = [A; 2]; 44 type Elem = [A; 2];
56 #[inline] 45 #[inline]

mercurial