// if an iterator disagrees (it jumped longer), it's current position becomes the new targetPosition
// for the others and he is considered "first" in the voting round (every iterator votes for himself ;-)
int i = 0;
//iterator initialize, just one "next" for each DocIdSetIterator
for ( ; i<iteratorSize; i++ ) {
final DocIdSetIterator iterator = iterators[i];
if ( ! iterator.next() ) {
//current iterator has no values, so skip all
return EmptyDocIdBitSet.instance;
}
final int position = iterator.doc();
if ( targetPosition==position ) {
votes++; //stopped as same position of others
}
else {
targetPosition = max( targetPosition, position );
if (targetPosition==position) //means it changed
votes=1;
}
}
final OpenBitSet result = new OpenBitSet( maxDocNumber );
// end iterator initialize
if (votes==iteratorSize) {
result.fastSet( targetPosition );
targetPosition++;
}
i=0;
votes=0; //could be smarted but would make the code even more complex for a minor optimization out of cycle.
// enter main loop:
while ( true ) {
final DocIdSetIterator iterator = iterators[i];
final boolean validPosition = iterator.skipTo( targetPosition );
if ( ! validPosition )
return result; //exit condition
final int position = iterator.doc();
if ( position == targetPosition ) {
if ( ++votes == iteratorSize ) {
result.fastSet( position );
votes = 0;
targetPosition++;