Terraria v1.4.4.9
Terraria source code documentation
Loading...
Searching...
No Matches
HuffmanTree.cs
Go to the documentation of this file.
2
3internal sealed class HuffmanTree
4{
5 private readonly int _tableBits;
6
7 private readonly short[] _table;
8
9 private readonly short[] _left;
10
11 private readonly short[] _right;
12
13 private readonly byte[] _codeLengthArray;
14
15 private readonly int _tableMask;
16
18
19
21
22
23 public HuffmanTree(byte[] codeLengths)
24 {
25 _codeLengthArray = codeLengths;
26 if (_codeLengthArray.Length == 288)
27 {
28 _tableBits = 9;
29 }
30 else
31 {
32 _tableBits = 7;
33 }
34 _tableMask = (1 << _tableBits) - 1;
35 _table = new short[1 << _tableBits];
36 _left = new short[2 * _codeLengthArray.Length];
37 _right = new short[2 * _codeLengthArray.Length];
39 }
40
41 private static byte[] GetStaticLiteralTreeLength()
42 {
43 byte[] array = new byte[288];
44 for (int i = 0; i <= 143; i++)
45 {
46 array[i] = 8;
47 }
48 for (int j = 144; j <= 255; j++)
49 {
50 array[j] = 9;
51 }
52 for (int k = 256; k <= 279; k++)
53 {
54 array[k] = 7;
55 }
56 for (int l = 280; l <= 287; l++)
57 {
58 array[l] = 8;
59 }
60 return array;
61 }
62
63 private static byte[] GetStaticDistanceTreeLength()
64 {
65 byte[] array = new byte[32];
66 for (int i = 0; i < 32; i++)
67 {
68 array[i] = 5;
69 }
70 return array;
71 }
72
73 private static uint BitReverse(uint code, int length)
74 {
75 uint num = 0u;
76 do
77 {
78 num |= code & 1u;
79 num <<= 1;
80 code >>= 1;
81 }
82 while (--length > 0);
83 return num >> 1;
84 }
85
86 private uint[] CalculateHuffmanCode()
87 {
88 uint[] array = new uint[17];
89 byte[] codeLengthArray = _codeLengthArray;
90 foreach (int num in codeLengthArray)
91 {
92 array[num]++;
93 }
94 array[0] = 0u;
95 uint[] array2 = new uint[17];
96 uint num2 = 0u;
97 for (int j = 1; j <= 16; j++)
98 {
99 num2 = (array2[j] = num2 + array[j - 1] << 1);
100 }
101 uint[] array3 = new uint[288];
102 for (int k = 0; k < _codeLengthArray.Length; k++)
103 {
104 int num3 = _codeLengthArray[k];
105 if (num3 > 0)
106 {
107 array3[k] = BitReverse(array2[num3], num3);
108 array2[num3]++;
109 }
110 }
111 return array3;
112 }
113
114 private void CreateTable()
115 {
116 uint[] array = CalculateHuffmanCode();
117 short num = (short)_codeLengthArray.Length;
118 for (int i = 0; i < _codeLengthArray.Length; i++)
119 {
120 int num2 = _codeLengthArray[i];
121 if (num2 <= 0)
122 {
123 continue;
124 }
125 int num3 = (int)array[i];
126 if (num2 <= _tableBits)
127 {
128 int num4 = 1 << num2;
129 if (num3 >= num4)
130 {
132 }
133 int num5 = 1 << _tableBits - num2;
134 for (int j = 0; j < num5; j++)
135 {
136 _table[num3] = (short)i;
137 num3 += num4;
138 }
139 continue;
140 }
141 int num6 = num2 - _tableBits;
142 int num7 = 1 << _tableBits;
143 int num8 = num3 & ((1 << _tableBits) - 1);
144 short[] array2 = _table;
145 do
146 {
147 short num9 = array2[num8];
148 if (num9 == 0)
149 {
150 array2[num8] = (short)(-num);
151 num9 = (short)(-num);
152 num++;
153 }
154 if (num9 > 0)
155 {
157 }
158 array2 = (((num3 & num7) != 0) ? _right : _left);
159 num8 = -num9;
160 num7 <<= 1;
161 num6--;
162 }
163 while (num6 != 0);
164 array2[num8] = (short)i;
165 }
166 }
167
169 {
170 uint num = input.TryLoad16Bits();
171 if (input.AvailableBits == 0)
172 {
173 return -1;
174 }
175 int num2 = _table[num & _tableMask];
176 if (num2 < 0)
177 {
178 uint num3 = (uint)(1 << _tableBits);
179 do
180 {
181 num2 = -num2;
182 num2 = (((num & num3) != 0) ? _right[num2] : _left[num2]);
183 num3 <<= 1;
184 }
185 while (num2 < 0);
186 }
187 int num4 = _codeLengthArray[num2];
188 if (num4 <= 0)
189 {
191 }
192 if (num4 > input.AvailableBits)
193 {
194 return -1;
195 }
196 input.SkipBits(num4);
197 return num2;
198 }
199}
int GetNextSymbol(InputBuffer input)
static byte[] GetStaticDistanceTreeLength()
static byte[] GetStaticLiteralTreeLength()
HuffmanTree(byte[] codeLengths)
static uint BitReverse(uint code, int length)
static HuffmanTree StaticDistanceTree
static HuffmanTree StaticLiteralLengthTree
static string InvalidHuffmanData
Definition SR.cs:26
Definition SR.cs:7