use std::cmp::Ordering;
use std::collections::BinaryHeap;
use crate::predicates::Predicate;
use crate::state::State;
#[derive(Clone, Debug, Default)]
pub struct PredicateHeap {
heap: BinaryHeap<PredicateToExplain>,
}
impl PredicateHeap {
pub fn is_empty(&self) -> bool {
self.heap.is_empty()
}
pub fn pop(&mut self) -> Option<Predicate> {
self.heap.pop().map(|to_explain| to_explain.predicate)
}
pub fn push(&mut self, predicate: Predicate, state: &State) {
let trail_position = state
.trail_position(predicate)
.expect("predicate must be true in given state");
let priority = if state.is_on_trail(predicate) {
trail_position * 2
} else {
trail_position * 2 + 1
};
self.heap.push(PredicateToExplain {
predicate,
priority,
});
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
struct PredicateToExplain {
predicate: Predicate,
priority: usize,
}
impl PartialOrd for PredicateToExplain {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for PredicateToExplain {
fn cmp(&self, other: &Self) -> Ordering {
self.priority.cmp(&other.priority)
}
}