/** We scan recursively the tree, making a copy that uses lightweight nodes. */ private TreeDecoder.Node rebuildTree( final LevelNode n ) { if ( n == null ) return null; if ( n.symbol != -1 ) return new TreeDecoder.LeafNode( n.symbol ); TreeDecoder.Node newNode = new TreeDecoder.Node(); newNode.left = rebuildTree( (LevelNode) n.left ); newNode.right = rebuildTree( (LevelNode) n.right ); return newNode; }
/** Create a new transformation strategy based on a prefix-free coder. * * @param coder a prefix-free coder. * @param char2symbol a map from character to symbols (the default returned value will be used for unknown symbols). * @param prefixFree whether it is required that the resulting bit vectors are prefix-free: in this case, symbol 0 will * be appended to each string, and will not be allowed to appear in any string. */ public PrefixCoderTransformationStrategy( final PrefixCoder coder, final Char2IntOpenHashMap char2symbol, final boolean prefixFree ) { this( coder.codeWords(), char2symbol, prefixFree ); }
public int decode( final BooleanIterator iterator ) { final int[] lengthIncrement = this.lengthIncrement; final long[] lastCodeWordPlusOne = this.lastCodeWordPlusOne; int curr = 0, l; long x; x = readLong( iterator, lengthIncrement[ curr ] ); for(;;) { if ( x < lastCodeWordPlusOne[ curr ] ) return symbol[ (int)( howManyUpToBlock[ curr ] - lastCodeWordPlusOne[ curr ] + x ) ]; l = lengthIncrement[ ++curr ]; x = x << l | readLong( iterator, l ); } }
/** * (Re-)constructs the canonical huffman code from the shortest code word, * the non-decreasing bit lengths of each code word, and the permutation of * the symbols corresponding to those bit lengths. This information is * necessary and sufficient to reconstruct a canonical huffman code. * * @param decoderInputs * This contains the necessary and sufficient information to * recreate the {@link PrefixCoder}. * * @return A new {@link PrefixCoder} instance for the corresponding * canonical huffman code. */ static public PrefixCoder newCoder(final DecoderInputs decoderInputs) { return newCoder(decoderInputs.getShortestCodeWord(), decoderInputs .getLengths(), decoderInputs.getSymbols()); }
/** Creates a new Huffman codec using the given vector of frequencies. * * @param frequency a vector of nonnnegative frequencies. */ public HuffmanCodec( final int[] frequency ) { this(frequency, new DecoderInputs()); }
/** Creates a new codeword-based decoder starting from a set * of complete, lexicographically ordered codewords. It * is responsability of the caller that the tree is well-formed, * that is, that the provided codewords are exactly the root-to-leaf * paths of such a tree. * * @param lexSortedCodeWord a vector of lexically sorted codeWords. * @param symbol a mapping from codewords to symbols. */ public TreeDecoder( BitVector[] lexSortedCodeWord, int[] symbol ) { this( buildTree( lexSortedCodeWord, symbol, 0, 0, lexSortedCodeWord.length ), lexSortedCodeWord.length ); }
@Override public BitVector[] codeWords() { return coder.codeWords(); } }
/** Creates a new Huffman codec using the given vector of frequencies. * * @param frequency a vector of nonnnegative frequencies. */ public HuffmanCodec(final int[] frequency) { this(intArray2LongArray(frequency)); }
/** We scan recursively the tree, making a copy that uses lightweight nodes. */ private TreeDecoder.Node rebuildTree( final LevelNode n ) { if ( n == null ) return null; if ( n.symbol != -1 ) return new TreeDecoder.LeafNode( n.symbol ); TreeDecoder.Node newNode = new TreeDecoder.Node(); newNode.left = rebuildTree( (LevelNode) n.left ); newNode.right = rebuildTree( (LevelNode) n.right ); return newNode; }
/** Create a new transformation strategy based on a prefix-free coder. * * @param coder a prefix-free coder. * @param char2symbol a map from character to symbols (the default returned value will be used for unknown symbols). * @param prefixFree whether it is required that the resulting bit vectors are prefix-free: in this case, symbol 0 will * be appended to each string, and will not be allowed to appear in any string. */ public PrefixCoderTransformationStrategy(final PrefixCoder coder, final Char2IntOpenHashMap char2symbol, final boolean prefixFree) { this(coder.codeWords(), char2symbol, prefixFree); }
/** Creates a new Huffman codec using the given vector of frequencies. * * @param frequency a vector of nonnnegative frequencies. */ public HuffmanCodec( final int[] frequency ) { this(frequency, new DecoderInputs()); }
public int decode( final BooleanIterator iterator ) { final int[] lengthIncrement = this.lengthIncrement; final long[] lastCodeWordPlusOne = this.lastCodeWordPlusOne; int curr = 0, l; long x; x = readLong( iterator, lengthIncrement[ curr ] ); for(;;) { if ( x < lastCodeWordPlusOne[ curr ] ) return symbol[ (int)( howManyUpToBlock[ curr ] - lastCodeWordPlusOne[ curr ] + x ) ]; l = lengthIncrement[ ++curr ]; x = x << l | readLong( iterator, l ); } }
/** Creates a new codeword-based decoder starting from a set * of complete, lexicographically ordered codewords. It * is responsibility of the caller that the tree is well-formed, * that is, that the provided codewords are exactly the root-to-leaf * paths of such a tree. * * @param lexSortedCodeWord a vector of lexically sorted codeWords. * @param symbol a mapping from codewords to symbols. */ public TreeDecoder(BitVector[] lexSortedCodeWord, int[] symbol) { this(buildTree(lexSortedCodeWord, symbol, 0, 0, lexSortedCodeWord.length), lexSortedCodeWord.length); }
/** We scan recursively the tree, making a copy that uses lightweight nodes. */ private TreeDecoder.Node rebuildTree(final LevelNode n) { if (n == null) return null; if (n.symbol != -1) return new TreeDecoder.LeafNode(n.symbol); TreeDecoder.Node newNode = new TreeDecoder.Node(); newNode.left = rebuildTree((LevelNode) n.left); newNode.right = rebuildTree((LevelNode) n.right); return newNode; }
/** Create a new transformation strategy based on a prefix-free coder. * * @param coder a prefix-free coder. * @param char2symbol a map from character to symbols (the default returned value will be used for unknown symbols). * @param prefixFree whether it is required that the resulting bit vectors are prefix-free: in this case, symbol 0 will * be appended to each string, and will not be allowed to appear in any string. */ public PrefixCoderTransformationStrategy( final PrefixCoder coder, final Char2IntOpenHashMap char2symbol, final boolean prefixFree ) { this( coder.codeWords(), char2symbol, prefixFree ); }
@Override public int decode(final BooleanIterator iterator) { final int[] lengthIncrement = this.lengthIncrement; final long[] lastCodeWordPlusOne = this.lastCodeWordPlusOne; int curr = 0, l; long x; x = readLong(iterator, lengthIncrement[curr]); for(;;) { if (x < lastCodeWordPlusOne[curr]) return symbol[(int)(howManyUpToBlock[curr] - lastCodeWordPlusOne[curr] + x)]; l = lengthIncrement[++curr]; x = x << l | readLong(iterator, l); } }
/** Creates a new codeword-based decoder starting from a set * of complete, lexicographically ordered codewords. It * is responsability of the caller that the tree is well-formed, * that is, that the provided codewords are exactly the root-to-leaf * paths of such a tree. * * @param lexSortedCodeWord a vector of lexically sorted codeWords. * @param symbol a mapping from codewords to symbols. */ public TreeDecoder( BitVector[] lexSortedCodeWord, int[] symbol ) { this( buildTree( lexSortedCodeWord, symbol, 0, 0, lexSortedCodeWord.length ), lexSortedCodeWord.length ); }