| 1 | package Hash::AutoHash::MultiValued; | ||||||||||||||||||||||||||||
| 2 | 11 | our $VERSION='1.17_01'; | |||||||||||||||||||||||||||
| 3 | 11 | $VERSION=eval $VERSION; # I think this is the accepted idiom.. | |||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | ################################################################################# | ||||||||||||||||||||||||||||
| 6 | # | ||||||||||||||||||||||||||||
| 7 | # Author: Nat Goodman | ||||||||||||||||||||||||||||
| 8 | # Created: 09-03-05 | ||||||||||||||||||||||||||||
| 9 | # $Id: | ||||||||||||||||||||||||||||
| 10 | # | ||||||||||||||||||||||||||||
| 11 | # AutoHash with multivalued elements | ||||||||||||||||||||||||||||
| 12 | # Inspired by Tie::Hash::Multivalue | ||||||||||||||||||||||||||||
| 13 | # | ||||||||||||||||||||||||||||
| 14 | ################################################################################# | ||||||||||||||||||||||||||||
| 15 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 16 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 17 | 11 | ▶ | use Hash::AutoHash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 18 | 11 | ▶ | use base qw(Hash::AutoHash); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 19 | |||||||||||||||||||||||||||||
| 20 | 11 | our @NORMAL_EXPORT_OK=@Hash::AutoHash::EXPORT_OK; | |||||||||||||||||||||||||||
| 21 | 11 | my $helper_class=__PACKAGE__.'::helper'; | |||||||||||||||||||||||||||
| 22 | 11 | our @EXPORT_OK=$helper_class->EXPORT_OK; | |||||||||||||||||||||||||||
| 23 | 11 | our @SUBCLASS_EXPORT_OK=$helper_class->SUBCLASS_EXPORT_OK; | |||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||
| 25 | # sub new { | ||||||||||||||||||||||||||||
| 26 | # my $class_or_self=@_>0 && shift; | ||||||||||||||||||||||||||||
| 27 | # # send to parent if called as object method. will access hash slot via AUTOLOAD | ||||||||||||||||||||||||||||
| 28 | # return $class_or_self->SUPER::new(@_) if ref $class_or_self; | ||||||||||||||||||||||||||||
| 29 | # # do regular 'new' via helper class if called as class method. | ||||||||||||||||||||||||||||
| 30 | # my $helper_class=$class_or_self.'::helper'; | ||||||||||||||||||||||||||||
| 31 | # $helper_class->_new($class_or_self,@_); | ||||||||||||||||||||||||||||
| 32 | # } | ||||||||||||||||||||||||||||
| 33 | |||||||||||||||||||||||||||||
| 34 | ################################################################################# | ||||||||||||||||||||||||||||
| 35 | # helper package exists to avoid polluting Hash::AutoHash::Args namespace with | ||||||||||||||||||||||||||||
| 36 | # subs that would mask accessor/mutator AUTOLOADs | ||||||||||||||||||||||||||||
| 37 | # functions herein (except _new) are exportable by Hash::AutoHash::Args | ||||||||||||||||||||||||||||
| 38 | ################################################################################# | ||||||||||||||||||||||||||||
| 39 | package Hash::AutoHash::MultiValued::helper; | ||||||||||||||||||||||||||||
| 40 | 11 | our $VERSION=$Hash::AutoHash::MultiValued::VERSION; | |||||||||||||||||||||||||||
| 41 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 42 | 11 | ▶ | use Carp; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 43 | BEGIN { | ||||||||||||||||||||||||||||
| 44 | 11 | ▶ | our @ISA=qw(Hash::AutoHash::helper); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||
| 46 | 11 | ▶ | use Hash::AutoHash qw(autohash_tie); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | sub _new { | ||||||||||||||||||||||||||||
| 49 | 175 | ▶ | my($helper_class,$class,@args)=@_; | ||||||||||||||||||||||||||
|
Subroutine
_new: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 50 | 175 | my $self=autohash_tie Hash::AutoHash::MultiValued::tie,@args; | |||||||||||||||||||||||||||
| 51 | 175 | bless $self,$class; | |||||||||||||||||||||||||||
| 52 | } | ||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||
| 54 | ################################################################################# | ||||||||||||||||||||||||||||
| 55 | # Tied hash which implements Hash::AutoHash::MultiValued | ||||||||||||||||||||||||||||
| 56 | ################################################################################# | ||||||||||||||||||||||||||||
| 57 | package Hash::AutoHash::MultiValued::tie; | ||||||||||||||||||||||||||||
| 58 | 11 | our $VERSION=$Hash::AutoHash::MultiValued::VERSION; | |||||||||||||||||||||||||||
| 59 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 60 | 11 | ▶ | use Tie::Hash; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 61 | 11 | ▶ | use List::MoreUtils qw(uniq); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 62 | 11 | our @ISA=qw(Tie::ExtraHash); | |||||||||||||||||||||||||||
| 63 | 11 | ▶ | use constant STORAGE=>0; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 64 | 11 | ▶ | use constant UNIQUE=>1; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 65 | 11 | ▶ | use constant FILTER=>2; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | sub TIEHASH { | ||||||||||||||||||||||||||||
| 68 | 175 | ▶ | my($class,@hash)=@_; | ||||||||||||||||||||||||||
|
Subroutine
TIEHASH: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 69 | 175 | my $self=bless [{}],$class; | |||||||||||||||||||||||||||
| 70 | 175 | ▶ | if (@hash==1) { # flatten if ARRAY or HASH | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 71 | 4 | my $hash=shift @hash; | |||||||||||||||||||||||||||
| 72 | 4 | ▶ | @hash=('ARRAY' eq ref $hash)? @$hash: ('HASH' eq ref $hash)? %$hash: (); | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 175 | ▶ | while (@hash>1) { # store initial values | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 75 | 304 | my($key,$value)=splice @hash,0,2; # shift 1st two elements | |||||||||||||||||||||||||||
| 76 | 304 | $self->STORE($key,$value); | |||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | 175 | $self; | |||||||||||||||||||||||||||
| 79 | } | ||||||||||||||||||||||||||||
| 80 | sub FETCH { | ||||||||||||||||||||||||||||
| 81 | 2464 | ▶ | my($self,$key)=@_; | ||||||||||||||||||||||||||
|
Subroutine
FETCH: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 82 | 2464 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||
| 83 | 2464 | my $values=$storage->{$key}; | |||||||||||||||||||||||||||
| 84 | 2464 | ▶ | wantarray? (defined $values? @$values: ()): $values; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 85 | } | ||||||||||||||||||||||||||||
| 86 | sub STORE { | ||||||||||||||||||||||||||||
| 87 | 600 | ▶ | my($self,$key,@new_values)=@_; | ||||||||||||||||||||||||||
|
Subroutine
STORE: called CC=9 SCAR=22.2
| |||||||||||||||||||||||||||||
| 88 | 600 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||
| 89 | 600 | my $values=$storage->{$key}; | |||||||||||||||||||||||||||
| 90 | 600 | ▶ | if (@new_values) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 91 | 600 | ▶ | @new_values=@{$new_values[0]} if @new_values==1 && 'ARRAY' eq ref $new_values[0]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%3/3
@new_values == 1 and "ARRAY" eq ref $new_values[0]
MC/DC100%2/2
@new_values == 1 and "ARRAY" eq ref $new_values[0]
@new_values == 1
"ARRAY" eq ref $new_values[0]
Truth table100%3/3
A and B
A= @new_values == 1B= "ARRAY" eq ref $new_values[0]
| |||||||||||||||||||||||||||||
| 92 | 600 | ▶ | if (my $unique=$self->[UNIQUE]) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 93 | 86 | my($a,$b); | |||||||||||||||||||||||||||
| 94 | 86 | for $a (@new_values) { | |||||||||||||||||||||||||||
| 95 | 124 | ▶ | push(@$values,$a) unless grep {$b=$_; &$unique($a,$b)} @$values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | }} | ||||||||||||||||||||||||||||
| 97 | else { | ||||||||||||||||||||||||||||
| 98 | 514 | push(@$values,@new_values); | |||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | 600 | $storage->{$key}=$values; | |||||||||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||||||||
| 102 | 600 | ▶ | wantarray? (defined $values? @$values: ()): $values; | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||
| 104 | sub unique { | ||||||||||||||||||||||||||||
| 105 | 75 | ▶ | my $self=shift; | ||||||||||||||||||||||||||
|
Subroutine
unique: called CC=9 SCAR=22.0
| |||||||||||||||||||||||||||||
| 106 | 75 | ▶ | return $self->[UNIQUE] unless @_; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 107 | 72 | my $unique=$self->[UNIQUE]=shift; | |||||||||||||||||||||||||||
| 108 | 72 | ▶ | $unique=$self->[UNIQUE]=sub {$_[0] eq $_[1]} if $unique && 'CODE' ne ref $unique; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
Branch: 1 branch
Condition100%3/3
$unique and "CODE" ne ref $unique
MC/DC100%2/2
$unique and "CODE" ne ref $unique
$unique
"CODE" ne ref $unique
Truth table100%3/3
A and B
A= $uniqueB= "CODE" ne ref $unique
| |||||||||||||||||||||||||||||
| 109 | 72 | ▶ | if ($unique) { # apply to existing values | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 110 | 51 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||
| 111 | 51 | my @values=grep {defined $_} values %$storage; | |||||||||||||||||||||||||||
| 112 | 51 | for my $values (@values) { | |||||||||||||||||||||||||||
| 113 | 82 | ▶ | next unless @$values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | # leave 1st value in @$values. put rest in @new_values | ||||||||||||||||||||||||||||
| 115 | 58 | my @new_values=splice(@$values,1); | |||||||||||||||||||||||||||
| 116 | 58 | my($a,$b); | |||||||||||||||||||||||||||
| 117 | 58 | for $a (@new_values) { | |||||||||||||||||||||||||||
| 118 | 51 | ▶ | push(@$values,$a) unless grep {$b=$_; &$unique($a,$b)} @$values; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 119 | }} | ||||||||||||||||||||||||||||
| 120 | } | ||||||||||||||||||||||||||||
| 121 | 72 | $unique; | |||||||||||||||||||||||||||
| 122 | } | ||||||||||||||||||||||||||||
| 123 | sub filter { | ||||||||||||||||||||||||||||
| 124 | 74 | ▶ | my $self=shift; | ||||||||||||||||||||||||||
|
Subroutine
filter: called CC=4 SCAR=13.9
| |||||||||||||||||||||||||||||
| 125 | 74 | ▶ | my $filter=@_? $self->[FILTER]=shift: $self->[FILTER]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 126 | 74 | ▶ | if ($filter) { # appy filter to existing values | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 127 | 50 | ▶ | $filter=$self->[FILTER]=\&uniq unless 'CODE' eq ref $filter; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 128 | 50 | my $storage=$self->[STORAGE]; | |||||||||||||||||||||||||||
| 129 | 50 | my @values=grep {defined $_} values %$storage; | |||||||||||||||||||||||||||
| 130 | 50 | map {@$_=&$filter(@$_)} @values; # updates each list in-place | |||||||||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||||||||
| 132 | 74 | $filter; | |||||||||||||||||||||||||||
| 133 | } | ||||||||||||||||||||||||||||
| 134 | |||||||||||||||||||||||||||||
| 135 | 11 | 1; | |||||||||||||||||||||||||||
| 136 | __END__ | ||||||||||||||||||||||||||||