| 1 | # Copyright 1996-2002 by Steven McDougall. | ||||||||||
| 2 | # This module is free software; you can redistribute it and/or modify it | ||||||||||
| 3 | # under the same terms as Perl itself. | ||||||||||
| 4 | |||||||||||
| 5 | package Set::Window; | ||||||||||
| 6 | |||||||||||
| 7 | 1 | ▶ | use 5.6.0; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 1 | ▶ | use vars qw($VERSION @ISA); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | |||||||||||
| 11 | 1 | require Exporter; | |||||||||
| 12 | |||||||||||
| 13 | 1 | @ISA = qw(Exporter); | |||||||||
| 14 | 1 | $VERSION = '1.01'; | |||||||||
| 15 | |||||||||||
| 16 | |||||||||||
| 17 | sub new_lr | ||||||||||
| 18 | 119 | ▶ | { | ||||||||
|
Subroutine
new_lr: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 19 | 119 | my($class, $left, $right) = @_; | |||||||||
| 20 | 119 | ▶ | $right < $left and return empty $class; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 21 | 100 | bless [$left, $right], $class | |||||||||
| 22 | } | ||||||||||
| 23 | |||||||||||
| 24 | |||||||||||
| 25 | sub new_ll | ||||||||||
| 26 | 22 | ▶ | { | ||||||||
|
Subroutine
new_ll: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 27 | 22 | my($class, $left, $length) = @_; | |||||||||
| 28 | 22 | ▶ | $length < 1 and return empty $class; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | 21 | bless [$left, $left+$length-1], $class | |||||||||
| 30 | } | ||||||||||
| 31 | |||||||||||
| 32 | |||||||||||
| 33 | sub left | ||||||||||
| 34 | 2 | ▶ | { | ||||||||
|
Subroutine
left: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 35 | 2 | my $window = shift; | |||||||||
| 36 | 2 | my($left, $right) = @$window; | |||||||||
| 37 | 2 | ▶ | $right < $left and return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 38 | 1 | $left | |||||||||
| 39 | } | ||||||||||
| 40 | |||||||||||
| 41 | |||||||||||
| 42 | sub right | ||||||||||
| 43 | 2 | ▶ | { | ||||||||
|
Subroutine
right: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 44 | 2 | my $window = shift; | |||||||||
| 45 | 2 | my($left, $right) = @$window; | |||||||||
| 46 | 2 | ▶ | $right < $left and return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 47 | 1 | $right | |||||||||
| 48 | } | ||||||||||
| 49 | |||||||||||
| 50 | |||||||||||
| 51 | sub size | ||||||||||
| 52 | 2 | ▶ | { | ||||||||
|
Subroutine
size: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 53 | 2 | my $window = shift; | |||||||||
| 54 | 2 | my($left, $right) = @$window; | |||||||||
| 55 | 2 | $right - $left + 1 | |||||||||
| 56 | } | ||||||||||
| 57 | |||||||||||
| 58 | 1 | *length = \&size; #deprecated | |||||||||
| 59 | |||||||||||
| 60 | |||||||||||
| 61 | sub elements | ||||||||||
| 62 | 3 | ▶ | { | ||||||||
|
Subroutine
elements: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 63 | 3 | my $window = shift; | |||||||||
| 64 | 3 | my($left, $right) = @$window; | |||||||||
| 65 | 3 | my @elements = ($left .. $right); | |||||||||
| 66 | 3 | ▶ | wantarray ? @elements : \@elements | ||||||||
|
Branch: 1 branch
| |||||||||||
| 67 | } | ||||||||||
| 68 | |||||||||||
| 69 | |||||||||||
| 70 | sub bounds | ||||||||||
| 71 | 3 | ▶ | { | ||||||||
|
Subroutine
bounds: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 72 | 3 | my $window = shift; | |||||||||
| 73 | 3 | my($left, $right) = @$window; | |||||||||
| 74 | 3 | ▶ | $right < $left and return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 75 | 2 | my @bounds = ($left, $right); | |||||||||
| 76 | 2 | ▶ | wantarray ? @bounds : \@bounds | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | } | ||||||||||
| 78 | |||||||||||
| 79 | |||||||||||
| 80 | sub empty | ||||||||||
| 81 | 101 | ▶ | { | ||||||||
|
Subroutine
empty: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 82 | 101 | my $arg = shift; | |||||||||
| 83 | 101 | my $ref = ref $arg; | |||||||||
| 84 | |||||||||||
| 85 | 101 | ▶ | $ref ? | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | $arg->[1] < $arg->[0] : | ||||||||||
| 87 | bless [0, -1], $arg | ||||||||||
| 88 | } | ||||||||||
| 89 | |||||||||||
| 90 | |||||||||||
| 91 | sub equal | ||||||||||
| 92 | 36 | ▶ | { | ||||||||
|
Subroutine
equal: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 93 | 36 | my($w1, $w2) = @_; | |||||||||
| 94 | 36 | ▶ | $w1->[0]==$w2->[0] and $w1->[1]==$w2->[1] | ||||||||
|
Branch: 1 branch
| |||||||||||
| 95 | } | ||||||||||
| 96 | |||||||||||
| 97 | |||||||||||
| 98 | sub equivalent | ||||||||||
| 99 | 36 | ▶ | { | ||||||||
|
Subroutine
equivalent: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 100 | 36 | my($w1, $w2) = @_; | |||||||||
| 101 | 36 | $w1->[1] - $w1->[0] == $w2->[1] - $w2->[0] | |||||||||
| 102 | } | ||||||||||
| 103 | |||||||||||
| 104 | |||||||||||
| 105 | sub copy | ||||||||||
| 106 | 34 | ▶ | { | ||||||||
|
Subroutine
copy: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 107 | 34 | my $window = shift; | |||||||||
| 108 | 34 | bless [ @$window ], ref $window | |||||||||
| 109 | } | ||||||||||
| 110 | |||||||||||
| 111 | |||||||||||
| 112 | sub offset | ||||||||||
| 113 | 8 | ▶ | { | ||||||||
|
Subroutine
offset: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||
| 114 | 8 | my($window, $offset) = @_; | |||||||||
| 115 | 8 | $window = copy $window; | |||||||||
| 116 | 8 | ▶ | empty $window and return $window; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 117 | |||||||||||
| 118 | 6 | $window->[0] += $offset; | |||||||||
| 119 | 6 | $window->[1] += $offset; | |||||||||
| 120 | 6 | $window | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | |||||||||||
| 124 | sub inset | ||||||||||
| 125 | 8 | ▶ | { | ||||||||
|
Subroutine
inset: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 126 | 8 | my($window, $inset) = @_; | |||||||||
| 127 | 8 | $window = copy $window; | |||||||||
| 128 | 8 | ▶ | empty $window and return $window; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 129 | |||||||||||
| 130 | 6 | $window->[0] += $inset; | |||||||||
| 131 | 6 | $window->[1] -= $inset; | |||||||||
| 132 | |||||||||||
| 133 | 6 | ▶ | empty $window and return empty (ref $window); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 134 | |||||||||||
| 135 | 4 | $window | |||||||||
| 136 | } | ||||||||||
| 137 | |||||||||||
| 138 | |||||||||||
| 139 | sub cover | ||||||||||
| 140 | 6 | ▶ | { | ||||||||
|
Subroutine
cover: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 141 | 6 | my(@windows) = grep { not empty $_ } @_; | |||||||||
| 142 | |||||||||||
| 143 | 6 | ▶ | @windows or return empty Set::Window; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 144 | |||||||||||
| 145 | 5 | my $window = shift @windows; | |||||||||
| 146 | 5 | my $cover = copy $window; | |||||||||
| 147 | |||||||||||
| 148 | 5 | for $window (@windows) | |||||||||
| 149 | { | ||||||||||
| 150 | 10 | ▶ | $cover->[0] > $window->[0] and $cover->[0] = $window->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 151 | 10 | ▶ | $cover->[1] < $window->[1] and $cover->[1] = $window->[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 152 | } | ||||||||||
| 153 | |||||||||||
| 154 | $cover | ||||||||||
| 155 | 5 | } | |||||||||
| 156 | |||||||||||
| 157 | |||||||||||
| 158 | sub intersect | ||||||||||
| 159 | 6 | ▶ | { | ||||||||
|
Subroutine
intersect: called CC=6 SCAR=17.9
Pod
documented
| |||||||||||
| 160 | 6 | my(@windows) = @_; | |||||||||
| 161 | |||||||||||
| 162 | 6 | ▶ | grep { empty $_ } @windows and return empty Set::Window; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 163 | |||||||||||
| 164 | 5 | my $window = shift @windows; | |||||||||
| 165 | 5 | my $core = copy $window; | |||||||||
| 166 | |||||||||||
| 167 | 5 | for $window (@windows) | |||||||||
| 168 | { | ||||||||||
| 169 | 10 | ▶ | $core->[0] < $window->[0] and $core->[0] = $window->[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 170 | 10 | ▶ | $core->[1] > $window->[1] and $core->[1] = $window->[1]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 171 | } | ||||||||||
| 172 | |||||||||||
| 173 | 5 | ▶ | empty $core and return empty Set::Window; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 174 | 4 | $core | |||||||||
| 175 | } | ||||||||||
| 176 | |||||||||||
| 177 | |||||||||||
| 178 | sub series | ||||||||||
| 179 | 20 | ▶ | { | ||||||||
|
Subroutine
series: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 180 | 20 | my($window, $length) = @_; | |||||||||
| 181 | 20 | ▶ | $length < 1 and return undef; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 182 | |||||||||||
| 183 | 18 | my($left, $right) = @$window; | |||||||||
| 184 | 18 | my @left = $left .. $right + 1 - $length; | |||||||||
| 185 | 18 | my $class = ref $window; | |||||||||
| 186 | 18 | my @series = map { $class->new_ll($_, $length) } @left; | |||||||||
| 187 | 18 | ▶ | wantarray ? @series : \@series | ||||||||
|
Branch: 1 branch
| |||||||||||
| 188 | } | ||||||||||
| 189 | |||||||||||
| 190 | 1 | 1 | |||||||||
| 191 | |||||||||||
| 192 | __END__ | ||||||||||