in reply to Re^5: Simple Matching
in thread Simple Matching
Captures are slow, so it would be faster to build a regex without captures I also generalised some of the matching. Input validation is not a goal here.
m{ ^ . (?: D (?: . .D ){4} | S (?: . .S ){4} | C (?: . .C ){4} | H (?: . .H ){4} ) \z }xs;
Or maybe even
built it manually here, but it can be built dynamically.m{ ^ . (?: D..D..D..D..D | S..S..S..S..S | C..C..C..C..C | H..H..H..H..H ) \z }xs;
Update: That last regex pattern gives me an idea:
my $masked = $_ & "\x00\xFF \x00\xFF \x00\xFF \x00\xFF \x00\xFF"; ( $masked eq "\x00D \x00D \x00D \x00D \x00D" || $masked eq "\x00S \x00S \x00S \x00S \x00S" || $masked eq "\x00C \x00C \x00C \x00C \x00C" || $masked eq "\x00H \x00H \x00H \x00H \x00H" )
Of course, if the input format was a number, it would be even faster:
sub build { return ( $_[0] << (16*4 + 2*5 ) ) + ( $_[1] << (16*4 + 2*4 ) ) + ( $_[2] << (16*3 + 2*4 ) ) + ( $_[3] << (16*3 + 2*3 ) ) + ( $_[4] << (16*2 + 2*3 ) ) + ( $_[5] << (16*2 + 2*2 ) ) + ( $_[6] << (16*1 + 2*2 ) ) + ( $_[7] << (16*1 + 2*1 ) ) + ( $_[8] << (16*0 + 2*1 ) ) + ( $_[9] << (16*0 + 2*0 ) ); } use constant { SUITS_MASK => build(0,3, 0,3, 0,3, 0,3, 0,3), ALL_D => build(0,0, 0,0, 0,0, 0,0, 0,0), ALL_S => build(0,1, 0,1, 0,1, 0,1, 0,1), ALL_C => build(0,2, 0,2, 0,2, 0,2, 0,2), ALL_H => build(0,3, 0,3, 0,3, 0,3, 0,3), }; my $suits = $_ & SUITS_MASK; ( $suits == ALL_D || $suits == ALL_S || $suits == ALL_C || $suits == ALL_H )
|
|---|