Denis, you have not told us whether your flags are mutually exclusive ($a can contain ABA - only) or they can be turned on at the same time ($a can contain ABA and SCO).
In the olden days, in the caves, they used *flags*. Each flag was denoted by a digit in a binary word. The binary word making up the "options". Flags as binary words had the added benefit of having a bunch of them turned on at the same time. The negative side is that they are all binary/integers and made little sense to the user without consulting the manual - though most times they were internal and nobody cared about their values, e.g. C's open()'s O_CREAT and O_APPEND flags.
Here is a refresher (translated from C):
#!/usr/bin/env perl use strict; use warnings; use Getopt::Long; # our constants use constant ABA => 0b00001; # 0b... for binary number use constant SCO => 0b00010; use constant ACC => 0b00100; my $a = 0b0; Getopt::Long::GetOptions( 'ABA' => sub { $a |= ABA }, 'SCO' => sub { $a |= SCO }, 'ACC' => sub { $a |= ACC }, ) or die "error command line"; if( $a & SCO ){ print "it has SCO!\n"; } if( $a & ABA ){ print "it has ABA!\n"; } if( $a & ACC ){ print "it has ACC!\n"; }
This is quite fast but I am sure there will be aesthetic objections including mine (which balances with my sentimental counter-objections).
In the Present, we do away with all these and instead use string constants and string comparisons which are more expensive.
I will not push any further the point of "cost" and "speed", but I will highlight the point of "multiple flags coexisting in a variable". Which is very useful as it reflects real-life situations. I am sure whizkids can make flags stored in strings turned on at the same time by devising a way to encode them in a string which can be robust yet ... quite unreadable, like the binary flags.
Against the binary flags is the MAXINT limitation which let's say is 2^64, it will limit the max number of flags to check on to 64 :(
PS. If this forum were another, I am sure someone would have suggested a class for handling this kind of situations.
PS2. Flags and Semaphores!
In reply to Re: Sparing multiple 'or's
by bliako
in thread Sparing multiple 'or's
by Denis
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |