M
mathieu
Hi,
I was trying to make an entry in a std::map be stored more
efficiently. The struct contained 3 booleans discribing it's property,
so I am trying to make it as compact as possible.
Using a std::vector<bool> in this case does not work, or at least is
not as efficient for my particular case (size was 20 bytes). Same goes
for tr1::array<bool,3>.
Is there anything else that I could have reused ?
Here is the current code:
// Compact struct to store up to 8 booleans:
struct B3
{
template <unsigned int TPos>
void SetB(bool v) {
if( v ) b.b |= (0x80 >> TPos%8);
else b.b &= (~(0x80 >> TPos%8));
}
template <unsigned int TPos>
bool GetB() const {
return b.b & (0x80 >> (TPos%8));
}
private:
union { unsigned char b; } b;
};
Thanks
-Mathieu
I was trying to make an entry in a std::map be stored more
efficiently. The struct contained 3 booleans discribing it's property,
so I am trying to make it as compact as possible.
Using a std::vector<bool> in this case does not work, or at least is
not as efficient for my particular case (size was 20 bytes). Same goes
for tr1::array<bool,3>.
Is there anything else that I could have reused ?
Here is the current code:
// Compact struct to store up to 8 booleans:
struct B3
{
template <unsigned int TPos>
void SetB(bool v) {
if( v ) b.b |= (0x80 >> TPos%8);
else b.b &= (~(0x80 >> TPos%8));
}
template <unsigned int TPos>
bool GetB() const {
return b.b & (0x80 >> (TPos%8));
}
private:
union { unsigned char b; } b;
};
Thanks
-Mathieu