forked from jweese/thrax
-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactoring of sequence file iterator.
- Loading branch information
Tobias Domhan
committed
Dec 1, 2016
1 parent
0d766be
commit 705fdb7
Showing
3 changed files
with
142 additions
and
71 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
67 changes: 67 additions & 0 deletions
67
src/edu/jhu/thrax/lexprob/SequenceFileTableEntryIterator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
package edu.jhu.thrax.lexprob; | ||
|
||
import java.io.IOException; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
import java.util.Optional; | ||
|
||
import org.apache.hadoop.io.FloatWritable; | ||
import org.apache.hadoop.io.LongWritable; | ||
import org.apache.hadoop.io.SequenceFile; | ||
|
||
public class SequenceFileTableEntryIterator implements Iterator<TableEntry> { | ||
|
||
private final SequenceFile.Reader reader; | ||
|
||
private final LongWritable pair = new LongWritable(); | ||
private final FloatWritable d = new FloatWritable(0.0f); | ||
|
||
private Optional<TableEntry> lookahead = Optional.empty(); | ||
private boolean finishedReading = false; | ||
|
||
public SequenceFileTableEntryIterator(SequenceFile.Reader reader) { | ||
this.reader = reader; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (lookahead.isPresent()) { | ||
return true; | ||
} | ||
lookahead = tryReadNext(); | ||
if (lookahead.isPresent()) { | ||
return true; | ||
} else { | ||
return false; | ||
} | ||
} | ||
|
||
@Override | ||
public TableEntry next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
TableEntry nextEntry = lookahead.get(); | ||
lookahead = Optional.empty(); | ||
return nextEntry; | ||
} | ||
|
||
private Optional<TableEntry> tryReadNext() { | ||
if (finishedReading) { | ||
return Optional.empty(); | ||
} | ||
try { | ||
boolean gotNew = reader.next(pair, d); | ||
if (gotNew) { | ||
// there was something to read | ||
return Optional.of(new TableEntry(pair, d)); | ||
} else { | ||
finishedReading = true; | ||
return Optional.empty(); | ||
} | ||
} catch (IOException e) { | ||
throw new RuntimeException(e); | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
package edu.jhu.thrax.util; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
import java.util.NoSuchElementException; | ||
|
||
public class ChainedIterators<T> implements Iterator<T> { | ||
|
||
private Iterator<? extends Iterator<T>> iteratorOfIterators; | ||
private Iterator<T> currentIterator; | ||
private boolean finished = false; | ||
|
||
public ChainedIterators(Iterator<? extends Iterator<T>> iteratorOfIterators) { | ||
this.iteratorOfIterators = iteratorOfIterators; | ||
moveToNextIterator(); | ||
} | ||
|
||
public ChainedIterators(Collection<? extends Iterator<T>> iteratorOfIterators) { | ||
this.iteratorOfIterators = iteratorOfIterators.iterator(); | ||
moveToNextIterator(); | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
if (finished) { | ||
return false; | ||
} | ||
if (currentIterator.hasNext()) { | ||
return true; | ||
} else { | ||
moveToNextIterator(); | ||
return !finished; | ||
} | ||
} | ||
|
||
@Override | ||
public T next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
return currentIterator.next(); | ||
} | ||
|
||
private void moveToNextIterator() { | ||
while (iteratorOfIterators.hasNext()) { | ||
currentIterator = iteratorOfIterators.next(); | ||
if (currentIterator.hasNext()) { | ||
finished = false; | ||
return; | ||
} | ||
} | ||
finished = true; | ||
} | ||
|
||
} |