Quiq/Array.pm

filter: Statementstmt 93.0% 215 / 231 Branchbran 81.8% 113 / 138 Conditioncond 68.2% 28 / 41 MC/DCmcdc 44.0% 11 / 25 Subroutinesub 97.3% 37 / 38 Podpod 100% 26 / 26 totaltotal 86.1% 430 / 499 CC 97 SCAR 18.6
CC
97
Cov
87%
shuffle
30.0
eq
13.0
__ANON__
12.4
CRAP 6.4
« Quiq/ImageMagick.pm ↑ Summary Quiq/Html/Widget/ReadOnly.pm »
1 # -----------------------------------------------------------------------------
2  
3 =encoding utf8
4  
5 =head1 NAME
6  
7 Quiq::Array - Operationen auf Arrays
8  
9 =head1 BASE CLASS
10  
11 L<Quiq::Object>
12  
13 =head1 DESCRIPTION
14  
15 Ein Objekt der Klasse repräsentiert ein Array. Jede der Methoden kann
16 sowohl auf ein Objekt der Klasse als auch als Klassenmethode
17 auf ein ungeblesstes oder eine andere Klasse geblesstes
18 Perl-Array angewendet werden.
19  
20 Aufruf als Objektmethode:
21  
22 $arr->$meth(...);
23  
24 Aufruf als Klassenmethode:
25  
26 $class->$meth(\@arr, ...);
27  
28 =cut
29  
30 # -----------------------------------------------------------------------------
31  
32 package Quiq::Array;
33 270 ▶use base qw/Quiq::Object/;
Subroutine BEGIN: called CC=1 SCAR=0.0
34  
35 270 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
36 270 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
37 270 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
38 270 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
39  
40 270 our $VERSION = '1.240';
41  
42 270 ▶use Encode ();
Subroutine BEGIN: called CC=1 SCAR=0.0
43 270 ▶use Quiq::Reference;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 270 ▶use Quiq::Math;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 # -----------------------------------------------------------------------------
47  
48 =head1 METHODS
49  
50 =head2 Konstruktor
51  
52 =head3 new() - Konstruktor
53  
54 =head4 Synopsis
55  
56 $arr = $class->new;
57 $arr = $class->new(\@arr);
58  
59 =head4 Description
60  
61 Instantiiere ein Array-Objekt und liefere eine Referenz auf dieses
62 Objekt zurück. Ohne Angabe einer Array-Referenz wird ein leeres
63 Array-Objekt instantiiert.
64  
65 =cut
66  
67 # -----------------------------------------------------------------------------
68  
69 76 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
70 76 my $class = shift;
71 76 ▶ my $arr = shift || [];
Condition100%2/2
shift() || []
l!l
3244
MC/DC100%1/1
shift() || []
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
72 76 return bless $arr,$class;
73 }
74  
75 # -----------------------------------------------------------------------------
76  
77 =head2 Operationen
78  
79 =head3 different() - Vergleiche Array gegen Array
80  
81 =head4 Synopsis
82  
83 ($only1A,$only2A,$bothA) = $arr1->different(\@arr2);
84 ($only1A,$only2A,$bothA) = $class->different(\@arr1,\@arr2);
85  
86 =head4 Alias
87  
88 compare()
89  
90 =head4 Description
91  
92 Vergleiche die Elemente der Arrays @$arr1 und @arr2 und liefere
93 Referenzen auf drei Arrays (Mengen) zurück:
94  
95 =over 4
96  
97 =item $only1A:
98  
99 Referenz auf die Liste der Elemente, die nur in @arr1
100 enthalten sind.
101  
102 =item $only2A:
103  
104 Referenz auf die Liste der Elemente, die nur in @arr2
105 enthalten sind.
106  
107 =item $bothA:
108  
109 Referenz auf die Liste der Elemente, die sowohl in @arr1
110 als auch in @arr2 enthalten sind.
111  
112 =back
113  
114 Die drei Ergebnislisten sind als Mengen zu sehen: Jedes Element taucht
115 in einer der drei Listen höchstens einmal auf, auch wenn es in den
116 Eingangslisten mehrfach vorkommt.
117  
118 Die gelieferten Arrays sind auf die Klasse geblesst.
119  
120 =head4 Example
121  
122 =over 2
123  
124 =item *
125  
126 Verwalte Objekte auf Datenbank
127  
128 Die Methode ist nützlich, wenn eine Menge von Objekten
129 auf einer Datenbank identisch zu einer Menge von Elementen
130 einer Benutzerauswahl gehalten werden soll. Die Objekte werden
131 durch ihre Objekt-Id identifiziert. Die Liste der
132 Datenbankobjekte sei @idsDb und die Liste der Objekte der
133 Benutzerauswahl sei @idsUser. Dann liefert der Aufruf
134  
135 ($idsNew,$idsDel) = $idsUserA->different(\@idsDb);
136  
137 mit @$idsNew die Liste der zur Datenbank hinzuzufügenden Objekte und
138 mit @$idsDel die Liste der von der Datenbank zu entfernenden Objekte.
139 Die Liste der identischen Objekte wird hier nicht benötigt.
140  
141 =item *
142  
143 Prüfe zwei Arrays auf Identiät
144  
145 Prüfe, ob zwei Arrays die gleichen Elemente enthalten, aber nicht
146 unbedingt in der gleichen Reihenfolge:
147  
148 ($only1,$only2) = $arr1->different(\@arr2);
149 if (!@$only1 && !@$only2) {
150 # @$arr1 und @$arr2 enthalten die gleichen Elemente
151 }
152  
153 =back
154  
155 =cut
156  
157 # -----------------------------------------------------------------------------
158  
159 4 ▶sub different {
Subroutine different: called CC=6 SCAR=17.9
Pod documented
160 4 my $arr1 = CORE::shift;
161 4 ▶ $arr1 = CORE::shift if !ref $arr1; # Klassenmethode
Branch: 1 branch
BranchTrueFalseTotal
unless ref $arr1 1 3 4
162 4 my $arr2 = CORE::shift;
163
164 # Hash mit den Elementen von @$arr1
165  
166 4 my (%arr1,%arr2);
167 4 @arr1{@$arr1} = (1) x @$arr1;
168 4 @arr2{@$arr2} = (1) x @$arr2;
169  
170 4 my (%seen,@arr1,@arr2,@arr);
171 4 for my $e (@$arr1,@$arr2) {
172 21 ▶ next if $seen{$e}++;
Branch: 1 branch
BranchTrueFalseTotal
if $seen{$e}++ 4 17 21
173 17 ▶ push @{$arr1{$e}? $arr2{$e}? \@arr: \@arr1: \@arr2},$e;
Branch: 2 branches
BranchTrueFalseTotal
$arr1{$e} ? : 8 9 17
$arr2{$e} ? : 4 4 8
174 }
175  
176 4 my $class = ref $arr1;
177  
178 return (
179 4 bless(\@arr1,$class),
180 bless(\@arr2,$class),
181 bless(\@arr,$class),
182 );
183 }
184  
185 {
186 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
187 270 *compare = \&different;
188 }
189  
190 # -----------------------------------------------------------------------------
191  
192 =head3 decode() - Dekodiere Array
193  
194 =head4 Synopsis
195  
196 $arr->decode($encoding);
197 $class->decode(\@arr,$encoding);
198  
199 =head4 Description
200  
201 Dekodiere die Elemente des Arrays gemäß Encoding $encoding.
202  
203 =cut
204  
205 # -----------------------------------------------------------------------------
206  
207 2 ▶sub decode {
Subroutine decode: called CC=2 SCAR=6.9
Pod documented
208 2 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 1 1 2
209 2 my $encoding = CORE::shift;
210  
211 2 @$arr = map {Encode::decode($encoding,$_)} @$arr;
212  
213 2 return;
214 }
215  
216 # -----------------------------------------------------------------------------
217  
218 =head3 exists() - Teste, ob Element existiert
219  
220 =head4 Synopsis
221  
222 $bool = $arr->exists($str);
223 $bool = $class->exists(\@arr,$str);
224  
225 =head4 Description
226  
227 Durchsuche $arr nach Element $str. Kommt $str in $arr vor,
228 liefere "wahr", sonst "falsch". Vergleichsoperator ist eq.
229  
230 =cut
231  
232 # -----------------------------------------------------------------------------
233  
234 5 ▶sub exists {
Subroutine exists: called CC=4 SCAR=13.9
Pod documented
235 5 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 1 5
236 5 my $str = CORE::shift;
237  
238 5 ▶ for (my $i = 0; $i < @$arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 29 1 30
239 29 ▶ if ($arr->[$i] eq $str) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$i] eq $str) 4 25 29
240 4 return 1;
241 }
242 }
243  
244 1 return 0;
245 }
246  
247 # -----------------------------------------------------------------------------
248  
249 =head3 extractKeyVal() - Extrahiere Paar, liefere Wert
250  
251 =head4 Synopsis
252  
253 $val = $arr->extractKeyVal($key);
254 $val = $arr->extractKeyVal($key,$step);
255 $val = $class->extractKeyVal(\@arr,$key);
256 $val = $class->extractKeyVal(\@arr,$key,$step);
257  
258 =head4 Alias
259  
260 extractPair()
261  
262 =head4 Description
263  
264 Durchsuche @arr nach Element $key und liefere das folgende
265 Element $val. Beide Elemente werden aus @arr entfernt. Kommt $key
266 in @arr nicht vor, liefere undef und lasse @arr unverändert.
267 Vergleichsoperator ist eq. Per Default wird das Array paarweise
268 durchsucht, d.h. der Defaultwert für $step ist 2. Wird $step auf
269 1 gesetzt, kann jedes Element den gesuchten $key enthalten.
270  
271 =cut
272  
273 # -----------------------------------------------------------------------------
274  
275 1 ▶sub extractKeyVal {
Subroutine extractKeyVal: called CC=5 SCAR=17.0
Pod documented
276 1 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 1 1
277 1 my $key = CORE::shift;
278 1 ▶ my $step = CORE::shift // 2;
Condition50%1/2
shift() // 2
l!l
01
MC/DC0%0/1
shift() // 2
shift()
Truth table50%1/2
A
A= shift()
Aresult
11
00
279  
280 1 ▶ for (my $i = 0; $i < @$arr; $i += $step) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 2 0 2
281 2 ▶ if ($arr->[$i] eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$i] eq $key) 1 1 2
282 1 return scalar CORE::splice @$arr,$i,2;
283 }
284 }
285  
286 0 return undef;
287 }
288  
289 {
290 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
291 270 *extractPair = \&extractKeyVal;
292 }
293  
294 # -----------------------------------------------------------------------------
295  
296 =head3 eq() - Vergleiche Arrays per eq
297  
298 =head4 Synopsis
299  
300 $bool = $arr->eq(\@arr);
301 $bool = $class->eq(\@arr1,\@arr2);
302  
303 =head4 Description
304  
305 Vergleiche @arr1 und @arr2 elementweise per eq. Liefere "wahr",
306 wenn alle Elemente identisch sind, andernfalls "falsch".
307  
308 Sind zwei Elemente undef, gelten sie als identisch.
309  
310 =cut
311  
312 # -----------------------------------------------------------------------------
313  
314 16 ▶sub eq {
Subroutine eq: called CC=9 SCAR=25.6
Pod documented
315 16 ▶ my $arr1 = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 15 1 16
316 16 my $arr2 = CORE::shift;
317  
318 16 ▶ return 0 if $#$arr1 != $#$arr2;
Branch: 1 branch
BranchTrueFalseTotal
if $arr1->$#* != $arr2->$#* 2 14 16
319  
320 14 my $l = @$arr1;
321 14 ▶ for (my $i = 0; $i < $l; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $l) 23 14 37
322 23 my $val1 = $arr1->[$i];
323 23 my $val2 = $arr2->[$i];
324  
325 # Wenn einer der Werte undefiniert ist,
326 # können wir nicht mit den normalen
327 # Operatoren vergleichen
328  
329 23 ▶ if (!defined $val1 || !defined $val2) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $val1 and defined $val2) 0 23 23
Condition33%1/3
defined $val1 and defined $val2
!ll&&!rl&&r
0023
MC/DC0%0/2
defined $val1 and defined $val2
defined $val1 defined $val2
Truth table33%1/3
A and B
A= defined $val1B= defined $val2
ABresult
0X0
100
111
330 0 ▶ return 0 if defined $val1 || defined $val2;
Branch: 1 branch
BranchTrueFalseTotal
if defined $val1 or defined $val2 0 0 0
Condition0%0/3
defined $val1 or defined $val2
l!l&&r!l&&!r
000
MC/DC0%0/2
defined $val1 or defined $val2
defined $val1 defined $val2
Truth table0%0/3
A or B
A= defined $val1B= defined $val2
ABresult
1X1
011
000
331 0 next;
332 }
333 23 ▶ return 0 if $val1 ne $val2;
Branch: 1 branch
BranchTrueFalseTotal
if $val1 ne $val2 0 23 23
334 }
335  
336 14 return 1;
337 }
338  
339 # -----------------------------------------------------------------------------
340  
341 =head3 findPairValue() - Liefere Wert zu Schlüssel
342  
343 =head4 Synopsis
344  
345 $val = $arr->findPairValue($key);
346 $val = $class->findPairValue(\@arr,$key);
347  
348 =head4 Returns
349  
350 Wert oder C<undef>
351  
352 =head4 Description
353  
354 Durchsuche $arr paarweise nach Element $key. Kommt es vor, liefere
355 dessen Wert. Kommt es nicht vor, liefere undef. Vergleichsoperator
356 ist eq.
357  
358 =cut
359  
360 # -----------------------------------------------------------------------------
361  
362 2 ▶sub findPairValue {
Subroutine findPairValue: called CC=4 SCAR=13.9
Pod documented
363 2 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
364 2 my $key = CORE::shift;
365  
366 2 ▶ for (my $i = 0; $i < @$arr; $i += 2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 5 1 6
367 5 ▶ if ($arr->[$i] eq $key) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$i] eq $key) 1 4 5
368 1 return $arr->[$i+1];
369 }
370 }
371  
372 1 return undef;
373 }
374  
375 # -----------------------------------------------------------------------------
376  
377 =head3 index() - Suche Element
378  
379 =head4 Synopsis
380  
381 $i = $arr->index($val);
382 $i = $class->index(\@arr,$val);
383  
384 =head4 Description
385  
386 Durchsuche @arr vom Anfang her nach Element $val und liefere
387 dessen Index zurück. Kommt $str in @arr nicht vor, liefere -1.
388 Vergleichsoperator ist eq.
389  
390 =cut
391  
392 # -----------------------------------------------------------------------------
393  
394 5 ▶sub index {
Subroutine index: called CC=4 SCAR=13.9
Pod documented
395 5 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 1 5
396 5 my $val = CORE::shift;
397  
398 5 ▶ for (my $i = 0; $i < @$arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 29 1 30
399 29 ▶ if ($arr->[$i] eq $val) {
Branch: 1 branch
BranchTrueFalseTotal
if ($arr->[$i] eq $val) 4 25 29
400 4 return $i;
401 }
402 }
403  
404 1 return -1;
405 }
406  
407 # -----------------------------------------------------------------------------
408  
409 =head3 last() - Liefere letztes Element
410  
411 =head4 Synopsis
412  
413 $e = $arr->last;
414 $e = $class->last(\@arr);
415  
416 =cut
417  
418 # -----------------------------------------------------------------------------
419  
420 3 ▶sub last {
Subroutine last: called CC=2 SCAR=6.9
Pod documented
421 3 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 1 3
422 3 return $arr->[-1];
423 }
424  
425 # -----------------------------------------------------------------------------
426  
427 =head3 maxLength() - Länge des längsten Elements
428  
429 =head4 Synopsis
430  
431 $l = $arr->maxLength;
432 $l = $class>maxLength(\@arr);
433  
434 =head4 Description
435  
436 Ermittele die Länge des längsten Arrayelements und liefere diese
437 zurück.
438  
439 =cut
440  
441 # -----------------------------------------------------------------------------
442  
443 3 ▶sub maxLength {
Subroutine maxLength: called CC=4 SCAR=13.9
Pod documented
444 3 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 1 3
445  
446 3 my $max = 0;
447 3 for (@$arr) {
448 10 my $l = length;
449 10 ▶ $max = $l if $l > $max;
Branch: 1 branch
BranchTrueFalseTotal
if $l > $max 4 6 10
450 }
451  
452 3 return $max;
453 }
454  
455 # -----------------------------------------------------------------------------
456  
457 =head3 pick() - Liefere Elemente nach Position heraus
458  
459 =head4 Synopsis
460  
461 $arr2 | @arr = $class->pick(\@arr,$n,$m);
462 $arr2 | @arr = $class->pick(\@arr,$n);
463 $arr2 | @arr = $arr->pick($n,$m);
464 $arr2 | @arr = $arr->pick($n);
465  
466 =head4 Description
467  
468 Picke jedes $n-te Array-Element ab Positon $m heraus, bilde aus diesen
469 Elementen ein neues Array und liefere dieses zurück.
470  
471 =cut
472  
473 # -----------------------------------------------------------------------------
474  
475 6 ▶sub pick {
Subroutine pick: called CC=6 SCAR=18.3
Pod documented
476 6 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 3 6
477 6 my $n = CORE::shift;
478 6 ▶ my $m = CORE::shift || 0;
Condition100%2/2
shift() || 0
l!l
24
MC/DC100%1/1
shift() || 0
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
479  
480 6 my @arr;
481 6 ▶ for (my $i = $m; $i < @$arr; $i += $n) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 14 6 20
482 14 CORE::push @arr,$arr->[$i];
483 }
484 6 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 0 6 6
485 0 return @arr;
486 }
487 6 ▶ if (my $class = ref $arr) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $class = ref $arr) 6 0 6
488 6 return bless \@arr,$class;
489 }
490  
491 0 return \@arr;
492 }
493  
494 # -----------------------------------------------------------------------------
495  
496 =head3 push() - Füge Element am Ende hinzu
497  
498 =head4 Synopsis
499  
500 $arr->push($e);
501 $class->push(\@arr,$e);
502  
503 =cut
504  
505 # -----------------------------------------------------------------------------
506  
507 2 ▶sub push {
Subroutine push: called CC=2 SCAR=7.0
Pod documented
508 2 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 0 2
509 2 my $e = CORE::shift;
510  
511 2 CORE::push @$arr,$e;
512  
513 2 return;
514 }
515  
516 # -----------------------------------------------------------------------------
517  
518 =head3 select() - Selektiere Array-Elemente
519  
520 =head4 Synopsis
521  
522 $arr2|@arr2 = $arr->select($test);
523 $arr2|@arr2 = $class->select(\@arr,$test);
524  
525 =head4 Description
526  
527 Wende Test $test auf alle Arrayelemente an und liefere ein Array mit
528 den Elementen zurück, die den Test erfüllen.
529  
530 Folgende Arten von Tests sind möglich:
531  
532 =over 4
533  
534 =item Regex qr/REGEX/
535  
536 Wende Regex-Test auf jedes Element an.
537  
538 =item Code-Referenz sub { CODE }
539  
540 Wende Subroutine-Test auf jedes Element an. Als erster
541 Parameter wird das zu testende Element übergeben. Die
542 Subroutine muss einen boolschen Wert liefern.
543  
544 =back
545  
546 =cut
547  
548 # -----------------------------------------------------------------------------
549  
550 4 ▶sub select {
Subroutine select: called CC=8 SCAR=20.8
Pod documented
551 4 my ($class,$arr) = Quiq::Object->this(CORE::shift); # Wir brauchen $class
552 4 ▶ $arr ||= CORE::shift;
Condition66%2/3
$arr ||= shift()
l!l&&r!l&&!r
220
MC/DC0%0/2
$arr ||= shift()
$arr shift()
Truth table66%2/3
A ||= B
A= $arrB= shift()
ABresult
1X1
011
000
553 4 my $test = CORE::shift;
554  
555 4 my @arr;
556 4 ▶ if (Quiq::Reference->isCodeRef($test)) {
Branch: 1 branch
BranchTrueFalseTotal
if ('Quiq::Reference'->isCodeRef($test)) { } 2 2 4
557 2 for (@$arr) {
558 8 ▶ CORE::push @arr,$_ if $test->($_);
Branch: 1 branch
BranchTrueFalseTotal
if &$test($_) 4 4 8
559 }
560 }
561 else {
562 2 for (@$arr) {
563 8 ▶ CORE::push @arr,$_ if /$test/;
Branch: 1 branch
BranchTrueFalseTotal
if /$test/ 4 4 8
564 }
565 }
566  
567 4 ▶ return wantarray? @arr: bless \@arr,$class;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 2 4
568 }
569  
570 # -----------------------------------------------------------------------------
571  
572 =head3 shuffle() - Verwürfele Array-Elemente
573  
574 =head4 Synopsis
575  
576 $arr->shuffle;
577 $arr->shuffle($factor);
578 $class->shuffle(\@arr);
579 $class->shuffle(\@arr,$factor);
580  
581 =head4 Arguments
582  
583 =over 4
584  
585 =item @arr
586  
587 Das zu mischende Array.
588  
589 =item $factor (Default: 100)
590  
591 Faktor für die Anzahl der Vertauschungsoperationen. Es werden
592 I<Arraygröße> * $factor Vertauschungsoperationen ausgeführt.
593  
594 =back
595  
596 =head4 Description
597  
598 Mische die Elemente des Array @arr, d.h. bringe sie in eine
599 zufällige Reihenfolge. Die Operation wird in-place ausgeführt.
600  
601 Die Methode liefert keinen Wert zurück.
602  
603 =cut
604  
605 # -----------------------------------------------------------------------------
606  
607 0 ▶sub shuffle {
Subroutine shuffle: not called CC=5 SCAR=34.0
Pod documented
608 0 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 0 0
609 0 ▶ my $factor = CORE::shift || 100;
Condition0%0/2
shift() || 100
l!l
00
MC/DC0%0/1
shift() || 100
shift()
Truth table0%0/2
A
A= shift()
Aresult
11
00
610  
611 0 my $size = @$arr;
612 0 ▶ for (my $i = 0; $i < $factor; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < $factor) 0 0 0
613 0 ▶ for (my $j = 0; $j < $size; $j++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($j < $size) 0 0 0
614 0 my $k = int rand $size;
615 0 @$arr[$k,$j] = @$arr[$j,$k];
616 }
617 }
618  
619 0 return;
620 }
621  
622 # -----------------------------------------------------------------------------
623  
624 =head3 sort() - Sortiere Elemente alphanumerisch
625  
626 =head4 Synopsis
627  
628 $arr | @arr = $arr->sort;
629 $arr | @arr = $class->sort(\@arr);
630  
631 =head4 Description
632  
633 Sortiere die Elemente des Array alphanumerisch.
634  
635 Im Skalar-Kontext sortiere die Elemente "in place" und liefere
636 die Array-Referenz zurück (Method-Chaining).
637  
638 Im List-Kontext liefere die Elemente sortiert zurück, ohne
639 den Inhalt des Array zu verändern.
640  
641 =cut
642  
643 # -----------------------------------------------------------------------------
644  
645 3 ▶sub sort {
Subroutine sort: called CC=3 SCAR=11.0
Pod documented
646 3 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 0 3
647  
648 3 ▶ if (wantarray) {
Branch: 1 branch
BranchTrueFalseTotal
if (wantarray) 1 2 3
649 1 return sort @$arr;
650 }
651  
652 2 @$arr = sort @$arr;
653 2 return $arr;
654 }
655  
656 # -----------------------------------------------------------------------------
657  
658 =head3 toHash() - Erzeuge Hash aus Array
659  
660 =head4 Synopsis
661  
662 %hash | $hashH = $arr->toHash;
663 %hash | $hashH = $arr->toHash($val);
664 %hash | $hashH = $class->toHash(\@arr);
665 %hash | $hashH = $class->toHash(\@arr,$val);
666  
667 =head4 Arguments
668  
669 =over 4
670  
671 =item @$arr, @arr
672  
673 Array.
674  
675 =item $val (Default: 1)
676  
677 Wert.
678  
679 =back
680  
681 =head4 Returns
682  
683 Hash. Im Skalarkontext wird eine Referenz auf den Hash geliefert.
684  
685 =head4 Description
686  
687 Erzeuge aus Array @$arr bzw. @arr einen Hash mit den Werten des Array
688 als Schlüssel und dem Wert $val als deren Werte und liefere diesen zurück.
689 Ist $val nicht angegeben, werden alle Werte des Hash auf 1 gesetzt.
690  
691 =cut
692  
693 # -----------------------------------------------------------------------------
694  
695 5 ▶sub toHash {
Subroutine toHash: called CC=4 SCAR=13.9
Pod documented
696 5 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 3 5
697 5 ▶ my $val = shift // 1;
Condition100%2/2
shift() // 1
l!l
41
MC/DC100%1/1
shift() // 1
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
698  
699 # FIXME: Ist das schneller?
700 # my %hash;
701 # @hash{@$arr} = ($val) x @$arr;
702  
703 5 my %hash = map {$_=>$val} @$arr;
704  
705 5 ▶ return wantarray? %hash: \%hash;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 2 3 5
706 }
707  
708 # -----------------------------------------------------------------------------
709  
710 =head2 Numerische Operationen
711  
712 =head3 gcd() - Größter gemeinsamer Teiler
713  
714 =head4 Synopsis
715  
716 $gcd = $arr->%METHOD;
717 $gcd = $class->gcd(\@arr);
718  
719 =head4 Description
720  
721 Berechne den größten gemeinsamen Teiler (greatest common divisor)
722 der natürlichen Zahlen in Array @$arr bzw. @arr und liefere diesen
723 zurück. Ist das Array leer, wird C<undef> geliefert. Enthält das
724 Array nur ein Element, wird dessen Wert geliefert.
725  
726 =cut
727  
728 # -----------------------------------------------------------------------------
729  
730 33 ▶sub gcd {
Subroutine gcd: called CC=4 SCAR=13.9
Pod documented
731 33 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 0 33 33
732  
733 33 my $gcd;
734 33 ▶ if (@$arr) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$arr) 32 1 33
735 32 $gcd = $arr->[0];
736 32 ▶ for (my $i = 1; $i < @$arr; $i++) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i < @$arr) 34 32 66
737 34 $gcd = Quiq::Math->gcd($gcd,$arr->[$i]);
738 }
739 }
740
741 33 return $gcd;
742 }
743  
744 # -----------------------------------------------------------------------------
745  
746 =head3 min() - Ermittele numerisches Minimum
747  
748 =head4 Synopsis
749  
750 $min = $arr->min;
751 $min = $class->min(\@arr);
752  
753 =head4 Description
754  
755 Ermittele die kleinste Zahl und liefere diese zurück. Enthält
756 $arr keine Elemente, liefere undef.
757  
758 =cut
759  
760 # -----------------------------------------------------------------------------
761  
762 13 ▶sub min {
Subroutine min: called CC=5 SCAR=16.1
Pod documented
763 13 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 11 2 13
764  
765 13 my $min;
766 13 for my $x (@$arr) {
767 93 ▶ $min = $x if !defined $min || $x < $min;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $x < $min 12 81 93
Condition66%2/3
not defined $min or $x < $min
l!l&&r!l&&!r
12081
MC/DC50%1/2
not defined $min or $x < $min
not defined $min $x < $min
Truth table66%2/3
not(A) or B
A= not defined $minB= $x < $min
ABresult
1X1
011
000
768 }
769  
770 13 return $min;
771 }
772  
773 # -----------------------------------------------------------------------------
774  
775 =head3 max() - Ermittele numerisches Maximum
776  
777 =head4 Synopsis
778  
779 $max = $arr->max;
780 $max = $class->max(\@arr);
781  
782 =head4 Description
783  
784 Ermittele die größte Zahl und liefere diese zurück. Enthält $arr
785 keine Elemente, liefere undef.
786  
787 =cut
788  
789 # -----------------------------------------------------------------------------
790  
791 11 ▶sub max {
Subroutine max: called CC=5 SCAR=16.1
Pod documented
792 11 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 1 11
793  
794 11 my $max;
795 11 for my $x (@$arr) {
796 90 ▶ $max = $x if !defined $max || $x > $max;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $x > $max 72 18 90
Condition100%3/3
not defined $max or $x > $max
l!l&&r!l&&!r
106218
MC/DC100%2/2
not defined $max or $x > $max
not defined $max $x > $max
Truth table100%3/3
not(A) or B
A= not defined $maxB= $x > $max
ABresult
1X1
011
000
797 }
798  
799 11 return $max;
800 }
801  
802 # -----------------------------------------------------------------------------
803  
804 =head3 minMax() - Ermittele numerisches Minimum und Maximum
805  
806 =head4 Synopsis
807  
808 ($min,$max) = $arr->minMax;
809 ($min,$max) = $class->minMax(\@arr);
810  
811 =head4 Description
812  
813 Ermittele die kleinste und die größte Zahl und liefere die beiden Werte
814 zurück. Enthält $arr keine Elemente, wird jeweils C<undef> geliefert.
815  
816 =cut
817  
818 # -----------------------------------------------------------------------------
819  
820 4 ▶sub minMax {
Subroutine minMax: called CC=7 SCAR=19.5
Pod documented
821 4 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 3 1 4
822  
823 4 my ($min,$max);
824 4 for my $x (@$arr) {
825 30 ▶ $min = $x if !defined $min || $x < $min;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $min or $x < $min 3 27 30
Condition66%2/3
not defined $min or $x < $min
l!l&&r!l&&!r
3027
MC/DC50%1/2
not defined $min or $x < $min
not defined $min $x < $min
Truth table66%2/3
not(A) or B
A= not defined $minB= $x < $min
ABresult
1X1
011
000
826 30 ▶ $max = $x if !defined $max || $x > $max;
Branch: 1 branch
BranchTrueFalseTotal
if not defined $max or $x > $max 12 18 30
Condition100%3/3
not defined $max or $x > $max
l!l&&r!l&&!r
3918
MC/DC100%2/2
not defined $max or $x > $max
not defined $max $x > $max
Truth table100%3/3
not(A) or B
A= not defined $maxB= $x > $max
ABresult
1X1
011
000
827 }
828  
829 4 return ($min,$max);
830 }
831  
832 # -----------------------------------------------------------------------------
833  
834 =head3 average() - Berechne Mittelwert
835  
836 =head4 Synopsis
837  
838 $x = $arr->average;
839 $x = $class->average(\@arr);
840  
841 =head4 Alias
842  
843 meanValue()
844  
845 =head4 Description
846  
847 Berechne das Arithmetische Mittel und liefere dieses
848 zurück. Enthält $arr keine Elemente, liefere undef.
849  
850 =cut
851  
852 # -----------------------------------------------------------------------------
853  
854 29 ▶sub average {
Subroutine average: called CC=4 SCAR=13.9
Pod documented
855 29 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 10 19 29
856  
857 29 ▶ return undef if !@$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 1 28 29
858  
859 28 my $sum = 0;
860 28 for my $x (@$arr) {
861 237 $sum += $x;
862 }
863  
864 28 return $sum/@$arr;
865 }
866  
867 {
868 270 ▶ no warnings 'once';
Subroutine BEGIN: called CC=1 SCAR=0.0
869 270 *meanValue = \&average;
870 }
871  
872 # -----------------------------------------------------------------------------
873  
874 =head3 standardDeviation() - Berechne Standardabweichung
875  
876 =head4 Synopsis
877  
878 $x = $arr->standardDeviation;
879 $x = $class->standardDeviation(\@arr);
880  
881 =head4 Description
882  
883 Berechne die Standardabweichung und liefere diese zurück. Enthält
884 $arr keine Elemente, liefere undef.
885  
886 =cut
887  
888 # -----------------------------------------------------------------------------
889  
890 18 ▶sub standardDeviation {
Subroutine standardDeviation: called CC=3 SCAR=11.0
Pod documented
891 18 my ($class,$arr) = Quiq::Object->this(CORE::shift); # Wir brauchen $class
892 18 ▶ $arr ||= CORE::shift;
Condition66%2/3
$arr ||= shift()
l!l&&r!l&&!r
1710
MC/DC0%0/2
$arr ||= shift()
$arr shift()
Truth table66%2/3
A ||= B
A= $arrB= shift()
ABresult
1X1
011
000
893  
894 18 ▶ return undef if !@$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 1 17 18
895 17 return sqrt $class->variance($arr);
896 }
897  
898 # -----------------------------------------------------------------------------
899  
900 =head3 variance() - Berechne Varianz
901  
902 =head4 Synopsis
903  
904 $x = $arr->variance;
905 $x = $class->variance(\@arr);
906  
907 =head4 Description
908  
909 Berechne die Varianz und liefere diese zurück. Enthält das Array
910 keine Elemente, liefere undef.
911  
912 =cut
913  
914 # -----------------------------------------------------------------------------
915  
916 21 ▶sub variance {
Subroutine variance: called CC=5 SCAR=16.1
Pod documented
917 21 my ($class,$arr) = Quiq::Object->this(CORE::shift); # Wir brauchen $class
918 21 ▶ $arr ||= CORE::shift;
Condition66%2/3
$arr ||= shift()
l!l&&r!l&&!r
3180
MC/DC0%0/2
$arr ||= shift()
$arr shift()
Truth table66%2/3
A ||= B
A= $arrB= shift()
ABresult
1X1
011
000
919  
920 21 ▶ return undef if !@$arr;
Branch: 1 branch
BranchTrueFalseTotal
unless @$arr 1 20 21
921 20 ▶ return 0 if @$arr == 1;
Branch: 1 branch
BranchTrueFalseTotal
if @$arr == 1 2 18 20
922  
923 18 my $meanVal = $class->meanValue($arr);
924  
925 18 my $sum = 0;
926 18 for my $x (@$arr) {
927 156 $sum += ($meanVal-$x)**2;
928 }
929  
930 18 return $sum/(@$arr-1);
931 }
932  
933 # -----------------------------------------------------------------------------
934  
935 =head3 median() - Ermittele den Median
936  
937 =head4 Synopsis
938  
939 $x = $arr->median;
940 $x = $class->median(\@arr);
941  
942 =head4 Description
943  
944 Ermittele den Median und liefere diesen zurück. Enthält das Array
945 keine Elemente, liefere undef.
946  
947 =cut
948  
949 # -----------------------------------------------------------------------------
950  
951 5 ▶sub median {
Subroutine median: called CC=4 SCAR=13.9
Pod documented
952 5 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 4 1 5
953  
954 5 my $size = @$arr;
955 5 ▶ if ($size == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size == 0) 1 4 5
956 # Array enthält keine Elemente
957 1 return undef;
958 }
959  
960 4 my @arr = sort {$a <=> $b} @$arr;
961 4 my $idx = int $size/2;
962 4 ▶ if ($size%2) {
Branch: 1 branch
BranchTrueFalseTotal
if ($size % 2) 3 1 4
963 # Ungerade Anzahl Elemente. Wir liefern das mittlere Element.
964 3 return $arr[$idx];
965 }
966  
967 # Gerade Anzahl Elemente. Wir liefern den Mittelwert
968 # der beiden mittleren Elemente.
969  
970 1 return ($arr[$idx-1]+$arr[$idx])/2;
971 }
972  
973 # -----------------------------------------------------------------------------
974  
975 =head2 Dump/Restore
976  
977 =head3 dump() - Erzeuge einzeilige, externe Repräsentation
978  
979 =head4 Synopsis
980  
981 $str = $arr->dump;
982 $str = $arr->dump($colSep);
983 $str = $class->dump(\@arr);
984 $str = $class->dump(\@arr,$colSep);
985  
986 =head4 Description
987  
988 Liefere eine einzeilige, externe Repräsentation für Array $arr bzw. @arr
989 im Format
990  
991 elem0|elem1|...|elemN
992  
993 Die Array-Elemente werden durch "|" (bzw. $colSep) getrennt. In den
994 Elementen werden folgende Wandlungen vorgenommen:
995  
996 undef -> '' (undef wird zu Leerstring)
997 \ -> \\ (Backslash wird verdoppelt)
998 $colSep -> \!
999 LF -> \n
1000 CR -> \r
1001  
1002 =cut
1003  
1004 # -----------------------------------------------------------------------------
1005  
1006 5 ▶sub dump {
Subroutine dump: called CC=7 SCAR=19.5
Pod documented
1007 5 ▶ my $arr = ref $_[0]? CORE::shift: CORE::splice @_,0,2;
Branch: 1 branch
BranchTrueFalseTotal
ref $_[0] ? : 2 3 5
1008 5 ▶ my $colSep = CORE::shift // '|';
Condition100%2/2
shift() // "|"
l!l
23
MC/DC100%1/1
shift() // "|"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1009  
1010 5 my $regex = qr/\Q$colSep/;
1011  
1012 5 my $str;
1013 5 for (@$arr) {
1014 19 ▶ if (!defined) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $_) 1 18 19
1015 1 $_ = '';
1016 }
1017  
1018 19 s/\\/\\\\/g;
1019 19 s/$regex/\\!/g;
1020 19 s/\n/\\n/g;
1021 19 s/\r/\\r/g;
1022  
1023 19 ▶ if (defined $str) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $str) 14 5 19
1024 14 $str .= $colSep;
1025 }
1026 19 $str .= $_;
1027 }
1028  
1029 5 ▶ return defined $str? $str: '';
Branch: 1 branch
BranchTrueFalseTotal
defined $str ? : 5 0 5
1030 }
1031  
1032 # -----------------------------------------------------------------------------
1033  
1034 =head3 restore() - Wandele einzeilige, externe Repräsentation in Array
1035  
1036 =head4 Synopsis
1037  
1038 $arr = $class->restore($str);
1039 $arr = $class->restore($str,$colSep);
1040  
1041 =head4 Description
1042  
1043 Wandele einzeilige, externe Array-Repräsentation (siehe Methode dump())
1044 in ein Array-Objekt und liefere dieses zurück.
1045  
1046 =cut
1047  
1048 # -----------------------------------------------------------------------------
1049  
1050 6 ▶sub restore {
Subroutine restore: called CC=3 SCAR=12.0
Pod documented
1051 6 my $class = CORE::shift;
1052 6 my $str = CORE::shift;
1053 6 ▶ my $colSep = CORE::shift // '|';
Condition100%2/2
shift() // "|"
l!l
33
MC/DC100%1/1
shift() // "|"
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
1054  
1055 2 ▶ my $f = sub {
Subroutine __ANON__: called CC=6 SCAR=25.2
1056 2 ▶ return '\\' if $_[0] eq '\\';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "\\" 0 2 2
1057 2 ▶ return $colSep if $_[0] eq '!';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "!" 1 1 2
1058 1 ▶ return "\n" if $_[0] eq 'n';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "n" 1 0 1
1059 0 ▶ return "\r" if $_[0] eq 'r';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "r" 0 0 0
1060 0 ▶ return "\x" if $_[0] eq 'x';
Branch: 1 branch
BranchTrueFalseTotal
if $_[0] eq "x" 0 0 0
1061  
1062 0 $class->throw(
1063 'ARR-00001: Inkorrekte Array-Repräsentation',
1064 EscapeSequence => "\\$_[0]",
1065 );
1066 6 };
1067  
1068 6 my @arr = split /\Q$colSep/,$str,-1;
1069 6 for (@arr) {
1070 16 s/\\(.)/$f->($1,$colSep)/ge;
1071 }
1072  
1073 6 return bless \@arr,$class;
1074 }
1075  
1076 # -----------------------------------------------------------------------------
1077  
1078 =head1 VERSION
1079  
1080 1.240
1081  
1082 =head1 AUTHOR
1083  
1084 Frank Seitz, L<http://fseitz.de/>
1085  
1086 =head1 COPYRIGHT
1087  
1088 Copyright (C) 2026 Frank Seitz
1089  
1090 =head1 LICENSE
1091  
1092 This code is free software; you can redistribute it and/or modify
1093 it under the same terms as Perl itself.
1094  
1095 =cut
1096  
1097 # -----------------------------------------------------------------------------
1098  
1099 270 1;
1100  
1101 # eof
« Quiq/ImageMagick.pm ↑ Summary Quiq/Html/Widget/ReadOnly.pm »