
Boost Users : 
Subject: Re: [Boostusers] Needs advices on design ( mpl or processor )
From: Allan Nielsen (a_at_[hidden])
Date: 20120110 15:08:47
Hi
> IIUC, the EnumUnion has no tag, which means you could store an Enum1
> and retrieve an Enum2 without any warning. Â Is that correct?
>
> In UnionModel, there's:
>
> Â Â static const size_t size
> Â Â Â = head::size > _tail::size ? head::size : _tail::size;
Almost, the EnumUnion has a tag, but this is only used to find the
specific union. Consider:
CompressedEnums<
unsigned,
EnumUnion<Tag1,
CompressedEnum<e1>,
CompressedEnum<Abc>,
CompressedEnum<YesNoMaby>
>,
EnumUnion<Tag2,
CompressedEnum<e1>,
CompressedEnum<Abc>,
CompressedEnum<YesNoMaby>
>
> double_union;
Here I have two union, where three enums shares the same storage.
> which makes sense because a union just has to be large enough to store
> the largest enumeration. Â However, in the case of StructModel_r, I
> don't understand:
>
> Â Â static const size_t size = head::size * _tail::size;
>
> around line 200. Â I would think that there would just be additions
> since you want to store one value after another. Â That's why, in my
> previous post, in template sum_bits, there's:
>
> Â Â Â , integral_c<unsigned,Bits+enum_bits<Enum>::size>
>
> Could you explain why multiplication instead of addition is used to
> calculate the size in StructModel_r?
This is because the offset is not really an offset, I just could not
come up with a better name. Here is the explanation:
Assume we need to store 5 different enumerated types which each
represents 3 different values ( like the A, B C).
These 5 enums can all together represent 3^5 = 243 different values (
AAAAA, AAAAB, AAAAC, AAABA, AAABB ...).
As 243 is less than 256 ( all the different values in a char), it is
possible to store the values in a char. But in a binary number system
2 bits are required to store one tristate value. If we use 3 * 5 bits
we end up with 15 bits ( more than one char) which is not the most
efficient encoding.
Therefore we encode this in a base_3 number system which is defined
by: ... d_2 * n^2 + d_1 * n^1 + d_0 * n^0, where d is the digits we
want to encode, and n is 3 because all the enums are tristates.
Example: Encode BCACB > 12021
1*3^4 + 2*3^3 + 0*3^2 + 2*3^1 + 1*3^0 = 142
To extract enum number 4 the reverse operation must be done: (142 / 3^3) % 3 = 2
If different enums are with different sizes are used then n in the
equation above will be different for each digit. Then to set digit
number 4 one has to multiply n_0 * n_1 * n_2 * n_3 and then use this
as base. Therefore the multiplications.
> Could you explain why multiplication instead of addition is used to
> calculate the size in StructModel_r?
Currently no, but I would like to add this. Also a range check on the
array access.
Best regards
Allan W. Nielsen
Boostusers list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net