| 1 | package Logic::Relational::Domain; | ||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||
| 3 | 30 | ▶ | use v5.38; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 4 | 30 | ▶ | use experimental 'signatures'; | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 5 | 30 | ▶ | use Carp qw(croak); | ||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||
| 7 | =head1 NAME | ||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||
| 9 | Logic::Relational::Domain - Represents a finite domain of allowed integer values. | ||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||
| 11 | =cut | ||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||
| 13 | 101 | ▶ | sub new ( $class, $args ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.8
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 14 | 101 | my %values; | |||||||||||||||||||||||||||||||||||
| 15 | 101 | ▶ | if ( exists $args->{min} && exists $args->{max} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
Condition66%2/3
exists $args->{"min"} and exists $args->{"max"}
MC/DC50%1/2
exists $args->{"min"} and exists $args->{"max"}
exists $args->{"min"}
exists $args->{"max"}
Truth table66%2/3
A and B
A= exists $args->{"min"}B= exists $args->{"max"}
| |||||||||||||||||||||||||||||||||||||
| 16 | 22 | my $min = $args->{min}; | |||||||||||||||||||||||||||||||||||
| 17 | 22 | my $max = $args->{max}; | |||||||||||||||||||||||||||||||||||
| 18 | 22 | ▶ | croak "Invalid range: min $min > max $max" if $min > $max; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 19 | 22 | for my $i ( $min .. $max ) { | |||||||||||||||||||||||||||||||||||
| 20 | 86 | $values{$i} = 1; | |||||||||||||||||||||||||||||||||||
| 21 | } | ||||||||||||||||||||||||||||||||||||
| 22 | } | ||||||||||||||||||||||||||||||||||||
| 23 | elsif ( exists $args->{values} ) { | ||||||||||||||||||||||||||||||||||||
| 24 | 2 | my $vals = $args->{values}; | |||||||||||||||||||||||||||||||||||
| 25 | 2 | ▶ | croak "values must be an array reference" unless ref($vals) eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 26 | 2 | for my $v (@$vals) { | |||||||||||||||||||||||||||||||||||
| 27 | 7 | ▶ | croak "Domain values must be integers: $v" unless $v =~ /^-?\d+$/x; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 28 | 7 | $values{$v} = 1; | |||||||||||||||||||||||||||||||||||
| 29 | } | ||||||||||||||||||||||||||||||||||||
| 30 | } | ||||||||||||||||||||||||||||||||||||
| 31 | elsif ( ref($args) eq 'HASH' ) { | ||||||||||||||||||||||||||||||||||||
| 32 | 77 | %values = %$args; | |||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||
| 34 | else { | ||||||||||||||||||||||||||||||||||||
| 35 | 0 | croak "Invalid arguments to Domain->new"; | |||||||||||||||||||||||||||||||||||
| 36 | } | ||||||||||||||||||||||||||||||||||||
| 37 | |||||||||||||||||||||||||||||||||||||
| 38 | 101 | return bless { values => \%values }, $class; | |||||||||||||||||||||||||||||||||||
| 39 | } | ||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||
| 41 | 187 | ▶ | sub values ($self) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
values: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 42 | 187 | my @sorted = sort { $a <=> $b } keys %{ $self->{values} }; | |||||||||||||||||||||||||||||||||||
| 43 | 187 | return @sorted; | |||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||
| 46 | 352 | ▶ | sub size ($self) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 47 | 352 | return scalar keys %{ $self->{values} }; | |||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||
| 50 | 121 | ▶ | sub contains ( $self, $val ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
contains: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 51 | 121 | ▶ | return exists $self->{values}{$val} ? 1 : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||
| 54 | 132 | ▶ | sub is_bound ($self) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
is_bound: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 55 | 132 | ▶ | return $self->size == 1 ? 1 : 0; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 56 | } | ||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||
| 58 | 33 | ▶ | sub bound_value ($self) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
bound_value: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 59 | 33 | ▶ | return unless $self->is_bound; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 60 | 33 | my ($val) = keys %{ $self->{values} }; | |||||||||||||||||||||||||||||||||||
| 61 | 33 | return $val; | |||||||||||||||||||||||||||||||||||
| 62 | } | ||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||
| 64 | 3 | ▶ | sub intersect ( $self, $other ) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
intersect: called CC=6 SCAR=18.0
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 65 | 3 | my %new_values; | |||||||||||||||||||||||||||||||||||
| 66 | 3 | ▶ | if ( ref($other) eq 'Logic::Relational::Domain' ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 67 | 2 | for my $v ( keys %{ $self->{values} } ) { | |||||||||||||||||||||||||||||||||||
| 68 | 8 | ▶ | if ( exists $other->{values}{$v} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 69 | 4 | $new_values{$v} = 1; | |||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||||||||||
| 72 | } | ||||||||||||||||||||||||||||||||||||
| 73 | else { | ||||||||||||||||||||||||||||||||||||
| 74 | 1 | ▶ | if ( exists $self->{values}{$other} ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 75 | 1 | $new_values{$other} = 1; | |||||||||||||||||||||||||||||||||||
| 76 | } | ||||||||||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||
| 79 | 3 | ▶ | return unless keys %new_values; | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 80 | 3 | return ref($self)->new( \%new_values ); | |||||||||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||
| 83 | 0 | ▶ | sub as_string ($self) { | ||||||||||||||||||||||||||||||||||
|
Subroutine
as_string: not called CC=2 SCAR=17.9
Pod
undocumented
| |||||||||||||||||||||||||||||||||||||
| 84 | 0 | my @vals = $self->values; | |||||||||||||||||||||||||||||||||||
| 85 | 0 | ▶ | if ( @vals > 5 ) { | ||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||
| 86 | return | ||||||||||||||||||||||||||||||||||||
| 87 | 0 | "domain(" | |||||||||||||||||||||||||||||||||||
| 88 | . $vals[0] . ".." | ||||||||||||||||||||||||||||||||||||
| 89 | . $vals[-1] | ||||||||||||||||||||||||||||||||||||
| 90 | . ", size=" | ||||||||||||||||||||||||||||||||||||
| 91 | . $self->size . ")"; | ||||||||||||||||||||||||||||||||||||
| 92 | } | ||||||||||||||||||||||||||||||||||||
| 93 | 0 | return "domain(" . join( ",", @vals ) . ")"; | |||||||||||||||||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||
| 96 | 30 | 1; | |||||||||||||||||||||||||||||||||||