@@ -87,7 +87,7 @@ private long getFallback(FSTCompiler.UnCompiledNode<T> nodeIn, long hash) throws
8787 if (node == 0 ) {
8888 // not found
8989 return 0 ;
90- } else if (nodesEqual (fallbackTable , nodeIn , node )) {
90+ } else if (fallbackTable . nodesEqual (nodeIn , node )) {
9191 // frozen version of this node is already here
9292 return node ;
9393 }
@@ -158,7 +158,7 @@ public long add(FSTCompiler.UnCompiledNode<T> nodeIn) throws IOException {
158158
159159 return node ;
160160
161- } else if (nodesEqual (primaryTable , nodeIn , node )) {
161+ } else if (primaryTable . nodesEqual (nodeIn , node )) {
162162 // same node (in frozen form) is already in primary table
163163 return node ;
164164 }
@@ -192,7 +192,7 @@ private long hash(FSTCompiler.UnCompiledNode<T> node) {
192192 // hash code for a frozen node. this must precisely match the hash computation of an unfrozen
193193 // node!
194194 private long hash (long node ) throws IOException {
195- FST .BytesReader in = getBytesReader (primaryTable , node );
195+ FST .BytesReader in = primaryTable . getBytesReader (node );
196196
197197 final int PRIME = 31 ;
198198
@@ -215,74 +215,6 @@ private long hash(long node) throws IOException {
215215 return h ;
216216 }
217217
218- /**
219- * Compares an unfrozen node (UnCompiledNode) with a frozen node at byte location address (long),
220- * returning true if they are equal.
221- */
222- private boolean nodesEqual (
223- PagedGrowableHash table , FSTCompiler .UnCompiledNode <T > node , long address )
224- throws IOException {
225- FST .BytesReader in = getBytesReader (table , address );
226- fstCompiler .fst .readFirstRealTargetArc (address , scratchArc , in );
227-
228- // fail fast for a node with fixed length arcs
229- if (scratchArc .bytesPerArc () != 0 ) {
230- assert node .numArcs > 0 ;
231- // the frozen node uses fixed-with arc encoding (same number of bytes per arc), but may be
232- // sparse or dense
233- switch (scratchArc .nodeFlags ()) {
234- case FST .ARCS_FOR_BINARY_SEARCH :
235- // sparse
236- if (node .numArcs != scratchArc .numArcs ()) {
237- return false ;
238- }
239- break ;
240- case FST .ARCS_FOR_DIRECT_ADDRESSING :
241- // dense -- compare both the number of labels allocated in the array (some of which may
242- // not actually be arcs), and the number of arcs
243- if ((node .arcs [node .numArcs - 1 ].label - node .arcs [0 ].label + 1 ) != scratchArc .numArcs ()
244- || node .numArcs != FST .Arc .BitTable .countBits (scratchArc , in )) {
245- return false ;
246- }
247- break ;
248- default :
249- throw new AssertionError ("unhandled scratchArc.nodeFlag() " + scratchArc .nodeFlags ());
250- }
251- }
252-
253- // compare arc by arc to see if there is a difference
254- for (int arcUpto = 0 ; arcUpto < node .numArcs ; arcUpto ++) {
255- final FSTCompiler .Arc <T > arc = node .arcs [arcUpto ];
256- if (arc .label != scratchArc .label ()
257- || arc .output .equals (scratchArc .output ()) == false
258- || ((FSTCompiler .CompiledNode ) arc .target ).node != scratchArc .target ()
259- || arc .nextFinalOutput .equals (scratchArc .nextFinalOutput ()) == false
260- || arc .isFinal != scratchArc .isFinal ()) {
261- return false ;
262- }
263-
264- if (scratchArc .isLast ()) {
265- if (arcUpto == node .numArcs - 1 ) {
266- return true ;
267- } else {
268- return false ;
269- }
270- }
271-
272- fstCompiler .fst .readNextRealArc (scratchArc , in );
273- }
274-
275- // unfrozen node has fewer arcs than frozen node
276-
277- return false ;
278- }
279-
280- private static <T > FST .BytesReader getBytesReader (
281- NodeHash <T >.PagedGrowableHash table , long address ) {
282- byte [] bytes = table .getBytes (address );
283- return new RelativeReverseBytesReader (bytes , address - bytes .length + 1 );
284- }
285-
286218 /** Inner class because it needs access to hash function and FST bytes. */
287219 private class PagedGrowableHash {
288220 private PagedGrowableWriter entries ;
@@ -359,5 +291,71 @@ private void rehash(long lastNodeAddress) throws IOException {
359291 mask = newMask ;
360292 entries = newEntries ;
361293 }
294+
295+ /**
296+ * Compares an unfrozen node (UnCompiledNode) with a frozen node at byte location address (long),
297+ * returning true if they are equal.
298+ */
299+ private boolean nodesEqual (FSTCompiler .UnCompiledNode <T > node , long address )
300+ throws IOException {
301+ FST .BytesReader in = getBytesReader (address );
302+ fstCompiler .fst .readFirstRealTargetArc (address , scratchArc , in );
303+
304+ // fail fast for a node with fixed length arcs
305+ if (scratchArc .bytesPerArc () != 0 ) {
306+ assert node .numArcs > 0 ;
307+ // the frozen node uses fixed-with arc encoding (same number of bytes per arc), but may be
308+ // sparse or dense
309+ switch (scratchArc .nodeFlags ()) {
310+ case FST .ARCS_FOR_BINARY_SEARCH :
311+ // sparse
312+ if (node .numArcs != scratchArc .numArcs ()) {
313+ return false ;
314+ }
315+ break ;
316+ case FST .ARCS_FOR_DIRECT_ADDRESSING :
317+ // dense -- compare both the number of labels allocated in the array (some of which may
318+ // not actually be arcs), and the number of arcs
319+ if ((node .arcs [node .numArcs - 1 ].label - node .arcs [0 ].label + 1 ) != scratchArc .numArcs ()
320+ || node .numArcs != FST .Arc .BitTable .countBits (scratchArc , in )) {
321+ return false ;
322+ }
323+ break ;
324+ default :
325+ throw new AssertionError ("unhandled scratchArc.nodeFlag() " + scratchArc .nodeFlags ());
326+ }
327+ }
328+
329+ // compare arc by arc to see if there is a difference
330+ for (int arcUpto = 0 ; arcUpto < node .numArcs ; arcUpto ++) {
331+ final FSTCompiler .Arc <T > arc = node .arcs [arcUpto ];
332+ if (arc .label != scratchArc .label ()
333+ || arc .output .equals (scratchArc .output ()) == false
334+ || ((FSTCompiler .CompiledNode ) arc .target ).node != scratchArc .target ()
335+ || arc .nextFinalOutput .equals (scratchArc .nextFinalOutput ()) == false
336+ || arc .isFinal != scratchArc .isFinal ()) {
337+ return false ;
338+ }
339+
340+ if (scratchArc .isLast ()) {
341+ if (arcUpto == node .numArcs - 1 ) {
342+ return true ;
343+ } else {
344+ return false ;
345+ }
346+ }
347+
348+ fstCompiler .fst .readNextRealArc (scratchArc , in );
349+ }
350+
351+ // unfrozen node has fewer arcs than frozen node
352+
353+ return false ;
354+ }
355+
356+ private <T > FST .BytesReader getBytesReader (long address ) {
357+ byte [] bytes = getBytes (address );
358+ return new RelativeReverseBytesReader (bytes , address - bytes .length + 1 );
359+ }
362360 }
363361}
0 commit comments