Calculate upper and lower bounds for deltas

This commit is contained in:
Joscha 2021-11-22 11:04:50 +00:00
parent 2c7aa78d08
commit 78bb0eb9e0
2 changed files with 96 additions and 0 deletions

View file

@ -7,6 +7,7 @@ use crate::files::commands::{Birthday, Command, File, Note, Spec, Task};
use self::entries::{DateRange, Entry, EntryKind, EntryMap};
mod delta;
pub mod entries;
#[derive(Debug, thiserror::Error)]

95
src/eval/delta.rs Normal file
View file

@ -0,0 +1,95 @@
use std::cmp::Ordering;
use crate::files::commands::{Delta, DeltaStep};
impl DeltaStep {
fn lower_bound(&self) -> i32 {
match self {
DeltaStep::Year(n) => {
if *n < 0 {
*n * 366
} else {
*n * 365
}
}
DeltaStep::Month(n) | DeltaStep::MonthReverse(n) => {
if *n < 0 {
*n * 31
} else {
*n * 28
}
}
DeltaStep::Day(n) => *n,
DeltaStep::Week(n) => *n * 7,
DeltaStep::Hour(n) => {
if *n < 0 {
*n / 24 + (*n % 24).signum()
} else {
*n / 24
}
}
DeltaStep::Minute(n) => {
if *n < 0 {
*n / (24 * 60) + (*n % (24 * 60)).signum()
} else {
*n / (24 * 60)
}
}
DeltaStep::Weekday(n, _) => match n.cmp(&0) {
Ordering::Less => *n * 7 - 1,
Ordering::Equal => 0,
Ordering::Greater => *n * 7 - 7,
},
}
}
fn upper_bound(&self) -> i32 {
match self {
DeltaStep::Year(n) => {
if *n > 0 {
*n * 366
} else {
*n * 365
}
}
DeltaStep::Month(n) | DeltaStep::MonthReverse(n) => {
if *n > 0 {
*n * 31
} else {
*n * 28
}
}
DeltaStep::Day(n) => *n,
DeltaStep::Week(n) => *n * 7,
DeltaStep::Hour(n) => {
if *n > 0 {
*n / 24 + (*n % 24).signum()
} else {
*n / 24
}
}
DeltaStep::Minute(n) => {
if *n > 0 {
*n / (24 * 60) + (*n % (24 * 60)).signum()
} else {
*n / (24 * 60)
}
}
DeltaStep::Weekday(n, _) => match n.cmp(&0) {
Ordering::Less => *n * 7 - 7,
Ordering::Equal => 0,
Ordering::Greater => *n * 7 - 1,
},
}
}
}
impl Delta {
pub fn lower_bound(&self) -> i32 {
self.0.iter().map(DeltaStep::lower_bound).sum()
}
pub fn upper_bound(&self) -> i32 {
self.0.iter().map(DeltaStep::upper_bound).sum()
}
}