1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#![allow(deprecated)]

use std::mem;

use cast::From as _0;

use crate::traits::Data;

macro_rules! impl_data {
    ($($ty:ty),+) => {
        $(
            impl Data for $ty {
                fn f64(self) -> f64 {
                    f64::cast(self)
                }
            }

            impl<'a> Data for &'a $ty {
                fn f64(self) -> f64 {
                    f64::cast(*self)
                }
            }
        )+
    }
}

impl_data!(f32, f64, i16, i32, i64, i8, isize, u16, u32, u64, u8, usize);

#[derive(Clone)]
pub struct Matrix {
    bytes: Vec<u8>,
    ncols: usize,
    nrows: usize,
}

impl Matrix {
    pub fn new<I>(rows: I, scale: <I::Item as Row>::Scale) -> Matrix
    where
        I: Iterator,
        I::Item: Row,
    {
        let ncols = I::Item::ncols();
        let bytes_per_row = ncols * mem::size_of::<f64>();
        let mut bytes = Vec::with_capacity(rows.size_hint().0 * bytes_per_row);

        let mut nrows = 0;
        for row in rows {
            nrows += 1;
            row.append_to(&mut bytes, scale);
        }

        Matrix {
            bytes,
            ncols,
            nrows,
        }
    }

    pub fn bytes(&self) -> &[u8] {
        &self.bytes
    }

    pub fn ncols(&self) -> usize {
        self.ncols
    }

    pub fn nrows(&self) -> usize {
        self.nrows
    }
}

/// Data that can serve as a row of the data matrix
pub trait Row {
    /// Private
    type Scale: Copy;

    /// Append this row to a buffer
    fn append_to(self, buffer: &mut Vec<u8>, scale: Self::Scale);
    /// Number of columns of the row
    fn ncols() -> usize;
}

fn write_f64(w: &mut impl std::io::Write, f: f64) -> std::io::Result<()> {
    w.write_all(&f.to_bits().to_le_bytes())
}

impl<A, B> Row for (A, B)
where
    A: Data,
    B: Data,
{
    type Scale = (f64, f64);

    fn append_to(self, buffer: &mut Vec<u8>, scale: (f64, f64)) {
        let (a, b) = self;

        write_f64(buffer, a.f64() * scale.0).unwrap();
        write_f64(buffer, b.f64() * scale.1).unwrap();
    }

    fn ncols() -> usize {
        2
    }
}

impl<A, B, C> Row for (A, B, C)
where
    A: Data,
    B: Data,
    C: Data,
{
    type Scale = (f64, f64, f64);

    fn append_to(self, buffer: &mut Vec<u8>, scale: (f64, f64, f64)) {
        let (a, b, c) = self;

        write_f64(buffer, a.f64() * scale.0).unwrap();
        write_f64(buffer, b.f64() * scale.1).unwrap();
        write_f64(buffer, c.f64() * scale.2).unwrap();
    }

    fn ncols() -> usize {
        3
    }
}

impl<A, B, C, D> Row for (A, B, C, D)
where
    A: Data,
    B: Data,
    C: Data,
    D: Data,
{
    type Scale = (f64, f64, f64, f64);

    fn append_to(self, buffer: &mut Vec<u8>, scale: (f64, f64, f64, f64)) {
        let (a, b, c, d) = self;

        write_f64(buffer, a.f64() * scale.0).unwrap();
        write_f64(buffer, b.f64() * scale.1).unwrap();
        write_f64(buffer, c.f64() * scale.2).unwrap();
        write_f64(buffer, d.f64() * scale.3).unwrap();
    }

    fn ncols() -> usize {
        4
    }
}

impl<A, B, C, D, E> Row for (A, B, C, D, E)
where
    A: Data,
    B: Data,
    C: Data,
    D: Data,
    E: Data,
{
    type Scale = (f64, f64, f64, f64, f64);

    #[cfg_attr(feature = "cargo-clippy", allow(clippy::many_single_char_names))]
    fn append_to(self, buffer: &mut Vec<u8>, scale: (f64, f64, f64, f64, f64)) {
        let (a, b, c, d, e) = self;

        write_f64(buffer, a.f64() * scale.0).unwrap();
        write_f64(buffer, b.f64() * scale.1).unwrap();
        write_f64(buffer, c.f64() * scale.2).unwrap();
        write_f64(buffer, d.f64() * scale.3).unwrap();
        write_f64(buffer, e.f64() * scale.4).unwrap();
    }

    fn ncols() -> usize {
        5
    }
}