CircularList.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 8 / 8 Conditioncond 50.0% 3 / 6 MC/DCmcdc 33.3% 1 / 3 Subroutinesub 100% 12 / 12 Podpod 100% 3 / 3 totaltotal 93.9% 78 / 83 CC 7 SCAR 10.3
CC
7
Cov
95%
insert
6.1
iterator
2.0
BEGIN
1.0
CRAP 2.8
« CircularList/Cell.pm ↑ Summary CircularList/Iterator.pm »
1 package Data::CircularList;
2  
3 5 ▶use 5.006;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 5 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 5 ▶use warnings FATAL => 'all';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 5 ▶use Data::CircularList::Cell;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 5 ▶use Data::CircularList::Iterator;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 5 ▶use parent qw/Class::Accessor/;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 5 __PACKAGE__->mk_accessors(qw/header/);
10 5 ▶use Scalar::Util qw/blessed/;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 5 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 sub DEBUG() {0}; # {0} when done
13  
14 =head1 NAME
15  
16 Data::CircularList - simple implementation for using CircularList data structure.
17  
18 =head1 VERSION
19  
20 Version 0.03
21  
22 =cut
23  
24 5 our $VERSION = '0.03';
25  
26  
27 =head1 SYNOPSIS
28  
29 Quick summary of what the module does.
30  
31 Perhaps a little code snippet.
32  
33 use Data::CircularList;
34  
35 my $list = Data::CircularList->new;
36 $list->insert(20);
37 $list->insert(15);
38 $list->insert(18);
39 $list->insert(37);
40 $list->insert(3);
41  
42 # display
43 my $iter = $list->iterator;
44 while ($iter->has_next) {
45 print $iter->next->data . "\n";
46 }
47 # you can see result sorted
48 # 3
49 # 15
50 # 18
51 # 20
52 # 37
53 # 3 <= 1st value is displayed
54 # 15 eternal loop
55 ...
56
57 # rotate display
58 my $iter = $list->iterator( rotate => 2 );
59 while ($iter->has_next) {
60 print $iter->next->data . "\n";
61 }
62 # you can see result sorted
63 # 3
64 # 15
65 # 18
66 # 20
67 # 37
68 # 3
69 # 15
70 # 18
71 # 20
72 # 37 <= end. $iter->has_next return true until second rotation completed.
73
74 # you can also use strings as cells
75 $list = Data::CircularList->new;
76 $list->insert('steeve');
77 $list->insert('hisashi');
78 $list->insert('takairo');
79 $list->insert('kazuyo');
80 $list->insert('jane');
81
82 # display
83 $iter = $list->iterator;
84 while ($iter->has_next) {
85 print $iter->next->data . "\n";
86 }
87 # you can see result sorted
88 # hisashi
89 # jane
90 # kazuyo
91 # steeve
92 # takahiro
93 # hisashi <= 1st value is displayed
94 # jane eternal loop
95 ...
96  
97 # display
98 $iter = $list->iterator( rotate => 2 );
99 while ($iter->has_next) {
100 print $iter->next->data . "\n";
101 }
102 # you can see result sorted
103 # hisashi
104 # jane
105 # kazuyo
106 # steeve
107 # takahiro
108 # hisashi
109 # jane
110 # kazuyo
111 # steeve
112 # takahiro <= end. $iter->has_next return true until second rotation completed.
113  
114 # you can also use some object as cells
115 $list = Data::CircularList->new;
116 $list->insert(Person->new(name => 'lally'));
117 $list->insert(Person->new(name => 'hisashi'));
118 $list->insert(Person->new(name => 'takairo'));
119 $list->insert(Person->new(name => 'kazuyo'));
120 $list->insert(Person->new(name => 'jane'));
121
122 # you have to implements compare_to method in you object.
123 # you have to write sort logic in compare_to method.
124 package Person;
125  
126 sub new {
127 my $class = shift;
128 my %args = @_;
129 my $self = {
130 name => $args{'name'},
131 length => length($args{'name'}),
132 };
133 bless $self => $class;
134 $self->length(length($args{'name'}));
135 return $self;
136 }
137  
138 # sort by length of name, and name
139 sub compare_to {
140 my $self = shift;
141 my $cell = shift;
142  
143 if ($self->length > $cell->length) {
144 return 1;
145 } elsif ($self->length == $cell->length) {
146 return $self->name gt $cell->name ? 1 : 0;
147 } else {
148 return 0;
149 }
150 }
151  
152 sub name {
153 my $self = shift;
154 return defined $self->{'name'} ? $self->{'name'} : undef;
155 }
156  
157 sub length {
158 my $self = shift;
159 return defined $self->{'length'} ? $self->{'length'} : undef;
160 }
161  
162 =head1 SUBROUTINES/METHODS
163  
164 =head2 new
165  
166 constructor. Any arguments don't require.
167  
168 =cut
169  
170 7 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
171 7 my $class = shift;
172 7 my $self = {};
173 $self->{'header'} = Data::CircularList::Cell->new("!!Circular List Header!"),
174 7 $self->{'header'}->next($self->{'header'}),
175 bless $self => $class;
176 7 return $self;
177 }
178  
179 =head2 insert($cell)
180  
181 insert a cell on the circular linked list.
182 You can see SYNOPSIS as a example.
183  
184 =cut
185  
186 36 ▶sub insert {
Subroutine insert: called CC=6 SCAR=18.0
Pod documented
187 36 my $self = shift;
188 36 my $cell = shift;
189 36 $cell = Data::CircularList::Cell->new($cell);
190  
191 36 my $p = $self->header->next;
192 36 my $q = $self->header;
193  
194 36 ▶ if ($p ne $q) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p ne $q) 30 6 36
195 30 ▶ while (defined($p) && $p->can('data') && $cell->compare_to($p->data) > 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $p and $p->can("data") and $cell->compare_to($p->data) > 0) 37 23 60
Condition50%3/6
defined $p and $p->can("data") and $cell->compare_to($p->data) > 0
!ll&&!rl&&r
02337
defined $p and $p->can("data")
!ll&&!rl&&r
0060
MC/DC33%1/3
defined $p and $p->can("data") and $cell->compare_to($p->data) > 0
defined $p $p->can("data") $cell->compare_to($p->data) > 0
Truth table50%2/4
A and B and C
A= defined $pB= $p->can("data")C= $cell->compare_to($p->data) > 0
ABCresult
0XX0
10X0
1100
1111
196 37 $q = $p;
197 37 $p = $p->next;
198 37 ▶ last if !blessed($p->data);
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($p->data) 7 30 37
199 }
200 }
201  
202 36 my $new_cell = Data::CircularList::Cell->new($cell);
203 36 $new_cell->next($p);
204 36 $q->next($new_cell);
205 }
206  
207 =head2 iterator
208  
209 get a iterator to traverse the circular linked list.
210 You can see SYNOPSIS as a example.
211  
212 =cut
213  
214 6 ▶sub iterator {
Subroutine iterator: called CC=2 SCAR=6.9
Pod documented
215 6 my $self = shift;
216 6 my %args = @_;
217 6 ▶ my $rotate = defined $args{'rotate'} ? $args{'rotate'} : undef;
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'rotate'} ? : 3 3 6
218 6 my $iter = Data::CircularList::Iterator->new($self, $rotate);
219 6 return $iter;
220 }
221  
222 # free memory of cicular data
223 7 ▶sub DESTROY {
Subroutine DESTROY: called CC=1 SCAR=0.0
224 7 my $self = shift;
225 7 delete $self->{'header'};
226 7 if (DEBUG) {
227 carp "destroying $self\n";
228 }
229 }
230  
231 =head1 AUTHOR
232  
233 shinchit, C<< <shinchi.xx at gmail.com> >>
234  
235 =head1 BUGS
236  
237 Please report any bugs or feature requests to C<bug-datastructure-circularlist at rt.cpan.org>, or through
238 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Data-CircularList>. I will be notified, and then you'll
239 automatically be notified of progress on your bug as I make changes.
240  
241  
242  
243  
244 =head1 SUPPORT
245  
246 You can find documentation for this module with the perldoc command.
247  
248 perldoc Data::CircularList
249  
250  
251 You can also look for information at:
252  
253 =over 4
254  
255 =item * RT: CPAN's request tracker (report bugs here)
256  
257 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Data-CircularList>
258  
259 =item * AnnoCPAN: Annotated CPAN documentation
260  
261 L<http://annocpan.org/dist/Data-CircularList>
262  
263 =item * CPAN Ratings
264  
265 L<http://cpanratings.perl.org/d/Data-CircularList>
266  
267 =item * Search CPAN
268  
269 L<http://search.cpan.org/dist/Data-CircularList/>
270  
271 =back
272  
273  
274 =head1 ACKNOWLEDGEMENTS
275  
276  
277 =head1 LICENSE AND COPYRIGHT
278  
279 Copyright 2014 shinchit.
280  
281 This program is free software; you can redistribute it and/or modify it
282 under the terms of the the Artistic License (2.0). You may obtain a
283 copy of the full license at:
284  
285 L<http://www.perlfoundation.org/artistic_license_2_0>
286  
287 Any use, modification, and distribution of the Standard or Modified
288 Versions is governed by this Artistic License. By using, modifying or
289 distributing the Package, you accept this license. Do not use, modify,
290 or distribute the Package, if you do not accept this license.
291  
292 If your Modified Version has been derived from a Modified Version made
293 by someone other than you, you are nevertheless required to ensure that
294 your Modified Version complies with the requirements of this license.
295  
296 This license does not grant you the right to use any trademark, service
297 mark, tradename, or logo of the Copyright Holder.
298  
299 This license includes the non-exclusive, worldwide, free-of-charge
300 patent license to make, have made, use, offer to sell, sell, import and
301 otherwise transfer the Package with respect to any patent claims
302 licensable by the Copyright Holder that are necessarily infringed by the
303 Package. If you institute patent litigation (including a cross-claim or
304 counterclaim) against any party alleging that the Package constitutes
305 direct or contributory patent infringement, then this Artistic License
306 to you shall terminate on the date that such litigation is filed.
307  
308 Disclaimer of Warranty: THE PACKAGE IS PROVIDED BY THE COPYRIGHT HOLDER
309 AND CONTRIBUTORS "AS IS' AND WITHOUT ANY EXPRESS OR IMPLIED WARRANTIES.
310 THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
311 PURPOSE, OR NON-INFRINGEMENT ARE DISCLAIMED TO THE EXTENT PERMITTED BY
312 YOUR LOCAL LAW. UNLESS REQUIRED BY LAW, NO COPYRIGHT HOLDER OR
313 CONTRIBUTOR WILL BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, OR
314 CONSEQUENTIAL DAMAGES ARISING IN ANY WAY OUT OF THE USE OF THE PACKAGE,
315 EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
316  
317  
318 =cut
319  
320 5 1; # End of Data::CircularList
« CircularList/Cell.pm ↑ Summary CircularList/Iterator.pm »