Skip to content

Discrete Refactoring #89

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

Merged
merged 8 commits into from
Dec 1, 2019
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
1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,6 @@ rand_distr = "0.2.2"
criterion = "0.3.0"
rayon = "1.2.0"
rand_xorshift = "0.2.0"
cpuprofiler = "*"

[target.'cfg(target_arch = "wasm32")'.dev-dependencies]
wasm-bindgen-test = "0.3.4"
Expand Down
3 changes: 1 addition & 2 deletions examples/histogram.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,12 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.y_label_area_size(40)
.margin(5)
.caption("Histogram Test", ("sans-serif", 50.0).into_font())
.build_ranged(0u32..10u32, 0u32..10u32)?;
.build_ranged((0u32..10u32).into_centric(), 0u32..10u32)?;

chart
.configure_mesh()
.disable_x_mesh()
.line_style_1(&WHITE.mix(0.3))
.x_label_offset(30)
.y_desc("Count")
.x_desc("Bucket")
.axis_desc_style(("sans-serif", 15).into_font())
Expand Down
2 changes: 1 addition & 1 deletion examples/normal-dist2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
.set_label_area_size(LabelAreaPosition::Bottom, 60)
.set_label_area_size(LabelAreaPosition::Right, 60)
.build_ranged(-4f64..4f64, 0f64..0.1)?
.set_secondary_coord((-40i32..40i32).into_centric(), 0u32..500u32);
.set_secondary_coord(-40i32..40i32, 0u32..500u32);

chart
.configure_mesh()
Expand Down
7 changes: 1 addition & 6 deletions src/coord/category.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::fmt;
use std::ops::Range;
use std::rc::Rc;

use super::{AsRangedCoord, Ranged};
use super::Ranged;

/// The category coordinate
pub struct Category<T: PartialEq> {
Expand Down Expand Up @@ -172,11 +172,6 @@ impl<T: PartialEq> Ranged for Category<T> {
}
}

impl<T: PartialEq> AsRangedCoord for Category<T> {
type CoordDescType = Self;
type Value = Category<T>;
}

#[cfg(test)]
mod test {
use super::*;
Expand Down
148 changes: 108 additions & 40 deletions src/coord/datetime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,14 +135,20 @@ impl<Z: TimeZone> Ranged for RangedDate<Z> {
}

impl<Z: TimeZone> DiscreteRanged for RangedDate<Z> {
type RangeParameter = ();
fn get_range_parameter(&self) {}
fn next_value(this: &Date<Z>, _: &()) -> Date<Z> {
this.clone() + Duration::days(1)
fn size(&self) -> usize {
((self.1.clone() - self.0.clone()).num_days().max(0) + 1) as usize
}

fn previous_value(this: &Date<Z>, _: &()) -> Date<Z> {
this.clone() - Duration::days(1)
fn index_of(&self, value: &Date<Z>) -> Option<usize> {
let ret = (value.clone() - self.0.clone()).num_days();
if ret < 0 {
return None;
}
Some(ret as usize)
}

fn from_index(&self, index: usize) -> Option<Date<Z>> {
Some(self.0.clone() + Duration::days(index as i64))
}
}

Expand All @@ -159,11 +165,6 @@ impl<Z: TimeZone> AsRangedCoord for Range<Date<Z>> {
#[derive(Clone)]
pub struct Monthly<T: TimeValue>(Range<T>);

impl<T: TimeValue + Clone> AsRangedCoord for Monthly<T> {
type CoordDescType = Monthly<T>;
type Value = T;
}

impl<T: TimeValue + Clone> Ranged for Monthly<T> {
type ValueType = T;

Expand Down Expand Up @@ -267,40 +268,57 @@ impl<T: TimeValue + Clone> Ranged for Monthly<T> {
}

impl<T: TimeValue + Clone> DiscreteRanged for Monthly<T> {
type RangeParameter = ();
fn get_range_parameter(&self) {}
fn next_value(this: &T, _: &()) -> T {
let mut year = this.date_ceil().year();
let mut month = this.date_ceil().month();
month += 1;
if month == 13 {
month = 1;
year += 1;
fn size(&self) -> usize {
let (start_year, start_month) = {
let ceil = self.0.start.date_ceil();
(ceil.year(), ceil.month())
};
let (end_year, end_month) = {
let floor = self.0.end.date_floor();
(floor.year(), floor.month())
};
((end_year - start_year).max(0) * 12
+ (1 - start_month as i32)
+ (end_month as i32 - 1)
+ 1)
.max(0) as usize
}

fn index_of(&self, value: &T) -> Option<usize> {
let this_year = value.date_floor().year();
let this_month = value.date_floor().month();

let start_year = self.0.start.date_ceil().year();
let start_month = self.0.start.date_ceil().month();

let ret = (this_year - start_year).max(0) * 12
+ (1 - start_month as i32)
+ (this_month as i32 - 1);
if ret >= 0 {
return Some(ret as usize);
}
T::earliest_after_date(this.timezone().ymd(year, month, this.date_ceil().day()))
None
}

fn previous_value(this: &T, _: &()) -> T {
let mut year = this.clone().date_floor().year();
let mut month = this.clone().date_floor().month();
month -= 1;
if month == 0 {
month = 12;
year -= 1;
fn from_index(&self, index: usize) -> Option<T> {
if index == 0 {
return Some(T::earliest_after_date(self.0.start.date_ceil()));
}
T::earliest_after_date(this.timezone().ymd(year, month, this.date_floor().day()))
let index_from_start_year = index + (self.0.start.date_ceil().month() - 1) as usize;
let year = self.0.start.date_ceil().year() + index_from_start_year as i32 / 12;
let month = index_from_start_year % 12;
Some(T::earliest_after_date(self.0.start.timezone().ymd(
year,
month as u32 + 1,
1,
)))
}
}

/// Indicate the coord has a yearly granularity.
#[derive(Clone)]
pub struct Yearly<T: TimeValue>(Range<T>);

impl<T: TimeValue + Clone> AsRangedCoord for Yearly<T> {
type CoordDescType = Yearly<T>;
type Value = T;
}

fn generate_yearly_keypoints<T: TimeValue>(
max_points: usize,
mut start_year: i32,
Expand Down Expand Up @@ -380,14 +398,29 @@ impl<T: TimeValue + Clone> Ranged for Yearly<T> {
}

impl<T: TimeValue + Clone> DiscreteRanged for Yearly<T> {
type RangeParameter = ();
fn get_range_parameter(&self) {}
fn next_value(this: &T, _: &()) -> T {
T::earliest_after_date(this.timezone().ymd(this.date_floor().year() + 1, 1, 1))
fn size(&self) -> usize {
let year_start = self.0.start.date_ceil().year();
let year_end = self.0.end.date_floor().year();
(year_end - year_start + 1) as usize
}

fn index_of(&self, value: &T) -> Option<usize> {
let year_start = self.0.start.date_ceil().year();
let year_value = value.date_floor().year();
let ret = year_value - year_start;
if ret < 0 {
return None;
}
Some(ret as usize)
}

fn previous_value(this: &T, _: &()) -> T {
T::earliest_after_date(this.timezone().ymd(this.date_ceil().year() - 1, 1, 1))
fn from_index(&self, index: usize) -> Option<T> {
let year = self.0.start.date_ceil().year() + index as i32;
let ret = T::earliest_after_date(self.0.start.timezone().ymd(year, 1, 1));
if ret.date_ceil() <= self.0.start.date_floor() {
return Some(self.0.start.clone());
}
Some(ret)
}
}

Expand Down Expand Up @@ -951,4 +984,39 @@ mod test {
assert!(max == min);
assert_eq!(max, 3600 * 2);
}

#[test]
fn test_date_discrete() {
let coord: RangedDate<Utc> = (Utc.ymd(2019, 1, 1)..Utc.ymd(2019, 12, 31)).into();
assert_eq!(coord.size(), 365);
assert_eq!(coord.index_of(&Utc.ymd(2019, 2, 28)), Some(31 + 28 - 1));
assert_eq!(coord.from_index(364), Some(Utc.ymd(2019, 12, 31)));
}

#[test]
fn test_monthly_discrete() {
let coord1 = (Utc.ymd(2019, 1, 10)..Utc.ymd(2019, 12, 31)).monthly();
let coord2 = (Utc.ymd(2019, 1, 10)..Utc.ymd(2020, 1, 1)).monthly();
assert_eq!(coord1.size(), 12);
assert_eq!(coord2.size(), 13);

for i in 1..=12 {
assert_eq!(coord1.from_index(i - 1).unwrap().month(), i as u32);
assert_eq!(
coord1.index_of(&coord1.from_index(i - 1).unwrap()).unwrap(),
i - 1
);
}
}

#[test]
fn test_yearly_discrete() {
let coord1 = (Utc.ymd(2000, 1, 10)..Utc.ymd(2019, 12, 31)).yearly();
assert_eq!(coord1.size(), 20);

for i in 0..20 {
assert_eq!(coord1.from_index(i).unwrap().year(), 2000 + i as i32);
assert_eq!(coord1.index_of(&coord1.from_index(i).unwrap()).unwrap(), i);
}
}
}
Loading