use crate::compiler::value::values::Value;
use crate::store::csv::CsvFunctionValueCursor;
use std::fmt::Debug;
use std::io;
use tracing::debug;
#[derive(Debug)]
pub struct VectorFunctionCursor {
domain: Vec<Vec<Value>>,
codomain: Vec<Vec<Value>>,
index: usize,
function_name: String,
}
#[derive(Debug)]
pub enum FunctionCursor {
Vector(VectorFunctionCursor),
Csv(CsvFunctionValueCursor),
}
impl FunctionCursorMethods for FunctionCursor {
fn next(&mut self) -> Option<Vec<Value>> {
match self {
FunctionCursor::Vector(v) => v.next(),
FunctionCursor::Csv(v) => v.next(),
}
}
fn reset(&mut self) {
return match self {
FunctionCursor::Vector(v) => v.reset(),
FunctionCursor::Csv(v) => v.reset(),
};
}
}
pub trait FunctionCursorMethods {
fn next(&mut self) -> Option<Vec<Value>>;
fn reset(&mut self);
}
impl VectorFunctionCursor {
pub fn new(domain: Vec<Vec<Value>>, codomain: Vec<Vec<Value>>, name: &str) -> Self {
VectorFunctionCursor {
domain,
codomain,
index: 0,
function_name: name.to_string(),
}
}
pub fn empty() -> Self {
VectorFunctionCursor {
domain: vec![],
codomain: vec![],
index: 0,
function_name: "".to_string(),
}
}
}
impl FunctionCursorMethods for VectorFunctionCursor {
fn next(&mut self) -> Option<Vec<Value>> {
if self.index >= self.domain.len() {
return None;
}
let mut result = vec![];
for dom_val in self.domain.get(self.index).unwrap() {
result.push(dom_val.clone());
}
for codom_val in self.codomain.get(self.index).unwrap() {
result.push(codom_val.clone());
}
self.index += 1;
let result = Some(result);
result
}
fn reset(&mut self) {
debug!("Function cursor for {} resetting", self.function_name);
self.index = 0;
}
}