Juha Manninen wrote 9 lines
In the latest Delphi VirtualTreeView version the typecast is changed
to Byte
or Word depending on the element count in a set.
I decided to do the same thing. Then I realized the code doesn't
work with FPC
2.2.4, so I added {$packset 1} or {$packset 2} directives.
{$packset 1} if elementcount < 9.
{$packset 2} if 8 < elementcount < 17.
And I changed the typecasts accordingly to Byte or Word.
You can just add {$packset 1} once. It simply tells the compiler that the minimal size by which it can increment the size of sets is 1 byte (rather than the default 4 bytes). It does not mean that the compiler will try to store all sets in 1 byte.
What happens if (without $packset directives) a set has 17 elements?
Does it use 3 or 4 bytes?
It will always be 4 bytes, even with {$packset 1}. Sets of 3 bytes do not exist in FPC nor Delphi, that's a special case (probably to make it easier to keep them in a register).
What if there are > 32 elements?
Then in the default case, the set will be 32 bytes. With {$packset x}, the size of the set will be rounded up to the nearest multiple of x that is large enough to hold all elements.
Show quoted text 6 lines
The casting here is used for streaming data, with Stream WriteBuffer
and
ReadBuffer.
Is there any better way of doing this than casting to Byte, Word or
LongWord
and counting the elements?
Something like this:
type
tsetarray = array[1..32] of byte;
psetarray = ^tsetarray;
var
i: longint;
begin
for i:=1 to sizeof(setvar) do
write(psetarray(@setvar)^[i])
end.