Initial import into git.
[galago.git] / java / galago / src / galago / retrieval / SparseFloatListReader.java
blob751d3588d1dd70801f08571496862d21507925a2
1 /*
2 * ScoreIndex
4 * October 2, 2007 -- Trevor Strohman
6 * BSD License (http://www.galagosearch.org/license)
7 */
9 package galago.retrieval;
11 import galago.retrieval.extents.ScoreIterator;
12 import galago.tupleflow.BufferedDataStream;
13 import galago.tupleflow.VByteInput;
14 import java.io.FileNotFoundException;
15 import java.io.IOException;
17 /**
19 * @author trevor
21 public class SparseFloatListReader {
22 public class ScoreListIterator implements ScoreIterator {
23 IndexReader.Iterator iterator;
25 VByteInput stream;
26 int documentCount;
27 int index;
29 int currentDocument;
30 double currentScore;
32 public ScoreListIterator( IndexReader.Iterator iterator ) throws IOException {
33 this.iterator = iterator;
34 load();
37 void load() throws IOException {
38 BufferedDataStream buffered = reader.blockStream(iterator);
39 stream = new VByteInput( buffered );
40 documentCount = stream.readInt();
41 index = -1;
42 currentDocument = 0;
44 if( documentCount > 0 )
45 read();
48 void read() throws IOException {
49 index += 1;
51 if( index < documentCount ) {
52 currentDocument += stream.readInt();
53 currentScore = stream.readFloat();
57 public void reset() throws IOException {
58 currentDocument = 0;
59 currentScore = 0;
60 load();
63 public int nextCandidate() {
64 return currentDocument;
67 public String currentTerm() {
68 return iterator.currentTerm();
71 public void nextTerm() throws IOException {
72 iterator.nextTerm();
73 load();
76 public boolean hasMatch(int document) {
77 return document == currentDocument;
80 public void moveTo(int document) throws IOException {
81 while( !isDone() && document > currentDocument )
82 read();
85 public void movePast(int document) throws IOException {
86 while( !isDone() && document >= currentDocument )
87 read();
90 public double score(int document, int length) {
91 if( document == currentDocument )
92 return currentScore;
93 return Double.NEGATIVE_INFINITY;
96 public boolean isDone() {
97 return index >= documentCount;
101 IndexReader reader;
103 public SparseFloatListReader( String pathname ) throws FileNotFoundException, IOException {
104 reader = new IndexReader( pathname );
107 public ScoreListIterator getIterator() throws IOException {
108 return new ScoreListIterator( reader.getIterator() );
111 public ScoreListIterator getScores( String term ) throws IOException {
112 IndexReader.Iterator iterator = reader.getTerm( term );
114 if( iterator != null )
115 return new ScoreListIterator( iterator );
117 return null;
120 void close() throws IOException {
121 reader.close();