lib/gziprb/deflate_huffman.rb
def canonical_encoding_from_element_counts(element_counts, max_code_length)
      if element_counts.size > 2 ** max_code_length
        raise "Too many elements (#{element_counts.size}) for max_code_length #{max_code_length}"
      end

      non_canonical_encoding = encoding_from_element_counts(element_counts)

      if non_canonical_encoding.empty?
        {}
      else
        # Lexicographical sort making elements with shorter codes appear first and elements with
        # equal-length codes appear in element order.
        code_lens, elems = non_canonical_encoding.map { |e, code| [code.length, e] }.sort.transpose

        overflows = non_canonical_encoding.values.select { |code| code.length > max_code_length }

        unless overflows.empty?
          code_lens, elems = handle_overflowing_codes(max_code_length, code_lens, elems, overflows)
        end

        elems.zip(codes_from_code_lengths(code_lens)).to_h
      end
    end
# Bit length tree codes are limited to 7 bits since their lengths are stored as 3 bit ints.
# Len/lit and distance codes are limited to 15 bits since their length is stored using the
# code-length alphabet, where 15 is the maximum code length as the lookbehind window is 2**15
# in size, in the worst case, we could require 15 bits for the distance. 16, 17, 18 are used
# for RLE.
Bit length tree codes are limited to 7 bits since their lengths are stored as 3 bit ints.
Len/lit and distance codes are limited to 15 bits since their length is stored using the
code-length alphabet, where 15 is the maximum code length as the lookbehind window is 2**15
in size, in the worst case, we could require 15 bits for the distance.
Bit length tree codes are limited to 7 bits since their lengths are stored as 3 bit ints . Len / lit and distance codes are limited to 15 bits since their length is stored using the code - length alphabet where 15 is the maximum code length as the lookbehind window is 2 ** 15 in size in the worst case we could require 15 bits for the distance .
def canonical_encoding_from_element_counts(element_counts, max_code_length)
      if element_counts.size > 2 ** max_code_length
        raise "Too many elements (#{element_counts.size}) for max_code_length #{max_code_length}"
      end
      non_canonical_encoding = encoding_from_element_counts(element_counts)
      if non_canonical_encoding.empty?
        {}
      else
        code_lens, elems = non_canonical_encoding.map { |e, code| [code.length, e] }.sort.transpose
        overflows = non_canonical_encoding.values.select { |code| code.length > max_code_length }
        unless overflows.empty?
          code_lens, elems = handle_overflowing_codes(max_code_length, code_lens, elems, overflows)
        end
        elems.zip(codes_from_code_lengths(code_lens)).to_h
      end
    end
def canonical_encoding_from_element_counts ( element_counts , max_code_length ) if element_counts . size > 2 ** max_code_length raise "Too many elements (#{element_counts.size}) for max_code_length #{max_code_length}" end non_canonical_encoding = encoding_from_element_counts ( element_counts ) if non_canonical_encoding . empty? { } else code_lens , elems = non_canonical_encoding . map { | e , code | [ code . length , e ] } . sort . transpose overflows = non_canonical_encoding . values . select { | code | code . length > max_code_length } unless overflows . empty? code_lens , elems = handle_overflowing_codes ( max_code_length , code_lens , elems , overflows ) end elems . zip ( codes_from_code_lengths ( code_lens ) ) . to_h end end
Bit length tree codes are limited to 7 bits since their lengths are stored as 3 bit ints.
Bit length tree codes are limited to 7 bits since their lengths are stored as 3 bit ints .
# Lexicographical sort making elements with shorter codes appear first and elements with # equal-length codes appear in element order.
element_counts
max_code_length