Skip to content

Commit

Permalink
add i64 and f32 numeric types
Browse files Browse the repository at this point in the history
  • Loading branch information
benpinno committed Nov 13, 2023
1 parent 45028b0 commit 174113f
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 9 deletions.
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.

64 changes: 58 additions & 6 deletions charming/src/datatype/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use super::CompositeValue;
#[serde(untagged)]
pub enum DataSource {
Integers(Vec<Vec<i32>>),
Floats(Vec<Vec<f64>>),
BigIntegers(Vec<Vec<i64>>),
Floats(Vec<Vec<f32>>),
BigFloats(Vec<Vec<f64>>),
Mixed(Vec<Vec<CompositeValue>>),
}

Expand All @@ -16,9 +18,21 @@ impl From<Vec<Vec<i32>>> for DataSource {
}
}

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

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

impl From<Vec<Vec<f64>>> for DataSource {
fn from(v: Vec<Vec<f64>>) -> Self {
DataSource::Floats(v)
DataSource::BigFloats(v)
}
}

Expand Down Expand Up @@ -55,10 +69,22 @@ mod test {
assert_eq!(n, NumericValue::Integer(42));
}

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

#[test]
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();
assert_eq!(n, NumericValue::Float(0.618));
assert_eq!(n, NumericValue::BigFloat(0.618));
}

#[test]
Expand All @@ -79,18 +105,44 @@ mod test {
assert_eq!(ds, DataSource::Integers(vec![vec![1, 2, 3], vec![4, 5, 6]]));
}

#[test]
fn data_frame_from_bigintegers() {
let ds: DataSource = vec![vec![1i64, 2i64, 3i64], vec![4i64, 5i64, 6i64]].into();
assert_eq!(
ds,
DataSource::BigIntegers(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.0f32, 2.0f32, 3.0f32],
vec![4.0f32, 5.0f32, 6.0f32]
])
);
}

#[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::BigFloats(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
18 changes: 16 additions & 2 deletions charming/src/datatype/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ use serde::{Deserialize, Serialize};
#[serde(untagged)]
pub enum NumericValue {
Integer(i32),
Float(f64),
BigInteger(i64),
Float(f32),
BigFloat(f64),
}

impl From<i32> for NumericValue {
Expand All @@ -13,9 +15,21 @@ impl From<i32> for NumericValue {
}
}

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

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

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

Expand Down

0 comments on commit 174113f

Please sign in to comment.