Debian Codegraph @

src/tools/org/h2/dev/util/BitStream.java

raw
1 /*
2 * Copyright 2004-2023 H2 Group. Multiple-Licensed under the MPL 2.0,
3 * and the EPL 1.0 (https://h2database.com/html/license.html).
4 * Initial Developer: H2 Group
5 */
6 package org.h2.dev.util;
7
8 import java.io.IOException;
9 import java.io.InputStream;
10 import java.io.OutputStream;
11 import java.util.PriorityQueue;
12
13 /**
14 * A stream that supports Golomb and Huffman coding.
15 */
16 public class BitStream {
17
18 private BitStream() {
19 // a utility class
20 }
21
22 /**
23 * A bit input stream.
24 */
25 public static class In {
26
27 private final InputStream in;
28 private int current = 0x10000;
29
30 public In(InputStream in) {
31 this.in = in;
32 }
33
34 /**
35 * Read a value that is stored as a Golomb code.
36 *
37 * @param divisor the divisor
38 * @return the value
39 */
40 public int readGolomb(int divisor) {
41 int q = 0;
42 while (readBit() == 1) {
43 q++;
44 }
45 int bit = 31 - Integer.numberOfLeadingZeros(divisor - 1);
46 int r = 0;
47 if (bit >= 0) {
48 int cutOff = (2 << bit) - divisor;
49 for (; bit > 0; bit--) {
50 r = (r << 1) + readBit();
51 }
52 if (r >= cutOff) {
53 r = (r << 1) + readBit() - cutOff;
54 }
55 }
56 return q * divisor + r;
57 }
58
59 /**
60 * Read a bit.
61 *
62 * @return the bit (0 or 1)
63 */
64 public int readBit() {
65 if (current >= 0x10000) {
66 try {
67 current = 0x100 | in.read();
68 if (current < 0) {
69 return -1;
70 }
71 } catch (IOException e) {
72 throw new IllegalStateException(e);
73 }
74 }
75 int bit = (current >>> 7) & 1;
76 current <<= 1;
77 return bit;
78 }
79
80 /**
81 * Close the stream. This will also close the underlying stream.
82 */
83 public void close() {
84 try {
85 in.close();
86 } catch (IOException e) {
87 throw new IllegalStateException(e);
88 }
89 }
90
91 }
92
93 /**
94 * A bit output stream.
95 */
96 public static class Out {
97
98 private final OutputStream out;
99 private int current = 1;
100
101 public Out(OutputStream out) {
102 this.out = out;
103 }
104
105 /**
106 * Write the Golomb code of a value.
107 *
108 * @param divisor the divisor
109 * @param value the value
110 */
111 public void writeGolomb(int divisor, int value) {
112 int q = value / divisor;
113 for (int i = 0; i < q; i++) {
114 writeBit(1);
115 }
116 writeBit(0);
117 int r = value - q * divisor;
118 int bit = 31 - Integer.numberOfLeadingZeros(divisor - 1);
119 if (r < ((2 << bit) - divisor)) {
120 bit--;
121 } else {
122 r += (2 << bit) - divisor;
123 }
124 for (; bit >= 0; bit--) {
125 writeBit((r >>> bit) & 1);
126 }
127 }
128
129 /**
130 * Get the size of the Golomb code for this value.
131 *
132 * @param divisor the divisor
133 * @param value the value
134 * @return the number of bits
135 */
136 public static int getGolombSize(int divisor, int value) {
137 int q = value / divisor;
138 int r = value - q * divisor;
139 int bit = 31 - Integer.numberOfLeadingZeros(divisor - 1);
140 if (r < ((2 << bit) - divisor)) {
141 bit--;
142 }
143 return bit + q + 2;
144 }
145
146 /**
147 * Write a bit.
148 *
149 * @param bit the bit (0 or 1)
150 */
151 public void writeBit(int bit) {
152 current = (current << 1) + bit;
153 if (current > 0xff) {
154 try {
155 out.write(current & 0xff);
156 } catch (IOException e) {
157 throw new IllegalStateException(e);
158 }
159 current = 1;
160 }
161 }
162
163 /**
164 * Flush the stream. This will at write at most 7 '0' bits.
165 * This will also flush the underlying stream.
166 */
167 public void flush() {
168 while (current > 1) {
169 writeBit(0);
170 }
171 try {
172 out.flush();
173 } catch (IOException e) {
174 throw new IllegalStateException(e);
175 }
176 }
177
178 /**
179 * Flush and close the stream.
180 * This will also close the underlying stream.
181 */
182 public void close() {
183 flush();
184 try {
185 out.close();
186 } catch (IOException e) {
187 throw new IllegalStateException(e);
188 }
189
190 }
191
192 }
193
194 /**
195 * A Huffman code.
196 */
197 public static class Huffman {
198
199 private final int[] codes;
200 private final Node tree;
201
202 public Huffman(int[] frequencies) {
203 PriorityQueue<Node> queue = new PriorityQueue<>();
204 for (int i = 0; i < frequencies.length; i++) {
205 int f = frequencies[i];
206 if (f > 0) {
207 queue.offer(new Node(i, f));
208 }
209 }
210 while (queue.size() > 1) {
211 queue.offer(new Node(queue.poll(), queue.poll()));
212 }
213 codes = new int[frequencies.length];
214 tree = queue.poll();
215 if (tree != null) {
216 tree.initCodes(codes, 1);
217 }
218 }
219
220 /**
221 * Write a value.
222 *
223 * @param out the output stream
224 * @param value the value to write
225 */
226 public void write(BitStream.Out out, int value) {
227 int code = codes[value];
228 int bitCount = 30 - Integer.numberOfLeadingZeros(code);
229 for (int i = bitCount; i >= 0; i--) {
230 out.writeBit((code >> i) & 1);
231 }
232 }
233
234 /**
235 * Read a value.
236 *
237 * @param in the input stream
238 * @return the value
239 */
240 public int read(BitStream.In in) {
241 Node n = tree;
242 while (n.left != null) {
243 n = in.readBit() == 1 ? n.right : n.left;
244 }
245 return n.value;
246 }
247
248 /**
249 * Get the number of bits of the Huffman code for this value.
250 *
251 * @param value the value
252 * @return the number of bits
253 */
254 public int getBitCount(int value) {
255 int code = codes[value];
256 return 30 - Integer.numberOfLeadingZeros(code);
257 }
258
259 }
260
261 /**
262 * A Huffman code node.
263 */
264 private static class Node implements Comparable<Node> {
265
266 int value;
267 Node left;
268 Node right;
269 private final int frequency;
270
271 Node(int value, int frequency) {
272 this.frequency = frequency;
273 this.value = value;
274 }
275
276 Node(Node left, Node right) {
277 this.left = left;
278 this.right = right;
279 this.frequency = left.frequency + right.frequency;
280 }
281
282 @Override
283 public int compareTo(Node o) {
284 return frequency - o.frequency;
285 }
286
287 void initCodes(int[] codes, int bits) {
288 if (left == null) {
289 codes[value] = bits;
290 } else {
291 left.initCodes(codes, bits << 1);
292 right.initCodes(codes, (bits << 1) + 1);
293 }
294 }
295
296 }
297
298 }