use std::cmp::Ordering;
use std::sync::Arc;
use crate::exclude::ExcludedKeys;
use crate::intervals::{IntervalSet, Span as MinuteSpan};
use crate::model::{Block, Participant};
use crate::rules::Applicable;
pub(crate) struct BlockedSpan<'a> {
pub(crate) occupied: MinuteSpan,
pub(crate) widened: MinuteSpan,
pub(crate) block: &'a Block,
}
pub(crate) struct BlockedIndex<'a> {
spans: Vec<BlockedSpan<'a>>,
max_occupied_end: Vec<i64>,
max_widened_end: Vec<i64>,
}
impl<'a> BlockedIndex<'a> {
pub(crate) fn spans(&self) -> &[BlockedSpan<'a>] {
&self.spans
}
pub(crate) fn first_overlapping_occupied(&self, span: &MinuteSpan) -> Option<&BlockedSpan<'a>> {
let first = self
.max_occupied_end
.partition_point(|&max_end| max_end <= span.start());
for blocked in &self.spans[first..] {
if blocked.occupied.start() >= span.end() {
break;
}
if overlaps(&blocked.occupied, span) {
return Some(blocked);
}
}
None
}
pub(crate) fn widened_candidates(
&self,
span: &MinuteSpan,
) -> impl Iterator<Item = &BlockedSpan<'a>> {
let first = self
.max_widened_end
.partition_point(|&max_end| max_end <= span.start());
let end = span.end();
self.spans[first..]
.iter()
.take_while(move |blocked| blocked.widened.start() < end)
}
}
pub(super) fn blocked_time<'a>(
rules: &Applicable<'_>,
participant: &'a Participant,
excluded: &ExcludedKeys<'_>,
) -> BlockedIndex<'a> {
let at_start = rules.clearance.after.max(rules.clearance.min_gap).minutes();
let at_end = rules
.clearance
.before
.max(rules.clearance.min_gap)
.minutes();
let mut spans = Vec::new();
for block in participant.blocks() {
if excluded.excludes(block.event_key.as_deref()) {
continue;
}
if !rules.blocks(block.status, &block.tags) {
continue;
}
let (start, end) = block.occupied_minutes();
let Ok(occupied) = MinuteSpan::new(start, end) else {
continue;
};
let Ok(widened) =
MinuteSpan::new(start.saturating_sub(at_start), end.saturating_add(at_end))
else {
continue;
};
spans.push(BlockedSpan {
occupied,
widened,
block,
});
}
spans.sort_by(canonical_block_order);
let mut max_occupied_end = Vec::with_capacity(spans.len());
let mut max_widened_end = Vec::with_capacity(spans.len());
let mut occ_max = i64::MIN;
let mut wid_max = i64::MIN;
for blocked in &spans {
occ_max = occ_max.max(blocked.occupied.end());
wid_max = wid_max.max(blocked.widened.end());
max_occupied_end.push(occ_max);
max_widened_end.push(wid_max);
}
BlockedIndex {
spans,
max_occupied_end,
max_widened_end,
}
}
fn canonical_block_order(left: &BlockedSpan<'_>, right: &BlockedSpan<'_>) -> Ordering {
left.occupied
.cmp(&right.occupied)
.then_with(|| left.block.event_key.cmp(&right.block.event_key))
.then_with(|| left.block.tags.cmp(&right.block.tags))
}
pub(super) fn blackout_overlapping<'a>(
blackouts: &[(&'a str, Arc<IntervalSet>)],
span: &MinuteSpan,
) -> Option<&'a str> {
blackouts
.iter()
.find(|(_, set)| set.overlaps(span))
.map(|(id, _)| *id)
}
pub(super) fn block_overlapping<'a, 'b>(
blocked: &'b BlockedIndex<'a>,
span: &MinuteSpan,
) -> Option<&'b BlockedSpan<'a>> {
blocked.first_overlapping_occupied(span)
}
pub(super) fn overlaps(left: &MinuteSpan, right: &MinuteSpan) -> bool {
left.start() < right.end() && right.start() < left.end()
}