File Coverage

blib/lib/Set/Window.pm
Criterion Covered Total %
statement 84 84 100.0
branch 41 42 97.6
condition n/a
subroutine 19 19 100.0
pod 16 16 100.0
total 160 161 99.3


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