| 1 |
package morfologik.fsa; |
| 2 |
|
| 3 |
import java.nio.ByteBuffer; |
| 4 |
import java.util.*; |
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
public final class ByteSequenceIterator implements Iterator<ByteBuffer> { |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
private final static int EXPECTED_MAX_STATES = 15; |
| 16 |
|
| 17 |
|
| 18 |
private final FSA fsa; |
| 19 |
|
| 20 |
|
| 21 |
private ByteBuffer nextElement; |
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
private byte[] buffer = new byte[EXPECTED_MAX_STATES]; |
| 28 |
|
| 29 |
|
| 30 |
private ByteBuffer bufferWrapper = ByteBuffer.wrap(buffer); |
| 31 |
|
| 32 |
|
| 33 |
private int[] arcs = new int[EXPECTED_MAX_STATES]; |
| 34 |
|
| 35 |
|
| 36 |
private int position; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public ByteSequenceIterator(FSA fsa) { |
| 44 |
this(fsa, fsa.getRootNode()); |
| 45 |
} |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
public ByteSequenceIterator(FSA fsa, int node) { |
| 53 |
this.fsa = fsa; |
| 54 |
|
| 55 |
if (fsa.getFirstArc(node) != 0) { |
| 56 |
restartFrom(node); |
| 57 |
} |
| 58 |
} |
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
public ByteSequenceIterator restartFrom(int node) { |
| 67 |
position = 0; |
| 68 |
bufferWrapper.clear(); |
| 69 |
nextElement = null; |
| 70 |
|
| 71 |
pushNode(node); |
| 72 |
return this; |
| 73 |
} |
| 74 |
|
| 75 |
|
| 76 |
@Override |
| 77 |
public boolean hasNext() { |
| 78 |
if (nextElement == null) { |
| 79 |
nextElement = advance(); |
| 80 |
} |
| 81 |
|
| 82 |
return nextElement != null; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
@Override |
| 90 |
public ByteBuffer next() { |
| 91 |
if (nextElement != null) { |
| 92 |
final ByteBuffer cache = nextElement; |
| 93 |
nextElement = null; |
| 94 |
return cache; |
| 95 |
} else { |
| 96 |
final ByteBuffer cache = advance(); |
| 97 |
if (cache == null) { |
| 98 |
throw new NoSuchElementException(); |
| 99 |
} |
| 100 |
return cache; |
| 101 |
} |
| 102 |
} |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
private final ByteBuffer advance() { |
| 108 |
if (position == 0) { |
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
while (position > 0) { |
| 113 |
final int lastIndex = position - 1; |
| 114 |
final int arc = arcs[lastIndex]; |
| 115 |
|
| 116 |
if (arc == 0) { |
| 117 |
|
| 118 |
position--; |
| 119 |
continue; |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
arcs[lastIndex] = fsa.getNextArc(arc); |
| 125 |
|
| 126 |
|
| 127 |
final int bufferLength = this.buffer.length; |
| 128 |
if (lastIndex >= bufferLength) { |
| 129 |
this.buffer = Arrays.copyOf(buffer, bufferLength + EXPECTED_MAX_STATES); |
| 130 |
this.bufferWrapper = ByteBuffer.wrap(buffer); |
| 131 |
} |
| 132 |
buffer[lastIndex] = fsa.getArcLabel(arc); |
| 133 |
|
| 134 |
if (!fsa.isArcTerminal(arc)) { |
| 135 |
|
| 136 |
pushNode(fsa.getEndNode(arc)); |
| 137 |
} |
| 138 |
|
| 139 |
if (fsa.isArcFinal(arc)) { |
| 140 |
bufferWrapper.clear(); |
| 141 |
bufferWrapper.limit(lastIndex + 1); |
| 142 |
return bufferWrapper; |
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
return null; |
| 147 |
} |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
@Override |
| 153 |
public void remove() { |
| 154 |
throw new UnsupportedOperationException("Read-only iterator."); |
| 155 |
} |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
private void pushNode(int node) { |
| 161 |
|
| 162 |
if (position == arcs.length) { |
| 163 |
arcs = Arrays.copyOf(arcs, arcs.length + EXPECTED_MAX_STATES); |
| 164 |
} |
| 165 |
|
| 166 |
arcs[position++] = fsa.getFirstArc(node); |
| 167 |
} |
| 168 |
} |