def zencode(lzops):
    """Translate the given LZ operations into our deflate-like encoding.

    This function encodes the literals and (length,distance) pairs of the
    LZ operation stream into a sequence of bytes, using a huffman-coding
    scheme similar to deflate.  It returns a three tuple (data, l_tree, d_tree)
    giving the encoded bytes, and encoded tree structures for decoding the
    literals/lengths and distances respectively.
    """
    lzops = list(clamp_lz_operations(lzops, 2**15-1))
    # Calculate frequencies for huffman coding trees.
    l_freqs = defaultdict(lambda: 0.0)
    d_freqs = defaultdict(lambda: 0.0)
    for op in lzops:
        if isinstance(op, LZLiteral):
            for c in op.data:
                l_freqs[ord(c)] += 1
        else:
            l_freqs[op.length - 3 + 257] += 1
            d_freqs[op.distance] += 1
    l_freqs[256] += 1
    # XXX TODO: try using extra-bits encoding as used by deflate.
    # Lit/len tree is generally small enough to include in full.
    l_codes, l_tree = enhuffen(l_freqs)
    # The distance tree has a lot of unique entries, so we only
    # include the most popular and encode the rest directly.
    # XXX TODO: dynamically decide how many to include.
    d_top = set()
    for f,d in sorted(((f,d) for (d,f) in d_freqs.iteritems()), reverse=True):
        d_top.add(d)
        if len(d_top) >= 1024:
            break
    d_top_freqs = defaultdict(lambda: 0.0)
    for (d,f) in d_freqs.iteritems():
        if d in d_top:
            d_top_freqs[d] = f
        else:
            d_top_freqs[0] += f
    d_top_codes, d_top_tree = enhuffen(d_top_freqs)
    # Build the final binary string.  This is a giant string of
    # "1" and "0" chars, which is pretty terrible but is easy to
    # work with in python.
    output = []
    for op in lzops:
        if isinstance(op, LZLiteral):
            for c in op.data:
                output.append(l_codes[ord(c)])
        else:
            output.append(l_codes[op.length - 3 + 257])
            if op.distance in d_top_codes:
                output.append(d_top_codes[op.distance])
            else:
                output.append(d_top_codes[0])
                # Encode in 15 bits; top bit is always zero
                output.append(bin(op.distance)[2:].rjust(15, "0"))
    output.append(l_codes[256])
    output = "".join(output)
    # Translate each eight bits into an actul byte.
    final = []
    for i in xrange(0, len(output), 8):
        byte = output[i:i+8].ljust(8, "0")
        final.append(chr(int(byte, 2)))
    final = "".join(final)
    return final, l_tree, d_top_tree
Translate the given LZ operations into our deflate-like encoding.

    This function encodes the literals and (length,distance) pairs of the
    LZ operation stream into a sequence of bytes, using a huffman-coding
    scheme similar to deflate.  It returns a three tuple (data, l_tree, d_tree)
    giving the encoded bytes, and encoded tree structures for decoding the
    literals/lengths and distances respectively.
    
tools/compress_memory_initializer.py