| 1 | package MDK::Common::DataStructure; | ||||||||||||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||||||||||||
| 3 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||
| 5 | MDK::Common::DataStructure - miscellaneous list/hash manipulation functions | ||||||||||||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||||||||||||
| 9 | use MDK::Common::DataStructure qw(:all); | ||||||||||||||||||||||||||||||||||||||
| 10 | |||||||||||||||||||||||||||||||||||||||
| 11 | =head1 EXPORTS | ||||||||||||||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||||||||||||
| 13 | =over | ||||||||||||||||||||||||||||||||||||||
| 14 | |||||||||||||||||||||||||||||||||||||||
| 15 | =item sort_numbers(LIST) | ||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||
| 17 | numerical sort (small numbers at beginning) | ||||||||||||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||||||||||||
| 19 | =item ikeys(HASH) | ||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||
| 21 | aka I<sorted integer keys>, as simple as C<sort { $a E<lt>=E<gt> $b } keys> | ||||||||||||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||||||||||||
| 23 | =item add2hash(HASH REF, HASH REF) | ||||||||||||||||||||||||||||||||||||||
| 24 | |||||||||||||||||||||||||||||||||||||||
| 25 | adds to the first hash the second hash if the key/value is not already there | ||||||||||||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||||||||||||
| 27 | =item add2hash_ | ||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||
| 29 | adds to the first hash the second hash if the key is not already there | ||||||||||||||||||||||||||||||||||||||
| 30 | |||||||||||||||||||||||||||||||||||||||
| 31 | =item put_in_hash | ||||||||||||||||||||||||||||||||||||||
| 32 | |||||||||||||||||||||||||||||||||||||||
| 33 | adds to the first hash the second hash, crushing existing key/values | ||||||||||||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||||||||||||
| 35 | =item member(SCALAR, LIST) | ||||||||||||||||||||||||||||||||||||||
| 36 | |||||||||||||||||||||||||||||||||||||||
| 37 | is the value in the list? | ||||||||||||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||||||||||||
| 39 | =item invbool(SCALAR REF) | ||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||
| 41 | toggles the boolean value | ||||||||||||||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||||||||||||
| 43 | =item listlength(LIST) | ||||||||||||||||||||||||||||||||||||||
| 44 | |||||||||||||||||||||||||||||||||||||||
| 45 | returns the length of the list. Useful in list (opposed to array) context: | ||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||
| 47 | sub f { "a", "b" } | ||||||||||||||||||||||||||||||||||||||
| 48 | my $l = listlength f(); | ||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||
| 50 | whereas C<scalar f()> would return "b" | ||||||||||||||||||||||||||||||||||||||
| 51 | |||||||||||||||||||||||||||||||||||||||
| 52 | =item deref(REF) | ||||||||||||||||||||||||||||||||||||||
| 53 | |||||||||||||||||||||||||||||||||||||||
| 54 | de-reference | ||||||||||||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||||||||||||
| 56 | =item deref_array(REF) | ||||||||||||||||||||||||||||||||||||||
| 57 | |||||||||||||||||||||||||||||||||||||||
| 58 | de-reference arrays: | ||||||||||||||||||||||||||||||||||||||
| 59 | |||||||||||||||||||||||||||||||||||||||
| 60 | deref_array [ "a", "b" ] #=> ("a", "b") | ||||||||||||||||||||||||||||||||||||||
| 61 | deref_array "a" #=> "a" | ||||||||||||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||||||||||||
| 63 | =item is_empty_array_ref(SCALAR) | ||||||||||||||||||||||||||||||||||||||
| 64 | |||||||||||||||||||||||||||||||||||||||
| 65 | is the scalar undefined or is the array empty | ||||||||||||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||||||||||||
| 67 | =item is_empty_hash_ref(SCALAR) | ||||||||||||||||||||||||||||||||||||||
| 68 | |||||||||||||||||||||||||||||||||||||||
| 69 | is the scalar undefined or is the hash empty | ||||||||||||||||||||||||||||||||||||||
| 70 | |||||||||||||||||||||||||||||||||||||||
| 71 | =item uniq(LIST) | ||||||||||||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||||||||||||
| 73 | returns the list with no duplicates (keeping the first elements) | ||||||||||||||||||||||||||||||||||||||
| 74 | |||||||||||||||||||||||||||||||||||||||
| 75 | =item uniq_ { CODE } LIST | ||||||||||||||||||||||||||||||||||||||
| 76 | |||||||||||||||||||||||||||||||||||||||
| 77 | returns the list with no duplicates according to the scalar results of CODE on each element of LIST (keeping the first elements) | ||||||||||||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||||||||||||
| 79 | uniq_ { $_->[1] } [ 1, "fo" ], [ 2, "fob" ], [ 3, "fo" ], [ 4, "bar" ] | ||||||||||||||||||||||||||||||||||||||
| 80 | |||||||||||||||||||||||||||||||||||||||
| 81 | gives [ 1, "fo" ], [ 2, "fob" ], [ 4, "bar" ] | ||||||||||||||||||||||||||||||||||||||
| 82 | |||||||||||||||||||||||||||||||||||||||
| 83 | =item difference2(ARRAY REF, ARRAY REF) | ||||||||||||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||||||||||||
| 85 | returns the first list without the element of the second list | ||||||||||||||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||||||||||||
| 87 | =item intersection(ARRAY REF, ARRAY REF, ...) | ||||||||||||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||||||||||||
| 89 | returns the elements which are in all lists | ||||||||||||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||||||||||||
| 91 | =item next_val_in_array(SCALAR, ARRAY REF) | ||||||||||||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||||||||||||
| 93 | finds the value that follow the scalar in the list (circular): | ||||||||||||||||||||||||||||||||||||||
| 94 | C<next_val_in_array(3, [1, 2, 3])> gives C<1> | ||||||||||||||||||||||||||||||||||||||
| 95 | (do not use a list with duplicates) | ||||||||||||||||||||||||||||||||||||||
| 96 | |||||||||||||||||||||||||||||||||||||||
| 97 | =item group_by2(LIST) | ||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||
| 99 | interprets the list as an ordered hash, returns a list of [key,value]: | ||||||||||||||||||||||||||||||||||||||
| 100 | C<group_by2(1 => 2, 3 => 4, 5 => 6)> gives C<[1,2], [3,4], [5,6]> | ||||||||||||||||||||||||||||||||||||||
| 101 | |||||||||||||||||||||||||||||||||||||||
| 102 | =item list2kv(LIST) | ||||||||||||||||||||||||||||||||||||||
| 103 | |||||||||||||||||||||||||||||||||||||||
| 104 | interprets the list as an ordered hash, returns the keys and the values: | ||||||||||||||||||||||||||||||||||||||
| 105 | C<list2kv(1 => 2, 3 => 4, 5 => 6)> gives C<[1,3,5], [2,4,6]> | ||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||
| 107 | =back | ||||||||||||||||||||||||||||||||||||||
| 108 | |||||||||||||||||||||||||||||||||||||||
| 109 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||
| 110 | |||||||||||||||||||||||||||||||||||||||
| 111 | L<MDK::Common> | ||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||
| 113 | =cut | ||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||
| 115 | |||||||||||||||||||||||||||||||||||||||
| 116 | 1 | ▶ | use MDK::Common::Math; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 117 | 1 | ▶ | use MDK::Common::Func; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||
| 120 | 1 | ▶ | use Exporter; | ||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||
| 121 | 1 | our @ISA = qw(Exporter); | |||||||||||||||||||||||||||||||||||||
| 122 | 1 | our @EXPORT_OK = qw(sort_numbers ikeys add2hash add2hash_ put_in_hash member invbool listlength deref deref_array is_empty_array_ref is_empty_hash_ref uniq uniq_ difference2 intersection next_val_in_array group_by2 list2kv); | |||||||||||||||||||||||||||||||||||||
| 123 | 1 | our %EXPORT_TAGS = (all => [ @EXPORT_OK ]); | |||||||||||||||||||||||||||||||||||||
| 124 | |||||||||||||||||||||||||||||||||||||||
| 125 | |||||||||||||||||||||||||||||||||||||||
| 126 | 0 | ▶ | sub sort_numbers { sort { $a <=> $b } @_ } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
sort_numbers: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 127 | 0 | ▶ | sub ikeys { my %l = @_; sort { $a <=> $b } keys %l } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
ikeys: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 128 | 0 | ▶ | sub put_in_hash { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { $a->{$k} = $v } $a } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
put_in_hash: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
Condition0%0/2
$b || {}
MC/DC0%0/1
$b || {}
$b
Truth table0%0/2
A
A= $b
| |||||||||||||||||||||||||||||||||||||||
| 129 | 0 | ▶ | sub add2hash { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { $a->{$k} ||= $v } $a } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
add2hash: not called CC=4 SCAR=30.0
Pod
documented
Branch: 1 branch
Condition0%0/5
$b || {}
$a->{$k} ||= $v
MC/DC0%0/1
$b || {}
$b
MC/DC0%0/2
$a->{$k} ||= $v
$a->{$k}
$v
Truth table0%0/2
A
A= $b
Truth table0%0/3
A ||= B
A= $a->{$k}B= $v
| |||||||||||||||||||||||||||||||||||||||
| 130 | 0 | ▶ | sub add2hash_ { my ($a, $b) = @_; while (my ($k, $v) = each %{$b || {}}) { exists $a->{$k} or $a->{$k} = $v } $a } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
add2hash_: not called CC=4 SCAR=30.0
Pod
documented
Branch: 2 branches
Condition0%0/2
$b || {}
MC/DC0%0/1
$b || {}
$b
Truth table0%0/2
A
A= $b
| |||||||||||||||||||||||||||||||||||||||
| 131 | 0 | ▶ | sub member { my $e = shift; foreach (@_) { $e eq $_ and return 1 } 0 } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
member: not called CC=3 SCAR=24.8
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 132 | 0 | ▶ | sub invbool { my $a = shift; $$a = !$$a; $$a } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
invbool: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 133 | 0 | ▶ | sub listlength { scalar @_ } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
listlength: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 134 | 0 | ▶ | sub strcpy { substr($_[0], $_[2] || 0, length $_[1]) = $_[1] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
strcpy: not called CC=2 SCAR=17.9
Pod
undocumented
Condition0%0/2
$_[2] || 0
MC/DC0%0/1
$_[2] || 0
$_[2]
Truth table0%0/2
A
A= $_[2]
| |||||||||||||||||||||||||||||||||||||||
| 135 | 0 | ▶ | sub deref { ref($_[0]) eq "ARRAY" ? @{$_[0]} : ref($_[0]) eq "HASH" ? %{$_[0]} : $_[0] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
deref: not called CC=3 SCAR=24.8
Pod
documented
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||
| 136 | 0 | ▶ | sub deref_array { ref($_[0]) eq "ARRAY" ? @{$_[0]} : $_[0] } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
deref_array: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||||||||||||
| 138 | 0 | ▶ | sub is_empty_array_ref { my $a = shift; !defined $a || @$a == 0 } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
is_empty_array_ref: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 139 | 0 | ▶ | sub is_empty_hash_ref { my $a = shift; !defined $a || keys(%$a) == 0 } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
is_empty_hash_ref: not called CC=2 SCAR=17.9
Pod
documented
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 140 | |||||||||||||||||||||||||||||||||||||||
| 141 | 0 | ▶ | sub uniq { my %l; $l{$_} = 1 foreach @_; grep { delete $l{$_} } @_ } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
uniq: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 142 | 0 | ▶ | sub difference2 { my %l; @l{@{$_[1]}} = (); grep { !exists $l{$_} } @{$_[0]} } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
difference2: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 143 | 0 | ▶ | sub intersection { my (%l, @m); @l{@{shift @_}} = (); foreach (@_) { @m = grep { exists $l{$_} } @$_; %l = (); @l{@m} = () } keys %l } | ||||||||||||||||||||||||||||||||||||
|
Subroutine
intersection: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 144 | |||||||||||||||||||||||||||||||||||||||
| 145 | 0 | ▶ | sub uniq_(&@) { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
uniq_: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 146 | 0 | my $f = shift; | |||||||||||||||||||||||||||||||||||||
| 147 | 0 | my %l; | |||||||||||||||||||||||||||||||||||||
| 148 | 0 | $l{$f->($_)} = 1 foreach @_; | |||||||||||||||||||||||||||||||||||||
| 149 | 0 | grep { delete $l{$f->($_)} } @_; | |||||||||||||||||||||||||||||||||||||
| 150 | } | ||||||||||||||||||||||||||||||||||||||
| 151 | |||||||||||||||||||||||||||||||||||||||
| 152 | |||||||||||||||||||||||||||||||||||||||
| 153 | 0 | ▶ | sub next_val_in_array { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
next_val_in_array: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 154 | 0 | my ($v, $l) = @_; | |||||||||||||||||||||||||||||||||||||
| 155 | 0 | ▶ | my %l = MDK::Common::Func::mapn(sub { @_ }, $l, [ @$l[1..$#$l], $l->[0] ]); | ||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||||||||||||
| 156 | 0 | $l{$v}; | |||||||||||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||||||||||||
| 159 | |||||||||||||||||||||||||||||||||||||||
| 160 | 0 | ▶ | sub list2kv { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
list2kv: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 161 | 0 | my (@k, @v); | |||||||||||||||||||||||||||||||||||||
| 162 | 0 | ▶ | for (my $i = 0; $i < @_; $i += 2) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 163 | 0 | push @k, $_[$i + 0]; | |||||||||||||||||||||||||||||||||||||
| 164 | 0 | push @v, $_[$i + 1]; | |||||||||||||||||||||||||||||||||||||
| 165 | } | ||||||||||||||||||||||||||||||||||||||
| 166 | 0 | \@k, \@v; | |||||||||||||||||||||||||||||||||||||
| 167 | } | ||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||
| 169 | 0 | ▶ | sub group_by2 { | ||||||||||||||||||||||||||||||||||||
|
Subroutine
group_by2: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||
| 170 | 0 | my @l; | |||||||||||||||||||||||||||||||||||||
| 171 | 0 | ▶ | for (my $i = 0; $i < @_; $i += 2) { | ||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||
| 172 | 0 | push @l, [ $_[$i], $_[$i+1] ]; | |||||||||||||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||||||||||||
| 174 | 0 | @l; | |||||||||||||||||||||||||||||||||||||
| 175 | } | ||||||||||||||||||||||||||||||||||||||
| 176 | |||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||
| 178 | 1 | 1; | |||||||||||||||||||||||||||||||||||||