it.unimi.dsi.mg4j.search
Class DifferenceDocumentIterator

java.lang.Object
  extended by it.unimi.dsi.fastutil.ints.AbstractIntIterator
      extended by it.unimi.dsi.mg4j.search.AbstractDocumentIterator
          extended by it.unimi.dsi.mg4j.search.DifferenceDocumentIterator
All Implemented Interfaces:
IntIterator, DocumentIterator, Iterable<Interval>, Iterator<Integer>

public class DifferenceDocumentIterator
extends AbstractDocumentIterator
implements DocumentIterator

A document iterator that computes the Brouwerian difference between two given document iterators.

In the lattice of interval antichains, the Brouwerian difference is obtained by deleting from the first operand all intervals that contain some interval of the second operand. Thus, Brouwerian difference can be fruitfully employed to kill intervals containing a term or, even more fruitfully, to change at query time the granularity of an index by subtracting from the results of a query those length-two intervals that cross the cutpoints between the desired parts of the index.

Additionally, this class provides interval enlargment—by using a suitable factory method each interval returned by the subtrahend will be enlarged to the left and to the right by the given amount (e.g., if the left margin is 1 and the right margin is 2 the interval [2..3] will turn into [1..5]).

Since:
1.2
Author:
Sebastiano Vigna

Nested Class Summary
 
Nested classes/interfaces inherited from class it.unimi.dsi.mg4j.search.AbstractDocumentIterator
AbstractDocumentIterator.AbstractIntervalIterator
 
Field Summary
 
Fields inherited from class it.unimi.dsi.mg4j.search.AbstractDocumentIterator
last, next
 
Constructor Summary
protected DifferenceDocumentIterator(DocumentIterator minuendIterator, DocumentIterator subtrahendIterator, int leftMargin, int rightMargin)
          Creates a new difference document iterator given a minuend and a subtrahend iterator.
 
Method Summary
 boolean accept(DocumentIteratorVisitor visitor)
          Accepts a visitor.
 boolean acceptOnTruePaths(DocumentIteratorVisitor visitor)
          Accepts a visitor after a call to DocumentIterator.nextDocument(), limiting recursion to true paths.
 void dispose()
          Disposes this document iterator, releasing all resources.
static DocumentIterator getInstance(DocumentIterator minuendIterator, DocumentIterator subtrahendIterator)
          Returns new difference document iterator given a minuend and a subtrahend iterator.
static DocumentIterator getInstance(DocumentIterator minuendIterator, DocumentIterator subtrahendIterator, int leftMargin, int rightMargin)
          Returns new difference document iterator given a minuend and a subtrahend iterator.
 ReferenceSet<Index> indices()
          Returns the set of indices over which this iterator is built.
 IntervalIterator intervalIterator()
          Returns the interval iterator of this document iterator for single-index queries.
 IntervalIterator intervalIterator(Index index)
          Returns the interval iterator of this document iterator for the given index.
 Reference2ReferenceMap<Index,IntervalIterator> intervalIterators()
          Returns an unmodifiable map from indices to interval iterators.
 int nextDocument()
          Returns the next document provided by this document iterator, or -1 if no more documents are available.
 int skipTo(int n)
          Skips all documents smaller than n.
 String toString()
           
 
Methods inherited from class it.unimi.dsi.mg4j.search.AbstractDocumentIterator
document, hasNext, iterator, nextInt
 
Methods inherited from class it.unimi.dsi.fastutil.ints.AbstractIntIterator
next, remove, skip
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 
Methods inherited from interface it.unimi.dsi.mg4j.search.DocumentIterator
document, iterator, nextInt
 
Methods inherited from interface it.unimi.dsi.fastutil.ints.IntIterator
skip
 
Methods inherited from interface java.util.Iterator
hasNext, next, remove
 

Constructor Detail

DifferenceDocumentIterator

protected DifferenceDocumentIterator(DocumentIterator minuendIterator,
                                     DocumentIterator subtrahendIterator,
                                     int leftMargin,
                                     int rightMargin)
Creates a new difference document iterator given a minuend and a subtrahend iterator.

Parameters:
minuendIterator - the minuend.
subtrahendIterator - the subtrahend.
Method Detail

getInstance

public static DocumentIterator getInstance(DocumentIterator minuendIterator,
                                           DocumentIterator subtrahendIterator)
Returns new difference document iterator given a minuend and a subtrahend iterator.

Parameters:
minuendIterator - the minuend.
subtrahendIterator - the subtrahend.

getInstance

public static DocumentIterator getInstance(DocumentIterator minuendIterator,
                                           DocumentIterator subtrahendIterator,
                                           int leftMargin,
                                           int rightMargin)
Returns new difference document iterator given a minuend and a subtrahend iterator.

Parameters:
minuendIterator - the minuend.
subtrahendIterator - the subtrahend.
leftMargin - a margin that will be added to the left of each interval.
rightMargin - a margin that will be added to the right of each interval.

indices

public ReferenceSet<Index> indices()
Description copied from interface: DocumentIterator
Returns the set of indices over which this iterator is built.

Specified by:
indices in interface DocumentIterator
Returns:
the set of indices over which this iterator is built.

nextDocument

public int nextDocument()
                 throws IOException
Description copied from interface: DocumentIterator
Returns the next document provided by this document iterator, or -1 if no more documents are available.

Warning: the specification of this method has significantly changed as of MG4J 1.2. The special return value -1 is used to mark the end of iteration (a NoSuchElementException would have been thrown before in that case, so ho harm should be caused by this change). The reason for this change is providing fully lazy iteration over documents. Fully lazy iteration does not provide an hasNext() method—you have to actually ask for the next element and check the return value. Fully lazy iteration is much lighter on method calls (half) and in most (if not all) MG4J classes leads to a much simpler logic. Moreover, DocumentIterator.nextDocument() can be specified as throwing an IOException, which avoids the pernicious proliferation of try/catch blocks in very short, low-level methods (it was having a detectable impact on performance).

Specified by:
nextDocument in interface DocumentIterator
Returns:
the next document, or -1 if no more documents are available.
Throws:
IOException

skipTo

public int skipTo(int n)
           throws IOException
Description copied from interface: DocumentIterator
Skips all documents smaller than n.

Define the current document k associated with this document iterator as follows:

If k is larger than or equal to n, then this method does nothing and returns k. Otherwise, a call to this method is equivalent to

 while( ( k = nextDocument() ) < n && k != -1 );
 return k == -1 ? Integer.MAX_VALUE : k;
 

Thus, when a result kInteger.MAX_VALUE is returned, the state of this iterator will be exactly the same as after a call to DocumentIterator.nextDocument() that returned k. In particular, the first document larger than or equal to n (when returned by this method) will not be returned by the next call to DocumentIterator.nextDocument().

Specified by:
skipTo in interface DocumentIterator
Parameters:
n - a document pointer.
Returns:
a document pointer larger than or equal to n if available, Integer.MAX_VALUE otherwise.
Throws:
IOException

intervalIterators

public Reference2ReferenceMap<Index,IntervalIterator> intervalIterators()
                                                                 throws IOException
Description copied from interface: DocumentIterator
Returns an unmodifiable map from indices to interval iterators.

After a call to DocumentIterator.nextDocument(), this map can be used to retrieve the intervals in the current document. An invocation of Map.get(java.lang.Object) on this map with argument index yields the same result as intervalIterator(index).

Specified by:
intervalIterators in interface DocumentIterator
Returns:
a map from indices to interval iterators over the current document.
Throws:
IOException
See Also:
DocumentIterator.intervalIterator(Index)

intervalIterator

public IntervalIterator intervalIterator()
                                  throws IOException
Description copied from interface: DocumentIterator
Returns the interval iterator of this document iterator for single-index queries.

This is a commodity method that can be used only for queries built over a single index.

Specified by:
intervalIterator in interface DocumentIterator
Returns:
an interval iterator.
Throws:
IOException
See Also:
DocumentIterator.intervalIterator(Index)

intervalIterator

public IntervalIterator intervalIterator(Index index)
                                  throws IOException
Description copied from interface: DocumentIterator
Returns the interval iterator of this document iterator for the given index.

After a call to DocumentIterator.nextDocument(), this iterator can be used to retrieve the intervals in the current document (the one returned by DocumentIterator.nextDocument()) for the index index.

Note that if all indices have positions, it is guaranteed that at least one index will return an interval. However, for disjunctive queries it cannot be guaranteed that all indices will return an interval.

Indices without positions always return IntervalIterators.TRUE. Thus, in presence of indices without positions it is possible that no intervals at all are available.

Specified by:
intervalIterator in interface DocumentIterator
Parameters:
index - an index (must be one over which the query was built).
Returns:
an interval iterator over the current document in index.
Throws:
IOException

dispose

public void dispose()
             throws IOException
Description copied from interface: DocumentIterator
Disposes this document iterator, releasing all resources.

This method should propagate down to the underlying index iterators, where it should release resources such as open files and network connections. If you're doing your own resource tracking and pooling, then you do not need to call this method.

Specified by:
dispose in interface DocumentIterator
Throws:
IOException

accept

public boolean accept(DocumentIteratorVisitor visitor)
               throws IOException
Description copied from interface: DocumentIterator
Accepts a visitor.

A document iterator is usually structured as composite, with operators as internal nodes and IndexIterators as leaves. This method implements the visitor pattern.

Specified by:
accept in interface DocumentIterator
Parameters:
visitor - the visitor.
Returns:
true if the visit should continue.
Throws:
IOException

acceptOnTruePaths

public boolean acceptOnTruePaths(DocumentIteratorVisitor visitor)
                          throws IOException
Description copied from interface: DocumentIterator
Accepts a visitor after a call to DocumentIterator.nextDocument(), limiting recursion to true paths.

After a call to DocumentIterator.nextDocument(), a document iterator is positioned over a document. This call is equivalent to DocumentIterator.accept(DocumentIteratorVisitor), but visits only along true paths.

We define a true path as a path from the root of the composite that passes only through nodes whose associated subtree is positioned on the same document of the root. Note that OrDocumentIterators detach exhausted iterators from the composite tree, so true paths define the subtree that is causing the current document to satisfy the query represented by this document iterator.

For more elaboration, and the main application of this method, see CounterCollectionVisitor.

Specified by:
acceptOnTruePaths in interface DocumentIterator
Parameters:
visitor - the visitor.
Returns:
true if the visit should continue.
Throws:
IOException
See Also:
DocumentIterator.accept(DocumentIteratorVisitor), CounterCollectionVisitor

toString

public String toString()
Overrides:
toString in class Object