Add a cache to the contains_documents success function

This commit is contained in:
Kerollmops 2020-06-14 13:10:30 +02:00
parent a8cda248b4
commit 8db16ff306
No known key found for this signature in database
GPG Key ID: 92ADA4E935E71FA4
2 changed files with 30 additions and 25 deletions

View File

@ -102,7 +102,7 @@ impl Node {
}
}
fn is_reachable<F>(&self, mut contains_documents: F) -> bool
fn is_reachable<F>(&self, contains_documents: &mut F) -> bool
where F: FnMut((usize, u32), (usize, u32)) -> bool,
{
match self {
@ -133,7 +133,7 @@ impl<F> BestProximity<F> {
}
impl<F> Iterator for BestProximity<F>
where F: FnMut((usize, u32), (usize, u32)) -> bool + Copy,
where F: FnMut((usize, u32), (usize, u32)) -> bool,
{
type Item = (u32, Vec<Vec<u32>>);
@ -144,13 +144,15 @@ where F: FnMut((usize, u32), (usize, u32)) -> bool + Copy,
return None;
}
let BestProximity { positions, best_proximity, contains_documents } = self;
let result = astar_bag(
&Node::Uninit, // start
|n| n.successors(&self.positions, self.best_proximity),
|n| n.successors(&positions, *best_proximity),
|_| 0, // heuristic
|n| { // success
let c = n.is_complete(&self.positions) && n.proximity() >= self.best_proximity;
if n.is_reachable(self.contains_documents) { Some(c) } else { None }
let c = n.is_complete(&positions) && n.proximity() >= *best_proximity;
if n.is_reachable(contains_documents) { Some(c) } else { None }
},
);

View File

@ -120,7 +120,9 @@ impl Index {
let mut documents = Vec::new();
let mut intersect_cache = HashMap::new();
let contains_documents = |(lword, lpos): (usize, u32), (rword, rpos)| {
*intersect_cache.entry(((lword, lpos), (rword, rpos))).or_insert_with(|| {
use std::iter::once;
let left = (&words[lword], lpos);
@ -146,6 +148,7 @@ impl Index {
}
intersect_docids.map_or(false, |i| !i.is_empty())
})
};
for (proximity, mut positions) in BestProximity::new(positions, contains_documents) {