Note that there are some explanatory texts on larger screens.

plurals
  1. POEnumerate possible set values in Delphi
    primarykey
    data
    text
    <p>I have a calculation algorithm in Delphi with a number of different options, and I need to try every combination of options to find an optimal solution.</p> <pre><code>TMyOption = (option1, option2, option3, option4); TMyOptions = set of TMyOption; </code></pre> <p>I wondered about using an Integer loop to enumerate them:</p> <pre><code>for EnumerationInteger := 0 to 15 do begin Options := TMyOptions(EnumerationInteger); end; </code></pre> <p>This does not compile. What I was wondering was if there was any fairly simple method to convert from Integer to Set (most questions on the Web try to go the other way, from Set to Integer), and if so what is it?</p> <p>Another possibility is to just use the Integer as a bit-field:</p> <pre><code>C_Option1 = 1; C_Option2 = 2; C_Option3 = 4; C_Option4 = 8; </code></pre> <p>and then test membership with a bitwise and:</p> <pre><code>if (Options and C_Option2) &gt; 0 then begin ... end; </code></pre> <p>I've tried this, and it works, but it feels like working with sets would be more natural and use the type system better (even though I'm going outside the said type system to enumerate the sets).</p> <p>Is there a better/safer way to enumerate all possible set combinations than enumerating the underlying integer representation?</p> <p>Notes:</p> <ol> <li>I know that the integer values of a set are not guaranteed in theory (though I suspect they are in practice if you don't play with the enumeration numbering).</li> <li>There could be more than four options (yes, I know that it grows exponentially and if there are too many options the algorithm could take forever).</li> </ol>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload