Skip to content

Commit

Permalink
issue/50 Small formatting change.
Browse files Browse the repository at this point in the history
  • Loading branch information
fulmicoton committed Nov 3, 2016
1 parent 9d3c999 commit f2df0bf
Show file tree
Hide file tree
Showing 19 changed files with 290 additions and 297 deletions.
47 changes: 21 additions & 26 deletions src/postings/docset.rs
Expand Up @@ -17,23 +17,23 @@ pub enum SkipResult {
}


/// Represents an iterable set of sorted doc ids.
/// Represents an iterable set of sorted doc ids.
pub trait DocSet {
/// Goes to the next element.
/// `.advance(...)` needs to be called a first time to point to the correct
/// element.
fn advance(&mut self,) -> bool;
fn advance(&mut self) -> bool;

/// After skipping, position the iterator in such a way that `.doc()`
/// will return a value greater than or equal to target.
///
///
/// SkipResult expresses whether the `target value` was reached, overstepped,
/// or if the `DocSet` was entirely consumed without finding any value
/// greater or equal to the `target`.
///
/// WARNING: Calling skip always advances the docset.
/// More specifically, if the docset is already positionned on the target
/// skipping will advance to the next position and return SkipResult::Overstep.
/// skipping will advance to the next position and return SkipResult::Overstep.
///
fn skip_next(&mut self, target: DocId) -> SkipResult {
self.advance();
Expand All @@ -43,32 +43,30 @@ pub trait DocSet {
if !self.advance() {
return SkipResult::End;
}
},
Ordering::Equal => { return SkipResult::Reached },
Ordering::Greater => { return SkipResult::OverStep },
}
Ordering::Equal => return SkipResult::Reached,
Ordering::Greater => return SkipResult::OverStep,
}
}
}

/// Returns the current document
fn doc(&self,) -> DocId;
fn doc(&self) -> DocId;

/// Advances the cursor to the next document
/// None is returned if the iterator has `DocSet`
/// has already been entirely consumed.
fn next(&mut self,) -> Option<DocId> {
/// None is returned if the iterator has `DocSet`
/// has already been entirely consumed.
fn next(&mut self) -> Option<DocId> {
if self.advance() {
Some(self.doc())
}
else {
} else {
None
}
}
}
}

impl<TDocSet: DocSet + ?Sized> DocSet for Box<TDocSet> {

fn advance(&mut self,) -> bool {
fn advance(&mut self) -> bool {
let unboxed: &mut TDocSet = self.borrow_mut();
unboxed.advance()
}
Expand All @@ -78,28 +76,25 @@ impl<TDocSet: DocSet + ?Sized> DocSet for Box<TDocSet> {
unboxed.skip_next(target)
}

fn doc(&self,) -> DocId {
fn doc(&self) -> DocId {
let unboxed: &TDocSet = self.borrow();
unboxed.doc()
}
}

impl<'a, TDocSet: DocSet> DocSet for &'a mut TDocSet {

fn advance(&mut self,) -> bool {
fn advance(&mut self) -> bool {
let unref: &mut TDocSet = *self;
unref.advance()
}

fn skip_next(&mut self, target: DocId) -> SkipResult {
let unref: &mut TDocSet = *self;
unref.skip_next(target)
}

fn doc(&self,) -> DocId {
fn doc(&self) -> DocId {
let unref: &TDocSet = *self;
unref.doc()
}
}


36 changes: 15 additions & 21 deletions src/postings/freq_handler.rs
Expand Up @@ -17,7 +17,7 @@ pub struct FreqHandler {


fn read_positions(data: &[u8]) -> Vec<u32> {
let mut composite_reader = CompositeDecoder::new();
let mut composite_reader = CompositeDecoder::new();
let mut readable: &[u8] = data;
let uncompressed_len = VInt::deserialize(&mut readable).unwrap().0 as usize;
composite_reader.uncompress_unsorted(readable, uncompressed_len);
Expand All @@ -27,17 +27,16 @@ fn read_positions(data: &[u8]) -> Vec<u32> {


impl FreqHandler {

/// Returns a `FreqHandler` that just decodes `DocId`s.
pub fn new_without_freq() -> FreqHandler {
FreqHandler {
freq_decoder: SIMDBlockDecoder::with_val(1u32),
positions: Vec::new(),
positions: Vec::new(),
option: SegmentPostingsOption::NoFreq,
positions_offsets: [0; NUM_DOCS_PER_BLOCK + 1],
}
}

/// Returns a `FreqHandler` that decodes `DocId`s and term frequencies.
pub fn new_with_freq() -> FreqHandler {
FreqHandler {
Expand All @@ -54,15 +53,15 @@ impl FreqHandler {
let positions = read_positions(position_data);
FreqHandler {
freq_decoder: SIMDBlockDecoder::new(),
positions: positions,
positions: positions,
option: SegmentPostingsOption::FreqAndPositions,
positions_offsets: [0; NUM_DOCS_PER_BLOCK + 1],
}
}
fn fill_positions_offset(&mut self,) {

fn fill_positions_offset(&mut self) {
let mut cur_position: usize = self.positions_offsets[NUM_DOCS_PER_BLOCK];
let mut i: usize = 0;
let mut i: usize = 0;
self.positions_offsets[i] = cur_position;
let mut last_cur_position = cur_position;
for &doc_freq in self.freq_decoder.output_array() {
Expand All @@ -78,16 +77,16 @@ impl FreqHandler {
last_cur_position = cur_position;
}
}


/// Accessor to term frequency
///
/// idx is the offset of the current doc in the block.
/// It takes value between 0 and 128.
pub fn freq(&self, idx: usize)-> u32 {
pub fn freq(&self, idx: usize) -> u32 {
self.freq_decoder.output(idx)
}

/// Accessor to the positions
///
/// idx is the offset of the current doc in the block.
Expand All @@ -97,24 +96,20 @@ impl FreqHandler {
let stop = self.positions_offsets[idx + 1];
&self.positions[start..stop]
}

/// Decompresses a complete frequency block
pub fn read_freq_block<'a>(&mut self, data: &'a [u8]) -> &'a [u8] {
match self.option {
SegmentPostingsOption::NoFreq => {
data
}
SegmentPostingsOption::Freq => {
self.freq_decoder.uncompress_block_unsorted(data)
}
SegmentPostingsOption::NoFreq => data,
SegmentPostingsOption::Freq => self.freq_decoder.uncompress_block_unsorted(data),
SegmentPostingsOption::FreqAndPositions => {
let remaining: &'a [u8] = self.freq_decoder.uncompress_block_unsorted(data);
self.fill_positions_offset();
remaining
}
}
}

/// Decompresses an incomplete frequency block
pub fn read_freq_vint(&mut self, data: &[u8], num_els: usize) {
match self.option {
Expand All @@ -128,5 +123,4 @@ impl FreqHandler {
}
}
}

}
14 changes: 8 additions & 6 deletions src/postings/intersection.rs
Expand Up @@ -7,7 +7,7 @@ use DocId;
/// Creates a `DocSet` that iterator through the intersection of two `DocSet`s.
pub struct IntersectionDocSet<TDocSet: DocSet> {
docsets: Vec<TDocSet>,
finished: bool,
finished: bool,
doc: DocId,
}

Expand All @@ -18,20 +18,22 @@ impl<TDocSet: DocSet> From<Vec<TDocSet>> for IntersectionDocSet<TDocSet> {
docsets: docsets,
finished: false,
doc: DocId::max_value(),
}
}
}
}

impl<TDocSet: DocSet> IntersectionDocSet<TDocSet> {
/// Returns an array to the underlying `DocSet`s of the intersection.
/// These `DocSet` are in the same position as the `IntersectionDocSet`,
/// so that user can access their `docfreq` and `positions`.
pub fn docsets(&self) -> &[TDocSet] {
&self.docsets[..]
}
}


impl<TDocSet: DocSet> DocSet for IntersectionDocSet<TDocSet> {

fn advance(&mut self,) -> bool {
fn advance(&mut self) -> bool {
if self.finished {
return false;
}
Expand Down Expand Up @@ -71,8 +73,8 @@ impl<TDocSet: DocSet> DocSet for IntersectionDocSet<TDocSet> {
}
}
}
fn doc(&self,) -> DocId {

fn doc(&self) -> DocId {
self.doc
}
}
22 changes: 9 additions & 13 deletions src/postings/offset_postings.rs
Expand Up @@ -15,7 +15,6 @@ pub struct OffsetPostings<'a> {
}

impl<'a> OffsetPostings<'a> {

/// Constructor
pub fn new(underlying: SegmentPostings<'a>, offset: DocId) -> OffsetPostings {
OffsetPostings {
Expand All @@ -26,38 +25,35 @@ impl<'a> OffsetPostings<'a> {
}

impl<'a> DocSet for OffsetPostings<'a> {
fn advance(&mut self,) -> bool {
fn advance(&mut self) -> bool {
self.underlying.advance()
}
fn doc(&self,) -> DocId {

fn doc(&self) -> DocId {
self.underlying.doc() + self.offset
}

fn skip_next(&mut self, target: DocId) -> SkipResult {
if target >= self.offset {
SkipResult::OverStep
}
else {
self.underlying.skip_next(target - self.offset)
} else {
self.underlying.skip_next(target - self.offset)
}
}
}

impl<'a> HasLen for OffsetPostings<'a> {
fn len(&self,) -> usize {
fn len(&self) -> usize {
self.underlying.len()
}
}

impl<'a> Postings for OffsetPostings<'a> {

fn term_freq(&self,) -> u32 {
fn term_freq(&self) -> u32 {
self.underlying.term_freq()
}

fn positions(&self) -> &[u32] {
self.underlying.positions()
}

}
19 changes: 6 additions & 13 deletions src/postings/postings.rs
Expand Up @@ -7,45 +7,38 @@ use postings::docset::DocSet;
/// containing the term. Optionally, for each document,
/// it may also give access to the term frequency
/// as well as the list of term positions.
///
///
/// Its main implementation is `SegmentPostings`,
/// but other implementations mocking `SegmentPostings` exist,
/// for merging segments or for testing.
pub trait Postings: DocSet {
/// Returns the term frequency
fn term_freq(&self,) -> u32;
fn term_freq(&self) -> u32;
/// Returns the list of positions of the term, expressed as a list of
/// token ordinals.
fn positions(&self) -> &[u32];
}

impl<TPostings: Postings> Postings for Box<TPostings> {

fn term_freq(&self,) -> u32 {
fn term_freq(&self) -> u32 {
let unboxed: &TPostings = self.borrow();
unboxed.term_freq()
}

fn positions(&self) -> &[u32] {
let unboxed: &TPostings = self.borrow();
unboxed.positions()
}

}

impl<'a, TPostings: Postings> Postings for &'a mut TPostings {

fn term_freq(&self,) -> u32 {
fn term_freq(&self) -> u32 {
let unref: &TPostings = *self;
unref.term_freq()
}

fn positions(&self) -> &[u32] {
let unref: &TPostings = *self;
unref.positions()
}

}



0 comments on commit f2df0bf

Please sign in to comment.