This is an attempt to improve my Perl: seek to and read bits, not bytes by explaining more thoroughly what I was trying to do.
I have x, a 9136 x 42 array of integers that I want to store super-efficiently in a file. The integers have the following constraints:
All of the 9136 integers in x[0..9135][0] are between -137438953472 and 137438953471, and can therefore be stored using 38 bits.
All of the 9136 integers in x[0..9135][1] are between -16777216 and 16777215, and can therefore be stored using 25 bits.
And so on... (the integer bit constraints are known in advance; Perl doesn't have to compute them)
Question: Using Perl, how do I efficiently store this array in a file?
Notes:
If an integer can be stored in 25 bits, it can also be stored in 4 bytes (32 bits), if you're willing to waste 7 bits. In my situation, however, every bit counts.
I want to use file seek() to find data quickly, not read sequentially through the file.
The array will normally be accessed as x[i]. In other words, I'll want the 42 integers corresponding to a given x[i], so these 42 integers should be stored close to each other (ideally, they should be stored adjacent to each other in the file)
My initial approach was to just lay down a bitstream, and then find a way to read it back and change it back into an integer. My original question focused on that, but perhaps there's a better solution to the bigger problem that I'm not seeing.
Far too much detail on what I'm doing: