I have a Delphi calculation algorithm with many different options, and I need to try every combination of options to find the best solution.
TMyOption = (option1, option2, option3, option4); TMyOptions = set of TMyOption;
I wondered about using an Integer loop to list them:
for EnumerationInteger := 0 to 15 do begin Options := TMyOptions(EnumerationInteger); end;
This does not compile. I was wondering if there was any pretty simple way to convert from Integer to Set (most questions on the Internet try to go the other way, from Set to Integer), and if so, what is it?
Another possibility is to simply use Integer as a bit field:
C_Option1 = 1; C_Option2 = 2; C_Option3 = 4; C_Option4 = 8;
and then check the membership bit by bit and:
if (Options and C_Option2) > 0 then begin ... end;
I tried this and it works, but it seems that working with sets will be more natural and it is better to use a type system (even if I go beyond the specified type system to enumerate sets).
Is there a better / safer way to list all the possible combinations of combinations than listing the main integer representation?
Notes:
- I know that the integer values ββof a set are not guaranteed in theory (although I suspect that they are in practice if you are not playing with enumeration numbering).
- There may be more than four options (yes, I know that it grows exponentially, and if there are too many options, the algorithm can last forever).
set delphi
Jonathan morgan
source share