| 1 | # vi: set ts=4 sw=4 ht=4 et : | ||||||||||||||
| 2 | package perl5i::2::HASH; | ||||||||||||||
| 3 | 109 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 109 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 109 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | # Don't accidentally turn carp/croak into methods. | ||||||||||||||
| 9 | 109 | require Carp::Fix::1_25; | |||||||||||||
| 10 | 109 | require Hash::StoredIterator; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 109 | ▶ | use perl5i::2::Signatures; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 109 | ▶ | method each($callback) { | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=4.1
Subroutine
each: not called CC=1 SCAR=4.9
Subroutine
__ANON__: not called CC=1 SCAR=4.9
| |||||||||||||||
| 15 | 0 | return Hash::StoredIterator::hmap( $callback, $self ); | |||||||||||||
| 16 | 109 | } | |||||||||||||
| 17 | |||||||||||||||
| 18 | sub flip { | ||||||||||||||
| 19 | Carp::Fix::1_25::croak("Can't flip hash with references as values") | ||||||||||||||
| 20 | 3 | ▶ | if grep { ref } values %{$_[0]}; | ||||||||||||
|
Subroutine
flip: called CC=3 SCAR=11.0
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 21 | |||||||||||||||
| 22 | 2 | my %flipped = reverse %{$_[0]}; | |||||||||||||
| 23 | |||||||||||||||
| 24 | 2 | ▶ | return wantarray ? %flipped : \%flipped; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 25 | } | ||||||||||||||
| 26 | |||||||||||||||
| 27 | sub merge { | ||||||||||||||
| 28 | 20 | ▶ | require Hash::Merge::Simple; | ||||||||||||
|
Subroutine
merge: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 29 | 20 | my $merged = Hash::Merge::Simple::merge(@_); | |||||||||||||
| 30 | |||||||||||||||
| 31 | 20 | ▶ | return wantarray ? %$merged : $merged; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | sub print { | ||||||||||||||
| 35 | 1 | ▶ | my $hash = shift; | ||||||||||||
|
Subroutine
print: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 36 | 1 | print join(" ", map { "$_ => $hash->{$_}" } keys %$hash); | |||||||||||||
| 37 | } | ||||||||||||||
| 38 | |||||||||||||||
| 39 | sub say { | ||||||||||||||
| 40 | 1 | ▶ | my $hash = shift; | ||||||||||||
|
Subroutine
say: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||
| 41 | 1 | print join(" ", map { "$_ => $hash->{$_}" } keys %$hash), "\n"; | |||||||||||||
| 42 | } | ||||||||||||||
| 43 | |||||||||||||||
| 44 | my $common = sub { | ||||||||||||||
| 45 | # Return all things in first array that are also present in second. | ||||||||||||||
| 46 | 7 | ▶ | my ($c, $d) = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 47 | |||||||||||||||
| 48 | 109 | ▶ | no warnings 'uninitialized'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 49 | 7 | my %seen = map { $_ => 1 } @$d; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 7 | my @common = grep { $seen{$_} } @$c; | |||||||||||||
| 52 | |||||||||||||||
| 53 | 7 | return \@common; | |||||||||||||
| 54 | 109 | }; | |||||||||||||
| 55 | |||||||||||||||
| 56 | sub diff { | ||||||||||||||
| 57 | 6 | ▶ | my ($base, @rest) = @_; | ||||||||||||
|
Subroutine
diff: called CC=9 SCAR=22.0
Pod
undocumented
| |||||||||||||||
| 58 | 6 | ▶ | unless (@rest) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 59 | 2 | ▶ | return wantarray ? %$base : $base; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 60 | } | ||||||||||||||
| 61 | |||||||||||||||
| 62 | 4 | ▶ | die "Arguments must be hash references" if grep { ref $_ ne 'HASH' } @rest; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | |||||||||||||||
| 64 | # make a copy so that we can delete kv pairs without modifying the | ||||||||||||||
| 65 | # original hashref. | ||||||||||||||
| 66 | 3 | my %base = %$base; | |||||||||||||
| 67 | |||||||||||||||
| 68 | 3 | require perl5i::2::equal; | |||||||||||||
| 69 | |||||||||||||||
| 70 | 3 | foreach my $hash (@rest) { | |||||||||||||
| 71 | |||||||||||||||
| 72 | 4 | my $common_keys = $common->( [ keys %$base ], [ keys %$hash ] ); | |||||||||||||
| 73 | |||||||||||||||
| 74 | 4 | ▶ | next unless @$common_keys; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | |||||||||||||||
| 76 | # Keys are equal, are values also equal? | ||||||||||||||
| 77 | 3 | foreach my $key (@$common_keys) { | |||||||||||||
| 78 | 5 | ▶ | delete $base{$key} if perl5i::2::equal::are_equal( $base->{$key}, $hash->{$key} ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 79 | } | ||||||||||||||
| 80 | |||||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 3 | ▶ | return wantarray ? %base : \%base; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | my $different = sub { | ||||||||||||||
| 87 | # Return all things in first array that are not present in second. | ||||||||||||||
| 88 | 4 | ▶ | my ($c, $d) = @_; | ||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||
| 89 | |||||||||||||||
| 90 | 109 | ▶ | no warnings 'uninitialized'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 91 | 4 | my %seen = map { $_ => 1 } @$d; | |||||||||||||
| 92 | |||||||||||||||
| 93 | 4 | my @different = grep { not $seen{$_} } @$c; | |||||||||||||
| 94 | |||||||||||||||
| 95 | 4 | return \@different; | |||||||||||||
| 96 | 109 | }; | |||||||||||||
| 97 | |||||||||||||||
| 98 | sub intersect { | ||||||||||||||
| 99 | 6 | ▶ | my ($base, @rest) = @_; | ||||||||||||
|
Subroutine
intersect: called CC=10 SCAR=23.1
Pod
undocumented
| |||||||||||||||
| 100 | |||||||||||||||
| 101 | 6 | ▶ | unless (@rest) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | 2 | ▶ | return wantarray ? %$base : $base; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | } | ||||||||||||||
| 104 | |||||||||||||||
| 105 | 4 | ▶ | die "Arguments must be hash references" if grep { ref $_ ne 'HASH' } @rest; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | # make a copy so that we can delete kv pairs without modifying the | ||||||||||||||
| 108 | # original hashref. | ||||||||||||||
| 109 | 3 | my %base = %$base; | |||||||||||||
| 110 | |||||||||||||||
| 111 | 3 | require perl5i::2::equal; | |||||||||||||
| 112 | |||||||||||||||
| 113 | 3 | foreach my $hash (@rest) { | |||||||||||||
| 114 | |||||||||||||||
| 115 | 4 | my $different_keys = $different->( [ keys %$base ], [ keys %$hash ] ); | |||||||||||||
| 116 | |||||||||||||||
| 117 | 4 | delete @base{@$different_keys}; | |||||||||||||
| 118 | |||||||||||||||
| 119 | 4 | ▶ | return wantarray ? () : {} unless %base; | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 120 | |||||||||||||||
| 121 | 3 | my $common_keys = $common->( [ keys %$base ], [ keys %$hash ] ); | |||||||||||||
| 122 | |||||||||||||||
| 123 | # Keys are equal, are values also equal? | ||||||||||||||
| 124 | 3 | foreach my $key (@$common_keys) { | |||||||||||||
| 125 | 5 | ▶ | delete $base{$key} unless perl5i::2::equal::are_equal( $base->{$key}, $hash->{$key} ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | } | ||||||||||||||
| 127 | |||||||||||||||
| 128 | } | ||||||||||||||
| 129 | |||||||||||||||
| 130 | 2 | ▶ | return wantarray ? %base : \%base; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 109 | 1; | |||||||||||||