| 1 | 3 | ▶ | use strict; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 2 | 3 | ▶ | use warnings; | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 3 | package Number::Tolerant::Union 1.711; # TRIAL | ||||||||||||||||||
| 4 | # ABSTRACT: unions of tolerance ranges | ||||||||||||||||||
| 5 | |||||||||||||||||||
| 6 | #pod =head1 SYNOPSIS | ||||||||||||||||||
| 7 | #pod | ||||||||||||||||||
| 8 | #pod use Number::Tolerant; | ||||||||||||||||||
| 9 | #pod | ||||||||||||||||||
| 10 | #pod my $range1 = tolerance(10 => to => 12); | ||||||||||||||||||
| 11 | #pod my $range2 = tolerance(14 => to => 16); | ||||||||||||||||||
| 12 | #pod | ||||||||||||||||||
| 13 | #pod my $union = $range1 | $range2; | ||||||||||||||||||
| 14 | #pod | ||||||||||||||||||
| 15 | #pod if ($11 == $union) { ... } # this will happen | ||||||||||||||||||
| 16 | #pod if ($12 == $union) { ... } # so will this | ||||||||||||||||||
| 17 | #pod | ||||||||||||||||||
| 18 | #pod if ($13 == $union) { ... } # nothing will happen here | ||||||||||||||||||
| 19 | #pod | ||||||||||||||||||
| 20 | #pod if ($14 == $union) { ... } # this will happen | ||||||||||||||||||
| 21 | #pod if ($15 == $union) { ... } # so will this | ||||||||||||||||||
| 22 | #pod | ||||||||||||||||||
| 23 | #pod =head1 DESCRIPTION | ||||||||||||||||||
| 24 | #pod | ||||||||||||||||||
| 25 | #pod Number::Tolerant::Union is used by L<Number::Tolerant> to represent the union | ||||||||||||||||||
| 26 | #pod of multiple tolerances. A subset of the same operators that function on a | ||||||||||||||||||
| 27 | #pod tolerance will function on a union of tolerances, as listed below. | ||||||||||||||||||
| 28 | #pod | ||||||||||||||||||
| 29 | #pod =head1 METHODS | ||||||||||||||||||
| 30 | #pod | ||||||||||||||||||
| 31 | #pod =head2 new | ||||||||||||||||||
| 32 | #pod | ||||||||||||||||||
| 33 | #pod my $union = Number::Tolerant::Union->new(@list_of_tolerances); | ||||||||||||||||||
| 34 | #pod | ||||||||||||||||||
| 35 | #pod There is a C<new> method on the Number::Tolerant::Union class, but unions are | ||||||||||||||||||
| 36 | #pod meant to be created with the C<|> operator on a Number::Tolerant tolerance. | ||||||||||||||||||
| 37 | #pod | ||||||||||||||||||
| 38 | #pod The arguments to C<new> are a list of numbers or tolerances to be unioned. | ||||||||||||||||||
| 39 | #pod | ||||||||||||||||||
| 40 | #pod Intersecting ranges are not converted into a single range, but this may change | ||||||||||||||||||
| 41 | #pod in the future. (For example, the union of "5 to 10" and "7 to 12" is not "5 to | ||||||||||||||||||
| 42 | #pod 12.") | ||||||||||||||||||
| 43 | #pod | ||||||||||||||||||
| 44 | #pod =cut | ||||||||||||||||||
| 45 | |||||||||||||||||||
| 46 | 12 | ▶ | sub new { | ||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 47 | 12 | my $class = shift; | |||||||||||||||||
| 48 | 12 | bless { options => [ @_ ] } => $class; | |||||||||||||||||
| 49 | } | ||||||||||||||||||
| 50 | |||||||||||||||||||
| 51 | #pod =head2 options | ||||||||||||||||||
| 52 | #pod | ||||||||||||||||||
| 53 | #pod This method will return a list of all the acceptable options for the union. | ||||||||||||||||||
| 54 | #pod | ||||||||||||||||||
| 55 | #pod =cut | ||||||||||||||||||
| 56 | |||||||||||||||||||
| 57 | 97 | ▶ | sub options { | ||||||||||||||||
|
Subroutine
options: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||
| 58 | 97 | my $self = shift; | |||||||||||||||||
| 59 | 97 | return @{$self->{options}}; | |||||||||||||||||
| 60 | } | ||||||||||||||||||
| 61 | |||||||||||||||||||
| 62 | #pod =head2 Overloading | ||||||||||||||||||
| 63 | #pod | ||||||||||||||||||
| 64 | #pod Tolerance unions overload a few operations, mostly comparisons. | ||||||||||||||||||
| 65 | #pod | ||||||||||||||||||
| 66 | #pod =over | ||||||||||||||||||
| 67 | #pod | ||||||||||||||||||
| 68 | #pod =item numification | ||||||||||||||||||
| 69 | #pod | ||||||||||||||||||
| 70 | #pod Unions numify to undef. If there's a better idea, I'd love to hear it. | ||||||||||||||||||
| 71 | #pod | ||||||||||||||||||
| 72 | #pod =item stringification | ||||||||||||||||||
| 73 | #pod | ||||||||||||||||||
| 74 | #pod A tolerance stringifies to a short description of itself. This is a set of the | ||||||||||||||||||
| 75 | #pod union's options, parentheses-enclosed and joined by the word "or" | ||||||||||||||||||
| 76 | #pod | ||||||||||||||||||
| 77 | #pod =item equality | ||||||||||||||||||
| 78 | #pod | ||||||||||||||||||
| 79 | #pod A number is equal to a union if it is equal to any of its options. | ||||||||||||||||||
| 80 | #pod | ||||||||||||||||||
| 81 | #pod =item comparison | ||||||||||||||||||
| 82 | #pod | ||||||||||||||||||
| 83 | #pod A number is greater than a union if it is greater than all its options. | ||||||||||||||||||
| 84 | #pod | ||||||||||||||||||
| 85 | #pod A number is less than a union if it is less than all its options. | ||||||||||||||||||
| 86 | #pod | ||||||||||||||||||
| 87 | #pod =item union intersection | ||||||||||||||||||
| 88 | #pod | ||||||||||||||||||
| 89 | #pod An intersection (C<&>) with a union is commutted across all options. In other | ||||||||||||||||||
| 90 | #pod words: | ||||||||||||||||||
| 91 | #pod | ||||||||||||||||||
| 92 | #pod (a | b | c) & d ==yields==> ((a & d) | (b & d) | (c & d)) | ||||||||||||||||||
| 93 | #pod | ||||||||||||||||||
| 94 | #pod Options that have no intersection with the new element are dropped. The | ||||||||||||||||||
| 95 | #pod intersection of a constant number and a union yields that number, if the number | ||||||||||||||||||
| 96 | #pod was in the union's ranges and otherwise yields nothing. | ||||||||||||||||||
| 97 | #pod | ||||||||||||||||||
| 98 | #pod =back | ||||||||||||||||||
| 99 | #pod | ||||||||||||||||||
| 100 | #pod =cut | ||||||||||||||||||
| 101 | |||||||||||||||||||
| 102 | use overload | ||||||||||||||||||
| 103 | 1 | ▶ | '0+' => sub { undef }, | ||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 104 | 1 | ▶ | '""' => sub { join(' or ', map { "($_)" } $_[0]->options) }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 105 | 32 | ▶ | '==' => sub { for ($_[0]->options) { return 1 if $_ == $_[1] } return 0 }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||
| 106 | 23 | ▶ | '!=' => sub { for ($_[0]->options) { return 0 if $_ == $_[1] } return 1 }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
Branch: 1 branch
| |||||||||||||||||||
| 107 | '>' => | ||||||||||||||||||
| 108 | 19 | ▶ | sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||
| 109 | 19 | ▶ | if ($_[2]) { for ($_[0]->options) { return 0 unless $_[1] > $_ } return 1 } | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 110 | 13 | ▶ | else { for ($_[0]->options) { return 0 unless $_[1] < $_ } return 1 } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 111 | }, | ||||||||||||||||||
| 112 | '<' => | ||||||||||||||||||
| 113 | 18 | ▶ | sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||
| 114 | 18 | ▶ | if ($_[2]) { for ($_[0]->options) { return 0 unless $_[1] < $_ } return 1 } | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 115 | 13 | ▶ | else { for ($_[0]->options) { return 0 unless $_[1] > $_ } return 1 } | ||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||
| 116 | }, | ||||||||||||||||||
| 117 | '<=>' => | ||||||||||||||||||
| 118 | 8 | ▶ | sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||||||||
| 119 | 8 | ▶ | if ($_[2]) { $_[0] < $_[1] ? 1 : $_[0] > $_[1] ? -1 : 0 } | ||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||
| 120 | 4 | ▶ | else { $_[0] > $_[1] ? 1 : $_[0] < $_[1] ? -1 : 0 } | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 121 | }, | ||||||||||||||||||
| 122 | 2 | ▶ | '|' => sub { __PACKAGE__->new($_[0]->options,$_[1]); }, | ||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||
| 123 | 3 | ▶ | '&' => sub { | ||||||||||||||||
|
Subroutine
__ANON__: called CC=3 SCAR=11.0
| |||||||||||||||||||
| 124 | 3 | eval { $_[1]->isa('Number::Tolerant') } | |||||||||||||||||
| 125 | 3 | ▶ | ? __PACKAGE__->new(map { $_ & $_[1] } $_[0]->options ) | ||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||
| 126 | : $_[1] == $_[0] | ||||||||||||||||||
| 127 | ? $_[1] | ||||||||||||||||||
| 128 | : (); | ||||||||||||||||||
| 129 | }, | ||||||||||||||||||
| 130 | 3 | fallback => 1; | |||||||||||||||||
| 131 | |||||||||||||||||||
| 132 | #pod =head1 TODO | ||||||||||||||||||
| 133 | #pod | ||||||||||||||||||
| 134 | #pod Who knows. Collapsing overlapping options, probably. | ||||||||||||||||||
| 135 | #pod | ||||||||||||||||||
| 136 | #pod =cut | ||||||||||||||||||
| 137 | |||||||||||||||||||
| 138 | 3 | 1; | |||||||||||||||||
| 139 | |||||||||||||||||||
| 140 | __END__ | ||||||||||||||||||