Imagine some ordinal type and a set enumeration of it in Delphi:
Code:
type | |
TLocation = (locHere, locThere, locElsewhere); | |
TLocations = Set of TLocation; | |
... | |
var locs: TLocations; | |
... | |
begin | |
Include(locs, locHere); | |
end; |
Now, imagine you have several loops and points where any TLocation is added to or substracted from locs. At a later point in your application you need the number of elements in locs. Although that seems totally trivial that is not implemented in Delphi's compiler. The only SET related procedures in Delphi's compiler are Include, Exclude and In - there is no Count method for SETs. Well, you can help out and write your own one, as I discovered here:
Code:
function CountSetItems(SetValue: Integer): Byte; | |
var | |
Mask: Integer; | |
begin | |
Mask := $80000000; | |
Result := 0; | |
while Mask <> 0 do begin | |
if SetValue and Mask <> 0 then | |
inc(Result); | |
Mask := Mask shr 1; | |
end; | |
end; |