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
pub use libcsv::{Reader, Writer, Error};
use rustc_serialize::{Decodable, Encodable};
use std::io::{Read, Write};
use super::super::matrix::{Matrix, BaseMatrix};
impl<T> Matrix<T> where T: Decodable {
pub fn read_csv<'a, R: Read>(mut reader: Reader<R>)
-> Result<Matrix<T>, Error> {
let header: Vec<String> = try!(reader.headers());
let mut nrows = 0;
let ncols = header.len();
let mut records: Vec<T> = vec![];
for record in reader.decode() {
let values: Vec<T> = try!(record);
records.extend(values);
nrows += 1;
}
Ok(Matrix::new(nrows, ncols, records))
}
}
impl<T> Matrix<T> where T: Encodable {
pub fn write_csv<W: Write>(&self, writer: &mut Writer<W>)
-> Result<(), Error> {
for row in self.row_iter() {
try!(writer.encode(row.raw_slice()));
}
Ok(())
}
}