diff options
author | Fredrik Meringdal <fmeringdal@hotmail.com> | 2021-05-12 17:51:32 +0200 |
---|---|---|
committer | Fredrik Meringdal <fmeringdal@hotmail.com> | 2021-05-12 17:51:32 +0200 |
commit | 3d848c02ddb5a1b2da013685bd8ed82b0d0d2553 (patch) | |
tree | f2408cbb8df8643c4400ba342adb8ba54594cc24 | |
parent | a2c30c2a5f377fb5f5b7c912a66f950950c760f2 (diff) | |
download | rust_rrule-3d848c02ddb5a1b2da013685bd8ed82b0d0d2553.zip |
masks
-rw-r--r-- | src/iter/iterinfo.rs | 32 | ||||
-rw-r--r-- | src/iter/masks.rs | 57 | ||||
-rw-r--r-- | src/iter/mod.rs | 14 | ||||
-rw-r--r-- | src/iter/monthinfo.rs | 4 | ||||
-rw-r--r-- | src/iter/yearinfo.rs | 42 | ||||
-rw-r--r-- | src/rrule_iter.rs | 2 |
6 files changed, 85 insertions, 66 deletions
diff --git a/src/iter/iterinfo.rs b/src/iter/iterinfo.rs index b4d943b..1a1a283 100644 --- a/src/iter/iterinfo.rs +++ b/src/iter/iterinfo.rs @@ -14,12 +14,16 @@ pub struct IterInfo { impl IterInfo { pub fn new(options: ParsedOptions) -> Self { - Self { + let mut ii = Self { options, yearinfo: None, monthinfo: None, eastermask: None, - } + }; + let counter_date = ii.options.dtstart; + ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); + + ii } pub fn rebuild(&mut self, year: isize, month: usize) { @@ -63,20 +67,20 @@ impl IterInfo { self.yearinfo.as_ref().map(|info| info.yearordinal) } - pub fn mrange(&self) -> Option<&Vec<usize>> { - self.yearinfo.as_ref().map(|info| &info.mrange) + pub fn mrange(&self) -> &[usize] { + self.yearinfo.as_ref().map(|info| &info.mrange).unwrap() } pub fn eastermask(&self) -> Option<&Vec<isize>> { self.eastermask.as_ref() } - pub fn wdaymask(&self) -> Option<&Vec<usize>> { - self.yearinfo.as_ref().map(|info| &info.wdaymask) + pub fn wdaymask(&self) -> &[usize] { + self.yearinfo.as_ref().map(|info| &info.wdaymask).unwrap() } - pub fn mmask(&self) -> Option<&Vec<usize>> { - self.yearinfo.as_ref().map(|info| &info.mmask) + pub fn mmask(&self) -> &[usize] { + self.yearinfo.as_ref().map(|info| &info.mmask).unwrap() } pub fn wnomask(&self) -> Option<&Vec<usize>> { @@ -94,12 +98,12 @@ impl IterInfo { self.yearinfo.as_ref().map(|info| info.nextyearlen) } - pub fn mdaymask(&self) -> Option<&Vec<isize>> { - self.yearinfo.as_ref().map(|info| &info.mdaymask) + pub fn mdaymask(&self) -> &[isize] { + self.yearinfo.as_ref().unwrap().mdaymask } - pub fn nmdaymask(&self) -> Option<&Vec<isize>> { - self.yearinfo.as_ref().map(|info| &info.nmdaymask) + pub fn nmdaymask(&self) -> &[isize] { + self.yearinfo.as_ref().unwrap().nmdaymask } pub fn ydayset(&self) -> (Vec<usize>, usize, usize) { @@ -112,7 +116,7 @@ impl IterInfo { } pub fn mdayset(&self, month: usize) -> (Vec<usize>, usize, usize) { - let mrange = self.mrange().unwrap(); + let mrange = self.mrange(); let start = mrange[month - 1]; let end = mrange[month]; let mut set = vec![0; self.yearlen().unwrap()]; @@ -138,7 +142,7 @@ impl IterInfo { } set[i] = i; i += 1; - if self.wdaymask().unwrap()[i] == self.options.wkst { + if self.wdaymask()[i] == self.options.wkst { break; } } diff --git a/src/iter/masks.rs b/src/iter/masks.rs index 970c570..63ec771 100644 --- a/src/iter/masks.rs +++ b/src/iter/masks.rs @@ -4,8 +4,14 @@ // Every mask is 7 days longer to handle cross-year weekly periods. -const WDAY: [[usize; 7]; 55] = [[0, 1, 2, 3, 4, 5, 6]; 55]; -const M365: [usize; 365 + 7] = [ +pub(crate) type WDAY_MASK = [usize; 7 * 55]; +pub(crate) type MASK_365 = [usize; 365 + 7]; +pub(crate) type MASK_366 = [usize; 366 + 7]; +pub(crate) type MASK_365_SIGNED = [isize; 365 + 7]; +pub(crate) type MASK_366_SIGNED = [isize; 366 + 7]; +pub(crate) type MONTH_RANGE_MASK = [usize; 13]; + +const M365: MASK_365 = [ // 31 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 28 @@ -36,7 +42,7 @@ const M365: [usize; 365 + 7] = [ // 7 ------------------------------------------------------------------------------------------ 1, 1, 1, 1, 1, 1, 1, ]; -const MDAY365: [usize; 365 + 7] = [ +const MDAY365: MASK_365_SIGNED = [ // 31 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, @@ -76,7 +82,7 @@ const MDAY365: [usize; 365 + 7] = [ // 7 ----------------------------------------------------------------------------------------- 1, 2, 3, 4, 5, 6, 7, ]; -const NMDAY365: [isize; 365 + 7] = [ +const NMDAY365: MASK_365_SIGNED = [ // 31 -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, @@ -116,9 +122,9 @@ const NMDAY365: [isize; 365 + 7] = [ // 7 ----------------------------------------------------------------------------------------- -31, -30, -29, -28, -27, -26, -25, ]; -const M365_RANGE: [usize; 13] = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]; +const M365_RANGE: MONTH_RANGE_MASK = [0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365]; -const M366: [usize; 366 + 7] = [ +const M366: MASK_366 = [ // 31 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 29 @@ -149,7 +155,7 @@ const M366: [usize; 366 + 7] = [ // 7 ------------------------------------------------------------------------------------------ 1, 1, 1, 1, 1, 1, 1, ]; -const MDAY366: [usize; 366 + 7] = [ +const MDAY366: MASK_366_SIGNED = [ // 31 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, @@ -189,8 +195,8 @@ const MDAY366: [usize; 366 + 7] = [ // 7 ----------------------------------------------------------------------------------------- 1, 2, 3, 4, 5, 6, 7, ]; -const M366_RANGE: [usize; 13] = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]; -const NMDAY366: [isize; 366 + 7] = [ +const M366_RANGE: MONTH_RANGE_MASK = [0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366]; +const NMDAY366: MASK_366_SIGNED = [ // 31 -31, -30, -29, -28, -27, -26, -25, -24, -23, -22, -21, -20, -19, -18, -17, -16, -15, -14, -13, -12, -11, -10, -9, -8, -7, -6, -5, -4, -3, -2, -1, @@ -230,18 +236,33 @@ const NMDAY366: [isize; 366 + 7] = [ // 7 ----------------------------------------------------------------------------------------- -31, -30, -29, -28, -27, -26, -25, ]; +const WDAY: WDAY_MASK = [ + 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, + 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, + 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, + 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, + 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, + 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, + 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, + 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, + 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, + 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, + 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, + 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, 6, 0, 1, 2, 3, 4, 5, + 6, +]; #[derive(Clone)] pub struct Masks { - pub wday: [[usize; 7]; 55], - pub m365: [usize; 365 + 7], - pub m365range: [usize; 13], - pub m366: [usize; 366 + 7], - pub m366range: [usize; 13], - pub mday365: [usize; 365 + 7], - pub mday366: [usize; 366 + 7], - pub nmday365: [isize; 365 + 7], - pub nmday366: [isize; 366 + 7], + pub wday: WDAY_MASK, + pub m365: MASK_365, + pub m365range: MONTH_RANGE_MASK, + pub m366: MASK_366, + pub m366range: MONTH_RANGE_MASK, + pub mday365: MASK_365_SIGNED, + pub mday366: MASK_366_SIGNED, + pub nmday365: MASK_365_SIGNED, + pub nmday366: MASK_366_SIGNED, } pub const MASKS: Masks = Masks { diff --git a/src/iter/mod.rs b/src/iter/mod.rs index 2d78bab..807e41f 100644 --- a/src/iter/mod.rs +++ b/src/iter/mod.rs @@ -156,24 +156,18 @@ pub fn increment_counter_date( } pub fn is_filtered(ii: &IterInfo, current_day: usize, options: &ParsedOptions) -> bool { - if !options.bymonth.is_empty() { - println!("Current day: {}", current_day); - println!("Byymonth: {:?}", options.bymonth); - println!("mask: {:?}", ii.mmask().unwrap()); - } - return (!options.bymonth.is_empty() - && !options.bymonth.contains(&ii.mmask().unwrap()[current_day])) + return (!options.bymonth.is_empty() && !options.bymonth.contains(&ii.mmask()[current_day])) || (not_empty(&options.byweekno) && (ii.wnomask().unwrap()[current_day]) == 0) || (not_empty(&options.byweekday) - && !includes(&options.byweekday, &ii.wdaymask().unwrap()[current_day])) + && !includes(&options.byweekday, &ii.wdaymask()[current_day])) || (ii.nwdaymask().is_some() && not_empty(ii.nwdaymask().unwrap()) && (ii.nwdaymask().unwrap()[current_day]) == 0) || (options.byeaster.is_some() && !(includes(ii.eastermask().unwrap(), &(current_day as isize)))) || ((not_empty(&options.bymonthday) || not_empty(&options.bynmonthday)) - && !includes(&options.bymonthday, &ii.mdaymask().unwrap()[current_day]) - && !includes(&options.bynmonthday, &ii.nmdaymask().unwrap()[current_day])) + && !includes(&options.bymonthday, &ii.mdaymask()[current_day]) + && !includes(&options.bynmonthday, &ii.nmdaymask()[current_day])) || (not_empty(&options.byyearday) && ((current_day < ii.yearlen().unwrap() && !includes(&options.byyearday, &(current_day as isize + 1)) diff --git a/src/iter/monthinfo.rs b/src/iter/monthinfo.rs index b5becfe..390555f 100644 --- a/src/iter/monthinfo.rs +++ b/src/iter/monthinfo.rs @@ -12,8 +12,8 @@ pub fn rebuild_month( year: isize, month: usize, yearlen: usize, - mrange: &Vec<usize>, - wdaymask: &Vec<usize>, + mrange: &[usize], + wdaymask: &[usize], options: &ParsedOptions, ) -> MonthInfo { let mut result = MonthInfo { diff --git a/src/iter/yearinfo.rs b/src/iter/yearinfo.rs index eb422f1..8c784b4 100644 --- a/src/iter/yearinfo.rs +++ b/src/iter/yearinfo.rs @@ -10,44 +10,44 @@ pub struct YearInfo { pub nextyearlen: usize, pub yearordinal: isize, pub yearweekday: usize, - pub mmask: Vec<usize>, - pub mrange: Vec<usize>, - pub mdaymask: Vec<isize>, - pub nmdaymask: Vec<isize>, - pub wdaymask: Vec<usize>, + pub mmask: &'static [usize], + pub mdaymask: &'static [isize], + pub nmdaymask: &'static [isize], + pub mrange: &'static [usize], + pub wdaymask: &'static [usize], pub wnomask: Option<Vec<usize>>, } pub struct BaseMasks { - mmask: Vec<usize>, - mdaymask: Vec<isize>, - nmdaymask: Vec<isize>, - wdaymask: Vec<usize>, - mrange: Vec<usize>, + mmask: &'static [usize], + mdaymask: &'static [isize], + nmdaymask: &'static [isize], + mrange: &'static [usize], + wdaymask: &'static [usize], } fn base_year_masks(year: i32) -> BaseMasks { - let masks = MASKS.clone(); + // let masks = MASKS.clone(); let firstyday = Utc.ymd(year, 1, 1).and_hms_milli(0, 0, 0, 0); let yearlen = get_year_len(year); let wday = get_weekday_val(&firstyday.weekday()) as usize; if yearlen == 365 { return BaseMasks { - mmask: masks.m365, - mdaymask: masks.mday365, - nmdaymask: masks.nmday365, - mrange: masks.m365range, - wdaymask: Vec::from(&masks.wday[wday..]), + mmask: &MASKS.m365, + mdaymask: &MASKS.mday365, + nmdaymask: &MASKS.nmday365, + mrange: &MASKS.m365range, + wdaymask: &MASKS.wday[wday..], }; } BaseMasks { - mmask: masks.m366, - mdaymask: masks.mday366, - nmdaymask: masks.nmday366, - mrange: masks.m366range, - wdaymask: Vec::from(&masks.wday[wday..]), + mmask: &MASKS.m366, + mdaymask: &MASKS.mday366, + nmdaymask: &MASKS.nmday366, + mrange: &MASKS.m366range, + wdaymask: &MASKS.wday[wday..], } } diff --git a/src/rrule_iter.rs b/src/rrule_iter.rs index 7ec0275..7286fac 100644 --- a/src/rrule_iter.rs +++ b/src/rrule_iter.rs @@ -181,7 +181,7 @@ impl IntoIterator for RRule { fn into_iter(self) -> Self::IntoIter { let mut ii = IterInfo::new(self.options); let counter_date = ii.options.dtstart; - ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); + // ii.rebuild(counter_date.year() as isize, counter_date.month() as usize); let timeset = make_timeset(&ii, &counter_date, &ii.options); |