PML/List.pm

filter: Statementstmt 27.1% 28 / 103 Branchbran 10.0% 4 / 40 Conditioncond 0.0% 0 / 15 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 38.4% 10 / 26 Podpod 100% 20 / 20 totaltotal 28.9% 62 / 214 CC 26 SCAR 24.2
CC
26
Cov
20%
insert_list
30.0
delete
20.0
replace_list
20.0
CRAP 11.2
« PML/Schema/Constant.pm ↑ Summary PML.pm »
1  
2 ############################################################
3  
4 =head1 NAME
5  
6 Treex::PML::List - lists of uniformly typed PML values
7  
8 =head1 DESCRIPTION
9  
10 This class implements the attribute value type 'list'.
11  
12 =over 4
13  
14 =cut
15  
16 package Treex::PML::List;
17 9 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
18 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
19  
20 9 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
21 9 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
22 9 $VERSION='2.29'; # version template
23 }
24 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
25  
26 =item Treex::PML::List->new (val1,val2,...)
27  
28 Create a new list (optionally populated with given values).
29  
30 NOTE: Don't call this constructor directly, use Treex::PML::Factory->createList() instead!
31  
32 =cut
33  
34 55 ▶sub new {
Subroutine new: called CC=1 SCAR=0.0
Pod documented
35 55 my $class = shift;
36 55 return bless [@_],$class;
37 }
38  
39 =item Treex::PML::List->new_from_ref (array_ref, reuse)
40  
41 Create a new list consisting of values in a given array reference.
42 Use this constructor instead of new() to pass large lists by reference. If
43 reuse is true, then the same array_ref scalar is used to represent the
44 Treex::PML::List object (i.e. blessed). Otherwise, a copy is created in
45 the constructor.
46  
47 NOTE: Don't call this constructor directly, use Treex::PML::Factory->createList() instead!
48  
49 =cut
50  
51 16518 ▶sub new_from_ref {
Subroutine new_from_ref: called CC=3 SCAR=11.2
Pod documented
52 16518 my ($class,$array,$reuse) = @_;
53 16518 ▶ if ($reuse) {
Branch: 1 branch
BranchTrueFalseTotal
if ($reuse) { } 9854 6664 16518
54 9854 ▶ if (UNIVERSAL::isa($array,'ARRAY')) {
Branch: 1 branch
BranchTrueFalseTotal
if (UNIVERSAL::isa($array, 'ARRAY')) { } 9854 0 9854
55 9854 return bless $array,$class;
56 } else {
57 0 croak("Usage: new_from_ref(ARRAY_REF,1) - arg 1 is not an ARRAY reference!");
58 }
59 } else {
60 6664 return bless [@$array],$class;
61 }
62 }
63  
64 =item $list->values ()
65  
66 Returns all its values (i.e. the list members).
67  
68 =cut
69  
70 5 ▶sub values {
Subroutine values: called CC=1 SCAR=0.0
Pod documented
71 5 return @{$_[0]};
72 }
73  
74 =item $list->count ()
75  
76 Return number of values in the list.
77  
78 =cut
79  
80 0 ▶sub count {
Subroutine count: not called CC=1 SCAR=6.9
Pod documented
81 0 return scalar(@{$_[0]});
82 }
83  
84 =item $list->append (@values)
85  
86 Append given values to the list.
87  
88 =cut
89  
90 0 ▶sub append {
Subroutine append: not called CC=1 SCAR=6.9
Pod documented
91 0 my $self = shift;
92 0 CORE::push(@$self,@_);
93 0 return $self;
94 }
95 9 ▶BEGIN{
Subroutine BEGIN: called CC=1 SCAR=0.0
96 9 *push = \&append;
97 }
98  
99 =item $list->push (@values)
100  
101 An alias for C<$list->append()).
102  
103 =cut
104  
105  
106 =item $list->append_list ($list2)
107  
108 Append values from a given list or ARRAY-reference to the current list.
109  
110 =cut
111  
112 0 ▶sub append_list {
Subroutine append_list: not called CC=1 SCAR=6.9
Pod documented
113 0 my ($self, $list) = @_;
114 0 CORE::push(@$self,@$list);
115 0 return $self;
116 }
117  
118  
119 =item $list->insert ($index, @values)
120  
121 Insert values before the value at a given position in the list. The
122 index of the first position in the list is 0. It is an error if
123 $index is less then 0. If $index equals the index of the last
124 value + 1, then values are appended to the list, but it is an error if
125 $index is greater than that.
126  
127 =cut
128  
129 0 ▶sub insert {
Subroutine insert: not called CC=1 SCAR=6.9
Pod documented
130 0 my $self = shift;
131 0 my $pos = shift;
132 0 $self->insert_list($pos,\@_);
133 0 return $self;
134 }
135  
136 =item $list->insert_list ($index, $list)
137  
138 Insert all values in $list before the value at a given position in the
139 current list. The index of the first position in the current list is
140 0. It is an error if $index is less then 0. If $index equals
141 the index of the last value + 1, then values are appended to the list,
142 but it is an error if $index is greater than that.
143  
144 =cut
145  
146 0 ▶sub insert_list {
Subroutine insert_list: not called CC=5 SCAR=34.0
Pod documented
147 0 ▶ die 'Usage: Treex::PML::List->insert_list($index,$list) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
148 if @_!=3;
149 0 my ($self,$pos,$list) = @_;
150 0 ▶ die 'Treex::PML::List->insert: position out of bounds' if ($pos<0 or $pos>@$self);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos > @$self 0 0 0
Condition0%0/3
$pos < 0 or $pos > @$self
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos > @$self
$pos < 0 $pos > @$self
Truth table0%0/3
A or B
A= $pos < 0B= $pos > @$self
ABresult
1X1
011
000
151 0 ▶ if ($pos==@$self) {
Branch: 1 branch
BranchTrueFalseTotal
if ($pos == @$self) { } 0 0 0
152 0 CORE::push(@$self,@$list);
153 } else {
154 0 splice @$self,$pos,0,@$list;
155 }
156 0 return $self;
157 }
158  
159 =item $list->delete ($index, $count)
160  
161 Delete $count values from the list starting at index $index.
162  
163 =cut
164  
165 0 ▶sub delete {
Subroutine delete: not called CC=4 SCAR=30.0
Pod documented
166 0 ▶ die 'Usage: Treex::PML::List->delete($index,$count) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
167 if @_!=3;
168 0 my ($self,$pos,$count) = @_;
169 0 ▶ die 'Treex::PML::List->insert: position out of bounds' if ($pos<0 or $pos>=@$self);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos >= @$self 0 0 0
Condition0%0/3
$pos < 0 or $pos >= @$self
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos >= @$self
$pos < 0 $pos >= @$self
Truth table0%0/3
A or B
A= $pos < 0B= $pos >= @$self
ABresult
1X1
011
000
170 0 splice @$self,$pos,$count;
171 0 return $self;
172 }
173  
174 =item $list->delete_value ($value)
175  
176 Delete all occurences of value $value. Values are compared as strings.
177  
178 =cut
179  
180 0 ▶sub delete_value {
Subroutine delete_value: not called CC=2 SCAR=17.9
Pod documented
181 0 ▶ die 'Usage: Treex::PML::List->delete_value($value) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
182 if @_!=2;
183 0 my ($self,$value) = @_;
184 0 @$self = grep { $_ ne $value } @$self;
185 0 return $self;
186 }
187  
188 =item $list->delete_values ($value1,$value2,...)
189  
190 Delete all occurences of values $value1, $value2,... Values are
191 compared as strings.
192  
193 =cut
194  
195 0 ▶sub delete_values {
Subroutine delete_values: not called CC=1 SCAR=6.9
Pod documented
196 0 my $self = shift;
197 0 my %d; @d{@_} = ();
198 0 @$self = grep { !exists($d{$_}) } @$self;
199 0 return $self;
200 }
201  
202 =item $list->replace ($index, $count, @list)
203  
204 Replacing $count values starting at index $index by values provided
205 in the @list (the count of values in @list may differ from $count).
206  
207 =cut
208  
209 0 ▶sub replace {
Subroutine replace: not called CC=2 SCAR=17.9
Pod documented
210 0 ▶ die 'Usage: Treex::PML::List->replace($index,$count,@list) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
unless @_ >= 3 0 0 0
211 unless @_>=3;
212 0 my $self = shift;
213 0 my $pos = shift;
214 0 my $count = shift;
215 0 $self->replace_list($pos,\@_);
216 0 return $self;
217 }
218  
219 =item $list->replace_list ($index, $count, $list)
220  
221 Like replace, but replacement values are taken from a Treex::PML::List
222 object $list.
223  
224 =cut
225  
226 0 ▶sub replace_list {
Subroutine replace_list: not called CC=4 SCAR=30.0
Pod documented
227 0 my ($self,$pos,$count,$list)=@_;
228 0 ▶ die 'Usage: Treex::PML::List->replace_list($index,$count,$list) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 4 0 0 0
229 if @_!=4;
230 0 ▶ die 'Treex::PML::List->replace_list: position out of bounds' if ($pos<0 or $pos>=@$self);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos >= @$self 0 0 0
Condition0%0/3
$pos < 0 or $pos >= @$self
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos >= @$self
$pos < 0 $pos >= @$self
Truth table0%0/3
A or B
A= $pos < 0B= $pos >= @$self
ABresult
1X1
011
000
231 0 splice @$self,$pos,$count,@$list;
232 0 return $self;
233 }
234  
235 =item $list->value_at ($index)
236  
237 Return value at index $index. This is in fact the same as
238 $list->[$index] only $index is checked to be non-negative and less
239 then the index of the last value.
240  
241 =cut
242  
243 0 ▶sub value_at {
Subroutine value_at: not called CC=4 SCAR=30.0
Pod documented
244 0 my ($self,$pos)=@_;
245 0 ▶ die 'Usage: Treex::PML::List->value_at($index) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
246 if @_!=2;
247 0 ▶ die 'Treex::PML::List->value_at: position out of bounds' if ($pos<0 or $pos>=@$self);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos >= @$self 0 0 0
Condition0%0/3
$pos < 0 or $pos >= @$self
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos >= @$self
$pos < 0 $pos >= @$self
Truth table0%0/3
A or B
A= $pos < 0B= $pos >= @$self
ABresult
1X1
011
000
248 0 return $self->[$pos];
249 }
250  
251 =item $list->set_value_at ($index,$value)
252  
253 Set value at index $index to $value. This is in fact the same as
254 assigning directly to $list->[$index], except that $index is checked
255 to be non-negative and less then the index of the last value. Returns
256 $value.
257  
258 =cut
259  
260 0 ▶sub set_value_at {
Subroutine set_value_at: not called CC=4 SCAR=30.0
Pod documented
261 0 my ($self,$pos,$value)=@_;
262 0 ▶ die 'Usage: Treex::PML::List->set_value_at($index,$value) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 3 0 0 0
263 if @_!=3;
264 0 ▶ die 'Treex::PML::List->set_value_index: position out of bounds' if ($pos<0 or $pos>=@$self);
Branch: 1 branch
BranchTrueFalseTotal
if $pos < 0 or $pos >= @$self 0 0 0
Condition0%0/3
$pos < 0 or $pos >= @$self
l!l&&r!l&&!r
000
MC/DC0%0/2
$pos < 0 or $pos >= @$self
$pos < 0 $pos >= @$self
Truth table0%0/3
A or B
A= $pos < 0B= $pos >= @$self
ABresult
1X1
011
000
265 0 return $self->[$pos] = $value;
266 }
267  
268 =item $list->index_of ($value)
269  
270 Search the list for the first occurence of value $value. Returns index
271 of the first occurence or undef if the value is not in the
272 list. (Values are compared as strings.)
273  
274 =cut
275  
276 0 ▶sub index_of {
Subroutine index_of: not called CC=2 SCAR=17.9
Pod documented
277 0 my ($self,$value)=@_;
278 0 ▶ die 'Usage: Treex::PML::List->index_of($value) (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 2 0 0 0
279 if @_!=2;
280 0 return &Treex::PML::Index;
281 }
282  
283 =item $list->unique_values ()
284  
285 Return unique values in the list (ordered by the index of the first
286 occurence). Values are compared as strings.
287  
288 =cut
289  
290 0 ▶sub unique_values {
Subroutine unique_values: not called CC=2 SCAR=17.9
Pod documented
291 0 ▶ die 'Usage: Treex::PML::List->unique_values() (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
292 if @_!=1;
293 0 my $self = shift;
294 0 my %a;
295 0 return grep { !($a{$_}++) } @$self;
296 }
297  
298 =item $list->unique_list ()
299  
300 Return a new Treex::PML::List object consisting of unique values in the
301 current list (ordered by the index of the first occurence). Values
302 are compared as strings.
303  
304 =cut
305  
306 0 ▶sub unique_list {
Subroutine unique_list: not called CC=2 SCAR=17.9
Pod documented
307 0 ▶ die 'Usage: Treex::PML::List->unique_values() (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
308 if @_!=1;
309 0 my $self = shift;
310 0 my %a;
311 0 my $class = ref $self;
312 0 return $class->new_from_ref([grep { !($a{$_}++) } @$self],1);
313 }
314  
315  
316 =item $list->make_unique ()
317  
318 Remove duplicated values from the list. Values are compared as
319 strings. Returns $list.
320  
321 =cut
322  
323 0 ▶sub make_unique {
Subroutine make_unique: not called CC=2 SCAR=17.9
Pod documented
324 0 ▶ die 'Usage: Treex::PML::List->make_unique() (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 0 0
325 if @_!=1;
326 0 my $self = shift;
327 0 my %a; @$self = grep { !($a{$_}++) } @$self;
328 0 return $self;
329 }
330  
331  
332  
333 =item $list->empty ()
334  
335 Remove all values from the list.
336  
337 =cut
338  
339 1 ▶sub empty {
Subroutine empty: called CC=2 SCAR=7.0
Pod documented
340 1 ▶ die 'Usage: Treex::PML::List->empty() (wrong number of arguments!)'
Branch: 1 branch
BranchTrueFalseTotal
if @_ != 1 0 1 1
341 if @_!=1;
342 1 my $self = shift;
343 1 @$self=();
344 1 return $self;
345 }
346  
347  
348 =back
349  
350 =cut
351  
352 =head1 SEE ALSO
353  
354 L<Treex::PML>, L<Treex::PML::Factory>, L<Treex::PML::Schema>, L<Treex::PML::Alt>
355  
356 =head1 COPYRIGHT AND LICENSE
357  
358 Copyright (C) 2006-2010 by Petr Pajas, 2010-2024 Jan Stepanek
359  
360 This library is free software; you can redistribute it and/or modify
361 it under the same terms as Perl itself, either Perl version 5.8.2 or,
362 at your option, any later version of Perl 5 you may have available.
363  
364 =cut
365  
366  
367 9 1;
« PML/Schema/Constant.pm ↑ Summary PML.pm »