Debian Codegraph @

src/tools/org/h2/dev/util/BinaryArithmeticStream.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 binary arithmetic stream.
15 */
16 public class BinaryArithmeticStream {
17
18 /**
19 * The maximum probability.
20 */
21 public static final int MAX_PROBABILITY = (1 << 12) - 1;
22
23 /**
24 * The low marker.
25 */
26 protected int low;
27
28 /**
29 * The high marker.
30 */
31 protected int high = 0xffffffff;
32
33 /**
34 * A binary arithmetic input stream.
35 */
36 public static class In extends BinaryArithmeticStream {
37
38 private final InputStream in;
39 private int data;
40
41 public In(InputStream in) throws IOException {
42 this.in = in;
43 data = ((in.read() & 0xff) << 24) |
44 ((in.read() & 0xff) << 16) |
45 ((in.read() & 0xff) << 8) |
46 (in.read() & 0xff);
47 }
48
49 /**
50 * Read a bit.
51 *
52 * @param probability the probability that the value is true
53 * @return the value
54 */
55 public boolean readBit(int probability) throws IOException {
56 int split = low + probability * ((high - low) >>> 12);
57 boolean value;
58 // compare unsigned
59 if (data + Integer.MIN_VALUE > split + Integer.MIN_VALUE) {
60 low = split + 1;
61 value = false;
62 } else {
63 high = split;
64 value = true;
65 }
66 while (low >>> 24 == high >>> 24) {
67 data = (data << 8) | (in.read() & 0xff);
68 low <<= 8;
69 high = (high << 8) | 0xff;
70 }
71 return value;
72 }
73
74 /**
75 * Read a value that is stored as a Golomb code.
76 *
77 * @param divisor the divisor
78 * @return the value
79 */
80 public int readGolomb(int divisor) throws IOException {
81 int q = 0;
82 while (readBit(MAX_PROBABILITY / 2)) {
83 q++;
84 }
85 int bit = 31 - Integer.numberOfLeadingZeros(divisor - 1);
86 int r = 0;
87 if (bit >= 0) {
88 int cutOff = (2 << bit) - divisor;
89 for (; bit > 0; bit--) {
90 r = (r << 1) + (readBit(MAX_PROBABILITY / 2) ? 1 : 0);
91 }
92 if (r >= cutOff) {
93 r = (r << 1) + (readBit(MAX_PROBABILITY / 2) ? 1 : 0) - cutOff;
94 }
95 }
96 return q * divisor + r;
97 }
98
99 }
100
101 /**
102 * A binary arithmetic output stream.
103 */
104 public static class Out extends BinaryArithmeticStream {
105
106 private final OutputStream out;
107
108 public Out(OutputStream out) {
109 this.out = out;
110 }
111
112 /**
113 * Write a bit.
114 *
115 * @param value the value
116 * @param probability the probability that the value is true
117 */
118 public void writeBit(boolean value, int probability) throws IOException {
119 int split = low + probability * ((high - low) >>> 12);
120 if (value) {
121 high = split;
122 } else {
123 low = split + 1;
124 }
125 while (low >>> 24 == high >>> 24) {
126 out.write(high >> 24);
127 low <<= 8;
128 high = (high << 8) | 0xff;
129 }
130 }
131
132 /**
133 * Flush the stream.
134 */
135 public void flush() throws IOException {
136 out.write(high >> 24);
137 out.write(high >> 16);
138 out.write(high >> 8);
139 out.write(high);
140 }
141
142 /**
143 * Write the Golomb code of a value.
144 *
145 * @param divisor the divisor
146 * @param value the value
147 */
148 public void writeGolomb(int divisor, int value) throws IOException {
149 int q = value / divisor;
150 for (int i = 0; i < q; i++) {
151 writeBit(true, MAX_PROBABILITY / 2);
152 }
153 writeBit(false, MAX_PROBABILITY / 2);
154 int r = value - q * divisor;
155 int bit = 31 - Integer.numberOfLeadingZeros(divisor - 1);
156 if (r < ((2 << bit) - divisor)) {
157 bit--;
158 } else {
159 r += (2 << bit) - divisor;
160 }
161 for (; bit >= 0; bit--) {
162 writeBit(((r >>> bit) & 1) == 1, MAX_PROBABILITY / 2);
163 }
164 }
165
166 }
167
168 /**
169 * A Huffman code table / tree.
170 */
171 public static class Huffman {
172
173 private final int[] codes;
174 private final Node tree;
175
176 public Huffman(int[] frequencies) {
177 PriorityQueue<Node> queue = new PriorityQueue<>();
178 for (int i = 0; i < frequencies.length; i++) {
179 int f = frequencies[i];
180 if (f > 0) {
181 queue.offer(new Node(i, f));
182 }
183 }
184 while (queue.size() > 1) {
185 queue.offer(new Node(queue.poll(), queue.poll()));
186 }
187 codes = new int[frequencies.length];
188 tree = queue.poll();
189 if (tree != null) {
190 tree.initCodes(codes, 1);
191 }
192 }
193
194 /**
195 * Write a value.
196 *
197 * @param out the output stream
198 * @param value the value to write
199 */
200 public void write(Out out, int value) throws IOException {
201 int code = codes[value];
202 int bitCount = 30 - Integer.numberOfLeadingZeros(code);
203 Node n = tree;
204 for (int i = bitCount; i >= 0; i--) {
205 boolean goRight = ((code >> i) & 1) == 1;
206 int prob = (int) ((long) MAX_PROBABILITY *
207 n.right.frequency / n.frequency);
208 out.writeBit(goRight, prob);
209 n = goRight ? n.right : n.left;
210 }
211 }
212
213 /**
214 * Read a value.
215 *
216 * @param in the input stream
217 * @return the value
218 */
219 public int read(In in) throws IOException {
220 Node n = tree;
221 while (n.left != null) {
222 int prob = (int) ((long) MAX_PROBABILITY *
223 n.right.frequency / n.frequency);
224 boolean goRight = in.readBit(prob);
225 n = goRight ? n.right : n.left;
226 }
227 return n.value;
228 }
229
230 }
231
232 /**
233 * A Huffman code node.
234 */
235 private static class Node implements Comparable<Node> {
236
237 int value;
238 Node left;
239 Node right;
240 final int frequency;
241
242 Node(int value, int frequency) {
243 this.frequency = frequency;
244 this.value = value;
245 }
246
247 Node(Node left, Node right) {
248 this.left = left;
249 this.right = right;
250 this.frequency = left.frequency + right.frequency;
251 }
252
253 @Override
254 public int compareTo(Node o) {
255 return frequency - o.frequency;
256 }
257
258 void initCodes(int[] codes, int bits) {
259 if (left == null) {
260 codes[value] = bits;
261 } else {
262 left.initCodes(codes, bits << 1);
263 right.initCodes(codes, (bits << 1) + 1);
264 }
265 }
266
267 }
268
269 }