Skip to content

Commit

Permalink
#4 Using peek_mut API from binary heap instead of replace
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Oct 14, 2016
1 parent b499d76 commit 746d628
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/datastruct/stacker/hashmap.rs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ mod tests {
use super::super::heap::{Heap, HeapAllocable};
use super::djb2;
use test::Bencher;
use std::hash::SipHasher;
use std::collections::hash_map::DefaultHasher;
use std::hash::Hasher;

struct TestValue {
Expand Down Expand Up @@ -223,7 +223,7 @@ mod tests {
fn bench_siphasher(bench: &mut Bencher) {
let v = String::from("abwer");
bench.iter(|| {
let mut h = SipHasher::new();
let mut h = DefaultHasher::new();
h.write(v.as_bytes());
h.finish()
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ mod tests {
}

#[test]
fn test_searcher() {
fn test_searcher_1() {
let mut schema_builder = SchemaBuilder::default();
let text_field = schema_builder.add_text_field("text", TEXT);
let schema = schema_builder.build();
Expand Down
50 changes: 29 additions & 21 deletions src/query/daat_multiterm_scorer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@ use Score;
/// * segment_ordinal - is the ordinal used to identify to which segment postings
/// this heap item belong to.
#[derive(Eq, PartialEq)]
struct HeapItem(DocId, u32);
struct HeapItem {
doc: DocId,
ord: u32,
}

/// HeapItem are ordered by the document
impl PartialOrd for HeapItem {
Expand All @@ -29,7 +32,7 @@ impl PartialOrd for HeapItem {

impl Ord for HeapItem {
fn cmp(&self, other:&Self) -> Ordering {
(other.0).cmp(&self.0)
(other.doc).cmp(&self.doc)
}
}

Expand Down Expand Up @@ -98,7 +101,10 @@ impl<TPostings: Postings, TAccumulator: MultiTermAccumulator> DAATMultiTermScore
.enumerate()
.map(|(ord, (doc, tf))| {
term_frequencies[ord] = tf;
HeapItem(doc, ord as u32)
HeapItem {
doc: doc,
ord: ord as u32
}
})
.collect();
DAATMultiTermScorer {
Expand Down Expand Up @@ -143,17 +149,19 @@ impl<TPostings: Postings, TAccumulator: MultiTermAccumulator> DAATMultiTermScore
/// # Panics
/// This method will panic if the head `SegmentPostings` is not empty.
fn advance_head(&mut self,) {
let ord = self.queue.peek().unwrap().1 as usize;
let cur_postings = &mut self.postings[ord];
if cur_postings.advance() {
let doc = cur_postings.doc();
self.term_frequencies[ord] = cur_postings.term_freq();
// peek & replace is incredibly
self.queue.replace(HeapItem(doc, ord as u32));
}
else {
self.queue.pop();

{
let mut mutable_head = self.queue.peek_mut().unwrap();
let cur_postings = &mut self.postings[mutable_head.ord as usize];
if cur_postings.advance() {
let doc = cur_postings.doc();
self.term_frequencies[mutable_head.ord as usize] = cur_postings.term_freq();
mutable_head.doc = doc;
return;
}

}
self.queue.pop();
}

/// Returns the field norm for the segment postings with the given ordinal,
Expand All @@ -177,10 +185,10 @@ impl<TPostings: Postings, TAccumulator: MultiTermAccumulator> DocSet for DAATMul
self.similarity.clear();
let mut ord_bitset = 0u64;
match self.queue.peek() {
Some(&HeapItem(doc, ord)) => {
self.doc = doc;
let ord: usize = ord as usize;
let fieldnorm = self.get_field_norm(ord, doc);
Some(ref heap_item) => {
self.doc = heap_item.doc;
let ord: usize = heap_item.ord as usize;
let fieldnorm = self.get_field_norm(ord, heap_item.doc);
let tf = self.term_frequencies[ord];
self.similarity.update(ord, tf, fieldnorm);
ord_bitset |= 1 << ord;
Expand All @@ -190,11 +198,11 @@ impl<TPostings: Postings, TAccumulator: MultiTermAccumulator> DocSet for DAATMul
}
}
self.advance_head();
while let Some(&HeapItem(peek_doc, peek_ord)) = self.queue.peek() {
if peek_doc == self.doc {
let peek_ord: usize = peek_ord as usize;
while let Some(&HeapItem { doc: doc, ord: ord}) = self.queue.peek() {
if doc == self.doc {
let peek_ord: usize = ord as usize;
let peek_tf = self.term_frequencies[peek_ord];
let peek_fieldnorm = self.get_field_norm(peek_ord, peek_doc);
let peek_fieldnorm = self.get_field_norm(peek_ord, doc);
self.similarity.update(peek_ord, peek_tf, peek_fieldnorm);
ord_bitset |= 1 << peek_ord;
}
Expand Down

0 comments on commit 746d628

Please sign in to comment.