Skip to content

Instantly share code, notes, and snippets.

@JordiPolo
Created November 16, 2017 12:10
Show Gist options
  • Save JordiPolo/f09cb74efd483a94f51b132b2489b534 to your computer and use it in GitHub Desktop.
Save JordiPolo/f09cb74efd483a94f51b132b2489b534 to your computer and use it in GitHub Desktop.
use std::hash::Hash;
#[derive(Clone)]
pub struct Indexer<U: Clone + Hash> {
pub values: Vec<U>,
htable: RefCell<HashMap<U, usize>>,
}
impl<U> Indexer<U>
where
U: Clone + Eq + Hash,
{
pub fn new(values: Vec<U>) -> Self {
Indexer {
values: values,
htable: RefCell::new(HashMap::new()),
}
}
}
impl<T: Clone + Eq + Hash> From<Vec<T>> for Indexer<T> {
fn from(values: Vec<T>) -> Self {
Indexer::new(values)
}
}
impl<T: Clone + Eq + Hash> Into<Vec<T>> for Indexer<T> {
fn into(self) -> Vec<T> {
self.values
}
}
// DataFrameBuider
pub struct DataFrameBuilder<I, C>
where I: Clone + Eq + Hash,
C: Clone + Eq + Hash
{
index: Option<Indexer<I>>,
columns: Option<Indexer<C>>,
}
impl<I, C> Default for DataFrameBuilder<I, C>
where
I: Clone + Eq + Hash,
C: Clone + Eq + Hash,
{
fn default() -> Self {
DataFrameBuilder {
index: None,
columns: None
}
}
}
impl<I, C> DataFrameBuilder<I, C>
where
I: Clone + Eq + Hash,
C: Clone + Eq + Hash,
{
pub fn new() -> Self {
DataFrameBuilder::default()
}
pub fn index<X>(&mut self, index: X) -> &mut Self
where
X: Into<Indexer<I>>
{
self.index = Some(index.into());
self
}
pub fn columns<X>(&mut self, columns: X) -> &mut Self
where
X: Into<Indexer<C>>
{
self.columns = Some(columns.into());
self
}
pub fn with_values(&self, values: Vec<Array>) -> DataFrame<I,C> {
self.columns = self.columns.or_else(||{
let columns: Vec<usize> = (0..values.len()).collect();
Some(columns.into()) // ERROR HERE
});
self.index = self.index.or_else(||{
let index: Vec<usize> = (0..values[0].len()).collect();
Some(index.into())
});
DataFrame::from_vec(values, self.index.unwrap(), self.columns.unwrap())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment