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