Debian Codegraph @

morfologik-fsa/src/main/java/morfologik/fsa/ByteSequenceIterator.java

raw symbols SCIP
1 package morfologik.fsa;
2
3 import java.nio.ByteBuffer;
4 import java.util.*;
5
6 /**
7 * An iterator that traverses the right language of a given node (all sequences
8 * reachable from a given node).
9 */
10 public final class ByteSequenceIterator implements Iterator<ByteBuffer> {
11 /**
12 * Default expected depth of the recursion stack (estimated longest sequence
13 * in the automaton). Buffers expand by the same value if exceeded.
14 */
15 private final static int EXPECTED_MAX_STATES = 15;
16
17 /** The FSA to which this iterator belongs. */
18 private final FSA fsa;
19
20 /** An internal cache for the next element in the FSA */
21 private ByteBuffer nextElement;
22
23 /**
24 * A buffer for the current sequence of bytes from the current node to the
25 * root.
26 */
27 private byte[] buffer = new byte[EXPECTED_MAX_STATES];
28
29 /** Reusable byte buffer wrapper around {@link #buffer}. */
30 private ByteBuffer bufferWrapper = ByteBuffer.wrap(buffer);
31
32 /** An arc stack for DFS when processing the automaton. */
33 private int[] arcs = new int[EXPECTED_MAX_STATES];
34
35 /** Current processing depth in {@link #arcs}. */
36 private int position;
37
38 /**
39 * Create an instance of the iterator iterating over all automaton sequences.
40 *
41 * @param fsa The automaton to iterate over.
42 */
43 public ByteSequenceIterator(FSA fsa) {
44 this(fsa, fsa.getRootNode());
45 }
46
47 /**
48 * Create an instance of the iterator for a given node.
49 * @param fsa The automaton to iterate over.
50 * @param node The starting node's identifier (can be the {@link FSA#getRootNode()}).
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 * Restart walking from <code>node</code>. Allows iterator reuse.
62 *
63 * @param node Restart the iterator from <code>node</code>.
64 * @return Returns <code>this</code> for call chaining.
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 /** Returns <code>true</code> if there are still elements in this iterator. */
76 @Override
77 public boolean hasNext() {
78 if (nextElement == null) {
79 nextElement = advance();
80 }
81
82 return nextElement != null;
83 }
84
85 /**
86 * @return Returns a {@link ByteBuffer} with the sequence corresponding to the
87 * next final state in the automaton.
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 * Advances to the next available final state.
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 // Remove the current node from the queue.
118 position--;
119 continue;
120 }
121
122 // Go to the next arc, but leave it on the stack
123 // so that we keep the recursion depth level accurate.
124 arcs[lastIndex] = fsa.getNextArc(arc);
125
126 // Expand buffer if needed.
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 // Recursively descend into the arc's node.
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 * Not implemented in this iterator.
151 */
152 @Override
153 public void remove() {
154 throw new UnsupportedOperationException("Read-only iterator.");
155 }
156
157 /**
158 * Descends to a given node, adds its arcs to the stack to be traversed.
159 */
160 private void pushNode(int node) {
161 // Expand buffers if needed.
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 }