pt.tumba.ngram.compression
Class ArithEncoder

java.lang.Object
  extended by pt.tumba.ngram.compression.ArithEncoder

public final class ArithEncoder
extends java.lang.Object

Performs arithmetic encoding, converting cumulative probability interval input into bit output. Cumulative probability intervals are given as integer counts low, high and total, with the range being [low/total,high/total).

Author:
Bruno Martins
See Also:
ArithDecoder, BitOutput

Field Summary
private  int _bitsToFollow
          Number of bits beyond first bit that were normalized.
protected  long _high
          The high bound on the current interval for coding.
protected  long _low
          The low bound on the current interval for coding.
private  BitOutput _out
          Bit output stream for writing encoding bits.
protected static int CODE_VALUE_BITS
          Precision of coding, expressed in number of bits used for arithmetic before shifting out partial results.
protected static long FIRST_QUARTER
          1/4 of the largest possible value plus one.
protected static long HALF
          1/2 of the largest possible value; 2 * FIRST_QUARTER
protected static long THIRD_QUARTER
          3/4 of the largest possible value; 3 * FIRST_QUARTER
protected static long TOP_VALUE
          The largest possible interval value.
 
Constructor Summary
ArithEncoder(BitOutput out)
          Construct an arithmetic coder from a bit output.
ArithEncoder(java.io.OutputStream out)
          Construct an arithmetic coder from an output stream.
 
Method Summary
private  void bitPlusFollowFalse()
          Write a false bit, and then a number of true bits equal to the number of bits to follow.
private  void bitPlusFollowTrue()
          Write a true bit, and then a number of false bits equal to the number of bits to follow.
 void close()
          Close the arithmetic encoder, writing all bits that are buffered and closing the underlying output streams.
 void encode(int[] counts)
          Encodes an interval expressed as a low count, high count and total count in an array {low,high,total}.
 void encode(int lowCount, int highCount, int totalCount)
          Encodes an interval expressed as a low count, high count and total count.
 void flush()
          Flushes bit output.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_low

protected long _low
The low bound on the current interval for coding. Initialized to zero.


_high

protected long _high
The high bound on the current interval for coding. Initialized to top value possible.


CODE_VALUE_BITS

protected static final int CODE_VALUE_BITS
Precision of coding, expressed in number of bits used for arithmetic before shifting out partial results.

See Also:
Constant Field Values

TOP_VALUE

protected static final long TOP_VALUE
The largest possible interval value. All 1s.

See Also:
Constant Field Values

FIRST_QUARTER

protected static final long FIRST_QUARTER
1/4 of the largest possible value plus one.

See Also:
Constant Field Values

HALF

protected static final long HALF
1/2 of the largest possible value; 2 * FIRST_QUARTER

See Also:
Constant Field Values

THIRD_QUARTER

protected static final long THIRD_QUARTER
3/4 of the largest possible value; 3 * FIRST_QUARTER

See Also:
Constant Field Values

_out

private final BitOutput _out
Bit output stream for writing encoding bits.


_bitsToFollow

private int _bitsToFollow
Number of bits beyond first bit that were normalized.

Constructor Detail

ArithEncoder

public ArithEncoder(BitOutput out)
Construct an arithmetic coder from a bit output.

Parameters:
out - Underlying bit output.

ArithEncoder

public ArithEncoder(java.io.OutputStream out)
Construct an arithmetic coder from an output stream.

Parameters:
out - Underlying output stream.
Method Detail

close

public void close()
           throws java.io.IOException
Close the arithmetic encoder, writing all bits that are buffered and closing the underlying output streams.

Throws:
java.io.IOException - If there is an exception writing to or closing the underlying output stream.

flush

public void flush()
           throws java.io.IOException
Flushes bit output.

Throws:
java.io.IOException - If there is an exception flushing the underlying output stream.

encode

public void encode(int[] counts)
            throws java.io.IOException
Encodes an interval expressed as a low count, high count and total count in an array {low,high,total}.

Parameters:
counts - Low, high and total counts of symbols.
Throws:
java.io.IOException - If there is an exception writing to the underlying stream.
See Also:
encode(int,int,int)

encode

public void encode(int lowCount,
                   int highCount,
                   int totalCount)
            throws java.io.IOException
Encodes an interval expressed as a low count, high count and total count. The high count is taken to be exclusive, and the resulting range is highCount - lowCount + 1.

Parameters:
lowCount - Cumulative count of symbols below current one.
highCount - Cumulative count of symbols below current one plus currnet one.
totalCount - Cumulative count of all symbols.
Throws:
java.io.IOException - If there is an exception writing to the underlying stream.
See Also:
encode(int[])

bitPlusFollowTrue

private void bitPlusFollowTrue()
                        throws java.io.IOException
Write a true bit, and then a number of false bits equal to the number of bits to follow.

Throws:
java.io.IOException - If there is an exception writing a bit.

bitPlusFollowFalse

private void bitPlusFollowFalse()
                         throws java.io.IOException
Write a false bit, and then a number of true bits equal to the number of bits to follow.

Throws:
java.io.IOException - If there is an exception writing a bit.