peroxide/util/writer.rs
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
//! More convenient matrix writer
pub use self::ToWriter::{Data, Header, Path, Round};
use std::collections::HashMap;
//use std::fs::File;
//use std::io::Write;
//use std::process::exit;
use crate::structure::matrix::Matrix;
#[derive(Debug, Clone, Copy, Hash, PartialOrd, PartialEq, Eq)]
pub enum ToWriter {
Header,
Round,
Data,
Path,
}
#[derive(Debug, Clone, Copy)]
pub enum Queue {
Matrix,
Vector,
}
/// Simple Matrix Writer
///
/// # Necessary Fields
/// * `data: Vec<Matrix>`
/// * `path: String`
///
/// # Option
/// * `header: Vec<String>`
/// * `round: u8`
#[derive(Debug, Clone)]
pub struct SimpleWriter {
header: Vec<String>,
round: u8,
matrices: Vec<Matrix>,
vectors: Vec<Vec<f64>>,
path: String,
queue: Vec<Queue>,
to_write: HashMap<ToWriter, bool>,
}
impl SimpleWriter {
pub fn new() -> SimpleWriter {
let mut default_to_write: HashMap<ToWriter, bool> = HashMap::new();
default_to_write.insert(Header, false);
default_to_write.insert(Round, false);
default_to_write.insert(Data, false);
default_to_write.insert(Path, false);
SimpleWriter {
header: vec![],
round: 0,
matrices: vec![],
vectors: vec![],
path: "".to_string(),
queue: vec![],
to_write: default_to_write,
}
}
pub fn insert_header(&mut self, head: Vec<&str>) -> &mut Self {
if let Some(x) = self.to_write.get_mut(&Header) {
*x = true
}
self.header = head
.into_iter()
.map(|t| t.to_owned())
.collect::<Vec<String>>();
self
}
pub fn set_round_level(&mut self, nth: u8) -> &mut Self {
if let Some(x) = self.to_write.get_mut(&Round) {
*x = true
}
self.round = nth;
self
}
pub fn insert_matrix(&mut self, mat: Matrix) -> &mut Self {
if let Some(x) = self.to_write.get_mut(&Data) {
*x = true
}
self.matrices.push(mat);
self.queue.push(Queue::Matrix);
self
}
pub fn insert_vector(&mut self, vec: Vec<f64>) -> &mut Self {
if let Some(x) = self.to_write.get_mut(&Data) {
*x = true
}
self.vectors.push(vec);
self.queue.push(Queue::Vector);
self
}
pub fn set_path(&mut self, path: &str) -> &mut Self {
if let Some(x) = self.to_write.get_mut(&Path) {
*x = true
}
self.path = path.to_owned();
self
}
pub fn write_csv(self) {
unimplemented!()
}
// pub fn write_pickle(&self) {
// let mut writer: Box<dyn Write>;
// // Error handling - Path
// if let Some(p) = self.to_write.get(&Path) {
// assert!(*p, "No determined path!");
// }
// // Error handling - Data
// if let Some(dat) = self.to_write.get(&Data) {
// assert!(*dat, "No inserted data!");
// }
// match File::create(self.path.clone()) {
// Ok(p) => writer = Box::new(p),
// Err(e) => {
// println!("{:?}", e);
// exit(1);
// }
// }
// if let Some(head) = self.to_write.get(&Header) {
// if *head {
// serde_pickle::to_writer(&mut writer, &self.header, true)
// .expect("Can't write header to pickle");
// }
// }
// let mut queued = self.queue.clone().into_iter();
// let mut matrices = self.matrices.clone().into_iter();
// let mut vectors = self.vectors.clone().into_iter();
// loop {
// match queued.next() {
// Some(Queue::Matrix) => {
// let mat = matrices.next().unwrap();
// mat.write_pickle(&mut writer)
// .expect("Can't insert matrices");
// }
// Some(Queue::Vector) => {
// let vec = vectors.next().unwrap();
// vec.write_pickle(&mut writer).expect("Can't insert vectors");
// }
// None => return,
// }
// }
// }
}