For a small project on processor affinity
, I want to create a simpler method than just a bunch of if(lvProcessors.Items[0].Checked == true && lvProcessors.Items[1] == true etc)
comparing their exact values to see which code needs to be transferred to an (IntPtr)
.
To make the code slightly more efficiënt, I would like to compare an array containing booleans
, to at least 14 other arrays containing booleans
.
Example code:
var CheckState = new[] { lvProcessors.Items[0].Checked, lvProcessors.Items[1].Checked, lvProcessors.Items[2].Checked, lvProcessors.Items[3].Checked };
//setcore1 == 1, setcore2 == 2, etc
var SetCore1 = new[] { true, false,false,false };
var SetCore2 = new[] { true, true, false, false };
var SetCore3 = new[] { false, true, false, false };
var SetCore4 = new[] { true, false, true, false };
var SetCore5 = new[] { false, true, true, false };
var SetCore6 = new[] { true, true, true, false };
var SetCore7 = new[] { false, false, false, true };
var SetCore8 = new[] { true, false, false, true };
var SetCore9 = new[] { false, true, false, true };
var SetCore10 = new[] { true, true, false, true };
var SetCore11 = new[] { false, false, true, true };
var SetCore12 = new[] { true, false, true, true };
var SetCore13 = new[] { false, true, true, true };
var SetCore14 = new[] { true, true, true, true };
int switchcounter = 1;
switch (switchcounter)
{
case 15:
break;
default:
if (Enumerable.SequenceEqual(CheckState,<insertdynamicarrayname))
{
AffinityState = (IntPtr)switchcounter;
}
else
{
switchcounter++;
goto default;
}
break;
}
So if the first checkbox
in the listview lvProcessors
is checked, the var CheckState
will generate an array containing { true, false,false,false }
This in turn must be compared to one of the SetCore
arrays, and will in this case cause a match with SetCore1
.
So what I would like to know is; how can I create a dynamic arrayname, based on the switchcounter
on the code, that will fuse "SetCore"
and switchcounter.ToString()
, thus creating SetCore1, SetCore2, SetCore3, etc
.
[EDIT]
As suggested by @Luaan, I've implemented his code to what I would've like it to be:
var SetCore1 = new[] { true, false, false, false};
[..]
var SetCore15 = new[] { true, true, true, true };
var allSets = new [] { SetCore1, SetCore2, SetCore3,SetCore4,SetCore5,SetCore6,SetCore7,SetCore8,SetCore9,SetCore10,SetCore11,SetCore12,SetCore13,SetCore14,SetCore15 };
foreach (var set in allSets)
{
MessageBox.Show(counter.ToString());
if (Enumerable.SequenceEqual(set, CheckState))
{
AffinityState = (IntPtr)counter;
break;
}
else if (Enumerable.SequenceEqual(set, CheckState) == false)
{
counter++;
}
}
EditProcess.ProcessorAffinity = (IntPtr)AffinityState;
In this code, depending on the input of the listviewcheckboxes
, it will match the depending result to a SetCore
and using the counter
it will give the proper int
which is converted at the end of the foreach
loop and set the very specific cores(i.e. 1, 2 or 0, 2, 3, etc) to be used for the selected process
which has been selected earlier in the code(not visible).
Thanks for the suggestion on bitmasks, they look useful, but currently I have no idea how to implement them without taking half my application apart.