blib/lib/Bitmask/Data.pm

filter: Statementstmt 75.4% 197 / 261 Branchbran 61.1% 33 / 54 Conditioncond 65.2% 15 / 23 MC/DCmcdc 35.7% 5 / 14 Subroutinesub 66.6% 34 / 51 Podpod 96.1% 25 / 26 totaltotal 72.0% 309 / 429 CC 38 SCAR 15.6
CC
38
Cov
72%
init
13.8
first
12.0
any2bitmask
6.2
CRAP 4.8
↑ Summary
1 # ============================================================================
2 package Bitmask::Data;
3 # ============================================================================
4 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶no if $] >= 5.017004, warnings => qw(experimental::smartmatch);
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 ▶use parent qw(Class::Data::Inheritable);
Subroutine BEGIN: called CC=1 SCAR=0.0
9 9 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 9 ▶use Config;
Subroutine BEGIN: called CC=1 SCAR=0.0
13 9 ▶use List::Util qw(reduce);
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶use Scalar::Util qw(blessed);
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 9 our $VERSION = version->new('2.05');
17 9 our $AUTHORITY = 'cpan:MAROS';
18  
19 9 our $ZERO = chr(0);
20 9 our $ONE = chr(1);
21  
22 =encoding utf8
23  
24 =head1 NAME
25  
26 Bitmask::Data - Handle unlimited length bitmasks in an easy and flexible way
27  
28 =head1 SYNOPSIS
29  
30 # Create a simple bitmask class
31 package MyBitmask;
32 use base qw(Bitmask::Data);
33 __PACKAGE__->bitmask_length(18);
34 __PACKAGE__->bitmask_default('0b000000000000000011');
35 __PACKAGE__->init(
36 'value1' => '0b000000000000000001',
37 'value2' => '0b000000000000000010',
38 'value2' => '0b000000000000000100',
39 'value4' => '0b000000000000001000',
40 'value5' => '0b000000000000010000',
41 ...
42 );
43
44 ## Somewhere else in your code
45 use MyBitmask;
46 my $bm1 = MyBitmask->new('value1','value3');
47 my $bm2 = MyBitmask->new('0b000000000000010010');
48 $bm1->add('value3');
49 my $bm3 = $bm1 | $bm2;
50 $bm3->string;
51  
52 =head1 DESCRIPTION
53  
54 This package helps you dealing with bitmasks. First you need to subclass
55 Bitmask::Data and set the bitmask values and length. (If you are only working
56 with a single bitmask in a simple application you might also initialize
57 the bitmask directly in the Bitmask::Data module).
58  
59 After the initialization you can create an arbitrary number of bitmask
60 objects which can be accessed and manipulated with convenient methods and
61 overloaded arithmetic and bit operators.
62  
63 Bitmask::Data does not store bitmasks as integers internally, but as
64 strings conststing of \0 and \1, hence makinging unlimited length bitmasks
65 possible.
66  
67 =head1 METHODS
68  
69 =head2 Class Methods
70  
71 =head3 bitmask_length
72  
73 Set/Get the length of the bitmask. Do not change this value after the
74 initialization.
75  
76 Bitmask length is unlimited.
77  
78 Default: 16
79  
80 =head3 bitmask_default
81  
82 Set/Get the default bitmask for empty Bitmask::Data objects.
83  
84 Default: undef
85  
86 =head3 bitmask_lazyinit
87  
88 If true warning for lazy initialization are disabled. (Lazy
89 initialization = call of init without bitmask bit values).
90  
91 Default: 0
92  
93 __PACKAGE__->bitmask_lazyinit(1);
94 __PACKAGE__->bitmask_length(6);
95 __PACKAGE__->init(
96 'value1', # will be 0b000001
97 'value2', # will be 0b000010
98 'value3' # will be 0b000100
99 );
100
101 If bitmask_lazyinit is 2 then bit values will be filled from left to right,
102 otherwise from right to left
103
104 __PACKAGE__->bitmask_lazyinit(2);
105 __PACKAGE__->bitmask_length(6);
106 __PACKAGE__->init(
107 'value1', # will be 0b100000
108 'value2', # will be 0b010000
109 'value3' # will be 0b001000
110 );
111  
112 =head3 bitmask_items
113  
114 HASHREF of all bitmask items, with values as keys and bitmask as values.
115  
116 =head3 init
117  
118 CLASS->init(LIST of VALUES);
119  
120 Initializes the bitmask class. You can supply a list of possible values.
121 Optionally you can also specify the bits for the mask by adding bit values
122 after the value.
123
124 CLASS->init(
125 'value1' => 0b000001,
126 'value2' => 0b000010,
127 'value3' => 0b001000,
128 'value4' => 0b010000,
129 );
130
131 With C<bitmask_lazyinit> enabled you can also skip the bitmask bit values
132  
133 CLASS->bitmask_lazyinit(1);
134 CLASS->init(
135 'value1',
136 'value2',
137 'value3',
138 'value4',
139 );
140  
141 Bits may be supplied as integers, strings or Math::BigInt objects
142 (not recommended).
143  
144 CLASS->init(
145 'value1' => 0b000001, # integer
146 'value2' => 2, # integer
147 'value3' => '0b000100' # string starting with '0b'
148 'value4' => '0B001000' # string starting with '0B'
149 'value5' => '\0\1\0\0\0\0' # string consisting of \0 and \1
150 'value6' => Math::BigInt->new("32") # Math::BigInt object
151 );
152  
153 =cut
154  
155 9 __PACKAGE__->mk_classdata( bitmask_length => 16 );
156 9 __PACKAGE__->mk_classdata( bitmask_items => {} );
157 9 __PACKAGE__->mk_classdata( bitmask_default => undef );
158 9 __PACKAGE__->mk_classdata( bitmask_lazyinit => 0 );
159 9 __PACKAGE__->mk_classdata( bitmask_empty => undef );
160 9 __PACKAGE__->mk_classdata( bitmask_full => undef );
161  
162 use overload
163 9 ▶ '<=>' => '_compare',
Subroutine BEGIN: called CC=1 SCAR=2.9
164 'cmp' => '_compare',
165 '==' => '_equals',
166 'eq' => '_equals',
167 4 ▶ '~~' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
168 4 my ($self,$value) = @_;
169 4 my $bitmask = $self->any2bitmask($value);
170 3 ▶ return (($bitmask & $self->{bitmask}) ne $self->bitmask_empty) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
($bitmask & $self->{'bitmask'}) ne $self->bitmask_empty ? : 2 1 3
171 },
172 0 ▶ 'bool' => sub {
Subroutine __ANON__: not called CC=2 SCAR=17.9
173 0 my ($self) = @_;
174 0 ▶ return ($self->{bitmask} ne $self->bitmask_empty) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$self->{'bitmask'} ne $self->bitmask_empty ? : 0 0 0
175 },
176 '0+' => 'integer',
177 '""' => 'string',
178 '+=' => 'add',
179 '-=' => 'remove',
180 2 ▶ '+' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
181 2 my ($self,$value) = @_;
182 2 return $self->clone->add($value);
183 },
184 1 ▶ '-' => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
185 1 my ($self,$value) = @_;
186 1 return $self->clone->remove($value);
187 },
188 0 ▶ '&' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
189 0 my ($self,$value) = @_;
190 0 my $bitmask = $self->any2bitmask($value);
191 0 return $self->new_from_bitmask($self->{bitmask} & $bitmask);
192 },
193 0 ▶ '^' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
194 0 my ($self,$value) = @_;
195 0 my $bitmask = $self->any2bitmask($value);
196 0 return $self->new_from_bitmask($self->{bitmask} ^ $bitmask);
197 },
198 0 ▶ '|' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
199 0 my ($self,$value) = @_;
200 0 my $bitmask = $self->any2bitmask($value);
201 0 return $self->new_from_bitmask($self->{bitmask} | $bitmask);
202 },
203 0 ▶ '&=' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
204 0 my ($self,$value) = @_;
205 0 my $bitmask = $self->any2bitmask($value);
206 0 $self->{bitmask} &= $bitmask;
207 #$self->{cache} = undef;
208 0 return $self;
209 },
210 0 ▶ '^=' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
211 0 my ($self,$value) = @_;
212 0 my $bitmask = $self->any2bitmask($value);
213 0 $self->{bitmask} ^= $bitmask;
214 #$self->{cache} = undef;
215 0 return $self;
216 },
217 0 ▶ '|=' => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
218 0 my ($self,$value) = @_;
219 0 my $bitmask = $self->any2bitmask($value);
220 0 $self->{bitmask} |= $bitmask;
221 #$self->{cache} = undef;
222 0 return $self;
223 },
224 0 ▶ "~" => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
225 0 my ($self) = @_;
226 0 return $self->clone->neg();
227 9 };
228
229 2 ▶sub _equals {
Subroutine _equals: called CC=1 SCAR=0.0
230 2 my ($self,$value) = @_;
231 2 my $bitmask = $self->any2bitmask($value);
232 2 return ($self->{bitmask} eq $bitmask);
233 }
234
235 0 ▶sub _compare {
Subroutine _compare: not called CC=1 SCAR=6.9
236 0 my ($self,$value) = @_;
237  
238 0 my $bitmask = $self->any2bitmask($value);
239
240 0 return $self->{bitmask} cmp $bitmask;
241 }
242  
243 8 ▶sub init {
Subroutine init: called CC=13 SCAR=26.3
Pod documented
244 8 my ($class,@params) = @_;
245  
246 8 my $length = $class->bitmask_length;
247  
248 8 ▶ croak('Bitmask length not set')
Branch: 1 branch
BranchTrueFalseTotal
unless $length and $length > 0 0 8 8
Condition33%1/3
$length and $length > 0
!ll&&!rl&&r
008
MC/DC0%0/2
$length and $length > 0
$length $length > 0
Truth table33%1/3
A and B
A= $lengthB= $length > 0
ABresult
0X0
100
111
249 unless $length && $length > 0;
250
251 8 $class->bitmask_empty($ZERO x $length);
252  
253 8 my $items = {};
254 8 my $count = 0;
255 8 my $bitmask_full = $class->bitmask_empty();
256
257 # Take first element from @params
258 8 ▶ while (my $name = shift(@params)) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $name = shift @params) 196 6 202
259 196 my ($bit,$bit_readable);
260  
261 196 $count++;
262
263 196 ▶ croak(sprintf('Too many values in bitmask: max <%i>',$class->bitmask_length))
Branch: 1 branch
BranchTrueFalseTotal
if $count > $class->bitmask_length 0 196 196
264 if $count > $class->bitmask_length;
265  
266 196 ▶ given ( $params[0] // '' ) {
Condition100%2/2
$params[0] // ""
l!l
1924
MC/DC100%1/1
$params[0] // ""
$params[0]
Truth table100%2/2
A
A= $params[0]
Aresult
11
00
267 196 ▶ when (blessed $_ && $_->isa('Math::BigInt')) {
Condition66%2/3
builtin::blessed($_) && $_->isa('Math::BigInt')
!ll&&!rl&&r
19402
MC/DC50%1/2
builtin::blessed($_) && $_->isa('Math::BigInt')
builtin::blessed($_) $_->isa('Math::BigInt')
Truth table66%2/3
A && B
A= builtin::blessed($_)B= $_->isa('Math::BigInt')
ABresult
0X0
100
111
268 0 $bit = $class->string2bit(shift(@params)->as_bin());
269 }
270 194 when (m/^\d+$/) {
271 14 $bit = $class->int2bit(shift(@params));
272 }
273 180 when (m/^0[bB][01]+$/) {
274 2 $bit = $class->string2bit(shift(@params));
275 }
276 178 when (m/^[$ZERO$ONE]+$/) {
277 0 $bit = $class->bit2bit(shift(@params));
278 }
279 178 default {
280 178 ▶ carp( "Lazy bitmask initialization detected: Please enable"
Branch: 1 branch
BranchTrueFalseTotal
unless $class->bitmask_lazyinit 0 178 178
281 . " <bitmask_lazyinit> or change init parameters" )
282 unless ( $class->bitmask_lazyinit );
283 178 $bit = $class->bitmask_empty;
284
285 178 ▶ if ($class->bitmask_lazyinit == 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($class->bitmask_lazyinit == 2) { } 5 173 178
286 5 substr($bit,($count-1),1,$ONE);
287 } else {
288 173 substr($bit,($length-$count),1,$ONE);
289 }
290
291 }
292 }
293
294 194 $bit_readable = $bit;
295 194 $bit_readable =~ tr/\0\1/01/;
296
297 194 ▶ croak(sprintf('Invalid bit value <%s>',$bit_readable))
Branch: 1 branch
BranchTrueFalseTotal
unless $bit =~ /^[$Bitmask::Data::ZERO$Bitmask::Data::ONE]{$length}$/ 0 194 194
298 unless $bit =~ /^[$ZERO$ONE]{$length}$/;
299  
300 croak(sprintf('Duplicate value <%s> in bitmask',$name))
301 194 ▶ if exists $items->{$name};
Branch: 1 branch
BranchTrueFalseTotal
if exists $items->{$name} 0 194 194
302  
303 croak(sprintf('Duplicate bit <%s> in bitmask',$bit_readable))
304 194 ▶ if grep { ($_ & $bit) ne $class->bitmask_empty } values %{$items};
Branch: 1 branch
BranchTrueFalseTotal
if grep {($_ & $bit) ne $class->bitmask_empty;} values %{$items;} 0 194 194
305  
306 194 $bitmask_full |= $bit;
307 194 $items->{$name} = $bit;
308 }
309  
310 6 $class->bitmask_full($bitmask_full);
311 6 $class->bitmask_items($items);
312 6 ▶ $class->bitmask_default($class->any2bitmask($class->bitmask_default))
Branch: 1 branch
BranchTrueFalseTotal
if defined $class->bitmask_default 1 5 6
313 if defined $class->bitmask_default;
314 6 return;
315 }
316  
317 =head3 int2bit
318  
319 my $bitmask_string = CLASS->int2bit(INTEGER);
320  
321 Helper method that turns an integer into the internal bitmask representation
322  
323 =cut
324  
325 23 ▶sub int2bit {
Subroutine int2bit: called CC=1 SCAR=0.0
Pod documented
326 23 my ($class,$integer) = @_;
327
328 23 my $bit = sprintf( '%0' . $class->bitmask_length . 'b', $integer );
329 23 $bit =~ tr/01/\0\1/;
330 23 return $bit;
331 }
332  
333 =head3 string2bit
334  
335 my $bitmask_string = CLASS->string2bit(STRING);
336  
337 Helper method that takes a string like '0B001010' or '0b010101' and turns it
338 into the internal bitmask representation
339  
340 =cut
341  
342 4 ▶sub string2bit {
Subroutine string2bit: called CC=1 SCAR=0.0
Pod documented
343 4 my ($class,$string) = @_;
344  
345 4 $string =~ s/^0[bB]//;
346 4 $string = sprintf( '%0' . $class->bitmask_length . 's', $string );
347 4 $string =~ tr/01/\0\1/;
348 4 return $string;
349 }
350  
351 3 ▶sub bit2bit {
Subroutine bit2bit: called CC=1 SCAR=0.0
Pod undocumented
352 3 my ($class,$bit) = @_;
353
354 3 $bit = $ZERO x ($class->bitmask_length - length($bit)) . $bit;
355 3 return $bit;
356 }
357  
358 =head3 any2bitmask
359  
360 my $bitmask_string = CLASS->any2bitmask(ANYTHING);
361  
362 Helper method that tries to turn arbitrary arguments into the internal bitmask
363 representation. This method can hanle
364  
365 =over
366  
367 =item * any Bitmask::Data object
368  
369 =item * Math::BigInt object
370  
371 =item * a string matching on of the bitmask values
372  
373 =item * a bitmask string consisting of \0 and \1 characters
374  
375 =item * a bitmask string starting with '0b' or '0B' and containing only 0 and 1
376  
377 =item * an integer
378  
379 =back
380  
381 =cut
382  
383 27 ▶sub any2bitmask {
Subroutine any2bitmask: called CC=6 SCAR=18.3
Pod documented
384 27 my ($class,$param) = @_;
385  
386 27 ▶ croak "Bitmask, Item or integer expected"
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 0 27 27
387 unless defined $param;
388  
389 27 my $length = $class->bitmask_length;
390 27 my $bit;
391 27 given ($param) {
392 27 ▶ when (blessed $param && $param->isa('Bitmask::Data')) {
Condition100%3/3
builtin::blessed($param) && $param->isa('Bitmask::Data')
!ll&&!rl&&r
2313
MC/DC100%2/2
builtin::blessed($param) && $param->isa('Bitmask::Data')
builtin::blessed($param) $param->isa('Bitmask::Data')
Truth table100%3/3
A && B
A= builtin::blessed($param)B= $param->isa('Bitmask::Data')
ABresult
0X0
100
111
393 2 $bit = $class->bit2bit($param->{bitmask});
394 }
395 24 ▶ when (blessed $param && $param->isa('Math::BigInt')) {
Condition66%2/3
builtin::blessed($param) && $param->isa('Math::BigInt')
!ll&&!rl&&r
2310
MC/DC0%0/2
builtin::blessed($param) && $param->isa('Math::BigInt')
builtin::blessed($param) $param->isa('Math::BigInt')
Truth table66%2/3
A && B
A= builtin::blessed($param)B= $param->isa('Math::BigInt')
ABresult
0X0
100
111
396 0 $bit = $class->string2bit($param->as_bin());
397 }
398 23 when ($param ~~ $class->bitmask_items) {
399 10 $bit = $class->bitmask_items->{$param};
400 }
401 13 when (m/^[$ZERO$ONE]+$/) {
402 1 $bit = $class->bit2bit($param);
403 }
404 12 when (m/^[01]{$length}$/) {
405 1 $bit = $class->string2bit($param);
406 }
407 11 when (m/^0[bB][01]+$/) {
408 1 $bit = $class->string2bit($param);
409 }
410 10 when (m/^\d+$/) {
411 9 $bit = $class->int2bit($param);
412 }
413 1 default {
414 1 croak sprintf('Could not turn <%s> into something meaningful',$param);
415 }
416 }
417
418 24 ▶ if (length $bit > $class->bitmask_length) {
Branch: 1 branch
BranchTrueFalseTotal
if (CORE::length $bit > $class->bitmask_length) 0 24 24
419 0 croak sprintf('<%s> exceeds maximum lenth of %i',$param,$class->bitmask_length);
420 }
421
422 24 ▶ if (($class->bitmask_full | $bit) ne $class->bitmask_full) {
Branch: 1 branch
BranchTrueFalseTotal
if (($class->bitmask_full | $bit) ne $class->bitmask_full) 0 24 24
423 0 croak sprintf('<%s> tries to set undefined bits',$param);
424 }
425  
426 24 return $bit;
427 }
428  
429 =head3 _parse_params
430  
431 my $bitmask_string = CLASS->_parse_params(LIST)
432  
433 Helper method for parsing params passed to various methods.
434  
435 =cut
436  
437 22 ▶sub _parse_params {
Subroutine _parse_params: called CC=4 SCAR=14.0
438 22 my ($class,@params) = @_;
439
440 22 my $result_bitmask = $class->bitmask_empty;
441
442 22 foreach my $param (@params) {
443 next
444 24 ▶ unless defined $param;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $param 4 20 24
445
446 20 my $bitmask;
447 20 ▶ if ( ref $param eq 'ARRAY' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $param eq 'ARRAY') { } 0 20 20
448 0 $bitmask = $class->_parse_params(@$param);
449 }
450 else {
451 20 $bitmask = $class->any2bitmask($param);
452 }
453  
454 18 $result_bitmask = $result_bitmask | $bitmask;
455 }
456  
457 20 return $result_bitmask;
458 }
459  
460 # TODO : method to return ordered bitmask items
461  
462 =head2 Overloaded operators
463  
464 Bitmask::Data uses overload by default.
465  
466 =over
467  
468 =item * Numeric context
469  
470 Returns bitmask integer value (see L<integer> method). For large bitmasks
471 (> 40 bits) this will always be a L<Math::BigInt> object (hence using this
472 method is not recommended).
473  
474 =item * Scalar context
475  
476 Returns bitmask string representation (see L<string> method)
477  
478 =item * ==, eq, <=>, cmp
479  
480 Works like 'has_any'
481  
482 =item * ~~
483  
484 Works like L<has_any>.
485  
486 $bm = Somebitmask->new('v1','v2');
487 $bm ~~ ['v1','v3'] # true, because 'v1' matches even if 'v3' is not set
488  
489 =item * +, -
490  
491 Adds/Removes bits to/from the bitmask without changing the current object.
492 The result is returned as a new Bitmask::Data object.
493  
494 =item * -=, +=
495  
496 Adds/Removes bits to/from the current bitmask object.
497  
498 =item * ~, ^, &, |
499  
500 Performs the bitwise operations without changing the current object.
501 The result is returned as a new Bitmask::Data object.
502  
503 =item * ^=, &=, |=
504  
505 Performs the bitwise operations on the current bitmask object.
506  
507 =back
508  
509 =head2 Constructors
510  
511 =head3 new
512  
513 my $bm = MyBitmask->new();
514 my $bm = MyBitmask->new('value1');
515 my $bm = MyBitmask->new('0b00010000010000');
516 my $bm = MyBitmask->new(124);
517 my $bm = MyBitmask->new(0b00010000010000);
518 my $bm = MyBitmask->new(0x2);
519 my $bm = MyBitmask->new($another_bm_object);
520 my $bm = MyBitmask->new("\0\1\0\0\1");
521 my $bm = MyBitmask->new('value2', 'value3');
522 my $bm = MyBitmask->new([32, 'value1', 0b00010000010000]);
523  
524 Create a new bitmask object. You can supply almost any combination of
525 ARRAYREFS, bits, integers, Bitmask::Data objects, Math::BigInt objects,
526 bitmasks and values, even mix different types. See L<any2bitmask> for details
527 on possible formats.
528  
529 =cut
530  
531 16 ▶sub new {
Subroutine new: called CC=3 SCAR=11.0
Pod documented
532 16 my ( $class, @args ) = @_;
533  
534 croak('Bitmask not initialized')
535 16 ▶ unless scalar keys %{ $class->bitmask_items };
Branch: 1 branch
BranchTrueFalseTotal
unless scalar keys %{$class->bitmask_items;} 1 15 16
536  
537 15 my $self = $class->new_from_bitmask($class->bitmask_empty);
538  
539 15 ▶ if (scalar @args) {
Branch: 1 branch
BranchTrueFalseTotal
if (scalar @args) { } 11 4 15
540 11 $self->set( @args );
541 } else {
542 4 $self->set( $class->bitmask_default );
543 }
544  
545 14 return $self;
546 }
547  
548 =head3 new_from_bitmask
549  
550 my $bm = MyBitmask->new_from_bitmask($bitmask_string);
551  
552 Create a new bitmask object from a bitmask string (as returned by many
553 helper methods).
554  
555 =cut
556  
557 18 ▶sub new_from_bitmask {
Subroutine new_from_bitmask: called CC=2 SCAR=6.9
Pod documented
558 18 my ( $class, $bitmask ) = @_;
559
560 18 ▶ $class = ref($class)
Branch: 1 branch
BranchTrueFalseTotal
if ref $class 3 15 18
561 if ref($class);
562
563 18 my $self = bless {
564 #cache => undef,
565 bitmask => $bitmask,
566 },$class;
567
568 18 return $self;
569 }
570  
571 =head2 Public Methods
572  
573 =head3 clone
574  
575 my $bm_new = $bm->clone();
576  
577 Clones an existing Bitmask::Data object and returns it.
578  
579 =cut
580  
581 3 ▶sub clone {
Subroutine clone: called CC=1 SCAR=0.0
Pod documented
582 3 my ( $self ) = @_;
583
584 3 my $new = $self->new_from_bitmask($self->{bitmask});
585 #$new->{cache} = $self->{cache};
586 3 return $new;
587 }
588  
589 =head3 set
590  
591 $bm->set(PARAMS);
592
593 This method resets the current bitmask and sets the supplied arguments.
594 Takes the same arguments as C<new>.
595  
596 Returns the object.
597  
598 =cut
599  
600 15 ▶sub set {
Subroutine set: called CC=1 SCAR=0.0
Pod documented
601 15 my ( $self, @args ) = @_;
602  
603 15 $self->{bitmask} = $self->bitmask_empty;
604 15 $self->add( @args );
605  
606 14 return $self;
607 }
608  
609 =head3 remove
610  
611 $bm->remove(PARAMS)
612
613 Removes the given values/bits from the bitmask. Takes the same arguments
614 as C<new>.
615  
616 Returns the object.
617  
618 =cut
619  
620 3 ▶sub remove {
Subroutine remove: called CC=1 SCAR=0.0
Pod documented
621 3 my ( $self, @args ) = @_;
622  
623 3 my $bitmask = $self->_parse_params(@args);
624  
625 2 $self->{bitmask} = $self->{bitmask} ^ ($self->{bitmask} & $bitmask);
626 #$self->{cache} = undef;
627
628 2 return $self;
629 }
630  
631  
632 =head3 add
633  
634 $bm->add(PARAMS)
635
636 Adds the given values/bits to the bitmask. Takes the same arguments
637 as C<new>.
638  
639 Returns the object.
640  
641 =cut
642  
643 19 ▶sub add {
Subroutine add: called CC=1 SCAR=0.0
Pod documented
644 19 my ( $self, @args ) = @_;
645  
646 19 my $bitmask = $self->_parse_params(@args);
647
648 18 $self->{bitmask} = $self->{bitmask} | $bitmask;
649 #$self->{cache} = undef;
650  
651 18 return $self;
652 }
653  
654 =head3 reset
655  
656 $bm->reset()
657
658 Resets the bitmask to the default (or empty) bitmask.
659  
660 Returns the object.
661  
662 =cut
663  
664 1 ▶sub reset {
Subroutine reset: called CC=2 SCAR=7.6
Pod documented
665 1 my ($self) = @_;
666
667 1 ▶ $self->{bitmask} = $self->bitmask_default || $self->bitmask_empty;
Condition33%1/3
$self->bitmask_default || $self->bitmask_empty
l!l&&r!l&&!r
100
MC/DC0%0/2
$self->bitmask_default || $self->bitmask_empty
$self->bitmask_default $self->bitmask_empty
Truth table33%1/3
A || B
A= $self->bitmask_defaultB= $self->bitmask_empty
ABresult
1X1
011
000
668 #$self->{cache} = undef;
669
670 1 return $self;
671 }
672  
673  
674 =head3 set_all
675  
676 $bm->set_all()
677
678 Sets all defined bits in the bitmask.
679  
680 Returns the object.
681  
682 =cut
683  
684 3 ▶sub set_all {
Subroutine set_all: called CC=1 SCAR=0.0
Pod documented
685 3 my ($self) = @_;
686
687 3 $self->{bitmask} = $self->bitmask_full;
688 #$self->{cache} = undef;
689
690 3 return $self;
691 }
692 9 *setall = \&set_all;
693  
694 =head3 neg
695  
696 $bm->neg()
697
698 Negates/Inverts the bitmask
699  
700 Returns the object.
701  
702 =cut
703  
704 1 ▶sub neg {
Subroutine neg: called CC=1 SCAR=0.0
Pod documented
705 1 my ( $self ) = @_;
706  
707 1 $self->{bitmask} =~ tr/\0\1/\1\0/;
708 1 $self->{bitmask} = $self->{bitmask} & $self->bitmask_full;
709 #$self->{cache} = undef;
710
711 1 return $self;
712 }
713  
714 =head3 list
715  
716 my @values = $bm->list();
717 OR
718 my $values = $bm->list();
719  
720 In list context, this returns a list of the set values in scalar context,
721 this returns an array reference to the list of values.
722  
723 =cut
724  
725 5 ▶sub list {
Subroutine list: called CC=4 SCAR=13.9
Pod documented
726 5 my ($self) = @_;
727
728 #return (wantarray ? @{$self->{cache}} : $self->{cache})
729 # if defined $self->{cache};
730
731 5 my @data;
732 5 ▶ while (my ($value,$bit) = each %{$self->bitmask_items()}) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %{$self->bitmask_items;}) 17 5 22
733 push @data,$value
734 17 ▶ if (($bit & $self->{bitmask}) ne $self->bitmask_empty);
Branch: 1 branch
BranchTrueFalseTotal
if ($bit & $self->{"bitmask"}) ne $self->bitmask_empty 13 4 17
735 }
736
737 #$self->{cache} = \@data;
738
739 5 ▶ return wantarray ? @data : \@data;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 5 0 5
740 }
741  
742 =head3 length
743  
744 my $length = $bm->length();
745  
746 Number of set bitmask values.
747  
748 =cut
749  
750 5 ▶sub length {
Subroutine length: called CC=1 SCAR=0.0
Pod documented
751 5 my ($self) = @_;
752
753 5 my @list = $self->list;
754 5 return scalar @list;
755 }
756  
757 =head3 first
758  
759 my $value = $bm->first()
760
761 Returns the first set value. The order is determined by the bit value.
762  
763 =cut
764  
765 0 ▶sub first {
Subroutine first: not called CC=3 SCAR=24.8
Pod documented
766 0 my ($self) = @_;
767
768 0 my $bitmask_items = $self->bitmask_items();
769 0 foreach my $key (sort { $bitmask_items->{$a} cmp $bitmask_items->{$b} } keys %{$bitmask_items}) {
770 return $key
771 0 ▶ if (($bitmask_items->{$key} & $self->{bitmask}) ne $self->bitmask_empty);
Branch: 1 branch
BranchTrueFalseTotal
if ($bitmask_items->{$key} & $self->{"bitmask"}) ne $self->bitmask_empty 0 0 0
772 }
773 0 return;
774 }
775  
776 =head3 integer
777  
778 my $integer = $bm->integer();
779  
780 Returns the bitmask as an integer. For bitmasks with a length > 40 this will
781 always be a L<Math::BigInt> object.
782  
783 =cut
784  
785 9 *mask = \&integer;
786 9 ▶sub integer {
Subroutine integer: called CC=4 SCAR=13.9
Pod documented
787 9 my ($self) = @_;
788
789 9 my $bitmask = $self->{bitmask};
790 9 $bitmask =~ tr/\0\1/01/;
791
792 9 ▶ if ($self->bitmask_length > 64 || ($self->bitmask_length > 32 && ! $Config{use64bitint})) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->bitmask_length > 64 or $self->bitmask_length > 32 and not $Bitmask::Data::Config{'use64bitint'}) { } 1 8 9
Condition66%4/6
$self->bitmask_length > 64 or $self->bitmask_length > 32 and not $Bitmask::Data::Config{"use64bitint"}
l!l&&r!l&&!r
108
$self->bitmask_length > 32 and not $Bitmask::Data::Config{"use64bitint"}
!ll&&!rl&&r
260
MC/DC33%1/3
$self->bitmask_length > 64 or $self->bitmask_length > 32 and not $Bitmask::Data::Config{"use64bitint"}
$self->bitmask_length > 64 $self->bitmask_length > 32 not $Bitmask::Data::Config{"use64bitint"}
Truth table75%3/4
A or B and not(C)
A= $self->bitmask_length > 64B= $self->bitmask_length > 32C= not $Bitmask::Data::Config{"use64bitint"}
ABCresult
1XX1
0111
00X0
0100
793 1 require Math::BigInt;
794 1 return Math::BigInt->from_bin("0b".$bitmask);
795 } else {
796 9 ▶ no warnings 'portable';
Subroutine BEGIN: called CC=1 SCAR=0.0
797 8 return oct("0b".$bitmask);
798 }
799 }
800  
801 =head3 string
802  
803 my $string = $bm->string();
804  
805 Returns the bitmask as a string of 0 and 1.
806  
807 =cut
808  
809 17 ▶sub string {
Subroutine string: called CC=1 SCAR=0.0
Pod documented
810 17 my ($self) = @_;
811 17 my $bitmask = $self->{bitmask};
812 17 $bitmask =~ tr/\0\1/01/;
813 17 return $bitmask;
814 }
815  
816 =head3 bitmask
817  
818 my $string = $bm->bitmask();
819  
820 Returns the bitmask in the internal representation: A string of \0 and \1
821  
822 =cut
823  
824 0 ▶sub bitmask {
Subroutine bitmask: not called CC=1 SCAR=6.9
Pod documented
825 0 my ($self) = @_;
826 0 return $self->{bitmask};
827 }
828  
829 =head3 sqlfilter_all
830  
831 This method can be used for database searches in conjunction with
832 L<SQL::Abstract> an POSTGRESQL (SQL::Abstract is used by L<DBIx::Class> for
833 generating searches). The search will find all database rows
834 with bitmask that have at least the given values set. (use
835 the C<sql> method for an exact match)
836  
837 Example how to use sqlfilter with SQL::Abstract:
838  
839 my($stmt, @bind) = $sql->select(
840 'mytable',
841 \@fields,
842 {
843 $bm->sqlfilter_all('mytable.bitmaskfield'),
844 }
845 );
846  
847 Example how to use sqlfilter with DBIx::Class:
848
849 my $list = $resultset->search(
850 {
851 $bm->sqlfilter_all('me.bitmaskfield'),
852 },
853 );
854  
855  
856 =cut
857  
858 0 ▶sub sqlfilter_all {
Subroutine sqlfilter_all: not called CC=1 SCAR=6.9
Pod documented
859 0 my ( $self, $field ) = @_;
860  
861 0 my $sql_mask = $self->string();
862 0 my $format = "bitand( $field, B'$sql_mask' )";
863 0 return ( $format, \" = B'$sql_mask'" );
864 }
865 9 *sqlfilter = \&sqlfilter_all;
866  
867 =head3 sqlfilter_any
868  
869 Works like C<sqlfilter_all> but checks for any bit matching
870  
871 =cut
872  
873 0 ▶sub sqlfilter_any {
Subroutine sqlfilter_any: not called CC=1 SCAR=6.9
Pod documented
874 0 my ( $self, $field ) = @_;
875  
876 0 my $sql_mask = $self->string();
877 0 my $format = "bitand( $field, B'$sql_mask' )";
878 0 my $empty_mask = $self->bitmask_empty;
879 0 $empty_mask =~ tr/\0\1/01/;
880 0 return ( $format, \" <> B'$empty_mask'" );
881 }
882  
883 =head3 sqlstring
884  
885 Returns the bitmask as a quoted string as needed by PostgreSQL:
886  
887 B'0000000000000001'::bit(16)
888  
889 =cut
890  
891 0 ▶sub sqlstring {
Subroutine sqlstring: not called CC=1 SCAR=6.9
Pod documented
892 0 my ( $self ) = @_;
893 0 return sprintf("B'%s'::bit(%i)",$self->string,$self->bitmask_length);
894 }
895  
896 =head3 has_all
897  
898 if ($bm->has_all(PARAMS)) {
899 # Do something
900 }
901  
902 Checks if all requestes bits/values are set and returns true or false.
903 This method takes the same arguments as C<new>.
904  
905 =cut
906  
907 0 ▶sub has_all {
Subroutine has_all: not called CC=2 SCAR=17.9
Pod documented
908 0 my ( $self, @args ) = @_;
909  
910 0 my $bitmask = $self->_parse_params(@args);
911
912 0 ▶ return (($bitmask & $self->{bitmask}) eq $bitmask) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
($bitmask & $self->{'bitmask'}) eq $bitmask ? : 0 0 0
913 }
914 9 *hasall = \&has_all;
915  
916 =head3 has_exact
917  
918 if ($bm->has_exact(PARAMS)) {
919 # Do something
920 }
921  
922 Checks if the set bits/values excactly match the supplied bits/values and
923 returns true or false.
924 This method takes the same arguments as C<new>.
925  
926 =cut
927  
928 0 ▶sub has_exact {
Subroutine has_exact: not called CC=2 SCAR=17.9
Pod documented
929 0 my ( $self, @args ) = @_;
930  
931 0 my $bitmask = $self->_parse_params(@args);
932  
933 0 ▶ return ($bitmask eq $self->{bitmask}) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$bitmask eq $self->{'bitmask'} ? : 0 0 0
934 }
935 9 *hasexact = \&has_exact;
936  
937 =head3 has_any
938  
939 if ($bm->has_any(PARAMS)) {
940 # Do something
941 }
942  
943 Checks if at least one set value/bit matches the supplied bits/values and
944 returns true or false.
945 This method takes the same arguments as C<new>.
946  
947 =cut
948  
949 0 ▶sub has_any {
Subroutine has_any: not called CC=2 SCAR=17.9
Pod documented
950 0 my ( $self, @args ) = @_;
951  
952 0 my $bitmask = $self->_parse_params(@args);
953
954 0 ▶ return (($bitmask & $self->{bitmask}) ne $self->bitmask_empty) ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
($bitmask & $self->{'bitmask'}) ne $self->bitmask_empty ? : 0 0 0
955 }
956 9 *hasany = \&has_any;
957  
958 9 1;
959  
960 =head1 CAVEATS
961  
962 Since Bitmask::Data is very liberal with input data you cannot use numbers
963 as bitmask values. (It would think that you are supplying an integer
964 bitmask and not a value)
965  
966 Bitmask::Data adds a considerable processing overhead to bitmask
967 manipulations. If you either don't need the extra comfort or use
968 bitmasks with less that 32 bits that you should consider using just the perl
969 built in bit operators on simple integer values.
970  
971 =head1 SUBCLASSING
972  
973 Bitmask::Data was designed to be subclassed.
974
975 package MyBitmask;
976 use parent qw(Bitmask::Data);
977 __PACKAGE__->bitmask_length(20); # Default length is 16
978 __PACKAGE__->init(
979 'value1' => 0b000000000000000001,
980 'value2' => 0x2,
981 'value2' => 4,
982 'value4', # lazy initlialization
983 'value5', # lazy initlialization
984 );
985  
986 =head1 WORKING WITH DATABASES
987  
988 This module comes with support for PostgreSQL databases (patches for other
989 database vendors are welcome).
990  
991 First you need to create the correct column types:
992  
993 CREATE TABLE bitmaskexample (
994 id integer DEFAULT nextval('pkey_seq'::regclass) NOT NULL,
995 bitmask bit(14),
996 otherfields character varying
997 );
998  
999 The length of the bitmask field must match C<CLASS-E<gt>bitmask_length>.
1000  
1001 This module provides three convenient methods to work with databases:
1002  
1003 =over
1004  
1005 =item * L<sqlfilter_all>: Search for matching bitmasks
1006  
1007 =item * L<sqlfilter_any>: Search for bitmasks with matching bits
1008  
1009 =item * L<string>: Print the bitmask string as used by the database
1010  
1011 =back
1012  
1013 If you are working with l<DBIx::Class> you might also install in- and
1014 deflators for Bitmask::Data objects:
1015  
1016 __PACKAGE__->inflate_column('fieldname',{
1017 inflate => sub {
1018 my $value = shift;
1019 return MyBitmask->new($value);
1020 },
1021 deflate => sub {
1022 my $value = shift;
1023 undef $value
1024 unless ref($value) && $value->isa('MyBitmask');
1025 $value //= MyBitmask->new();
1026 return $value->string;
1027 },
1028 });
1029  
1030 =head1 SUPPORT
1031  
1032 Please report any bugs or feature requests to
1033 C<bug-bitmask-data@rt.cpan.org>, or through the web interface at
1034 L<http://rt.cpan.org/Public/Bug/Report.html?Queue=Bitmask::Data>.
1035 I will be notified and then you'll automatically be notified of the progress
1036 on your report as I make changes.
1037  
1038 =head1 AUTHOR
1039  
1040 Klaus Ita
1041 koki [at] worstofall.com
1042  
1043 Maroš Kollár
1044 CPAN ID: MAROS
1045 maros [at] k-1.com
1046
1047 L<http://www.k-1.com>
1048  
1049 =head1 ACKNOWLEDGEMENTS
1050  
1051 This module was originally written by Klaus Ita (Koki) for Revdev
1052 L<http://www.revdev.at>, a nice litte software company I (Maros) run with
1053 Koki and Domm (L<http://search.cpan.org/~domm/>).
1054  
1055 =head1 COPYRIGHT & LICENSE
1056  
1057 Bitmask::Data is Copyright (c) 2008 Klaus Ita, Maroš Kollár
1058 - L<http://www.revdev.at>
1059  
1060 This program is free software; you can redistribute it and/or modify it under
1061 the same terms as Perl itself.
1062  
1063 The full text of the license can be found in the
1064 LICENSE file included with this module.
1065  
1066 =cut
↑ Summary