Hey all, looking for some smarts on the following problem:
I have the following two function definitions:
The intent is to be able to *losslessly* encode/decode any instance of that struct to/from an array of ints.
This problem can be solved trivially. For example:
However, that's hugely wasteful. 256 bits to store 132 bits of data. We can trivially see how we could improve things just by packing the bool parameters into one int instead of using 4 individual ints. i.e.
So that's better. Only 160 bits instead of 256. However, 160 bits is still hugely wasteful. In practice, while those int inputs could potentially have any value, it's far more likely that they will fall into certain ranges.
For example, here are some things that we know about the input data.
S.a: Can cover the entire range of ints. But lower values are far more likely than higher values. 99% of the time it can fit in 20 bits.
S.b: Similar to A, except 99% of the time, it can fit in 10bits. If it is 0, then S.c and S.d will be 0.
S.c: more often 0 than 1.
S.d: more often 0 than 1.
S.e: 99% of the time it will fit in 4 bits.
S.f: Similar to A, except 99% of the time, it can fit in 10bits. If it is 0, then S.g and S.h will be 0.
S.g: more often 0 than 1.
S.h: more often 0 than 1.
Knowing that in the worst case we'll only need 5 ints (as shown above), one approach would be to can manually come up with a scheme that, based on the input values picks an encoding format that will be anywhere from 1-5 ints.
Decoding would then check the length of the array and properly unpack things accordingly. For example, we could imagine a 1-int encoding like so:
This would work but would:
a) require a lot of manual effort defining those encoded bit patterns.
2) be fragile in the presence of wanting to add more data to S.
So what i'm looking for is a generalized algorithm for taking these ints and booleans**, and producing packed arrays given the knowledge that the ints are normally low, and the booleans are normally false. At worst, the packing should never be more than Ceiling(input_bits / 32). i.e. worse case we can always just encode each int, and then pack all the booleans together. However, at best, you'd be able to fit these values far more often into 1-2 ints, and only rarely have to go beyond that.
It should be generalized in the sense that adding more inputs into S should only require tweaking things a tiny amount (as opposed to the above approaches i mentioned where you'd need to fix up a lot of encoding/decoding cases).
** We could also generalize this to just take in ints and not have booleans. After all, you could always just pack all booleans. The booleans are just interesting because they're more often 0 than 1.
Anyone have experience here, or thoughts on a good way to approach this?
Thanks!
I have the following two function definitions:
Code:
struct S {
int a;
int b;
bool c;
bool d;
int e;
int f;
bool g;
bool h;
}
public static int[] encode(S value) {
... encode the value into an int[] ...
}
public static S decode(int[] encoded) {
// decode the int[]. Note: only the values and the length
// of the array are available here.
...
}
The intent is to be able to *losslessly* encode/decode any instance of that struct to/from an array of ints.
This problem can be solved trivially. For example:
Code:
public static int[] encode(S s) {
return new [] { s.a, s.b, s.c ? 1 : 0, s.d ? 1 : 0, s.e, s.f, s.g ? 1 : 0, s.h ? 1 : 0 };
}
public static S decode(int[] en) {
return new S { a = en[0], b = en[1], c = en[2] == 1, d = en[3] == 1,
e = en[4], f = en[5], g = en[6] == 1, h = en[7] == 1 };
}
However, that's hugely wasteful. 256 bits to store 132 bits of data. We can trivially see how we could improve things just by packing the bool parameters into one int instead of using 4 individual ints. i.e.
Code:
public static int[] encode(S s) {
return new [] { s.a, s.b, s.e, s.f,
(s.c ? 1 : 0) | (s.d ? (1 << 1 : 0) | (s.g ? (1 << 2) : 0) | (s.h ? (1 << 3) : 0) };
}
public static S decode(int[] en) {
return new S { a = en[0], b = en[1], e = en[4], f = en[5],
// unpack the bits into: c, d, g and h
};
}
So that's better. Only 160 bits instead of 256. However, 160 bits is still hugely wasteful. In practice, while those int inputs could potentially have any value, it's far more likely that they will fall into certain ranges.
For example, here are some things that we know about the input data.
S.a: Can cover the entire range of ints. But lower values are far more likely than higher values. 99% of the time it can fit in 20 bits.
S.b: Similar to A, except 99% of the time, it can fit in 10bits. If it is 0, then S.c and S.d will be 0.
S.c: more often 0 than 1.
S.d: more often 0 than 1.
S.e: 99% of the time it will fit in 4 bits.
S.f: Similar to A, except 99% of the time, it can fit in 10bits. If it is 0, then S.g and S.h will be 0.
S.g: more often 0 than 1.
S.h: more often 0 than 1.
Knowing that in the worst case we'll only need 5 ints (as shown above), one approach would be to can manually come up with a scheme that, based on the input values picks an encoding format that will be anywhere from 1-5 ints.
Decoding would then check the length of the array and properly unpack things accordingly. For example, we could imagine a 1-int encoding like so:
Code:
public static int[] encode(S value) {
if (S.a < (1 << 16) && S.b < (1 << 5) && S.e < (1 << 5) && s.f < (1 << 5) &&
!S.c && !S.d && !S.g && !S.h) {
return new [] { (S.a << 16) | (S.b << 10) | (S.e << 5) | s.f }
}
... rest of the cases.
}
public static S decode(int[] en) {
if (en.length == 1) {
// decode accordingly.
}
... rest of the cases
}
This would work but would:
a) require a lot of manual effort defining those encoded bit patterns.
2) be fragile in the presence of wanting to add more data to S.
So what i'm looking for is a generalized algorithm for taking these ints and booleans**, and producing packed arrays given the knowledge that the ints are normally low, and the booleans are normally false. At worst, the packing should never be more than Ceiling(input_bits / 32). i.e. worse case we can always just encode each int, and then pack all the booleans together. However, at best, you'd be able to fit these values far more often into 1-2 ints, and only rarely have to go beyond that.
It should be generalized in the sense that adding more inputs into S should only require tweaking things a tiny amount (as opposed to the above approaches i mentioned where you'd need to fix up a lot of encoding/decoding cases).
** We could also generalize this to just take in ints and not have booleans. After all, you could always just pack all booleans. The booleans are just interesting because they're more often 0 than 1.
Anyone have experience here, or thoughts on a good way to approach this?
Thanks!