Class BytesTrie

  • All Implemented Interfaces:
    Cloneable, Iterable<BytesTrie.Entry>

    public final class BytesTrie
    extends Object
    implements Cloneable, Iterable<BytesTrie.Entry>
    Light-weight, non-const reader class for a BytesTrie. Traverses a byte-serialized data structure with minimal state, for mapping byte sequences to non-negative integer values.

    This class is not intended for public subclassing.

    Author:
    Markus W. Scherer
    • Constructor Detail

      • BytesTrie

        public BytesTrie​(byte[] trieBytes,
                         int offset)
        Constructs a BytesTrie reader instance.

        The array must contain a copy of a byte sequence from the BytesTrieBuilder, with the offset indicating the first byte of that sequence. The BytesTrie object will not read more bytes than the BytesTrieBuilder generated in the corresponding build() call.

        The array is not copied/cloned and must not be modified while the BytesTrie object is in use.

        Parameters:
        trieBytes - Bytes array that contains the serialized trie.
        offset - Root offset of the trie in the array.
    • Method Detail

      • reset

        public BytesTrie reset()
        Resets this trie to its initial state.
        Returns:
        this
      • current

        public BytesTrie.Result current()
        Determines whether the byte sequence so far matches, whether it has a value, and whether another input byte can continue a matching byte sequence.
        Returns:
        The match/value Result.
      • first

        public BytesTrie.Result first​(int inByte)
        Traverses the trie from the initial state for this input byte. Equivalent to reset().next(inByte).
        Parameters:
        inByte - Input byte value. Values -0x100..-1 are treated like 0..0xff. Values below -0x100 and above 0xff will never match.
        Returns:
        The match/value Result.
      • next

        public BytesTrie.Result next​(int inByte)
        Traverses the trie from the current state for this input byte.
        Parameters:
        inByte - Input byte value. Values -0x100..-1 are treated like 0..0xff. Values below -0x100 and above 0xff will never match.
        Returns:
        The match/value Result.
      • next

        public BytesTrie.Result next​(byte[] s,
                                     int sIndex,
                                     int sLimit)
        Traverses the trie from the current state for this byte sequence. Equivalent to
         Result result=current();
         for(each c in s)
           if(!result.hasNext()) return Result.NO_MATCH;
           result=next(c);
         return result;
         
        Parameters:
        s - Contains a string or byte sequence.
        sIndex - The start index of the byte sequence in s.
        sLimit - The (exclusive) end index of the byte sequence in s.
        Returns:
        The match/value Result.
      • getValue

        public int getValue()
        Returns a matching byte sequence's value if called immediately after current()/first()/next() returned Result.INTERMEDIATE_VALUE or Result.FINAL_VALUE. getValue() can be called multiple times. Do not call getValue() after Result.NO_MATCH or Result.NO_VALUE!
        Returns:
        The value for the byte sequence so far.
      • getUniqueValue

        public long getUniqueValue()
        Determines whether all byte sequences reachable from the current state map to the same value, and if so, returns that value.
        Returns:
        The unique value in bits 32..1 with bit 0 set, if all byte sequences reachable from the current state map to the same value; otherwise returns 0.
      • getNextBytes

        public int getNextBytes​(Appendable out)
        Finds each byte which continues the byte sequence from the current state. That is, each byte b for which it would be next(b)!=Result.NO_MATCH now.
        Parameters:
        out - Each next byte is 0-extended to a char and appended to this object. (Only uses the out.append(c) method.)
        Returns:
        The number of bytes which continue the byte sequence from here.
      • iterator

        public BytesTrie.Iterator iterator​(int maxStringLength)
        Iterates from the current state of this trie.
        Parameters:
        maxStringLength - If 0, the iterator returns full strings/byte sequences. Otherwise, the iterator returns strings with this maximum length.
        Returns:
        A new BytesTrie.Iterator.
      • iterator

        public static BytesTrie.Iterator iterator​(byte[] trieBytes,
                                                  int offset,
                                                  int maxStringLength)
        Iterates from the root of a byte-serialized BytesTrie.
        Parameters:
        trieBytes - Bytes array that contains the serialized trie.
        offset - Root offset of the trie in the array.
        maxStringLength - If 0, the iterator returns full strings/byte sequences. Otherwise, the iterator returns strings with this maximum length.
        Returns:
        A new BytesTrie.Iterator.