From: linux@horizon.com
To: spearce@spearce.org
Cc: git@vger.kernel.org, linux@horizon.com
Subject: Re: pack v4 status
Date: 28 Feb 2007 05:04:56 -0500 [thread overview]
Message-ID: <20070228100456.13486.qmail@science.horizon.com> (raw)
Just a minor note:
I you want to have an arbitrary-sized string table without special cases,
the standard binary Huffman tree construction algorithm can be extended
quite simply to a radix-256 tree, so every code is an integer number
of bytes long.
Take the input strings, and sort them in order of frequency of appearance.
Pad them at the end with dummy strings (frequency = 0) until the number
equals 1 mod 255.
Then take the last (least frequent) 256 strings off the list, and
replace them with a single prefix whose frequency is the sum of all of
the components. Each of the selected strings will be encoded as the
prefix (whose code has not yet been decided) plus 256 possible byte values.
Insert the new prefix into the list in sorted order.
Repeat until the list has only one entry, which is represented by a
zero-length prefix. (Since each step replaces 256 entries by 1 entry,
the number of entries never changes mod 255, so the algorithm always will
converge to one entry.)
This is generally encoded more compactly as a "canonical Huffman tree".
For each string, forget the actual encoding decided on here, and just
remember the number of bytes assigned to encode it; this is the only
number you need to record to reconstruct the tree for decoding.
Then count the number of strings to be encoded with 1, 2, 3... bytes.
To decode, read one byte. If the byte is less than the number of 1-byte
strings, its value is the string's position in the 1-byte string table.
If the byte is >= the number of 1-byte strings, subtract the number of 1-byte
strings, multiply by 256, and add a following byte. This is the position in
the 2-byte string table.
If the value is >= the number of 2-byte strings, subtract, multiply by
256, and add another trailing byte. This is the position in the 3-byte
string table. Repeat as necessary.
There are elaborations on this to bound the maximum number of bytes
required; see the zlib source for details.
If you have a significant number of unique (frequency == 1) strings,
you can encode them in-line after a common "unique string" prefix
(which can have a frequency greater than 1).
I'm not sure if you can follow all that (it helps to understand Huffmann
trees already), but I can explain at greater length if necessary.
The important thing is that it's quite straightforward and provides
a general solution.
next reply other threads:[~2007-02-28 11:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2007-02-28 10:04 linux [this message]
-- strict thread matches above, loose matches on Subject: below --
2007-02-27 15:50 pack v4 status Shawn O. Pearce
2007-02-27 21:51 ` Linus Torvalds
2007-02-27 22:15 ` Johannes Schindelin
2007-02-27 22:33 ` Nicolas Pitre
2007-02-27 22:32 ` Nicolas Pitre
2007-02-27 22:36 ` Junio C Hamano
2007-02-28 3:45 ` Shawn O. Pearce
2007-02-28 1:19 ` Nicolas Pitre
2007-02-28 4:13 ` Shawn O. Pearce
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20070228100456.13486.qmail@science.horizon.com \
--to=linux@horizon.com \
--cc=git@vger.kernel.org \
--cc=spearce@spearce.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).