001: package it.unimi.dsi.mg4j.index.cluster;
002:
003: /*
004: * MG4J: Managing Gigabytes for Java
005: *
006: * Copyright (C) 2006-2007 Sebastiano Vigna
007: *
008: * This library is free software; you can redistribute it and/or modify it
009: * under the terms of the GNU Lesser General Public License as published by the Free
010: * Software Foundation; either version 2.1 of the License, or (at your option)
011: * any later version.
012: *
013: * This library is distributed in the hope that it will be useful, but
014: * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
015: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License
016: * for more details.
017: *
018: * You should have received a copy of the GNU Lesser General Public License
019: * along with this program; if not, write to the Free Software
020: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
021: *
022: */
023: import java.io.IOException;
024:
025: import it.unimi.dsi.fastutil.ints.IntIterator;
026: import it.unimi.dsi.mg4j.index.Index;
027: import it.unimi.dsi.mg4j.index.IndexIterator;
028: import it.unimi.dsi.mg4j.index.payload.Payload;
029: import it.unimi.dsi.mg4j.search.visitor.DocumentIteratorVisitor;
030:
031: /** An index iterator merging iterators from local indices.
032: *
033: * @author Alessandro Arrabito
034: * @author Sebastiano Vigna
035: */
036:
037: public class DocumentalMergedClusterIndexIterator extends
038: DocumentalMergedClusterDocumentIterator implements
039: IndexIterator {
040: /** A cached copy of the iterators, to avoid type casts. */
041: protected final IndexIterator[] indexIterator;
042: /** The precomputed frequency. */
043: protected final int frequency;
044: /** The reference index. */
045: protected final DocumentalMergedCluster index;
046: /** The term associated to this index iterator. */
047: protected String term;
048: /** The id associated to this index iterator. */
049: protected int id;
050:
051: public DocumentalMergedClusterIndexIterator(
052: final DocumentalClusterIndexReader indexReader,
053: final IndexIterator[] indexIterator, final int[] usedIndex)
054: throws IOException {
055: super (indexReader, indexIterator, usedIndex);
056: this .index = (DocumentalMergedCluster) indexReader.index;
057: this .indexIterator = indexIterator;
058: int t = 0;
059: for (int i = indexIterator.length; i-- != 0;)
060: t += indexIterator[i].frequency();
061: frequency = t;
062: }
063:
064: public void term(final CharSequence term) {
065: this .term = term == null ? null : term.toString();
066: }
067:
068: public String term() {
069: return term;
070: }
071:
072: public void id(final int id) {
073: this .id = id;
074: }
075:
076: public int id() {
077: return id;
078: }
079:
080: public Index index() {
081: return index;
082: }
083:
084: public int frequency() {
085: return frequency;
086: }
087:
088: public Payload payload() throws IOException {
089: if (currentIterator < 0)
090: throw new IllegalStateException(
091: "There is no current payload: nextDocument() has never been called");
092: return indexIterator[currentIterator].payload();
093: }
094:
095: public int count() throws IOException {
096: if (currentIterator < 0)
097: throw new IllegalStateException(
098: "There is no current count: nextDocument() has never been called");
099: return indexIterator[currentIterator].count();
100: }
101:
102: public IntIterator positions() throws IOException {
103: if (currentIterator < 0)
104: throw new IllegalStateException(
105: "There are no current positions: nextDocument() has never been called");
106: return indexIterator[currentIterator].positions();
107: }
108:
109: public int positions(int[] positions) throws IOException {
110: if (currentIterator < 0)
111: throw new IllegalStateException(
112: "There are no current positions: nextDocument() has never been called");
113: return indexIterator[currentIterator].positions(positions);
114: }
115:
116: public int[] positionArray() throws IOException {
117: if (currentIterator < 0)
118: throw new IllegalStateException(
119: "There are no current positions: nextDocument() has never been called");
120: return indexIterator[currentIterator].positionArray();
121: }
122:
123: public boolean accept(final DocumentIteratorVisitor visitor)
124: throws IOException {
125: return visitor.visit(this );
126: }
127:
128: public boolean acceptOnTruePaths(
129: final DocumentIteratorVisitor visitor) throws IOException {
130: return visitor.visit(this );
131: }
132:
133: public int termNumber() {
134: // ALERT: don't we need some remapping?
135: return indexIterator[currentIterator].termNumber();
136: }
137:
138: }
|