[Haskell-cafe] Mixing Unboxed Mutable Vectors and Parsers

Myles C. Maxfield myles.maxfield at gmail.com
Sun Apr 8 20:17:47 CEST 2012

It's a JPEG parser.

Progressive JPEG is set up where there's a vector Word8s, and some of
the entries in the vector may be 0. The JPEG has a stream of bits, and
the decoder is supposed to shift in one bit to each successive element
in the vector, skipping over 0s, and stop when it reaches some
specified number of 0s.

So if your partially decoded vector is
<2, 8, 0, 12, 0, 10, 6, 0, 2, 10>
and the jpeg has this bit stream
<1, 1, 0, 1, 0, 0, 1, 0, ...>
and the jpeg says "shift in until the 3rd zero is found"
that would result in the partially decoded vector being
<3, 9, 0, 12, 0, 11, 6, 0, 2, 10>
with the leftover part of the stream being
<0, 1, 0, ...>

The JPEG parser has to keep track of where it is in the partially
decoded vector to know how many bits to shift in, and where they
belong, so the next iteration is aligned to the right place. It would
be possible to keep track of this stuff throughout the parsing, and
have the result of the parse be a second "delta" framebuffer and apply
it to the original after each scan is parsed, but that's fairly ugly
and I'd like to avoid doing that.

If that's what I have to do, though, I guess I have to do it. Isn't
there a better way?


On Sat, Apr 7, 2012 at 11:56 PM, Stephen Tetley
<stephen.tetley at gmail.com> wrote:
> Hi Myles
> It seems odd to mix parsing (consuming input) with mutation.
> What problem are you trying to solve and are you sure you can't get
> better phase separation than this paragraph suggests?
>> My first idea was to simply parse all the deltas, and later apply them
>> to the input list. However, I can't do that because the value of the
>> deltas depend on the value they're modifying.

More information about the Haskell-Cafe mailing list