Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add i64 and f32 numeric types #26

Merged
merged 2 commits into from
Nov 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 69 additions & 6 deletions charming/src/datatype/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,37 @@ use super::CompositeValue;
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize)]
#[serde(untagged)]
pub enum DataSource {
Integers(Vec<Vec<i32>>),
Integers(Vec<Vec<i64>>),
Floats(Vec<Vec<f64>>),
Mixed(Vec<Vec<CompositeValue>>),
}

impl From<Vec<Vec<i32>>> for DataSource {
fn from(v: Vec<Vec<i32>>) -> Self {
let t: Vec<Vec<i64>> = v
.iter()
.map(|x| x.iter().map(|y| *y as i64).collect())
.collect();
DataSource::Integers(t)
}
}

impl From<Vec<Vec<i64>>> for DataSource {
fn from(v: Vec<Vec<i64>>) -> Self {
DataSource::Integers(v)
}
}

impl From<Vec<Vec<f32>>> for DataSource {
fn from(v: Vec<Vec<f32>>) -> Self {
let t: Vec<Vec<f64>> = v
.iter()
.map(|x| x.iter().map(|y| *y as f64).collect())
.collect();
DataSource::Floats(t)
}
}

impl From<Vec<Vec<f64>>> for DataSource {
fn from(v: Vec<Vec<f64>>) -> Self {
DataSource::Floats(v)
Expand Down Expand Up @@ -45,6 +65,7 @@ macro_rules! ds {

#[cfg(test)]
mod test {

use crate::datatype::NumericValue;

use super::*;
Expand All @@ -55,6 +76,19 @@ mod test {
assert_eq!(n, NumericValue::Integer(42));
}

#[test]
fn numeric_value_from_i64() {
let n: NumericValue = 42i64.into();
assert_eq!(n, NumericValue::Integer(42));
}

#[test]
#[should_panic]
fn numeric_value_from_f32() {
let n: NumericValue = 0.618f32.into();
assert_eq!(n, NumericValue::Float(0.618));
}

#[test]
fn numeric_value_from_f64() {
let n: NumericValue = 0.618f64.into();
Expand All @@ -75,22 +109,51 @@ mod test {

#[test]
fn data_frame_from_integers() {
let ds: DataSource = vec![vec![1, 2, 3], vec![4, 5, 6]].into();
assert_eq!(ds, DataSource::Integers(vec![vec![1, 2, 3], vec![4, 5, 6]]));
let ds: DataSource = vec![vec![1i32, 2i32, 3i32], vec![4i32, 5i32, 6i32]].into();
assert_eq!(
ds,
DataSource::Integers(vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]])
);
}

#[test]
fn data_frame_from_bigintegers() {
let ds: DataSource = vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]].into();
assert_eq!(
ds,
DataSource::Integers(vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]])
);
}

#[test]
fn data_frame_from_floats() {
let ds: DataSource = vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]].into();
let ds: DataSource =
vec![vec![1.0f32, 2.0f32, 3.0f32], vec![4.0f32, 5.0f32, 6.0f32]].into();
assert_eq!(
ds,
DataSource::Floats(vec![vec![1.0, 2.0, 3.0], vec![4.0, 5.0, 6.0]])
DataSource::Floats(vec![
vec![1.0f64, 2.0f64, 3.0f64],
vec![4.0f64, 5.0f64, 6.0f64]
])
);
}

#[test]
fn data_frame_from_bigfloats() {
let ds: DataSource =
vec![vec![1.0f64, 2.0f64, 3.0f64], vec![4.0f64, 5.0f64, 6.0f64]].into();
assert_eq!(
ds,
DataSource::Floats(vec![
vec![1.0f64, 2.0f64, 3.0f64],
vec![4.0f64, 5.0f64, 6.0f64]
])
);
}

#[test]
fn data_frame_from_mixed() {
let ds = ds!([1, "Tuesday", 3.0], ["Monday", 2, "Wednesday"]);
let ds = ds!([1i32, "Tuesday", 3.0f32], ["Monday", 2i32, "Wednesday"]);
assert_eq!(
ds,
DataSource::Mixed(vec![
Expand Down
14 changes: 13 additions & 1 deletion charming/src/datatype/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,28 @@ use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, PartialOrd, Serialize, Deserialize)]
#[serde(untagged)]
pub enum NumericValue {
Integer(i32),
Integer(i64),
Float(f64),
}

impl From<i32> for NumericValue {
fn from(n: i32) -> Self {
NumericValue::Integer(n as i64)
}
}

impl From<i64> for NumericValue {
fn from(n: i64) -> Self {
NumericValue::Integer(n)
}
}

impl From<f32> for NumericValue {
fn from(n: f32) -> Self {
NumericValue::Float(n as f64)
}
}

impl From<f64> for NumericValue {
fn from(n: f64) -> Self {
NumericValue::Float(n)
Expand Down