Question

Should be an easy one.

I'm working on Scala trying to handle long sequences of binary data. That is long lists of 0's and 1's. What is the 'best' way to store/access this kind of data.

The important point here is memory optimisation, so I would like to avoid using an entire byte to store a boolean. Also access is somwhat important, so I would like to avoid paking them into bytes and then into arrays.

Is a BitMap a good idea? Is there such a class in scala?

if not, would it be best to use ByteArray? How would you implement this?

Any other ideas?

Thanks,

Was it helpful?

Solution

You can use java.util.BitSet (perhaps with a couple if clever explicits to make it more Scala-like).

If that is still too costly I would write a class that uses an array internally and pack the bits into ints or bytes.

OTHER TIPS

If your values are not uniformly distributed have (significantly more 0s than 1s) you can use run-length encoding to encode the image data. This is the encoding used by Fax.

There are two encoding options:

  • use RLE for black and white
  • use only RLE for one color and use a direct encoding if you encode the other color (or mixed sections)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top