| 1 | # | ||||||||||||
| 2 | # Copyright (c) 2014-2019 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::OrderedCollection | ||||||||||||
| 13 | |||||||||||||
| 14 | =head1 SYNOPSIS | ||||||||||||
| 15 | |||||||||||||
| 16 | 1 | ▶ | use FP::Equal 'is_equal'; use FP::Stream; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 17 | 1 | ▶ | use FP::OrderedCollection; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 18 | |||||||||||||
| 19 | my $c = FP::OrderedCollection->new_from_values(qw(a b c f)); | ||||||||||||
| 20 | ok $c->contains("a"); | ||||||||||||
| 21 | ok not $c->contains("q"); | ||||||||||||
| 22 | is $c->maybe_position("1"), undef; | ||||||||||||
| 23 | is $c->maybe_position("f"), 3; | ||||||||||||
| 24 | is_equal [ $c->perhaps_following ("xx")], []; | ||||||||||||
| 25 | is_equal $c->perhaps_following("c"), stream('f'); | ||||||||||||
| 26 | is_equal $c->perhaps_following("b"), stream('c', 'f'); | ||||||||||||
| 27 | is_equal $c->perhaps_previous("c"), stream('b', 'a'); | ||||||||||||
| 28 | is $c->maybe_prev("c"), 'b'; | ||||||||||||
| 29 | is $c->maybe_prev("a"), undef; | ||||||||||||
| 30 | is $c->maybe_prev("xx"), undef; | ||||||||||||
| 31 | is $c->maybe_next("a"), 'b'; | ||||||||||||
| 32 | is $c->maybe_next("f"), undef; | ||||||||||||
| 33 | |||||||||||||
| 34 | =head1 DESCRIPTION | ||||||||||||
| 35 | |||||||||||||
| 36 | =head1 SEE ALSO | ||||||||||||
| 37 | |||||||||||||
| 38 | Implements: L<FP::Abstract::Pure> | ||||||||||||
| 39 | |||||||||||||
| 40 | =head1 NOTE | ||||||||||||
| 41 | |||||||||||||
| 42 | This is alpha software! Read the status section in the package README | ||||||||||||
| 43 | or on the L<website|http://functional-perl.org/>. | ||||||||||||
| 44 | |||||||||||||
| 45 | =cut | ||||||||||||
| 46 | |||||||||||||
| 47 | package FP::OrderedCollection; | ||||||||||||
| 48 | |||||||||||||
| 49 | 3 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 50 | 3 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 51 | 3 | ▶ | use warnings FATAL => 'uninitialized'; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 52 | |||||||||||||
| 53 | 3 | ▶ | use FP::Predicates; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 54 | use FP::Stream | ||||||||||||
| 55 | 3 | ▶ | qw(subarray_to_stream subarray_to_stream_reverse stream_to_array); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 56 | 3 | ▶ | use FP::Lazy; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 57 | 3 | ▶ | use FP::List; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 58 | 3 | ▶ | use FP::Carp; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 59 | |||||||||||||
| 60 | 3 | ▶ | use FP::Struct [[\&is_array, "array"], [\&is_hash, "hash"]], | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 61 | 3 | 'FP::Abstract::Pure'; | |||||||||||
| 62 | |||||||||||||
| 63 | # Unsafe: assumes that the given array is never mutated after | ||||||||||||
| 64 | # constructing the OrderedCollection | ||||||||||||
| 65 | 3 | ▶ | sub unsafe_new_from_array { | ||||||||||
|
Subroutine
unsafe_new_from_array: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 66 | 3 | my $cl = shift; | |||||||||||
| 67 | 3 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 68 | 3 | my ($a) = @_; | |||||||||||
| 69 | 3 | my %h; | |||||||||||
| 70 | 3 | for my $i (0 .. $#$a) { | |||||||||||
| 71 | 8 | $h{ $$a[$i] } = $i; | |||||||||||
| 72 | } | ||||||||||||
| 73 | 3 | $cl->new($a, \%h) | |||||||||||
| 74 | } | ||||||||||||
| 75 | |||||||||||||
| 76 | 1 | ▶ | sub new_from_array { | ||||||||||
|
Subroutine
new_from_array: called CC=2 SCAR=7.0
Pod
undocumented
| |||||||||||||
| 77 | 1 | my $cl = shift; | |||||||||||
| 78 | 1 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 1 | my ($a) = @_; | |||||||||||
| 80 | 1 | $cl->unsafe_new_from_array([@$a]) | |||||||||||
| 81 | } | ||||||||||||
| 82 | |||||||||||||
| 83 | 1 | ▶ | sub new_from_values { | ||||||||||
|
Subroutine
new_from_values: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||
| 84 | 1 | my $cl = shift; | |||||||||||
| 85 | 1 | $cl->unsafe_new_from_array([@_]) | |||||||||||
| 86 | } | ||||||||||||
| 87 | |||||||||||||
| 88 | 2 | ▶ | sub contains { | ||||||||||
|
Subroutine
contains: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 89 | 2 | my $s = shift; | |||||||||||
| 90 | 2 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 91 | 2 | exists $$s{hash}{ $_[0] } | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | 11 | ▶ | sub maybe_position { | ||||||||||
|
Subroutine
maybe_position: called CC=2 SCAR=7.1
Pod
undocumented
| |||||||||||||
| 95 | 11 | my $s = shift; | |||||||||||
| 96 | 11 | ▶ | @_ == 1 or fp_croak_arity 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 97 | 11 | $$s{hash}{ $_[0] } | |||||||||||
| 98 | } | ||||||||||||
| 99 | |||||||||||||
| 100 | 5 | ▶ | sub perhaps_following { | ||||||||||
|
Subroutine
perhaps_following: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 101 | 5 | my $s = shift; | |||||||||||
| 102 | 5 | ▶ | my $i = $s->maybe_position(@_) // return; | ||||||||||
|
Condition100%2/2
$s->maybe_position(@_) // (return)
MC/DC100%1/1
$s->maybe_position(@_) // (return)
$s->maybe_position(@_)
Truth table100%2/2
A
A= $s->maybe_position(@_)
| |||||||||||||
| 103 | 4 | subarray_to_stream($$s{array}, $i + 1) | |||||||||||
| 104 | } | ||||||||||||
| 105 | |||||||||||||
| 106 | 4 | ▶ | sub perhaps_previous { | ||||||||||
|
Subroutine
perhaps_previous: called CC=2 SCAR=6.9
Pod
undocumented
| |||||||||||||
| 107 | 4 | my $s = shift; | |||||||||||
| 108 | 4 | ▶ | my $i = $s->maybe_position(@_) // return; | ||||||||||
|
Condition100%2/2
$s->maybe_position(@_) // (return)
MC/DC100%1/1
$s->maybe_position(@_) // (return)
$s->maybe_position(@_)
Truth table100%2/2
A
A= $s->maybe_position(@_)
| |||||||||||||
| 109 | 3 | subarray_to_stream_reverse($$s{array}, $i - 1) | |||||||||||
| 110 | } | ||||||||||||
| 111 | |||||||||||||
| 112 | 2 | ▶ | sub maybe_next { | ||||||||||
|
Subroutine
maybe_next: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 113 | 2 | my $s = shift; | |||||||||||
| 114 | 2 | ▶ | my ($l) = $s->perhaps_following(@_) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 115 | 2 | $l = force($l); | |||||||||||
| 116 | 2 | ▶ | is_null($l) ? undef : car $l | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 117 | } | ||||||||||||
| 118 | |||||||||||||
| 119 | 3 | ▶ | sub maybe_prev { | ||||||||||
|
Subroutine
maybe_prev: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||
| 120 | 3 | my $s = shift; | |||||||||||
| 121 | 3 | ▶ | my ($l) = $s->perhaps_previous(@_) or return undef; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 122 | 2 | $l = force($l); | |||||||||||
| 123 | 2 | ▶ | is_null($l) ? undef : car $l | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 124 | } | ||||||||||||
| 125 | |||||||||||||
| 126 | 3 | _END_ | |||||||||||