In short, to achieve the max space optimization, I have to divide the
double union into many parts. To save memory (if you want to use
char for example, we may divide the union into eight small parts). Personally I want to keep
boolean and then use the
char generic allocated memory space instead, eight bits for a single
boolean value. But I'm still confused - A
boolean value should take exactly a single bit. I don't want to waste memory so much, but if I do so, I have to spent a plenty of time and add a bunch of code...
Speed vs Memory? I have no idea about
memory, but I considered that memory is the most important thing, rather than speed. The next problem is how to access an element correctly (especially
char and boolean). Like I said before,
double union is a primary element value. But the union can be divided - 8x smaller for
char and 64x for boolean values. So, you cannot use the regular accessing method by accessing an element via an index value directly (Why? Because the double itself also is a container) Max allowed range for
char type is 8, and boolean is 64. Otherwise, the parser will create & allocate a new value element (8 bytes per allocation) to store and handle all remaining data. E.g :
1 2 3 4 5 6 7 8
|
char *s = "0123456789"; // 10 bytes
Data("0123456789"); //First-8-byte holds "01234567" and "89" is moved to the next 8-byte element.
cout << s[4]; //Ok
cout << Data.value[0].chr[4]; // Ok
cout << s[9]; // Ok
cout << string.value[0].chr[9]; // Wrong - Out of range
cout <<string.value[1].chr[1]; // Ok
| |
I don't know the best defintion for variable data managing & structure is, but now here is my lastest version :
1 2 3 4 5 6 7 8 9 10 11 12
|
struct Var
{
union PrimValue
{
union Chr
{
bool b1 : 1, b2 : 1, b3 : 1, b4 : 1, b5 : 1, b6 : 1, b7 : b8 : 1;
}chr[8];
double fValue;
};
std::vector<PrimValue> value;
};
| |