| 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 GPL 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 GPL | ||
| 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 | ###################################### | ||
| 165 | ###################################### | ||
| 166 | |||
| 167 | =pod | ||
| 168 | |||
| 169 | =encoding UTF-8 | ||
| 170 | |||
| 171 | =head1 NAME | ||
| 172 | |||
| 173 | =over 2 | ||
| 174 | |||
| 175 | =item B<uHTML::ListFuncs> - More list functions for B<perl> | ||
| 176 | |||
| 177 | =back | ||
| 178 | |||
| 179 | =head1 VERSION | ||
| 180 | |||
| 181 | Version 0.81 | ||
| 182 | |||
| 183 | =head1 DESCRIPTION | ||
| 184 | |||
| 185 | This library provides the functions sample, shuffle, uniq, section and difference for perl. | ||
| 186 | This library is used by many of the B<uHTML> libraries. It can be of use without B<uHTML> as well. | ||
| 187 | |||
| 188 | |||
| 189 | |||
| 190 | B< > | ||
| 191 | |||
| 192 | =head1 perl functions provided by the B<hHTML::ListFuncs> library | ||
| 193 | |||
| 194 | B< > | ||
| 195 | |||
| 196 | |||
| 197 | |||
| 198 | =head2 uniq(@list) | ||
| 199 | |||
| 200 | =head3 Overview | ||
| 201 | |||
| 202 | The function uniq removes identical consecutive values from @list and returns the result, similar to uniq from GNU core utils. | ||
| 203 | |||
| 204 | =head3 Parameters | ||
| 205 | |||
| 206 | =head4 @list | ||
| 207 | |||
| 208 | List to process. | ||
| 209 | |||
| 210 | =head3 Example | ||
| 211 | |||
| 212 | foreach( uniq sort @list ) {...} | ||
| 213 | |||
| 214 | =head2 section($list1,$list2,$list3,...) | ||
| 215 | |||
| 216 | =head3 Overview | ||
| 217 | |||
| 218 | The function section computes the section of several lists. | ||
| 219 | |||
| 220 | =head3 Parameters | ||
| 221 | |||
| 222 | =head4 $list1,$list2,... | ||
| 223 | |||
| 224 | References of lists from which the section has to be computed. | ||
| 225 | |||
| 226 | =head3 Example | ||
| 227 | |||
| 228 | foreach( section( \@list1,\@list2 ) ) {...} | ||
| 229 | |||
| 230 | =head2 difference($list1,$list2) | ||
| 231 | |||
| 232 | =head3 Overview | ||
| 233 | |||
| 234 | The function difference removes from @{$list1} all elements contained in @{$list2}. | ||
| 235 | |||
| 236 | =head3 Parameters | ||
| 237 | |||
| 238 | =head4 $list1 | ||
| 239 | |||
| 240 | Reference of the original list from which elements will be removed. | ||
| 241 | |||
| 242 | =head4 $list2 | ||
| 243 | |||
| 244 | References of list with elements that will be removed from @{$list1}. | ||
| 245 | |||
| 246 | =head3 Example | ||
| 247 | |||
| 248 | foreach( difference( \@list1,\@list2 ) ) {...} | ||
| 249 | |||
| 250 | =head2 shuffle($list) | ||
| 251 | |||
| 252 | =head3 Overview | ||
| 253 | |||
| 254 | The function shuffle shuffles randomly @{$list}. | ||
| 255 | |||
| 256 | =head3 Parameters | ||
| 257 | |||
| 258 | =head4 $list | ||
| 259 | |||
| 260 | Reference of the list to be shuffled | ||
| 261 | |||
| 262 | =head3 Example | ||
| 263 | |||
| 264 | shuffle( \@list ) ; | ||
| 265 | |||
| 266 | =head2 sample($list,$count) | ||
| 267 | |||
| 268 | =head3 Overview | ||
| 269 | |||
| 270 | The function sample selects more or less evenly picked $count random elements from @{$list1}. The function returns a reference to a list with the samples. | ||
| 271 | |||
| 272 | =head3 Parameters | ||
| 273 | |||
| 274 | =head4 $list | ||
| 275 | |||
| 276 | Reference of the list from which elements will be picked. | ||
| 277 | |||
| 278 | =head4 $count | ||
| 279 | |||
| 280 | Count of elements to be picked from @{$list}. | ||
| 281 | |||
| 282 | =head3 Example | ||
| 283 | |||
| 284 | foreach( @{sample( \@list,$count )} ) {...} | ||
| 285 | |||
| 286 | |||
| 287 | |||
| 288 | |||
| 289 | =head1 SEE ALSO | ||
| 290 | |||
| 291 | perl(1), http://www.uhtml.de/en/doc/ListFuncs.uhtml | ||
| 292 | |||
| 293 | |||
| 294 | |||
| 295 | =head1 AUTHOR | ||
| 296 | |||
| 297 | Roland Mosler (Roland.Mosler@Place.Ug) | ||
| 298 | |||
| 299 | =head1 COPYRIGHT | ||
| 300 | |||
| 301 | Copyright 2009 Roland Mosler. All rights reserved. | ||
| 302 | |||
| 303 | This library is free software; you can redistribute it and/or | ||
| 304 | modify it under the same terms as Perl itself. | ||
| 305 | |||
| 306 |