Another “switch for strings” - but not only for strings and not only “switch”
This article was provoked by an interesting topic on habrahabr.ru/post/166201 about the Switch operator for C ++ and the desire to provide the community with another idea and embodiment for evaluation.
One of the drawbacks of the Switch operator in C ++ is the limited type of data to which it can be applied and the author of the article by reference brought an interesting solution.
I’ll try to propose another solution, which may not be so good, but does not require additional features of the language and works for any types of variables in C ++ and also processes all possible truth options for the above conditions.
Those. Another “switch for strings” - but not only for strings and not only “switch”.
The given solution does not pretend to be original and it is expected that the idea has already appeared on the web pages more than once, but in the form in which I suggest it has not yet been seen, I apologize if such an option has already been described by someone.
The idea is to use the ternary operator?: In the Switch condition,
let's say “test” is a test variable and “var_ i” i = 1, .., 4 are the variables with which it needs to be compared
from the construction
bool bool_1, bool_2, bool_3, bool_4;
bool_1 = (test == var_1);
bool_2 = (test == var_2);
bool_3 = (test == var_3);
bool_4 = (test == var_4);
it is clear that bool_ ii = 1, .., 4 are logical conditions and the variables test and var_i i = 1, .., 4 can be of any type, most importantly they are of the same type.
the idea in the next line is
switch ((bool_1? 1: 0) | (bool_2? 2: 0) | (bool_3? 4: 0) | (bool_4? 8: 0))
and that switch will get the result for the decision “or” operations on binary powers of two.
This gives not only a classic switch, but also the promised processing of all truth options i.e. all 16 options for 4 conditions.
Conditions in switch can be added in any quantity, keeping successive powers of two in the second argument of each ternary operator.
Explanation further:
First, about the ternary operator - he, as you know, will choose the power of two or the next 0 following the truth of the variable bool_ ii = 1, .., 4. Degrees of two were chosen, of course, not by chance, they make it possible to process all possible truth versions.
If we have one and only one of the conditions bool_ i, then the switch will work in a classic way and select bool_ i since all the others will give 0. That is, The result of switch will be the condition in which the variable bool_ i is true.
If several different bool_i variables are true, then the mathematical clause of several different degrees of two will appear in the switch condition. Since this is a different binary level twos and ones that have at various locations and will continue as a result of logical disjunction while the remaining places will remain 0
Ie Switch will switch to the case where just these several different bool_i variables are true and the other conditions are false.
Below is a working version in which in the classical case of truth of only one of the conditions bool_ i, the test variable test is assigned the corresponding value of the var_i variable.
For the rest of the truth distribution options, I have given the corresponding truth descriptions in English.
switch ((bool_1? 1: 0) | (bool_2? 2: 0) | (bool_3? 4: 0) | (bool_4? 8: 0))
{
case 1: test = var_1;
break;
case 2: test = var_2;
break;
case 4: test = var_3;
break;
case 8: test = var_4;
break;
case 3: std :: cout << “bool_1 bool_2 true bool_3 bool_4 false” << std :: endl;
break;
case 5: std :: cout << “bool_1 bool_3 true bool_2 bool_4 false” << std :: endl;
break;
case 9: std :: cout << “bool_1 bool_4 true bool_2 bool_3 false” << std :: endl;
break;
case 6: std :: cout << “bool_2 bool_3 true bool_1 bool_4 false” << std :: endl;
break;
case 10: std :: cout << “bool_2 bool_4 true bool_1 bool_3 false” << std :: endl;
break;
case 12: std :: cout << “bool_3 bool_4 true bool_1 bool_2 false” << std :: endl;
break;
case 7: std :: cout << “bool_3 bool_2 bool_1 true bool_4 false” << std :: endl;
break;
case 11: std :: cout << “bool_4 bool_2 bool_1 true bool_3 false” << std :: endl;
break;
case 14: std :: cout << “bool_4 bool_2 bool_3 true bool_1 false” << std :: endl;
break;
case 13: std :: cout << “bool_4 bool_1 bool_3 true bool_2 false” << std :: endl;
break;
case 15: std :: cout << “bool_4 bool_1 bool_3 bool_2 true” << std :: endl;
break;
default: std :: cout << “this is the same as 0 bool_1 bool_2 bool_3 bool_4 false” << std :: endl;
} the
given variant is checked under Debian.
Of course, you can write a script that will automatically generate such a switch, not only for 4 but also for any number of conditions. Since switch has no limit on the number of conditions, the limit will be only used memory.
Thank you in advance for all comments and suggestions.
One of the drawbacks of the Switch operator in C ++ is the limited type of data to which it can be applied and the author of the article by reference brought an interesting solution.
I’ll try to propose another solution, which may not be so good, but does not require additional features of the language and works for any types of variables in C ++ and also processes all possible truth options for the above conditions.
Those. Another “switch for strings” - but not only for strings and not only “switch”.
The given solution does not pretend to be original and it is expected that the idea has already appeared on the web pages more than once, but in the form in which I suggest it has not yet been seen, I apologize if such an option has already been described by someone.
The idea is to use the ternary operator?: In the Switch condition,
let's say “test” is a test variable and “var_ i” i = 1, .., 4 are the variables with which it needs to be compared
from the construction
bool bool_1, bool_2, bool_3, bool_4;
bool_1 = (test == var_1);
bool_2 = (test == var_2);
bool_3 = (test == var_3);
bool_4 = (test == var_4);
it is clear that bool_ ii = 1, .., 4 are logical conditions and the variables test and var_i i = 1, .., 4 can be of any type, most importantly they are of the same type.
the idea in the next line is
switch ((bool_1? 1: 0) | (bool_2? 2: 0) | (bool_3? 4: 0) | (bool_4? 8: 0))
and that switch will get the result for the decision “or” operations on binary powers of two.
This gives not only a classic switch, but also the promised processing of all truth options i.e. all 16 options for 4 conditions.
Conditions in switch can be added in any quantity, keeping successive powers of two in the second argument of each ternary operator.
Explanation further:
First, about the ternary operator - he, as you know, will choose the power of two or the next 0 following the truth of the variable bool_ ii = 1, .., 4. Degrees of two were chosen, of course, not by chance, they make it possible to process all possible truth versions.
If we have one and only one of the conditions bool_ i, then the switch will work in a classic way and select bool_ i since all the others will give 0. That is, The result of switch will be the condition in which the variable bool_ i is true.
If several different bool_i variables are true, then the mathematical clause of several different degrees of two will appear in the switch condition. Since this is a different binary level twos and ones that have at various locations and will continue as a result of logical disjunction while the remaining places will remain 0
Ie Switch will switch to the case where just these several different bool_i variables are true and the other conditions are false.
Below is a working version in which in the classical case of truth of only one of the conditions bool_ i, the test variable test is assigned the corresponding value of the var_i variable.
For the rest of the truth distribution options, I have given the corresponding truth descriptions in English.
switch ((bool_1? 1: 0) | (bool_2? 2: 0) | (bool_3? 4: 0) | (bool_4? 8: 0))
{
case 1: test = var_1;
break;
case 2: test = var_2;
break;
case 4: test = var_3;
break;
case 8: test = var_4;
break;
case 3: std :: cout << “bool_1 bool_2 true bool_3 bool_4 false” << std :: endl;
break;
case 5: std :: cout << “bool_1 bool_3 true bool_2 bool_4 false” << std :: endl;
break;
case 9: std :: cout << “bool_1 bool_4 true bool_2 bool_3 false” << std :: endl;
break;
case 6: std :: cout << “bool_2 bool_3 true bool_1 bool_4 false” << std :: endl;
break;
case 10: std :: cout << “bool_2 bool_4 true bool_1 bool_3 false” << std :: endl;
break;
case 12: std :: cout << “bool_3 bool_4 true bool_1 bool_2 false” << std :: endl;
break;
case 7: std :: cout << “bool_3 bool_2 bool_1 true bool_4 false” << std :: endl;
break;
case 11: std :: cout << “bool_4 bool_2 bool_1 true bool_3 false” << std :: endl;
break;
case 14: std :: cout << “bool_4 bool_2 bool_3 true bool_1 false” << std :: endl;
break;
case 13: std :: cout << “bool_4 bool_1 bool_3 true bool_2 false” << std :: endl;
break;
case 15: std :: cout << “bool_4 bool_1 bool_3 bool_2 true” << std :: endl;
break;
default: std :: cout << “this is the same as 0 bool_1 bool_2 bool_3 bool_4 false” << std :: endl;
} the
given variant is checked under Debian.
Of course, you can write a script that will automatically generate such a switch, not only for 4 but also for any number of conditions. Since switch has no limit on the number of conditions, the limit will be only used memory.
Thank you in advance for all comments and suggestions.