uHTML/ListFuncs.pm untested

filter: Statementstmt 0.0% 0 / 107 Branchbran 0.0% 0 / 42 Conditioncond 0.0% 0 / 18 Subroutinesub 0.0% 0 / 9 Podpod 0.0% 0 / 6 totaltotal 0.0% 0 / 182 CC 22 SCAR 37.3
CC
22
Cov
0%
section
90.0
section_old
42.0
difference
20.0
CRAP 41.7
1  
2  
3 #
4 # Author: Roland Mosler - Roland@Place.Ug
5 #
6 # Das ist eine uHTML-Bibliothek von Place.Ug
7 # Es ist erlaubt dieses Paket unter der aktuellen GNU LGPL zu nutzen
8 # Bei Weiterentwicklungen ist die Ursprungsbibliothek zu nennen
9 #
10 # Fehler und Verbesserungen bitte an uHTML@Place.Ug
11 #
12 # This is a uHTML library from Place.Ug
13 # It is allowed to use this library under the actual GNU LGPL
14 # The name of this library is to be named in all derivations
15 #
16 # Please report errors to uHTML@Place.Ug
17 #
18 # � Roland Mosler, Place.Ug
19 #
20  
21  
22  
23 use strict;
24  
25 package uHTML::ListFuncs ;
26  
27 use version ; our $VERSION = "0.81" ;
28  
29 require Exporter;
30 our @ISA = qw(Exporter);
31 our @EXPORT_OK = qw( uniq section difference shuffle sample ); # symbols to export on request
32  
33  
34 sub uniq
35 {
36 my( $old,@out ) ;
37  
38 foreach( @_ )
39 {
40 push @out,($old = $_) unless( $old eq $_ ) ;
41 }
42 return @out ;
43 }
44  
45 sub section_old
46 {
47 my( $n ) = $#_ ;
48  
49 return( ref $_[0] eq 'ARRAY' ? @{$_[0]} : () ) unless $n > 0 ;
50  
51 my( @T,@O,$old,$i ) ;
52  
53 foreach( @_ )
54 {
55 push @T,@{$_} if ref eq 'ARRAY' ;
56 }
57 $old = '' ;
58 foreach( sort @T )
59 {
60 if( $old eq $_ )
61 {
62 push @O,$_ unless --$i ;
63 }
64 else
65 {
66 $old = $_ ;
67 $i = $n ;
68 }
69 }
70 return @O ;
71 }
72  
73 sub section
74 {
75 my( $n ) = $#_ ;
76  
77 return( ref $_[0] eq 'ARRAY' ? @{$_[0]} : () ) unless $n > 0 ;
78  
79 my( @T,@P,@R,$i,$v,$c ) ;
80  
81 foreach( @_ )
82 {
83 next unless ref eq 'ARRAY' ;
84 return () unless @{$_} ;
85 my @A = uniq sort @{$_} ;
86 push @T,\@A ;
87 }
88 for( $v=$T[0]->[0]; @{$T[0]}; )
89 {
90 for( $i=0; $i<=$#T; $i++ )
91 {
92 while( $T[$i]->[0] lt $v )
93 {
94 shift @{$T[$i]} ;
95 return @R unless @{$T[$i]} ;
96 }
97 if( $T[$i]->[0] gt $v )
98 {
99 $v = $T[$i]->[0] ;
100 last ;
101 }
102 }
103 if( $i>$#T ) # all the same
104 {
105 push @R,$v ;
106 shift @{$T[0]} ;
107 return @R unless @{$T[0]} ;
108 $v = $T[0]->[0] ;
109 }
110 }
111 return @R ;
112 }
113  
114 sub difference( $$ )
115 {
116 return () unless ref $_[0] eq 'ARRAY' and @{$_[0]} ;
117 return @{$_[0]} unless ref $_[1] eq 'ARRAY' and @{$_[1]} ;
118 my @M = sort @{$_[0]} ;
119 my @S = sort @{$_[1]} ;
120 my( @O,$i,$j ) ;
121  
122 for( $i=$j=0; $i<=$#M; $i++ )
123 {
124 $j++ while( $j<=$#S and $M[$i] gt $S[$j] ) ;
125 push @O,$M[$i] unless $j<=$#S and $M[$i] eq $S[$j] ;
126 }
127 return @O ;
128 }
129  
130 sub shuffle
131 {
132 return unless ref(my $A = shift) eq 'ARRAY' ;
133  
134 my( $i,$k ) ;
135  
136 for( $k=$#{$A}; $k>0; $k-- )
137 {
138 ($i = int(rand($k+1))) < $k and ($A->[$k],$A->[$i]) = ($A->[$i],$A->[$k]) ;
139 }
140 return $A ;
141 }
142  
143 sub sample
144 {
145 my( $List,$length ) = @_ ;
146 return undef unless ref $List eq 'ARRAY' and $length > 0 ;
147 return $List unless $length < @{$List} ;
148  
149 my( $i,$j,$r,@SL ) ;
150  
151 $r = scalar(@{$List})/$length ;
152 $j = 0 ;
153 while( $j<@{$List} )
154 {
155 $i = $j ;
156 $j = int( $i + $r + 0.5 ) ;
157 push @SL,$List->[$i+1<$j ? int($i + rand($j - $i) + 0.5) : $i] ;
158 }
159 return \@SL ;
160 }
161  
162 1;
163  
164 __END__