polars_time/windows/
bounds.rs

1use super::group_by::ClosedWindow;
2
3#[derive(Copy, Clone, Debug)]
4pub struct Bounds {
5    pub(crate) start: i64,
6    pub(crate) stop: i64,
7}
8
9impl Bounds {
10    /// Create a new [`Bounds`] and check the input is correct.
11    pub(crate) fn new_checked(start: i64, stop: i64) -> Self {
12        assert!(
13            start <= stop,
14            "boundary start must be smaller than stop; is your time column sorted in ascending order?\
15            \nIf you did a group_by, note that null values are a separate group."
16        );
17        Self::new(start, stop)
18    }
19
20    /// Create a new [`Bounds`] without checking input correctness.
21    pub(crate) fn new(start: i64, stop: i64) -> Self {
22        Bounds { start, stop }
23    }
24
25    /// Duration in unit for this Boundary
26    #[inline]
27    pub(crate) fn duration(&self) -> i64 {
28        self.stop - self.start
29    }
30
31    // check if unit is within bounds
32    #[inline]
33    pub(crate) fn is_member(&self, t: i64, closed: ClosedWindow) -> bool {
34        match closed {
35            ClosedWindow::Right => t > self.start && t <= self.stop,
36            ClosedWindow::Left => t >= self.start && t < self.stop,
37            ClosedWindow::None => t > self.start && t < self.stop,
38            ClosedWindow::Both => t >= self.start && t <= self.stop,
39        }
40    }
41
42    #[inline]
43    pub(crate) fn is_member_entry(&self, t: i64, closed: ClosedWindow) -> bool {
44        match closed {
45            ClosedWindow::Right => t > self.start,
46            ClosedWindow::Left => t >= self.start,
47            ClosedWindow::None => t > self.start,
48            ClosedWindow::Both => t >= self.start,
49        }
50    }
51
52    #[inline]
53    pub(crate) fn is_member_exit(&self, t: i64, closed: ClosedWindow) -> bool {
54        match closed {
55            ClosedWindow::Right => t <= self.stop,
56            ClosedWindow::Left => t < self.stop,
57            ClosedWindow::None => t < self.stop,
58            ClosedWindow::Both => t <= self.stop,
59        }
60    }
61
62    #[inline]
63    pub(crate) fn is_future(&self, t: i64, closed: ClosedWindow) -> bool {
64        match closed {
65            ClosedWindow::Left | ClosedWindow::None => self.stop <= t,
66            ClosedWindow::Both | ClosedWindow::Right => self.stop < t,
67        }
68    }
69
70    #[inline]
71    pub(crate) fn is_past(&self, t: i64, closed: ClosedWindow) -> bool {
72        match closed {
73            ClosedWindow::Left | ClosedWindow::Both => self.start > t,
74            ClosedWindow::None | ClosedWindow::Right => self.start >= t,
75        }
76    }
77}