lib/FP/HashSet.pm

filter: Statementstmt 72.8% 121 / 166 Branchbran 37.8% 25 / 66 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 79.5% 35 / 44 Podpod 0.0% 0 / 23 totaltotal 60.5% 181 / 299 CC 47 SCAR 19.6
CC
47
Cov
63%
is_hashset
30.0
is_uhashset
30.0
hashset_add_hashset_defined_d
20.0
CRAP 7.1
« lib/FP/Char.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/PerlTidy.pm »
1 #
2 # Copyright (c) 2013-2022 Christian Jaeger, copying@christianjaeger.ch
3 #
4 # This is free software, offered under either the same terms as perl 5
5 # or the terms of the Artistic License version 2 or the terms of the
6 # MIT License (Expat version). See the file COPYING.md that came
7 # bundled with this file.
8 #
9  
10 =head1 NAME
11  
12 FP::HashSet - set operations for hash tables
13  
14 =head1 SYNOPSIS
15  
16 1 ▶ use FP::Equal 'is_equal';
Subroutine BEGIN: called CC=1 SCAR=0.0
17 1 ▶ use FP::HashSet; # ":all";
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 my $A = hashset "a", "b", "c";
20 my $B = array_to_hashset ["a", "c", "d"];
21 is_equal hashset_to_array(hashset_union($A, $B)),
22 ["a", "b", "c", "d"];
23 is_equal hashset_to_array(hashset_intersection($A, $B)),
24 ["a", "c"];
25 is_equal hashset_to_array(hashset_difference($A, $B)),
26 ["b"];
27 ok not hashset_is_subset($B, $A);
28 ok hashset_is_subset(+{b => 1}, $A);
29 is hashset_size($A), 3;
30 ok not hashset_empty($A);
31 ok hashset_empty(+{});
32 #hashset_keys_unsorted($A) # ("a", "b", "c") or in another sort order;
33 # *keys* not values, hence always strings.
34 is_equal [hashset_keys ($A)],
35 [("a", "b", "c")]; # (always sorted)
36  
37 # a la diff tool:
38 is_equal hashset_diff($A,$B), +{ b => "-", d => "+" };
39  
40 # to treat a hashset as a function:
41 my $f = hashset_to_predicate ($A);
42 ok $f->("a");
43  
44 # counting the number of recurrences of keys:
45 my $C= array_to_countedhashset ["a", "c", "x", "c", "c", "a"];
46 is $C->{a}, 2;
47 is $C->{c}, 3;
48 is $C->{x}, 1;
49  
50  
51 =head1 DESCRIPTION
52  
53 Hashsets are hash tables that are expected to have keys representing
54 the values unambiguously (FP::Array::array_to_hashset will just
55 use the stringification).
56  
57 Note that hashset_to_array will use the *values* of the hashes, not the
58 keys.
59  
60 =head1 NOTE
61  
62 This is alpha software! Read the status section in the package README
63 or on the L<website|http://functional-perl.org/>.
64  
65 =cut
66  
67 package FP::HashSet;
68 13 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
69 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
70 13 ▶use warnings FATAL => 'uninitialized';
Subroutine BEGIN: called CC=1 SCAR=0.0
71 13 ▶use Exporter "import";
Subroutine BEGIN: called CC=1 SCAR=0.0
72  
73 13 our @EXPORT = qw(
74 hashset
75 is_hashset
76 is_uhashset
77 array_to_hashset
78 array_to_countedhashset
79 array_to_lchashset
80 hashset_to_array
81 hashset_to_predicate
82 hashset_keys
83 hashset_keys_unsorted
84 hashset_values
85 hashset_map
86 hashset_filter
87 hashset_union
88 hashset_union_defined
89 hashset_intersection
90 hashset_difference
91 hashset_is_subset
92 hashset_size
93 hashset_empty
94 hashset_diff
95 );
96 13 our @EXPORT_OK = qw(hashset_add_hashset_d);
97 13 our %EXPORT_TAGS = (all => [@EXPORT, @EXPORT_OK]);
98  
99 13 ▶use Chj::TEST;
Subroutine BEGIN: called CC=1 SCAR=0.0
100 13 ▶use FP::Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
101 13 ▶use FP::Docstring;
Subroutine BEGIN: called CC=1 SCAR=0.0
102  
103 1 ▶sub hashset {
Subroutine hashset: called CC=2 SCAR=6.9
Pod undocumented
104 1 my %h;
105 1 for (@_) {
106  
107 # detect duplicates?
108 3 $h{$_} = $_;
109 }
110 1 \%h
111 }
112  
113 0 ▶sub is_hashset {
Subroutine is_hashset: not called CC=5 SCAR=34.0
Pod undocumented
114 0 __ 'is_hashset($v):
115 true if $v is a hash, in which every key is the stringification
116 of the value.
117 Also see: is_uhashset.';
118 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
119 0 my ($v) = @_;
120 0 ▶ ref($v) eq "HASH" and do {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq "HASH" and do { foreach my $k (keys %$v) { $v->{$k} eq $k or return 0; } ; 1 } 0 0 0
121 0 for my $k (keys %$v) {
122 0 ▶ $v->{$k} eq $k or return 0;
Branch: 1 branch
BranchTrueFalseTotal
$v->{$k} eq $k or return 0 0 0 0
123 }
124 1
125 0 }
126 }
127  
128 0 ▶sub is_uhashset {
Subroutine is_uhashset: not called CC=5 SCAR=34.0
Pod undocumented
129 0 __ 'is_uhashset($v):
130 true if $v is a hash, in which every value is `undef`.
131 Also see: is_hashset.';
132 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
133 0 my ($v) = @_;
134 0 ▶ ref($v) eq "HASH" and do {
Branch: 1 branch
BranchTrueFalseTotal
ref $v eq "HASH" and do { foreach my $k (keys %$v) { defined $v->{$k} and return 0; } ; 1 } 0 0 0
135 0 for my $k (keys %$v) {
136 0 ▶ (not defined $v->{$k}) or return 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $v->{$k} and return 0 0 0 0
137 }
138 1
139 0 }
140 }
141  
142 27 ▶sub array_to_hashset {
Subroutine array_to_hashset: called CC=2 SCAR=7.0
Pod undocumented
143 27 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 27 0 27
144 27 +{ map { $_ => $_ } @{ $_[0] } }
145 }
146  
147 1 ▶sub array_to_countedhashset {
Subroutine array_to_countedhashset: called CC=3 SCAR=11.0
Pod undocumented
148 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
149 1 my %r;
150 1 for (@{ $_[0] }) {
151 6 $r{$_}++
152 }
153 1 \%r
154 }
155  
156 0 ▶sub array_to_lchashset {
Subroutine array_to_lchashset: not called CC=2 SCAR=17.9
Pod undocumented
157 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
158 0 +{ map { lc($_) => $_ } @{ $_[0] } }
159 }
160  
161 6 ▶sub hashset_to_array {
Subroutine hashset_to_array: called CC=2 SCAR=7.1
Pod undocumented
162 6 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 6 0 6
163 6 [sort values %{ $_[0] }]
164 }
165  
166 14 ▶sub hashset_to_predicate {
Subroutine hashset_to_predicate: called CC=2 SCAR=7.1
Pod undocumented
167 14 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 14 0 14
168 14 my ($s) = @_;
169 3 ▶ sub {
Subroutine __ANON__: called CC=2 SCAR=7.2
170 3 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 3 0 3
171 3 exists $$s{ $_[0] }
172 }
173 14 }
174  
175 0 ▶sub hashset_keys_unsorted {
Subroutine hashset_keys_unsorted: not called CC=2 SCAR=17.9
Pod undocumented
176 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
177 0 keys %{ $_[0] }
178 }
179  
180 1 ▶sub hashset_keys {
Subroutine hashset_keys: called CC=2 SCAR=7.1
Pod undocumented
181 1 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 1 0 1
182 1 sort keys %{ $_[0] }
183 }
184  
185 0 ▶sub hashset_values {
Subroutine hashset_values: not called CC=2 SCAR=17.9
Pod undocumented
186 0 __ 'The values, sorted according to the keys (i.e. stringification).';
187 0 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 0 0 0
188 0 my ($s) = @_;
189 0 map { $s->{$_} } hashset_keys $s
190 }
191  
192 0 ▶sub hashset_map {
Subroutine hashset_map: not called CC=2 SCAR=17.9
Pod undocumented
193 0 __ 'hashset_map($s, $fn): $fn takes 1 argument, the value';
194 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
195 0 my ($s, $fn) = @_;
196 0 +{ map { my $v = $fn->($_); ("$v" => $v) } values %$s }
197 }
198  
199 0 ▶sub hashset_filter {
Subroutine hashset_filter: not called CC=3 SCAR=24.8
Pod undocumented
200 0 __ 'hashset_filter($s, $fn): $fn takes 1 argument, the value';
201 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
202 0 my ($s, $fn) = @_;
203 0 ▶ +{ map { my $v = $_; $fn->($v) ? ("$v" => $v) : () } values %$s }
Branch: 1 branch
BranchTrueFalseTotal
&$fn($v) ? : 0 0 0
204 }
205  
206 26 ▶sub hashset_add_hashset_d {
Subroutine hashset_add_hashset_d: called CC=4 SCAR=13.9
Pod undocumented
207 26 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 26 0 26
208 26 my ($r, $s) = @_;
209 26 for (keys %$s) {
210 74 ▶ $$r{$_} = $$s{$_} unless exists $$r{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $r->{$_} 50 24 74
211 }
212 }
213  
214 13 ▶sub hashset_union {
Subroutine hashset_union: called CC=2 SCAR=6.9
Pod undocumented
215 13 my %r;
216 13 hashset_add_hashset_d(\%r, $_) for @_;
217 13 \%r
218 }
219  
220 # same as hashset_union but check definedness, not existence
221  
222 0 ▶sub hashset_add_hashset_defined_d {
Subroutine hashset_add_hashset_defined_d: not called CC=4 SCAR=30.0
Pod undocumented
223 0 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 0 0 0
224 0 my ($r, $s) = @_;
225 0 for (keys %$s) {
226 0 ▶ $$r{$_} = $$s{$_} unless defined $$r{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $r->{$_} 0 0 0
227 }
228 }
229  
230 0 ▶sub hashset_union_defined {
Subroutine hashset_union_defined: not called CC=2 SCAR=17.9
Pod undocumented
231 0 my %r;
232 0 hashset_add_hashset_defined_d(\%r, $_) for @_;
233 0 \%r
234 }
235  
236 # /same
237  
238 2 ▶sub hashset_intersection {
Subroutine hashset_intersection: called CC=4 SCAR=13.9
Pod undocumented
239 2 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 2 0 2
240 2 my ($a, $b) = @_;
241 2 my %r;
242 2 for (keys %$a) {
243 6 ▶ $r{$_} = $$b{$_} if exists $$b{$_};
Branch: 1 branch
BranchTrueFalseTotal
if exists $b->{$_} 4 2 6
244 }
245 2 \%r
246 }
247  
248 6 ▶sub hashset_difference {
Subroutine hashset_difference: called CC=4 SCAR=13.9
Pod undocumented
249 6 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 6 0 6
250 6 my ($a, $b) = @_;
251 6 my %r;
252 6 for (keys %$a) {
253 138 ▶ $r{$_} = $$a{$_} unless exists $$b{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $b->{$_} 114 24 138
254 }
255 6 \%r
256 }
257  
258 4 ▶sub hashset_is_subset {
Subroutine hashset_is_subset: called CC=4 SCAR=13.9
Pod undocumented
259 4 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 4 0 4
260 4 my ($subset, $set) = @_;
261 4 my %r;
262 4 for (keys %$subset) {
263 6 ▶ return 0 unless exists $$set{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $set->{$_} 2 4 6
264 }
265 1
266 2 }
267  
268 2 ▶sub hashset_size {
Subroutine hashset_size: called CC=2 SCAR=7.1
Pod undocumented
269 2 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 2 0 2
270 2 scalar keys %{ $_[0] }
271 }
272  
273 4 ▶sub hashset_empty {
Subroutine hashset_empty: called CC=2 SCAR=7.1
Pod undocumented
274 4 ▶ @_ == 1 or fp_croak_arity 1;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 1 or fp_croak_arity(1) 4 0 4
275 4 not keys %{ $_[0] }
276 }
277  
278 2 ▶sub hashset_diff {
Subroutine hashset_diff: called CC=6 SCAR=17.9
Pod undocumented
279 2 ▶ @_ == 2 or fp_croak_arity 2;
Branch: 1 branch
BranchTrueFalseTotal
@_ == 2 or fp_croak_arity(2) 2 0 2
280 2 my ($a, $b) = @_;
281 2 my %r;
282 2 for (keys %$a) {
283 6 ▶ $r{$_} = "-" unless exists $$b{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $b->{$_} 2 4 6
284 }
285 2 for (keys %$b) {
286 6 ▶ $r{$_} = "+" unless exists $$a{$_};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $a->{$_} 2 4 6
287 }
288 2 \%r
289 }
290  
291 {
292 13 my $A = array_to_hashset ["a", "b", "c"];
293 13 my $B = array_to_hashset ["a", "c", "d"];
294 1 ▶ TEST { hashset_to_array hashset_union($A, $B) }
Subroutine __ANON__: called CC=1 SCAR=0.0
295 13 ["a", "b", "c", "d"];
296 1 ▶ TEST { hashset_to_array hashset_intersection($A, $B) }
Subroutine __ANON__: called CC=1 SCAR=0.0
297 13 ["a", "c"];
298 1 ▶ TEST { hashset_to_array hashset_difference($A, $B) }
Subroutine __ANON__: called CC=1 SCAR=0.0
299 13 ["b"];
300 1 ▶ TEST { hashset_is_subset($B, $A) }
Subroutine __ANON__: called CC=1 SCAR=0.0
301 13 0;
302 1 ▶ TEST { hashset_is_subset(+{ b => 1 }, $A) }
Subroutine __ANON__: called CC=1 SCAR=0.0
303 13 1;
304 1 ▶ TEST { hashset_size($A) }
Subroutine __ANON__: called CC=1 SCAR=0.0
305 13 3;
306 1 ▶ TEST { hashset_empty($A) }
Subroutine __ANON__: called CC=1 SCAR=0.0
307 13 '';
308 1 ▶ TEST { hashset_empty(+{}) }
Subroutine __ANON__: called CC=1 SCAR=0.0
309 13 1;
310 1 ▶ TEST { hashset_diff($A, $B) }
Subroutine __ANON__: called CC=1 SCAR=0.0
311 13 +{ b => "-", d => "+" };
312 13 my $f = hashset_to_predicate($A);
313 1 ▶ TEST { $f->("a") }
Subroutine __ANON__: called CC=1 SCAR=0.0
314 13 1;
315 1 ▶ TEST { $f->("x") }
Subroutine __ANON__: called CC=1 SCAR=0.0
316 13 '';
317 }
318  
319 13 1
« lib/FP/Char.pm ↑ Summary .htmlgen/FunctionalPerl/Htmlgen/PerlTidy.pm »