1baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpackage SevenZip.Compression.RangeCoder;
2baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
3baa3858d3f5d128a5c8466b700098109edcad5f2repo syncpublic class BitTreeDecoder
4baa3858d3f5d128a5c8466b700098109edcad5f2repo sync{
5baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	short[] Models;
6baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	int NumBitLevels;
7baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
8baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public BitTreeDecoder(int numBitLevels)
9baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
10baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		NumBitLevels = numBitLevels;
11baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		Models = new short[1 << numBitLevels];
12baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
13baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
14baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public void Init()
15baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
16baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		Decoder.InitBitModels(Models);
17baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
18baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
19baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public int Decode(Decoder rangeDecoder) throws java.io.IOException
20baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
21baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int m = 1;
22baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		for (int bitIndex = NumBitLevels; bitIndex != 0; bitIndex--)
23baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m = (m << 1) + rangeDecoder.DecodeBit(Models, m);
24baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return m - (1 << NumBitLevels);
25baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
26baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
27baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public int ReverseDecode(Decoder rangeDecoder) throws java.io.IOException
28baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
29baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int m = 1;
30baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int symbol = 0;
31baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
32baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
33baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int bit = rangeDecoder.DecodeBit(Models, m);
34baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m <<= 1;
35baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m += bit;
36baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			symbol |= (bit << bitIndex);
37baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
38baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return symbol;
39baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
40baa3858d3f5d128a5c8466b700098109edcad5f2repo sync
41baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	public static int ReverseDecode(short[] Models, int startIndex,
42baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			Decoder rangeDecoder, int NumBitLevels) throws java.io.IOException
43baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	{
44baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int m = 1;
45baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		int symbol = 0;
46baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		for (int bitIndex = 0; bitIndex < NumBitLevels; bitIndex++)
47baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		{
48baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			int bit = rangeDecoder.DecodeBit(Models, startIndex + m);
49baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m <<= 1;
50baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			m += bit;
51baa3858d3f5d128a5c8466b700098109edcad5f2repo sync			symbol |= (bit << bitIndex);
52baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		}
53baa3858d3f5d128a5c8466b700098109edcad5f2repo sync		return symbol;
54baa3858d3f5d128a5c8466b700098109edcad5f2repo sync	}
55baa3858d3f5d128a5c8466b700098109edcad5f2repo sync}
56