willow_data_model/grouping/
area_of_interest.rs

1use crate::grouping::area::Area;
2
3/// A grouping of [`crate::Entry`]s that are among the newest in some [store](https://willowprotocol.org/specs/data-model/index.html#store).
4///
5/// [Definition](https://willowprotocol.org/specs/grouping-entries/index.html#aois).
6#[derive(Debug, Clone, Eq, PartialEq, PartialOrd, Ord, Hash)]
7pub struct AreaOfInterest<const MCL: usize, const MCC: usize, const MPL: usize, S> {
8    /// To be included in this [`AreaOfInterest`], an [`crate::Entry`] must be included in the [`Area`].
9    pub area: Area<MCL, MCC, MPL, S>,
10    /// To be included in this AreaOfInterest, an Entry’s timestamp must be among the max_count greatest Timestamps, unless max_count is zero.
11    pub max_count: u64,
12    /// The total payload_lengths of all included Entries is at most max_size, unless max_size is zero.
13    pub max_size: u64,
14}
15
16impl<const MCL: usize, const MCC: usize, const MPL: usize, S> AreaOfInterest<MCL, MCC, MPL, S> {
17    /// Creates a new [`AreaOfInterest`].
18    pub fn new(area: Area<MCL, MCC, MPL, S>, max_count: u64, max_size: u64) -> Self {
19        Self {
20            area,
21            max_count,
22            max_size,
23        }
24    }
25}
26
27impl<const MCL: usize, const MCC: usize, const MPL: usize, S> AreaOfInterest<MCL, MCC, MPL, S>
28where
29    S: PartialOrd + Clone,
30{
31    /// Returns the intersection of this [`AreaOfInterest`] with another.
32    ///
33    /// [Definition](https://willowprotocol.org/specs/grouping-entries/index.html#aoi_intersection).
34    pub fn intersection(
35        &self,
36        other: &AreaOfInterest<MCL, MCC, MPL, S>,
37    ) -> Option<AreaOfInterest<MCL, MCC, MPL, S>> {
38        self.area
39            .intersection(&other.area)
40            .map(|area_intersection| Self {
41                area: area_intersection,
42                max_count: core::cmp::min(self.max_count, other.max_count),
43                max_size: self.max_size.min(other.max_size),
44            })
45    }
46}