| 1 | package AmberDB::Array; | ||
| 2 | |||
| 3 | use 5.016; | ||
| 4 | use warnings; | ||
| 5 | use Carp qw(croak cluck); | ||
| 6 | |||
| 7 | our $VERSION = '5.26.6'; | ||
| 8 | my $CREATED = '2018-02-23'; | ||
| 9 | |||
| 10 | # TODO | ||
| 11 | # ... | ||
| 12 | |||
| 13 | # my $Array = $run->use_module("AmberDB::Array"); | ||
| 14 | # ------------------------------------------------ | ||
| 15 | sub new { | ||
| 16 | |||
| 17 | my $class = shift; | ||
| 18 | |||
| 19 | my %input = @_; | ||
| 20 | |||
| 21 | # Load inputs. | ||
| 22 | my $self = {}; | ||
| 23 | foreach ( keys %input ) { | ||
| 24 | $self->{ uc($_) } = $input{$_}; | ||
| 25 | } | ||
| 26 | |||
| 27 | bless $self, $class; | ||
| 28 | return $self; | ||
| 29 | } | ||
| 30 | |||
| 31 | # my @list = qw/item1 item2 item3 item1 item2/; | ||
| 32 | # my @nodup = $Array->array_nodup(@list); | ||
| 33 | # deletes duplicates without breaking the list order | ||
| 34 | # ------------------------------------------------ | ||
| 35 | sub array_nodup { | ||
| 36 | |||
| 37 | my $self = shift; | ||
| 38 | |||
| 39 | # Fast path when passed an array reference: avoids Perl stack copying | ||
| 40 | if ( @_ == 1 && ref( $_[0] ) eq 'ARRAY' ) { | ||
| 41 | my %exist; | ||
| 42 | my @res = grep { defined($_) && $_ ne '' && !$exist{$_}++ } @{ $_[0] }; | ||
| 43 | return wantarray ? @res : \@res; | ||
| 44 | } | ||
| 45 | |||
| 46 | scalar @_ or return; | ||
| 47 | |||
| 48 | my %exist; | ||
| 49 | my @arrays = grep { defined($_) && $_ ne '' && !$exist{$_}++ } @_; | ||
| 50 | |||
| 51 | return @arrays; | ||
| 52 | } | ||
| 53 | |||
| 54 | # my $a1 = [ qw/a b c d e/ ]; | ||
| 55 | # my $a2 = [ qw/b c d f g/ ]; | ||
| 56 | # my $a3 = [ qw/c d h i/ ]; | ||
| 57 | # my $cropped = $Array->array_crop($a1, $a2, $a3); | ||
| 58 | # find intersection areas of lists (Shortest-List-First with early-exit)... | ||
| 59 | # ------------------------------------------------ | ||
| 60 | sub array_crop { | ||
| 61 | |||
| 62 | my ( $self, @arrays ) = @_; | ||
| 63 | |||
| 64 | scalar @arrays or return []; | ||
| 65 | |||
| 66 | # Ensure all elements are array references; if any is empty or not an ARRAY, intersection is empty | ||
| 67 | for my $arr (@arrays) { | ||
| 68 | return [] unless ref($arr) eq 'ARRAY' && @$arr; | ||
| 69 | } | ||
| 70 | |||
| 71 | if ( @arrays == 1 ) { | ||
| 72 | my %seen; | ||
| 73 | my @res = grep { defined($_) && $_ ne '' && !$seen{$_}++ } @{ $arrays[0] }; | ||
| 74 | return \@res; | ||
| 75 | } | ||
| 76 | |||
| 77 | # Shortest-List-First: Sort arrays by element count ascending | ||
| 78 | @arrays = sort { scalar(@$a) <=> scalar(@$b) } @arrays; | ||
| 79 | |||
| 80 | my $smallest = shift @arrays; | ||
| 81 | my %candidates; | ||
| 82 | foreach my $item (@$smallest) { | ||
| 83 | $candidates{$item} = 1 if defined $item && $item ne ''; | ||
| 84 | } | ||
| 85 | return [] unless %candidates; | ||
| 86 | |||
| 87 | # Sequentially filter candidates against subsequent arrays | ||
| 88 | foreach my $arr (@arrays) { | ||
| 89 | my %seen; | ||
| 90 | foreach my $item (@$arr) { | ||
| 91 | $seen{$item} = 1 if defined $item; | ||
| 92 | } | ||
| 93 | foreach my $cand ( keys %candidates ) { | ||
| 94 | delete $candidates{$cand} unless exists $seen{$cand}; | ||
| 95 | } | ||
| 96 | return [] unless %candidates; # Early exit if intersection is empty | ||
| 97 | } | ||
| 98 | |||
| 99 | # Reconstruct result maintaining order of the smallest array (deduplicated) | ||
| 100 | my %emitted; | ||
| 101 | my @result = grep { exists $candidates{$_} && !$emitted{$_}++ } @$smallest; | ||
| 102 | |||
| 103 | return \@result; | ||
| 104 | } | ||
| 105 | |||
| 106 | # my $l1 = [ qw/a b c/ ]; | ||
| 107 | # my $l2 = [ qw/c d e/ ]; | ||
| 108 | # my $l3 = [ qw/e f g/ ]; | ||
| 109 | # my $merged = $Array->array_add($l1, $l2, $l3); | ||
| 110 | # merges lists without duplicates... | ||
| 111 | # ------------------------------------------------ | ||
| 112 | sub array_add { | ||
| 113 | |||
| 114 | my @result; | ||
| 115 | my $exist = {}; | ||
| 116 | my ( $self, @arrays ) = @_; | ||
| 117 | |||
| 118 | scalar @arrays or return []; | ||
| 119 | |||
| 120 | foreach my $liste (@arrays) { | ||
| 121 | ref($liste) eq 'ARRAY' or next; | ||
| 122 | foreach my $list (@$liste) { | ||
| 123 | if ( $exist->{$list} ) { next } | ||
| 124 | $exist->{$list} = 1; | ||
| 125 | push @result, $list; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | |||
| 129 | return \@result; | ||
| 130 | } | ||
| 131 | |||
| 132 | # my $kalan = $Array->array_punch($liste1, $liste2, $liste3); | ||
| 133 | # subtracts items in subsequent lists from first list (single-pass)... | ||
| 134 | # ------------------------------------------------ | ||
| 135 | sub array_punch { | ||
| 136 | |||
| 137 | my ( $self, $array, @arrays ) = @_; | ||
| 138 | |||
| 139 | $array or return []; | ||
| 140 | ref($array) eq 'ARRAY' or return []; | ||
| 141 | |||
| 142 | my %exclude; | ||
| 143 | foreach my $liste (@arrays) { | ||
| 144 | next unless ref($liste) eq 'ARRAY'; | ||
| 145 | foreach my $part (@$liste) { | ||
| 146 | $exclude{$part} = 1 if defined $part; | ||
| 147 | } | ||
| 148 | } | ||
| 149 | |||
| 150 | my %seen; | ||
| 151 | my @result; | ||
| 152 | foreach my $part (@$array) { | ||
| 153 | next if !defined $part || exists $exclude{$part} || $seen{$part}++; | ||
| 154 | push @result, $part; | ||
| 155 | } | ||
| 156 | |||
| 157 | return \@result; | ||
| 158 | } | ||
| 159 | |||
| 160 | # my $l1 = [ qw/a b c d e/ ]; | ||
| 161 | # my $l2 = [ qw/b c/ ]; | ||
| 162 | # my $l3 = [ qw/d/ ]; | ||
| 163 | # my $diff = $Array->array_substr($l1, $l2, $l3); | ||
| 164 | # removes later ones from first list... | ||
| 165 | # ------------------------------------------------ | ||
| 166 | sub array_substr { | ||
| 167 | |||
| 168 | my ( $self, $array, @arrays ) = @_; | ||
| 169 | |||
| 170 | my @result; | ||
| 171 | my $exist = {}; | ||
| 172 | my $array1 = $array; | ||
| 173 | foreach my $field (@$array1) { | ||
| 174 | $exist->{$field} = 1; | ||
| 175 | } | ||
| 176 | foreach my $liste (@arrays) { | ||
| 177 | ref($liste) eq 'ARRAY' or next; | ||
| 178 | foreach my $field (@$liste) { | ||
| 179 | delete( $exist->{$field} ); | ||
| 180 | } | ||
| 181 | } | ||
| 182 | foreach my $field (@$array1) { | ||
| 183 | $exist->{$field} or next; | ||
| 184 | push @result, $field; | ||
| 185 | } | ||
| 186 | |||
| 187 | return \@result; | ||
| 188 | } | ||
| 189 | |||
| 190 | # to use perl code as a filter | ||
| 191 | # @arrays = $Array->array_filter($predicate, @arrays); | ||
| 192 | # $predicate can be a CODE reference (preferred) or a string (deprecated). | ||
| 193 | # ------------------------------------------------ | ||
| 194 | sub array_filter { | ||
| 195 | |||
| 196 | my ( $self, $predicate, @arrays ) = @_; | ||
| 197 | |||
| 198 | $predicate or return @arrays; | ||
| 199 | |||
| 200 | # CODE reference path: secure, fast, no eval overhead | ||
| 201 | if ( ref($predicate) eq 'CODE' ) { | ||
| 202 | return grep { $predicate->($_) } @arrays; | ||
| 203 | } else { | ||
| 204 | return @arrays; | ||
| 205 | } | ||
| 206 | } | ||
| 207 | |||
| 208 | # my $l1 = [ qw/a b c d e/ ]; | ||
| 209 | # my $sub = $Array->array_substrno($l1, 2, 4); | ||
| 210 | # removes given list entry numbers from the list... | ||
| 211 | # ------------------------------------------------ | ||
| 212 | sub array_substrno { | ||
| 213 | |||
| 214 | my ( $self, $arraydata, @arraydel ) = @_; | ||
| 215 | my %arraydel = map { $_ => 1 } @arraydel; | ||
| 216 | |||
| 217 | my @result; | ||
| 218 | for ( my $i = 0 ; $i < @$arraydata ; $i++ ) { | ||
| 219 | next if ( $arraydel{$i} ); | ||
| 220 | push @result, $arraydata->[$i]; | ||
| 221 | } | ||
| 222 | |||
| 223 | return \@result; | ||
| 224 | } | ||
| 225 | |||
| 226 | # TODO: check and test | ||
| 227 | # my $l1 = [ qw/a b c/ ]; | ||
| 228 | # my $l2 = [ qw/a b c/ ]; | ||
| 229 | # my $ok = $Array->array_compare($l1, $l2); | ||
| 230 | # compare two array... | ||
| 231 | # ------------------------------------------------ | ||
| 232 | sub array_compare { | ||
| 233 | |||
| 234 | my ( $self, $array1, $array2 ) = @_; | ||
| 235 | |||
| 236 | scalar @$array1 == scalar @$array2 or return 0; | ||
| 237 | |||
| 238 | for ( my $i = 0 ; $i < scalar @$array1 ; $i++ ) { | ||
| 239 | return 0 if ( $array1->[$i] ne $array2->[$i] ); | ||
| 240 | } | ||
| 241 | |||
| 242 | return 1; | ||
| 243 | } | ||
| 244 | |||
| 245 | # Finds the longest in sublists... | ||
| 246 | # my @new_list = $Array->array_sublist($deep, @records); | ||
| 247 | # ------------------------------------------------ | ||
| 248 | sub array_sublist { | ||
| 249 | |||
| 250 | my ( $self, $deep, @records ) = @_; | ||
| 251 | |||
| 252 | $deep =~ /^(2|3|4|6|12)$/ or $deep = 2; | ||
| 253 | |||
| 254 | my @result; | ||
| 255 | for ( my $i = 0 ; $i < @records ; $i += $deep ) { | ||
| 256 | my @line; | ||
| 257 | for ( my $j = 0 ; $j < $deep ; $j++ ) { | ||
| 258 | push @line, $records[ $i + $j ] // ""; | ||
| 259 | } | ||
| 260 | push @result, \@line; | ||
| 261 | } | ||
| 262 | |||
| 263 | return @result; | ||
| 264 | } | ||
| 265 | |||
| 266 | # gets the size of list matrix... | ||
| 267 | # ------------------------------------------------ | ||
| 268 | sub array_size { | ||
| 269 | |||
| 270 | my ( $self, @lines ) = @_; | ||
| 271 | |||
| 272 | my $max_line = scalar @lines; | ||
| 273 | $max_line or return 0; | ||
| 274 | |||
| 275 | my $max_blok = 1; | ||
| 276 | foreach my $line (@lines) { | ||
| 277 | ref $line eq "ARRAY" or next; | ||
| 278 | my $fld = scalar @$line; | ||
| 279 | $max_blok = $fld > $max_blok ? $fld : $max_blok; | ||
| 280 | } | ||
| 281 | |||
| 282 | return ( ( $max_line - 1 ), ( $max_blok - 1 ) ); | ||
| 283 | } | ||
| 284 | |||
| 285 | # my @picked = $adb->array_pick($indexes, @record); | ||
| 286 | # takes blocks at given index list to build a new list. | ||
| 287 | # ------------------------------------------------ | ||
| 288 | sub array_pick { | ||
| 289 | |||
| 290 | my ( $self, $indexes, @record ) = @_; | ||
| 291 | |||
| 292 | ref($indexes) eq 'ARRAY' or return (); | ||
| 293 | @record or return (); | ||
| 294 | |||
| 295 | return map { $record[$_] } @$indexes; | ||
| 296 | } | ||
| 297 | |||
| 298 | # my $array2 = $adb->deep_copy($array); | ||
| 299 | # ------------------------------------------------ | ||
| 300 | sub deep_copy { | ||
| 301 | my ( $self, $src ) = @_; | ||
| 302 | |||
| 303 | if (ref $src eq 'HASH') { | ||
| 304 | return { map { $_ => $self->deep_copy($src->{$_}) } keys %$src }; | ||
| 305 | } elsif (ref $src eq 'ARRAY') { | ||
| 306 | return [ map { $self->deep_copy($_) } @$src ]; | ||
| 307 | } else { | ||
| 308 | return $src; # scalar, undef, number etc. | ||
| 309 | } | ||
| 310 | } | ||
| 311 | |||
| 312 | # Non-destructively compares two hash references and returns key differences. | ||
| 313 | # my $diff = $adb->hash_diff($hash1, $hash2); | ||
| 314 | # Returns: { hash1 => { keys only in hash1 }, hash2 => { keys only in hash2 } } | ||
| 315 | # ------------------------------------------------ | ||
| 316 | sub hash_diff { | ||
| 317 | my ( $self, $hash1, $hash2 ) = @_; | ||
| 318 | |||
| 319 | $hash1 = {} unless defined $hash1 && ref($hash1) eq 'HASH'; | ||
| 320 | $hash2 = {} unless defined $hash2 && ref($hash2) eq 'HASH'; | ||
| 321 | |||
| 322 | my %h1 = %$hash1; | ||
| 323 | my %h2 = %$hash2; | ||
| 324 | |||
| 325 | foreach my $key ( keys %h1 ) { | ||
| 326 | if ( exists $h2{$key} ) { | ||
| 327 | delete $h1{$key}; | ||
| 328 | delete $h2{$key}; | ||
| 329 | } | ||
| 330 | } | ||
| 331 | |||
| 332 | my %diff; | ||
| 333 | $diff{hash1} = \%h1 if keys %h1; | ||
| 334 | $diff{hash2} = \%h2 if keys %h2; | ||
| 335 | |||
| 336 | return \%diff; | ||
| 337 | } | ||
| 338 | |||
| 339 | |||
| 340 | # shuffles the list order and creates a new list. | ||
| 341 | # ------------------------------------------------ | ||
| 342 | sub array_shuffle { | ||
| 343 | |||
| 344 | my ( $self, @array ) = @_; | ||
| 345 | for my $i ( reverse 1 .. $#array ) { | ||
| 346 | my $r = int rand( $i + 1 ); | ||
| 347 | @array[ $i, $r ] = @array[ $r, $i ]; | ||
| 348 | } | ||
| 349 | return @array; | ||
| 350 | } | ||
| 351 | |||
| 352 | # inverts the list matrix... | ||
| 353 | # ------------------------------------------------ | ||
| 354 | sub inverse_matrix { | ||
| 355 | |||
| 356 | my ( $self, @lines ) = @_; | ||
| 357 | |||
| 358 | my @result; | ||
| 359 | my ( $max_line, $max_blok ) = $self->array_size(@lines); | ||
| 360 | |||
| 361 | for my $i ( 0 .. $max_line ) { | ||
| 362 | if ( ref $lines[$i] ne "ARRAY" ) { | ||
| 363 | $lines[$i] = [ $lines[$i] ]; | ||
| 364 | } | ||
| 365 | for my $j ( 0 .. $max_blok ) { | ||
| 366 | $result[$j][$i] = $lines[$i][$j]; | ||
| 367 | } | ||
| 368 | } | ||
| 369 | |||
| 370 | return @result; | ||
| 371 | } | ||
| 372 | |||
| 373 | # Generalized array sorting function. | ||
| 374 | # my @sorted = $Array->array_sort($type, $direct, $field, @records); | ||
| 375 | # my @sorted = $Array->array_sort($type, $direct, $field, \@records); | ||
| 376 | # Parameters: | ||
| 377 | # $type: 'num' (numeric comparison <=>) or 'ascii' (string comparison cmp). Auto-detects if undef. | ||
| 378 | # $direct: 0 / 'asc' / undef (ascending) or 1 / 'desc' / 'reverse' / '-' (descending / reversed). | ||
| 379 | # $field: element column/block index (e.g. 0, 1, 2) when elements are ARRAY refs. If defined 0: compares $a->[0] and $b->[0]. | ||
| 380 | # @records: list of scalars or list of array references. | ||
| 381 | # ------------------------------------------------ | ||
| 382 | sub array_sort { | ||
| 383 | |||
| 384 | my ( $self, $type, $direct, $field, @records ) = @_; | ||
| 385 | |||
| 386 | # Support passing a single array reference of records | ||
| 387 | if ( @records == 1 && ref( $records[0] ) eq 'ARRAY' && !defined $field ) { | ||
| 388 | @records = @{ $records[0] }; | ||
| 389 | } | ||
| 390 | |||
| 391 | return () unless @records; | ||
| 392 | |||
| 393 | # Normalize direction: 1, 'desc', 'reverse', '-' => descending | ||
| 394 | my $is_desc = ( defined $direct && ( $direct eq '1' || lc($direct) eq 'desc' || lc($direct) eq 'reverse' || $direct eq '-' ) ) ? 1 : 0; | ||
| 395 | |||
| 396 | # Check if elements are ARRAY refs | ||
| 397 | my $is_aoa = ( ref( $records[0] ) eq 'ARRAY' ) ? 1 : 0; | ||
| 398 | |||
| 399 | # If field is not defined but elements are ARRAY refs, default field to 0 | ||
| 400 | if ( !defined $field && $is_aoa ) { | ||
| 401 | $field = 0; | ||
| 402 | } | ||
| 403 | |||
| 404 | # Normalize and auto-detect type | ||
| 405 | $type = lc( $type // '' ); | ||
| 406 | if ( !$type || $type eq 'auto' ) { | ||
| 407 | my $sample; | ||
| 408 | for my $r (@records) { | ||
| 409 | my $val = defined $field | ||
| 410 | ? ( ( ref($r) eq 'ARRAY' ) ? $r->[$field] : $r ) | ||
| 411 | : $r; | ||
| 412 | if ( defined $val && $val ne '' ) { | ||
| 413 | $sample = $val; | ||
| 414 | last; | ||
| 415 | } | ||
| 416 | } | ||
| 417 | $type = ( defined $sample && $sample =~ /^-?[0-9]+(?:[.,][0-9]+)?$/ ) ? 'num' : 'ascii'; | ||
| 418 | } | ||
| 419 | my $is_num = ( $type eq 'num' || $type eq 'numeric' || $type eq 'int' || $type eq 'float' || $type eq 'decimal' ) ? 1 : 0; | ||
| 420 | |||
| 421 | my $to_num = sub { | ||
| 422 | my $v = shift; | ||
| 423 | return 0 unless defined $v && length($v); | ||
| 424 | $v =~ tr/,/./; | ||
| 425 | $v =~ s/[^\d.-]//g; | ||
| 426 | return length($v) ? 0 + $v : 0; | ||
| 427 | }; | ||
| 428 | |||
| 429 | # Sorting execution | ||
| 430 | if ( defined $field && $is_aoa ) { | ||
| 431 | if ($is_num) { | ||
| 432 | @records = $is_desc | ||
| 433 | ? sort { $to_num->($b->[$field]) <=> $to_num->($a->[$field]) } @records | ||
| 434 | : sort { $to_num->($a->[$field]) <=> $to_num->($b->[$field]) } @records; | ||
| 435 | } | ||
| 436 | else { | ||
| 437 | @records = $is_desc | ||
| 438 | ? sort { ( $b->[$field] // '' ) cmp ( $a->[$field] // '' ) } @records | ||
| 439 | : sort { ( $a->[$field] // '' ) cmp ( $b->[$field] // '' ) } @records; | ||
| 440 | } | ||
| 441 | } | ||
| 442 | else { | ||
| 443 | if ($is_num) { | ||
| 444 | @records = $is_desc | ||
| 445 | ? sort { $to_num->($b) <=> $to_num->($a) } @records | ||
| 446 | : sort { $to_num->($a) <=> $to_num->($b) } @records; | ||
| 447 | } | ||
| 448 | else { | ||
| 449 | @records = $is_desc | ||
| 450 | ? sort { ( $b // '' ) cmp ( $a // '' ) } @records | ||
| 451 | : sort { ( $a // '' ) cmp ( $b // '' ) } @records; | ||
| 452 | } | ||
| 453 | } | ||
| 454 | |||
| 455 | return wantarray ? @records : \@records; | ||
| 456 | } | ||
| 457 | |||
| 458 | 1; | ||
| 459 | |||
| 460 | __END__ |