HuffmanTreeGroup.cs 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. namespace System.Net.ApiService.Compression.Brotli {
  2. /// <summary>Contains a collection of huffman trees with the same alphabet size.</summary>
  3. internal sealed class HuffmanTreeGroup {
  4. /// <summary>The maximal alphabet size in this group.</summary>
  5. private int alphabetSize;
  6. /// <summary>Storage for Huffman lookup tables.</summary>
  7. internal int[] codes;
  8. /// <summary>
  9. /// Offsets of distinct lookup tables in
  10. /// <see cref="codes"/>
  11. /// storage.
  12. /// </summary>
  13. internal int[] trees;
  14. /// <summary>Initializes the Huffman tree group.</summary>
  15. /// <param name="group">POJO to be initialised</param>
  16. /// <param name="alphabetSize">the maximal alphabet size in this group</param>
  17. /// <param name="n">number of Huffman codes</param>
  18. internal static void Init(System.Net.ApiService.Compression.Brotli.HuffmanTreeGroup group, int alphabetSize, int n) {
  19. group.alphabetSize = alphabetSize;
  20. group.codes = new int[n * System.Net.ApiService.Compression.Brotli.Huffman.HuffmanMaxTableSize];
  21. group.trees = new int[n];
  22. }
  23. /// <summary>Decodes Huffman trees from input stream and constructs lookup tables.</summary>
  24. /// <param name="group">target POJO</param>
  25. /// <param name="br">data source</param>
  26. internal static void Decode(System.Net.ApiService.Compression.Brotli.HuffmanTreeGroup group, System.Net.ApiService.Compression.Brotli.BitReader br) {
  27. int next = 0;
  28. int n = group.trees.Length;
  29. for (int i = 0; i < n; i++) {
  30. group.trees[i] = next;
  31. System.Net.ApiService.Compression.Brotli.Decode.ReadHuffmanCode(group.alphabetSize, group.codes, next, br);
  32. next += System.Net.ApiService.Compression.Brotli.Huffman.HuffmanMaxTableSize;
  33. }
  34. }
  35. }
  36. }