aboutsummaryrefslogtreecommitdiff
path: root/java/org/brotli/dec/HuffmanTreeGroup.java
blob: 3b1c82fc483f8e0273a5dd03bc449882b2962d4d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
/* Copyright 2015 Google Inc. All Rights Reserved.

   Distributed under MIT license.
   See file LICENSE for detail or copy at https://opensource.org/licenses/MIT
*/

package org.brotli.dec;

/**
 * Contains a collection of huffman trees with the same alphabet size.
 */
final class HuffmanTreeGroup {

  /**
   * The maximal alphabet size in this group.
   */
  private int alphabetSize;

  /**
   * Storage for Huffman lookup tables.
   */
  int[] codes;

  /**
   * Offsets of distinct lookup tables in {@link #codes} storage.
   */
  int[] trees;

  /**
   * Initializes the Huffman tree group.
   *
   * @param group POJO to be initialised
   * @param alphabetSize the maximal alphabet size in this group
   * @param n number of Huffman codes
   */
  static void init(HuffmanTreeGroup group, int alphabetSize, int n) {
    group.alphabetSize = alphabetSize;
    group.codes = new int[n * Huffman.HUFFMAN_MAX_TABLE_SIZE];
    group.trees = new int[n];
  }

  /**
   * Decodes Huffman trees from input stream and constructs lookup tables.
   *
   * @param group target POJO
   * @param br data source
   */
  static void decode(HuffmanTreeGroup group, BitReader br) {
    int next = 0;
    int n = group.trees.length;
    for (int i = 0; i < n; i++) {
      group.trees[i] = next;
      Decode.readHuffmanCode(group.alphabetSize, group.codes, next, br);
      next += Huffman.HUFFMAN_MAX_TABLE_SIZE;
    }
  }
}