IYW574EKVRH2QJ7GFNX4FMCNI7EMLNYYIC6NGHVIJVDEWSDL42GQC
/// Represents a date a granularity of one month or larger. Dates can also act
/// as durations and be added and subtracted.
/// A date with monthly granularity.
#[derive(Clone, Copy, Debug, Eq)]
/// A date with granularity of one month or larger. Dates can also act as durations and be added
/// and subtracted.
///
/// To avoid pulling in redundant dependences, the responsibility for parsing a date in string form
/// is left to the client code.
#[derive(Clone, Copy, Eq)]
}
}
pub struct RegularTimeSeriesIter<'a, const N: usize> {
start_date: MonthlyDate,
end_date: MonthlyDate,
date_points: &'a Vec<DatePoint<N>>,
counter: usize,
}
impl<'a, const N: usize> Iterator for RegularTimeSeriesIter<'a, N> {
type Item = DatePoint<N>;
fn next(&mut self) -> Option<Self::Item> {
// Beyond the end of self.date_points.
if self.counter >= self.date_points.len() {
None
} else {
// Counter points into self.date_points and before start date.
if self.date_points[self.counter].date() < self.start_date {
self.counter += 1;
self.next()
// Counter points into self.date_points but past end date.
} else if self.date_points[self.counter].date() > self.end_date {
return None
// Counter points into self.date_points and inside range.
} else {
self.counter += 1;
return Some(self.date_points[self.counter - 1])
}
}
}
#[test]
fn test_iter() {
let date1 = MonthlyDate::ym(1995, 11);
let date2 = MonthlyDate::ym(1995, 12);
let date3 = MonthlyDate::ym(1996, 1);
let date4 = MonthlyDate::ym(1996, 2);
let date5 = MonthlyDate::ym(1996, 3);
let dp1 = DatePoint::new(date1, [1.2]);
let dp2 = DatePoint::new(date2, [1.4]);
let dp3 = DatePoint::new(date3, [1.6]);
let dp4 = DatePoint::new(date4, [1.8]);
let dp5 = DatePoint::new(date5, [2.0]);
let v = vec!( dp1, dp2, dp3, dp4, dp5);
let rts: RegularTimeSeries<1> = TimeSeries::new(v).try_into().unwrap();
let date_range = DateRange::new(Some(date2), Some(date4));
let mut iter = rts.iter(date_range);
assert_eq!(iter.next().unwrap().date(), date2);
assert_eq!(iter.next().unwrap().date(), date3);
assert_eq!(iter.next().unwrap().date(), date4);
assert!(iter.next().is_none());
pub fn iter(&self, date_range: DateRange) -> RegularTimeSeriesIter<N> {
let ts_start_date = self.ts.0[0].date();
let start_date = match date_range.start_date {
None => ts_start_date,
Some(start) => ts_start_date.max(start),
};
let ts_end_date = *(&(self.ts.0).last().unwrap().date());
let end_date = match date_range.end_date {
None => ts_end_date,
Some(end) => ts_end_date.min(end),
};
RegularTimeSeriesIter {
start_date,
end_date,
date_points: &self.ts.0,
counter: 0,
}
}
/// Return an iterator over points.
pub fn iter(&self, dr: DateRange) -> impl Iterator + '_ {
self.ts.0.iter()
.skip_while(move |p1| {
if let Some(range_date) = dr.first_date() {
// /// Return an iterator over points.
// pub fn iter(&self, dr: DateRange) -> impl Iterator + '_ {
// self.ts.0.iter()
// .skip_while(move |p1| {
// if let Some(range_date) = dr.first_date() {