private void computeLabelsIndex ( final FSA fsa ) { // Compute labels count. final int [ ] countByValue = new int [ 256 ] ; fsa . visitAllStates ( new StateVisitor ( ) { public boolean accept ( int state ) { for ( int arc = fsa . getFirstArc ( state ) ; arc != 0 ; arc = fsa . getNextArc ( arc ) ) countByValue [ fsa . getArcLabel ( arc ) & 0xff ] ++ ; return true ; } } ) ; // Order by descending frequency of counts and increasing label value. Comparator < IntIntHolder > comparator = new Comparator < IntIntHolder > ( ) { public int compare ( IntIntHolder o1 , IntIntHolder o2 ) { int countDiff = o2 . b - o1 . b ; if ( countDiff == 0 ) { countDiff = o1 . a - o2 . a ; } return countDiff ; } } ; TreeSet < IntIntHolder > labelAndCount = new TreeSet < IntIntHolder > ( comparator ) ; for ( int label = 0 ; label < countByValue . length ; label ++ ) { if ( countByValue [ label ] > 0 ) { labelAndCount . add ( new IntIntHolder ( label , countByValue [ label ] ) ) ; } } labelsIndex = new byte [ 1 + Math . min ( labelAndCount . size ( ) , CFSA2 . LABEL_INDEX_SIZE ) ] ; labelsInvIndex = new int [ 256 ] ; for ( int i = labelsIndex . length - 1 ; i > 0 && ! labelAndCount . isEmpty ( ) ; i -- ) { IntIntHolder p = labelAndCount . first ( ) ; labelAndCount . remove ( p ) ; labelsInvIndex [ p . a ] = i ; labelsIndex [ i ] = ( byte ) p . a ; } }
Compute a set of labels to be integrated with the flags field .