| 1 | package Sidef::Types::Array::Array; | ||
| 2 | |||
| 3 | use utf8; | ||
| 4 | use 5.016; | ||
| 5 | |||
| 6 | use List::Util qw(); | ||
| 7 | |||
| 8 | use parent qw( | ||
| 9 | Sidef::Object::Object | ||
| 10 | ); | ||
| 11 | |||
| 12 | use overload | ||
| 13 | q{""} => \&_dump, | ||
| 14 | q{0+} => sub { scalar(@{$_[0]}) }, | ||
| 15 | q{bool} => sub { scalar(@{$_[0]}) }; | ||
| 16 | |||
| 17 | use Sidef::Types::Number::Number; | ||
| 18 | use Sidef::Types::Block::Block; | ||
| 19 | |||
| 20 | sub new { | ||
| 21 | (@_ == 2 && ref($_[1]) eq 'ARRAY') | ||
| 22 | ? bless($_[1]) | ||
| 23 | : do { | ||
| 24 | shift(@_); | ||
| 25 | bless [@_]; | ||
| 26 | }; | ||
| 27 | } | ||
| 28 | |||
| 29 | *call = \&new; | ||
| 30 | |||
| 31 | sub get_value { | ||
| 32 | my %addr; | ||
| 33 | |||
| 34 | my $sub = sub { | ||
| 35 | my ($obj) = @_; | ||
| 36 | |||
| 37 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 38 | |||
| 39 | exists($addr{$refaddr}) | ||
| 40 | && return $addr{$refaddr}; | ||
| 41 | |||
| 42 | my @array; | ||
| 43 | $addr{$refaddr} = \@array; | ||
| 44 | |||
| 45 | foreach my $item (@$obj) { | ||
| 46 | if (CORE::index(ref($item), 'Sidef::') == 0) { | ||
| 47 | CORE::push(@array, $item->get_value); | ||
| 48 | } | ||
| 49 | else { | ||
| 50 | CORE::push(@array, $item); | ||
| 51 | } | ||
| 52 | } | ||
| 53 | |||
| 54 | $addr{$refaddr}; | ||
| 55 | }; | ||
| 56 | |||
| 57 | no warnings 'redefine'; | ||
| 58 | local *Sidef::Types::Array::Array::get_value = $sub; | ||
| 59 | $sub->($_[0]); | ||
| 60 | } | ||
| 61 | |||
| 62 | sub unroll_operator { | ||
| 63 | my ($self, $operator, $arg) = @_; | ||
| 64 | |||
| 65 | if (ref($arg) ne ref($self)) { | ||
| 66 | $arg = $arg->to_a; | ||
| 67 | } | ||
| 68 | |||
| 69 | $operator = "$operator" if ref($operator); | ||
| 70 | |||
| 71 | my @array; | ||
| 72 | |||
| 73 | my @arg = @$arg; | ||
| 74 | my @self = @$self; | ||
| 75 | |||
| 76 | (my $argc = @arg) || return bless(\@self, ref($self)); | ||
| 77 | my $selfc = @self; | ||
| 78 | |||
| 79 | my $max = $argc > $selfc ? $argc - 1 : $selfc - 1; | ||
| 80 | |||
| 81 | foreach my $i (0 .. $max) { | ||
| 82 | CORE::push(@array, $self[$i % $selfc]->$operator($arg[$i % $argc])); | ||
| 83 | } | ||
| 84 | |||
| 85 | bless \@array, ref($self); | ||
| 86 | } | ||
| 87 | |||
| 88 | *unroll_op = \&unroll_operator; | ||
| 89 | |||
| 90 | sub map_operator { | ||
| 91 | my ($self, $operator, @args) = @_; | ||
| 92 | |||
| 93 | $operator = "$operator" if ref($operator); | ||
| 94 | |||
| 95 | my @array; | ||
| 96 | foreach my $i (0 .. $#$self) { | ||
| 97 | CORE::push(@array, $self->[$i]->$operator(@args)); | ||
| 98 | } | ||
| 99 | |||
| 100 | bless \@array, ref($self); | ||
| 101 | } | ||
| 102 | |||
| 103 | *map_op = \&map_operator; | ||
| 104 | |||
| 105 | sub pam_operator { | ||
| 106 | my ($self, $operator, $arg) = @_; | ||
| 107 | |||
| 108 | $operator = "$operator" if ref($operator); | ||
| 109 | |||
| 110 | my @array; | ||
| 111 | foreach my $i (0 .. $#$self) { | ||
| 112 | CORE::push(@array, $arg->$operator($self->[$i])); | ||
| 113 | } | ||
| 114 | |||
| 115 | bless \@array, ref($self); | ||
| 116 | } | ||
| 117 | |||
| 118 | *pam_op = \&pam_operator; | ||
| 119 | |||
| 120 | sub reduce_operator { | ||
| 121 | my ($self, $operator, $initial) = @_; | ||
| 122 | |||
| 123 | $operator = "$operator" if ref($operator); | ||
| 124 | |||
| 125 | my ($from, $x) = ( | ||
| 126 | defined($initial) | ||
| 127 | ? (0, $initial) | ||
| 128 | : (1, $self->[0]) | ||
| 129 | ); | ||
| 130 | |||
| 131 | foreach my $i ($from .. $#$self) { | ||
| 132 | $x = $x->$operator($self->[$i]); | ||
| 133 | } | ||
| 134 | $x; | ||
| 135 | } | ||
| 136 | |||
| 137 | *reduce_op = \&reduce_operator; | ||
| 138 | |||
| 139 | sub cross_operator { | ||
| 140 | my ($self, $operator, $arg) = @_; | ||
| 141 | |||
| 142 | if (ref($arg) ne ref($self)) { | ||
| 143 | $arg = $arg->to_a; | ||
| 144 | } | ||
| 145 | |||
| 146 | $operator = "$operator" if ref($operator); | ||
| 147 | |||
| 148 | my @arg = @$arg; | ||
| 149 | |||
| 150 | my @array; | ||
| 151 | if ($operator eq '') { | ||
| 152 | foreach my $i (@$self) { | ||
| 153 | foreach my $j (@arg) { | ||
| 154 | CORE::push(@array, bless [$i, $j]); | ||
| 155 | } | ||
| 156 | } | ||
| 157 | } | ||
| 158 | else { | ||
| 159 | foreach my $i (@$self) { | ||
| 160 | foreach my $j (@arg) { | ||
| 161 | CORE::push(@array, $i->$operator($j)); | ||
| 162 | } | ||
| 163 | } | ||
| 164 | } | ||
| 165 | |||
| 166 | bless \@array, ref($self); | ||
| 167 | } | ||
| 168 | |||
| 169 | *cross_op = \&cross_operator; | ||
| 170 | |||
| 171 | sub zip_operator { | ||
| 172 | my ($self, $operator, $arg) = @_; | ||
| 173 | |||
| 174 | if (ref($arg) ne ref($self)) { | ||
| 175 | $arg = $arg->to_a; | ||
| 176 | } | ||
| 177 | |||
| 178 | $operator = "$operator" if ref($operator); | ||
| 179 | |||
| 180 | my @arg = @$arg; | ||
| 181 | my @self = @$self; | ||
| 182 | |||
| 183 | my $self_len = $#self; | ||
| 184 | my $arg_len = $#arg; | ||
| 185 | my $min = $self_len < $arg_len ? $self_len : $arg_len; | ||
| 186 | |||
| 187 | my @array; | ||
| 188 | if ($operator eq '') { | ||
| 189 | foreach my $i (0 .. $min) { | ||
| 190 | CORE::push(@array, bless [$self[$i], $arg[$i]]); | ||
| 191 | } | ||
| 192 | } | ||
| 193 | else { | ||
| 194 | foreach my $i (0 .. $min) { | ||
| 195 | CORE::push(@array, $self[$i]->$operator($arg[$i])); | ||
| 196 | } | ||
| 197 | } | ||
| 198 | |||
| 199 | bless \@array, ref($self); | ||
| 200 | } | ||
| 201 | |||
| 202 | *zip_op = \&zip_operator; | ||
| 203 | |||
| 204 | sub scalar_operator { | ||
| 205 | my ($self, $operator, $scalar) = @_; | ||
| 206 | |||
| 207 | $operator = "$operator" if ref($operator); | ||
| 208 | |||
| 209 | my %addr; # support for cyclic references | ||
| 210 | |||
| 211 | sub { | ||
| 212 | my ($obj) = @_; | ||
| 213 | |||
| 214 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 215 | |||
| 216 | exists($addr{$refaddr}) | ||
| 217 | && return $addr{$refaddr}; | ||
| 218 | |||
| 219 | my @array; | ||
| 220 | $addr{$refaddr} = bless(\@array, ref($obj)); | ||
| 221 | |||
| 222 | foreach my $item (@$obj) { | ||
| 223 | if (ref($item) eq __PACKAGE__ or UNIVERSAL::isa($item, __PACKAGE__)) { | ||
| 224 | CORE::push(@array, __SUB__->($item)); | ||
| 225 | } | ||
| 226 | else { | ||
| 227 | if ($operator eq '') { | ||
| 228 | CORE::push(@array, bless [$item, $scalar]); | ||
| 229 | } | ||
| 230 | else { | ||
| 231 | CORE::push(@array, $item->$operator($scalar)); | ||
| 232 | } | ||
| 233 | } | ||
| 234 | } | ||
| 235 | |||
| 236 | $addr{$refaddr}; | ||
| 237 | } | ||
| 238 | ->($self); | ||
| 239 | } | ||
| 240 | |||
| 241 | *scalar_op = \&scalar_operator; | ||
| 242 | |||
| 243 | sub rscalar_operator { | ||
| 244 | my ($self, $operator, $scalar) = @_; | ||
| 245 | |||
| 246 | $operator = "$operator" if ref($operator); | ||
| 247 | |||
| 248 | my %addr; # support for cyclic references | ||
| 249 | |||
| 250 | sub { | ||
| 251 | my ($obj) = @_; | ||
| 252 | |||
| 253 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 254 | |||
| 255 | exists($addr{$refaddr}) | ||
| 256 | && return $addr{$refaddr}; | ||
| 257 | |||
| 258 | my @array; | ||
| 259 | $addr{$refaddr} = bless(\@array, ref($self)); | ||
| 260 | |||
| 261 | foreach my $item (@$obj) { | ||
| 262 | if (ref($item) eq __PACKAGE__ or UNIVERSAL::isa($item, __PACKAGE__)) { | ||
| 263 | CORE::push(@array, __SUB__->($item)); | ||
| 264 | } | ||
| 265 | else { | ||
| 266 | if ($operator eq '') { | ||
| 267 | CORE::push(@array, bless [$scalar, $item]); | ||
| 268 | } | ||
| 269 | else { | ||
| 270 | CORE::push(@array, $scalar->$operator($item)); | ||
| 271 | } | ||
| 272 | } | ||
| 273 | } | ||
| 274 | |||
| 275 | $addr{$refaddr}; | ||
| 276 | } | ||
| 277 | ->($self); | ||
| 278 | } | ||
| 279 | |||
| 280 | *rscalar_op = \&rscalar_operator; | ||
| 281 | |||
| 282 | sub wise_operator { | ||
| 283 | my ($m1, $operator, $m2) = @_; | ||
| 284 | |||
| 285 | if (ref($m2) ne ref($m1)) { | ||
| 286 | $m2 = $m2->to_a; | ||
| 287 | } | ||
| 288 | |||
| 289 | $operator = "$operator" if ref($operator); | ||
| 290 | |||
| 291 | my %addr; # support for cyclic references | ||
| 292 | |||
| 293 | sub { | ||
| 294 | my ($obj1, $obj2) = @_; | ||
| 295 | |||
| 296 | my $refaddr1 = Scalar::Util::refaddr($obj1); | ||
| 297 | |||
| 298 | exists($addr{$refaddr1}) | ||
| 299 | && return $addr{$refaddr1}; | ||
| 300 | |||
| 301 | my @array; | ||
| 302 | |||
| 303 | $addr{$refaddr1} = bless(\@array, ref($obj1)); | ||
| 304 | |||
| 305 | for my $i (0 .. $#{$obj1}) { | ||
| 306 | if (ref($obj1->[$i]) eq __PACKAGE__ or UNIVERSAL::isa($obj1->[$i], __PACKAGE__)) { | ||
| 307 | CORE::push(@array, __SUB__->($obj1->[$i], $obj2->[$i])); | ||
| 308 | } | ||
| 309 | else { | ||
| 310 | if ($operator eq '') { | ||
| 311 | CORE::push(@array, bless [$obj1->[$i], $obj2->[$i]]); | ||
| 312 | } | ||
| 313 | else { | ||
| 314 | CORE::push(@array, $obj1->[$i]->$operator($obj2->[$i])); | ||
| 315 | } | ||
| 316 | } | ||
| 317 | } | ||
| 318 | |||
| 319 | $addr{$refaddr1}; | ||
| 320 | } | ||
| 321 | ->($m1, $m2); | ||
| 322 | } | ||
| 323 | |||
| 324 | *wise_op = \&wise_operator; | ||
| 325 | |||
| 326 | sub combine { | ||
| 327 | my ($self, $block) = @_; | ||
| 328 | |||
| 329 | my %addr; # support for cyclic references | ||
| 330 | |||
| 331 | sub { | ||
| 332 | my (@arrays) = @_; | ||
| 333 | |||
| 334 | my @array; | ||
| 335 | my $blessed_array = bless \@array, ref($self); | ||
| 336 | |||
| 337 | # Check any references already computed | ||
| 338 | foreach my $obj (@arrays) { | ||
| 339 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 340 | exists($addr{$refaddr}) && return $addr{$refaddr}; | ||
| 341 | } | ||
| 342 | |||
| 343 | # Store the references of each object | ||
| 344 | foreach my $obj (@arrays) { | ||
| 345 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 346 | $addr{$refaddr} = $blessed_array; | ||
| 347 | } | ||
| 348 | |||
| 349 | @arrays || return $blessed_array; | ||
| 350 | |||
| 351 | my $first = $arrays[0]; | ||
| 352 | |||
| 353 | foreach my $i (0 .. $#{$first}) { | ||
| 354 | if (ref($first->[$i]) eq __PACKAGE__ or UNIVERSAL::isa($first->[$i], __PACKAGE__)) { | ||
| 355 | CORE::push(@array, __SUB__->(map { $_->[$i] } @arrays)); | ||
| 356 | } | ||
| 357 | else { | ||
| 358 | CORE::push(@array, $block->run(map { $_->[$i] } @arrays)); | ||
| 359 | } | ||
| 360 | } | ||
| 361 | |||
| 362 | $blessed_array; | ||
| 363 | } | ||
| 364 | ->(@$self); | ||
| 365 | } | ||
| 366 | |||
| 367 | sub mul { | ||
| 368 | my ($self, $num) = @_; | ||
| 369 | bless [(@$self) x CORE::int($num)]; | ||
| 370 | } | ||
| 371 | |||
| 372 | sub div { | ||
| 373 | my ($self, $num) = @_; | ||
| 374 | |||
| 375 | my @obj = @$self; | ||
| 376 | |||
| 377 | $num = CORE::int($num); | ||
| 378 | $num > 0 or return undef; | ||
| 379 | |||
| 380 | my @array; | ||
| 381 | my $len = CORE::int(scalar(@obj) / $num); | ||
| 382 | |||
| 383 | $len || return undef; | ||
| 384 | |||
| 385 | my $i = 1; | ||
| 386 | my $pos = $len; | ||
| 387 | while (@obj) { | ||
| 388 | my $j = $pos - $i * CORE::int($len); | ||
| 389 | $pos -= $j if $j >= 1; | ||
| 390 | CORE::push(@array, bless [CORE::splice(@obj, 0, $len + $j)]); | ||
| 391 | $pos += $len; | ||
| 392 | $i++; | ||
| 393 | } | ||
| 394 | |||
| 395 | bless \@array; | ||
| 396 | } | ||
| 397 | |||
| 398 | sub part { | ||
| 399 | my ($self, $num) = @_; | ||
| 400 | |||
| 401 | my @first = @$self; | ||
| 402 | my @second = splice(@first, CORE::int($num)); | ||
| 403 | |||
| 404 | (bless(\@first), bless(\@second)); | ||
| 405 | } | ||
| 406 | |||
| 407 | *partition = \∂ | ||
| 408 | |||
| 409 | sub segment { | ||
| 410 | my ($self, @indices) = @_; | ||
| 411 | |||
| 412 | my @parts; | ||
| 413 | my $prev_i = 0; | ||
| 414 | my $end = $#{$self}; | ||
| 415 | |||
| 416 | foreach my $i (@indices) { | ||
| 417 | $i = CORE::int($i); | ||
| 418 | $i = $end if ($i > $end); | ||
| 419 | $i = $end + $i + 1 if ($i < 0); | ||
| 420 | CORE::push(@parts, bless [@{$self}[$prev_i .. $i]]); | ||
| 421 | $prev_i = $i + 1; | ||
| 422 | } | ||
| 423 | |||
| 424 | if ($prev_i <= $end) { | ||
| 425 | CORE::push(@parts, bless [@{$self}[$prev_i .. $end]]); | ||
| 426 | } | ||
| 427 | |||
| 428 | bless \@parts; | ||
| 429 | } | ||
| 430 | |||
| 431 | sub segment_by { | ||
| 432 | my ($self, $block) = @_; | ||
| 433 | |||
| 434 | my @indices; | ||
| 435 | foreach my $i (0 .. $#$self) { | ||
| 436 | if ($block->run($self->[$i])) { | ||
| 437 | CORE::push(@indices, $i); | ||
| 438 | } | ||
| 439 | } | ||
| 440 | |||
| 441 | $self->segment(@indices); | ||
| 442 | } | ||
| 443 | |||
| 444 | sub split_by { | ||
| 445 | my ($self, $block) = @_; | ||
| 446 | |||
| 447 | my @tmp; | ||
| 448 | my @array; | ||
| 449 | |||
| 450 | foreach my $item (@$self) { | ||
| 451 | if ($block->run($item)) { | ||
| 452 | CORE::push(@array, [CORE::splice(@tmp)]); | ||
| 453 | } | ||
| 454 | else { | ||
| 455 | CORE::push(@tmp, $item); | ||
| 456 | } | ||
| 457 | } | ||
| 458 | |||
| 459 | if (@tmp) { | ||
| 460 | CORE::push(@array, \@tmp); | ||
| 461 | } | ||
| 462 | |||
| 463 | @array = map { bless $_ } @array; | ||
| 464 | bless \@array; | ||
| 465 | } | ||
| 466 | |||
| 467 | sub split { | ||
| 468 | my ($self, $obj) = @_; | ||
| 469 | |||
| 470 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 471 | goto &split_by; | ||
| 472 | } | ||
| 473 | |||
| 474 | my @tmp; | ||
| 475 | my @array; | ||
| 476 | |||
| 477 | foreach my $item (@$self) { | ||
| 478 | if ($item eq $obj) { | ||
| 479 | CORE::push(@array, [CORE::splice(@tmp)]); | ||
| 480 | } | ||
| 481 | else { | ||
| 482 | CORE::push(@tmp, $item); | ||
| 483 | } | ||
| 484 | } | ||
| 485 | |||
| 486 | if (@tmp) { | ||
| 487 | CORE::push(@array, \@tmp); | ||
| 488 | } | ||
| 489 | |||
| 490 | @array = map { bless $_ } @array; | ||
| 491 | bless \@array; | ||
| 492 | } | ||
| 493 | |||
| 494 | sub or { | ||
| 495 | my ($self, $array) = @_; | ||
| 496 | |||
| 497 | if (ref($array) ne ref($self)) { | ||
| 498 | $array = $array->to_a; | ||
| 499 | } | ||
| 500 | |||
| 501 | #$self->and($array)->concat($self->xor($array)); | ||
| 502 | #$self->concat($array)->uniq; | ||
| 503 | |||
| 504 | my @x = CORE::sort { $a cmp $b } @$self; | ||
| 505 | my @y = CORE::sort { $a cmp $b } @$array; | ||
| 506 | |||
| 507 | my $endx = $#x; | ||
| 508 | my $endy = $#y; | ||
| 509 | |||
| 510 | my $i = 0; | ||
| 511 | my $j = 0; | ||
| 512 | |||
| 513 | my ($cmp, @new); | ||
| 514 | |||
| 515 | while (1) { | ||
| 516 | |||
| 517 | $cmp = CORE::int($x[$i] cmp $y[$j]); | ||
| 518 | |||
| 519 | if ($cmp < 0) { | ||
| 520 | CORE::push @new, $x[$i]; | ||
| 521 | ++$i; | ||
| 522 | } | ||
| 523 | elsif ($cmp > 0) { | ||
| 524 | CORE::push @new, $y[$j]; | ||
| 525 | ++$j; | ||
| 526 | } | ||
| 527 | else { | ||
| 528 | CORE::push @new, $x[$i]; | ||
| 529 | ++$i; | ||
| 530 | ++$j; | ||
| 531 | } | ||
| 532 | |||
| 533 | if ($i > $endx) { | ||
| 534 | CORE::push @new, @y[$j .. $endy]; | ||
| 535 | last; | ||
| 536 | } | ||
| 537 | elsif ($j > $endy) { | ||
| 538 | CORE::push @new, @x[$i .. $endx]; | ||
| 539 | last; | ||
| 540 | } | ||
| 541 | } | ||
| 542 | |||
| 543 | bless \@new; | ||
| 544 | } | ||
| 545 | |||
| 546 | sub xor { | ||
| 547 | my ($self, $array) = @_; | ||
| 548 | |||
| 549 | if (ref($array) ne ref($self)) { | ||
| 550 | $array = $array->to_a; | ||
| 551 | } | ||
| 552 | |||
| 553 | my @x = CORE::sort { $a cmp $b } @$self; | ||
| 554 | my @y = CORE::sort { $a cmp $b } @$array; | ||
| 555 | |||
| 556 | my $endx = $#x; | ||
| 557 | my $endy = $#y; | ||
| 558 | |||
| 559 | my $i = 0; | ||
| 560 | my $j = 0; | ||
| 561 | |||
| 562 | my ($cmp, @new); | ||
| 563 | |||
| 564 | while (1) { | ||
| 565 | |||
| 566 | $cmp = CORE::int($x[$i] cmp $y[$j]); | ||
| 567 | |||
| 568 | if ($cmp < 0) { | ||
| 569 | CORE::push @new, $x[$i]; | ||
| 570 | ++$i; | ||
| 571 | } | ||
| 572 | elsif ($cmp > 0) { | ||
| 573 | CORE::push @new, $y[$j]; | ||
| 574 | ++$j; | ||
| 575 | } | ||
| 576 | else { | ||
| 577 | #my $k = $i; | ||
| 578 | #do { ++$i } while ($i <= $endx and $x[$i] eq $y[$j]); | ||
| 579 | #do { ++$j } while ($j <= $endy and $x[$k] eq $y[$j]); | ||
| 580 | |||
| 581 | ++$i; | ||
| 582 | ++$j; | ||
| 583 | } | ||
| 584 | |||
| 585 | if ($i > $endx) { | ||
| 586 | CORE::push @new, @y[$j .. $endy]; | ||
| 587 | last; | ||
| 588 | } | ||
| 589 | elsif ($j > $endy) { | ||
| 590 | CORE::push @new, @x[$i .. $endx]; | ||
| 591 | last; | ||
| 592 | } | ||
| 593 | } | ||
| 594 | |||
| 595 | bless \@new; | ||
| 596 | } | ||
| 597 | |||
| 598 | sub and { | ||
| 599 | my ($self, $array) = @_; | ||
| 600 | |||
| 601 | if (ref($array) ne ref($self)) { | ||
| 602 | $array = $array->to_a; | ||
| 603 | } | ||
| 604 | |||
| 605 | my @x = CORE::sort { $a cmp $b } @$self; | ||
| 606 | my @y = CORE::sort { $a cmp $b } @$array; | ||
| 607 | |||
| 608 | my $i = 0; | ||
| 609 | my $j = 0; | ||
| 610 | |||
| 611 | my $end1 = @x; | ||
| 612 | my $end2 = @y; | ||
| 613 | |||
| 614 | my ($cmp, @new); | ||
| 615 | while ($i < $end1 and $j < $end2) { | ||
| 616 | |||
| 617 | $cmp = CORE::int($x[$i] cmp $y[$j]); | ||
| 618 | |||
| 619 | if ($cmp < 0) { | ||
| 620 | ++$i; | ||
| 621 | } | ||
| 622 | elsif ($cmp > 0) { | ||
| 623 | ++$j; | ||
| 624 | } | ||
| 625 | else { | ||
| 626 | CORE::push @new, $x[$i]; | ||
| 627 | ++$i; | ||
| 628 | ++$j; | ||
| 629 | } | ||
| 630 | } | ||
| 631 | |||
| 632 | bless \@new; | ||
| 633 | } | ||
| 634 | |||
| 635 | sub diff { | ||
| 636 | my ($self, $array) = @_; | ||
| 637 | |||
| 638 | if (ref($array) ne ref($self)) { | ||
| 639 | $array = $array->to_a; | ||
| 640 | } | ||
| 641 | |||
| 642 | my @x = CORE::sort { $a cmp $b } @$self; | ||
| 643 | my @y = CORE::sort { $a cmp $b } @$array; | ||
| 644 | |||
| 645 | my $i = 0; | ||
| 646 | my $j = 0; | ||
| 647 | |||
| 648 | my $end1 = @x; | ||
| 649 | my $end2 = @y; | ||
| 650 | |||
| 651 | my ($cmp, @new); | ||
| 652 | while ($i < $end1 and $j < $end2) { | ||
| 653 | |||
| 654 | $cmp = CORE::int($x[$i] cmp $y[$j]); | ||
| 655 | |||
| 656 | if ($cmp < 0) { | ||
| 657 | CORE::push @new, $x[$i]; | ||
| 658 | ++$i; | ||
| 659 | } | ||
| 660 | elsif ($cmp > 0) { | ||
| 661 | ++$j; | ||
| 662 | } | ||
| 663 | else { | ||
| 664 | # 1 while (++$i < $end1 and $x[$i] eq $y[$j]); | ||
| 665 | ++$i; | ||
| 666 | ++$j; | ||
| 667 | } | ||
| 668 | } | ||
| 669 | |||
| 670 | if ($i < $end1) { | ||
| 671 | CORE::push @new, @x[$i .. $#x]; | ||
| 672 | } | ||
| 673 | |||
| 674 | bless \@new; | ||
| 675 | } | ||
| 676 | |||
| 677 | *sub = \&diff; | ||
| 678 | |||
| 679 | sub concat { | ||
| 680 | my ($self, $arg) = @_; | ||
| 681 | |||
| 682 | ref($self) eq ref($arg) | ||
| 683 | ? bless([@$self, @$arg]) | ||
| 684 | : bless([@$self, $arg]); | ||
| 685 | } | ||
| 686 | |||
| 687 | *add = \&concat; | ||
| 688 | |||
| 689 | sub levenshtein { | ||
| 690 | my ($self, $arg) = @_; | ||
| 691 | |||
| 692 | if (ref($arg) ne ref($self)) { | ||
| 693 | $arg = $arg->to_a; | ||
| 694 | } | ||
| 695 | |||
| 696 | my @s = @$self; | ||
| 697 | my @t = @$arg; | ||
| 698 | |||
| 699 | my $len1 = scalar(@s); | ||
| 700 | my $len2 = scalar(@t); | ||
| 701 | |||
| 702 | my @d = ([0 .. $len2], map { [$_] } 1 .. $len1); | ||
| 703 | foreach my $i (1 .. $len1) { | ||
| 704 | foreach my $j (1 .. $len2) { | ||
| 705 | $d[$i][$j] = | ||
| 706 | $s[$i - 1] eq $t[$j - 1] | ||
| 707 | ? $d[$i - 1][$j - 1] | ||
| 708 | : List::Util::min($d[$i - 1][$j], $d[$i][$j - 1], $d[$i - 1][$j - 1]) + 1; | ||
| 709 | } | ||
| 710 | } | ||
| 711 | |||
| 712 | Sidef::Types::Number::Number->new($d[-1][-1]); | ||
| 713 | } | ||
| 714 | |||
| 715 | *lev = \&levenshtein; | ||
| 716 | *leven = \&levenshtein; | ||
| 717 | |||
| 718 | sub jaro { | ||
| 719 | my ($self, $arg, $winkler) = @_; | ||
| 720 | |||
| 721 | if (ref($arg) ne ref($self)) { | ||
| 722 | $arg = $arg->to_a; | ||
| 723 | } | ||
| 724 | |||
| 725 | my @s = @$self; | ||
| 726 | my @t = @$arg; | ||
| 727 | |||
| 728 | my $s_len = @s; | ||
| 729 | my $t_len = @t; | ||
| 730 | |||
| 731 | if ($s_len == 0 and $t_len == 0) { | ||
| 732 | return 1; | ||
| 733 | } | ||
| 734 | |||
| 735 | my $match_distance = CORE::int(List::Util::max($s_len, $t_len) / 2) - 1; | ||
| 736 | |||
| 737 | my @s_matches; | ||
| 738 | my @t_matches; | ||
| 739 | |||
| 740 | my $matches = 0; | ||
| 741 | foreach my $i (0 .. $#s) { | ||
| 742 | |||
| 743 | my $start = List::Util::max(0, $i - $match_distance); | ||
| 744 | my $end = List::Util::min($i + $match_distance + 1, $t_len); | ||
| 745 | |||
| 746 | foreach my $j ($start .. $end - 1) { | ||
| 747 | $t_matches[$j] and next; | ||
| 748 | $s[$i] eq $t[$j] or next; | ||
| 749 | $s_matches[$i] = 1; | ||
| 750 | $t_matches[$j] = 1; | ||
| 751 | $matches++; | ||
| 752 | last; | ||
| 753 | } | ||
| 754 | } | ||
| 755 | |||
| 756 | return Sidef::Types::Number::Number::ZERO if $matches == 0; | ||
| 757 | |||
| 758 | my $k = 0; | ||
| 759 | my $transpositions = 0; | ||
| 760 | |||
| 761 | foreach my $i (0 .. $#s) { | ||
| 762 | $s_matches[$i] or next; | ||
| 763 | until ($t_matches[$k]) { ++$k } | ||
| 764 | $s[$i] eq $t[$k] or ++$transpositions; | ||
| 765 | ++$k; | ||
| 766 | } | ||
| 767 | |||
| 768 | my $jaro = (($matches / $s_len) + ($matches / $t_len) + (($matches - $transpositions / 2) / $matches)) / 3; | ||
| 769 | |||
| 770 | $winkler || return Sidef::Types::Number::Number->new($jaro); # return the Jaro similarity instead of Jaro-Winkler | ||
| 771 | |||
| 772 | my $prefix = 0; | ||
| 773 | foreach my $i (0 .. List::Util::min(3, $#t, $#s)) { | ||
| 774 | $s[$i] eq $t[$i] ? ++$prefix : last; | ||
| 775 | } | ||
| 776 | |||
| 777 | Sidef::Types::Number::Number->new($jaro + $prefix * 0.1 * (1 - $jaro)); | ||
| 778 | } | ||
| 779 | |||
| 780 | sub count_by { | ||
| 781 | my ($self, $block) = @_; | ||
| 782 | |||
| 783 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 784 | |||
| 785 | my $counter = 0; | ||
| 786 | |||
| 787 | foreach my $item (@$self) { | ||
| 788 | if ($block->run($item)) { | ||
| 789 | ++$counter; | ||
| 790 | } | ||
| 791 | } | ||
| 792 | |||
| 793 | Sidef::Types::Number::Number::_set_int($counter); | ||
| 794 | } | ||
| 795 | |||
| 796 | sub count { | ||
| 797 | my ($self, $obj) = @_; | ||
| 798 | |||
| 799 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 800 | goto &count_by; | ||
| 801 | } | ||
| 802 | |||
| 803 | my $counter = 0; | ||
| 804 | foreach my $item (@$self) { | ||
| 805 | if ($item eq $obj) { | ||
| 806 | ++$counter; | ||
| 807 | } | ||
| 808 | } | ||
| 809 | |||
| 810 | Sidef::Types::Number::Number::_set_int($counter); | ||
| 811 | } | ||
| 812 | |||
| 813 | sub cmp { | ||
| 814 | my ($self, $array) = @_; | ||
| 815 | |||
| 816 | my %addr; # support for cyclic references | ||
| 817 | |||
| 818 | my $sub = sub { | ||
| 819 | my ($a1, $a2) = @_; | ||
| 820 | |||
| 821 | my $l1 = $#$a1; | ||
| 822 | my $l2 = $#$a2; | ||
| 823 | |||
| 824 | my $min = $l1 < $l2 ? $l1 : $l2; | ||
| 825 | |||
| 826 | my $refaddr1 = Scalar::Util::refaddr($a1); | ||
| 827 | my $refaddr2 = Scalar::Util::refaddr($a2); | ||
| 828 | |||
| 829 | if ($refaddr1 == $refaddr2) { | ||
| 830 | return Sidef::Types::Number::Number::ZERO; | ||
| 831 | } | ||
| 832 | |||
| 833 | exists($addr{$refaddr1}) | ||
| 834 | and return $addr{$refaddr1}; | ||
| 835 | |||
| 836 | exists($addr{$refaddr2}) | ||
| 837 | and return $addr{$refaddr2}; | ||
| 838 | |||
| 839 | my $cmp1 = $refaddr1 <=> $refaddr2; | ||
| 840 | my $cmp2 = $refaddr2 <=> $refaddr1; | ||
| 841 | |||
| 842 | $addr{$refaddr1} = ( | ||
| 843 | $cmp1 == $cmp2 ? Sidef::Types::Number::Number::ZERO | ||
| 844 | : $cmp1 < 0 ? Sidef::Types::Number::Number::MONE | ||
| 845 | : Sidef::Types::Number::Number::ONE | ||
| 846 | ); | ||
| 847 | |||
| 848 | $addr{$refaddr2} = ( | ||
| 849 | $cmp1 == $cmp2 ? Sidef::Types::Number::Number::ZERO | ||
| 850 | : $cmp2 < 0 ? Sidef::Types::Number::Number::MONE | ||
| 851 | : Sidef::Types::Number::Number::ONE | ||
| 852 | ); | ||
| 853 | |||
| 854 | foreach my $i (0 .. $min) { | ||
| 855 | if (my $cmp = CORE::int($a1->[$i] cmp $a2->[$i])) { | ||
| 856 | return ( | ||
| 857 | $cmp < 0 | ||
| 858 | ? Sidef::Types::Number::Number::MONE | ||
| 859 | : Sidef::Types::Number::Number::ONE | ||
| 860 | ); | ||
| 861 | } | ||
| 862 | } | ||
| 863 | |||
| 864 | $l1 == $l2 ? Sidef::Types::Number::Number::ZERO | ||
| 865 | : $l1 < $l2 ? Sidef::Types::Number::Number::MONE | ||
| 866 | : Sidef::Types::Number::Number::ONE; | ||
| 867 | }; | ||
| 868 | |||
| 869 | no strict 'refs'; | ||
| 870 | no warnings 'redefine'; | ||
| 871 | |||
| 872 | local *Sidef::Types::Array::Array::cmp = $sub; | ||
| 873 | local *{'Sidef::Types::Array::Array::<=>'} = $sub; | ||
| 874 | $sub->($self, $array); | ||
| 875 | } | ||
| 876 | |||
| 877 | sub lt { | ||
| 878 | my ($self, $array) = @_; | ||
| 879 | $self->cmp($array)->lt(Sidef::Types::Number::Number::ZERO) | ||
| 880 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 881 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 882 | } | ||
| 883 | |||
| 884 | sub le { | ||
| 885 | my ($self, $array) = @_; | ||
| 886 | $self->cmp($array)->le(Sidef::Types::Number::Number::ZERO) | ||
| 887 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 888 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 889 | } | ||
| 890 | |||
| 891 | sub gt { | ||
| 892 | my ($self, $array) = @_; | ||
| 893 | $self->cmp($array)->gt(Sidef::Types::Number::Number::ZERO) | ||
| 894 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 895 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 896 | } | ||
| 897 | |||
| 898 | sub ge { | ||
| 899 | my ($self, $array) = @_; | ||
| 900 | $self->cmp($array)->ge(Sidef::Types::Number::Number::ZERO) | ||
| 901 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 902 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 903 | } | ||
| 904 | |||
| 905 | sub eq { | ||
| 906 | my ($self, $array) = @_; | ||
| 907 | |||
| 908 | my %addr; # support for cyclic references | ||
| 909 | |||
| 910 | my $sub = sub { | ||
| 911 | my ($a1, $a2) = @_; | ||
| 912 | |||
| 913 | if ($#$a1 != $#$a2) { | ||
| 914 | return Sidef::Types::Bool::Bool::FALSE; | ||
| 915 | } | ||
| 916 | |||
| 917 | my $refaddr1 = Scalar::Util::refaddr($a1); | ||
| 918 | my $refaddr2 = Scalar::Util::refaddr($a2); | ||
| 919 | |||
| 920 | if ($refaddr1 == $refaddr2) { | ||
| 921 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 922 | } | ||
| 923 | |||
| 924 | exists($addr{$refaddr1}) | ||
| 925 | and return $addr{$refaddr1}; | ||
| 926 | |||
| 927 | exists($addr{$refaddr2}) | ||
| 928 | and return $addr{$refaddr2}; | ||
| 929 | |||
| 930 | $addr{$refaddr1} = Sidef::Types::Bool::Bool::FALSE; | ||
| 931 | $addr{$refaddr2} = Sidef::Types::Bool::Bool::FALSE; | ||
| 932 | |||
| 933 | my $i = -1; | ||
| 934 | foreach my $item (@$a1) { | ||
| 935 | ($item eq $a2->[++$i]) | ||
| 936 | or return Sidef::Types::Bool::Bool::FALSE; | ||
| 937 | } | ||
| 938 | |||
| 939 | (Sidef::Types::Bool::Bool::TRUE); | ||
| 940 | }; | ||
| 941 | |||
| 942 | no strict 'refs'; | ||
| 943 | no warnings 'redefine'; | ||
| 944 | |||
| 945 | local *Sidef::Types::Array::Array::eq = $sub; | ||
| 946 | local *{'Sidef::Types::Array::Array::=='} = $sub; | ||
| 947 | $sub->($self, $array); | ||
| 948 | } | ||
| 949 | |||
| 950 | sub ne { | ||
| 951 | my ($self, $array) = @_; | ||
| 952 | $self->eq($array)->not; | ||
| 953 | } | ||
| 954 | |||
| 955 | sub make { | ||
| 956 | my ($self, $size, $obj) = @_; | ||
| 957 | bless([($obj) x $size]); | ||
| 958 | } | ||
| 959 | |||
| 960 | sub make_by { | ||
| 961 | my ($self, $size, $block) = @_; | ||
| 962 | |||
| 963 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 964 | |||
| 965 | my @arr; | ||
| 966 | foreach my $i (0 .. CORE::int($size) - 1) { | ||
| 967 | CORE::push(@arr, $block->run(Sidef::Types::Number::Number::_set_int($i))); | ||
| 968 | } | ||
| 969 | |||
| 970 | bless \@arr; | ||
| 971 | } | ||
| 972 | |||
| 973 | sub _int_min_max { | ||
| 974 | my ($self, $order) = @_; | ||
| 975 | |||
| 976 | @$self || return undef; | ||
| 977 | |||
| 978 | my $item = $self->[0]; | ||
| 979 | |||
| 980 | foreach my $i (1 .. $#$self) { | ||
| 981 | my $value = $self->[$i]; | ||
| 982 | $item = $value if (($$value <=> $$item) == $order); | ||
| 983 | } | ||
| 984 | |||
| 985 | $item; | ||
| 986 | } | ||
| 987 | |||
| 988 | sub imax { | ||
| 989 | @_ = ($_[0], 1); | ||
| 990 | goto &_int_min_max; | ||
| 991 | } | ||
| 992 | |||
| 993 | sub imin { | ||
| 994 | @_ = ($_[0], -1); | ||
| 995 | goto &_int_min_max; | ||
| 996 | } | ||
| 997 | |||
| 998 | sub iminmax { | ||
| 999 | my ($self) = @_; | ||
| 1000 | ($self->imin, $self->imax); | ||
| 1001 | } | ||
| 1002 | |||
| 1003 | sub _min_max { | ||
| 1004 | my ($self, $order) = @_; | ||
| 1005 | |||
| 1006 | @$self || return undef; | ||
| 1007 | |||
| 1008 | my $item = $self->[0]; | ||
| 1009 | |||
| 1010 | foreach my $i (1 .. $#$self) { | ||
| 1011 | my $value = $self->[$i]; | ||
| 1012 | $item = $value if (CORE::int($value cmp $item) == $order); | ||
| 1013 | } | ||
| 1014 | |||
| 1015 | $item; | ||
| 1016 | } | ||
| 1017 | |||
| 1018 | sub max { | ||
| 1019 | @_ = ($_[0], 1); | ||
| 1020 | goto &_min_max; | ||
| 1021 | } | ||
| 1022 | |||
| 1023 | sub min { | ||
| 1024 | @_ = ($_[0], -1); | ||
| 1025 | goto &_min_max; | ||
| 1026 | } | ||
| 1027 | |||
| 1028 | sub minmax { | ||
| 1029 | my ($self) = @_; | ||
| 1030 | ($self->min, $self->max); | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | sub collapse { | ||
| 1034 | my ($self, $initial) = @_; | ||
| 1035 | $self->reduce_operator('+', $initial); | ||
| 1036 | } | ||
| 1037 | |||
| 1038 | sub _reduce_by { | ||
| 1039 | my ($self, $method, $result, $callback) = @_; | ||
| 1040 | |||
| 1041 | my @list; | ||
| 1042 | my $count = 0; | ||
| 1043 | |||
| 1044 | foreach my $k (0 .. $#$self) { | ||
| 1045 | CORE::push(@list, $callback->($k, $self->[$k])); | ||
| 1046 | |||
| 1047 | if (++$count > 1e5) { | ||
| 1048 | $count = 0; | ||
| 1049 | $result = $result->$method(CORE::splice(@list)); | ||
| 1050 | } | ||
| 1051 | } | ||
| 1052 | |||
| 1053 | if (@list) { | ||
| 1054 | $result = $result->$method(CORE::splice(@list)); | ||
| 1055 | } | ||
| 1056 | |||
| 1057 | $result; | ||
| 1058 | } | ||
| 1059 | |||
| 1060 | sub sum_by { | ||
| 1061 | my ($self, $block) = @_; | ||
| 1062 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1063 | $self->_reduce_by('sum', Sidef::Types::Number::Number::ZERO, sub { $block->run($_[1]) }); | ||
| 1064 | } | ||
| 1065 | |||
| 1066 | sub sum_kv { | ||
| 1067 | my ($self, $block) = @_; | ||
| 1068 | $self->_reduce_by('sum', Sidef::Types::Number::Number::ZERO, sub { $block->run(Sidef::Types::Number::Number::_set_int($_[0]), $_[1]) }); | ||
| 1069 | } | ||
| 1070 | |||
| 1071 | sub sum_2d { | ||
| 1072 | my ($self, $block) = @_; | ||
| 1073 | $self->map_2d($block)->sum; | ||
| 1074 | } | ||
| 1075 | |||
| 1076 | sub prod_2d { | ||
| 1077 | my ($self, $block) = @_; | ||
| 1078 | $self->map_2d($block)->prod; | ||
| 1079 | } | ||
| 1080 | |||
| 1081 | sub sum { | ||
| 1082 | my ($self, $arg) = @_; | ||
| 1083 | |||
| 1084 | if (defined($arg)) { | ||
| 1085 | goto &sum_by; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | Sidef::Types::Number::Number::sum(@$self); | ||
| 1089 | } | ||
| 1090 | |||
| 1091 | sub summod { | ||
| 1092 | my ($self, $mod) = @_; | ||
| 1093 | |||
| 1094 | my $sum = Sidef::Types::Number::Number::ZERO; | ||
| 1095 | foreach my $k (@$self) { | ||
| 1096 | $sum = $sum->addmod($k, $mod); | ||
| 1097 | } | ||
| 1098 | return $sum->mod($mod); | ||
| 1099 | } | ||
| 1100 | |||
| 1101 | sub avg_by { | ||
| 1102 | my ($self, $block) = @_; | ||
| 1103 | $self->map($block)->avg; | ||
| 1104 | } | ||
| 1105 | |||
| 1106 | *arithmetic_mean_by = \&avg_by; | ||
| 1107 | |||
| 1108 | sub avg { | ||
| 1109 | my ($self, $arg) = @_; | ||
| 1110 | |||
| 1111 | if (defined($arg)) { | ||
| 1112 | goto &avg_by; | ||
| 1113 | } | ||
| 1114 | |||
| 1115 | Sidef::Types::Number::Number::arithmetic_mean(@$self); | ||
| 1116 | } | ||
| 1117 | |||
| 1118 | *arithmetic_mean = \&avg; | ||
| 1119 | |||
| 1120 | sub geometric_mean_by { | ||
| 1121 | my ($self, $block) = @_; | ||
| 1122 | $self->map($block)->geometric_mean; | ||
| 1123 | } | ||
| 1124 | |||
| 1125 | sub geometric_mean { | ||
| 1126 | my ($self, $arg) = @_; | ||
| 1127 | |||
| 1128 | if (defined($arg)) { | ||
| 1129 | goto &geometric_mean_by; | ||
| 1130 | } | ||
| 1131 | |||
| 1132 | Sidef::Types::Number::Number::geometric_mean(@$self); | ||
| 1133 | } | ||
| 1134 | |||
| 1135 | sub harmonic_mean_by { | ||
| 1136 | my ($self, $block) = @_; | ||
| 1137 | $self->map($block)->harmonic_mean; | ||
| 1138 | } | ||
| 1139 | |||
| 1140 | sub harmonic_mean { | ||
| 1141 | my ($self, $arg) = @_; | ||
| 1142 | |||
| 1143 | if (defined($arg)) { | ||
| 1144 | goto &harmonic_mean_by; | ||
| 1145 | } | ||
| 1146 | |||
| 1147 | Sidef::Types::Number::Number::harmonic_mean(@$self); | ||
| 1148 | } | ||
| 1149 | |||
| 1150 | sub median_by { | ||
| 1151 | my ($self, $block) = @_; | ||
| 1152 | $self->map($block)->median; | ||
| 1153 | } | ||
| 1154 | |||
| 1155 | sub median { | ||
| 1156 | my ($self, $arg) = @_; | ||
| 1157 | |||
| 1158 | if (defined($arg)) { | ||
| 1159 | goto &median_by; | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | Sidef::Types::Number::Number::median(@$self); | ||
| 1163 | } | ||
| 1164 | |||
| 1165 | sub variance_by { | ||
| 1166 | my ($self, $block) = @_; | ||
| 1167 | $self->map($block)->variance; | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | sub variance { | ||
| 1171 | my ($self, $arg) = @_; | ||
| 1172 | |||
| 1173 | if (defined($arg)) { | ||
| 1174 | goto &variance_by; | ||
| 1175 | } | ||
| 1176 | |||
| 1177 | Sidef::Types::Number::Number::variance(@$self); | ||
| 1178 | } | ||
| 1179 | |||
| 1180 | sub stddev_by { | ||
| 1181 | my ($self, $block) = @_; | ||
| 1182 | $self->map($block)->stddev; | ||
| 1183 | } | ||
| 1184 | |||
| 1185 | sub stddev { | ||
| 1186 | my ($self, $arg) = @_; | ||
| 1187 | |||
| 1188 | if (defined($arg)) { | ||
| 1189 | goto &stddev_by; | ||
| 1190 | } | ||
| 1191 | |||
| 1192 | Sidef::Types::Number::Number::stddev(@$self); | ||
| 1193 | } | ||
| 1194 | |||
| 1195 | sub prod_by { | ||
| 1196 | my ($self, $block) = @_; | ||
| 1197 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1198 | $self->_reduce_by('prod', Sidef::Types::Number::Number::ONE, sub { $block->run($_[1]) }); | ||
| 1199 | } | ||
| 1200 | |||
| 1201 | sub prod_kv { | ||
| 1202 | my ($self, $block) = @_; | ||
| 1203 | $self->_reduce_by('prod', Sidef::Types::Number::Number::ONE, sub { $block->run(Sidef::Types::Number::Number::_set_int($_[0]), $_[1]) }); | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | sub prod { | ||
| 1207 | my ($self, $arg) = @_; | ||
| 1208 | |||
| 1209 | if (defined($arg)) { | ||
| 1210 | goto &prod_by; | ||
| 1211 | } | ||
| 1212 | |||
| 1213 | Sidef::Types::Number::Number::prod(@$self); | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | sub prodmod { | ||
| 1217 | my ($self, $mod) = @_; | ||
| 1218 | |||
| 1219 | my $prod = Sidef::Types::Number::Number::ONE; | ||
| 1220 | foreach my $k (@$self) { | ||
| 1221 | $prod = $prod->mulmod($k, $mod); | ||
| 1222 | } | ||
| 1223 | return $prod->mod($mod); | ||
| 1224 | } | ||
| 1225 | |||
| 1226 | sub gcd_by { | ||
| 1227 | my ($self, $block) = @_; | ||
| 1228 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1229 | $self->_reduce_by('gcd', Sidef::Types::Number::Number::ZERO, sub { $block->run($_[1]) }); | ||
| 1230 | } | ||
| 1231 | |||
| 1232 | sub gcud_by { | ||
| 1233 | my ($self, $block) = @_; | ||
| 1234 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1235 | $self->_reduce_by('gcud', Sidef::Types::Number::Number::ZERO, sub { $block->run($_[1]) }); | ||
| 1236 | } | ||
| 1237 | |||
| 1238 | sub gcd { | ||
| 1239 | my ($self, $block) = @_; | ||
| 1240 | |||
| 1241 | if (defined($block)) { | ||
| 1242 | goto &gcd_by; | ||
| 1243 | } | ||
| 1244 | |||
| 1245 | Sidef::Types::Number::Number::gcd(@$self); | ||
| 1246 | } | ||
| 1247 | |||
| 1248 | sub gcud { | ||
| 1249 | my ($self, $block) = @_; | ||
| 1250 | |||
| 1251 | if (defined($block)) { | ||
| 1252 | goto &gcud_by; | ||
| 1253 | } | ||
| 1254 | |||
| 1255 | Sidef::Types::Number::Number::gcud(@$self); | ||
| 1256 | } | ||
| 1257 | |||
| 1258 | sub lcm_by { | ||
| 1259 | my ($self, $block) = @_; | ||
| 1260 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1261 | $self->_reduce_by('lcm', Sidef::Types::Number::Number::ONE, sub { $block->run($_[1]) }); | ||
| 1262 | } | ||
| 1263 | |||
| 1264 | sub lcm { | ||
| 1265 | my ($self, $block) = @_; | ||
| 1266 | |||
| 1267 | if (defined($block)) { | ||
| 1268 | goto &lcm_by; | ||
| 1269 | } | ||
| 1270 | |||
| 1271 | Sidef::Types::Number::Number::lcm(@$self); | ||
| 1272 | } | ||
| 1273 | |||
| 1274 | sub all_prime { | ||
| 1275 | my ($self) = @_; | ||
| 1276 | Sidef::Types::Number::Number::all_prime(@$self); | ||
| 1277 | } | ||
| 1278 | |||
| 1279 | sub all_composite { | ||
| 1280 | my ($self) = @_; | ||
| 1281 | Sidef::Types::Number::Number::all_composite(@$self); | ||
| 1282 | } | ||
| 1283 | |||
| 1284 | sub digits2num { | ||
| 1285 | my ($self, $base) = @_; | ||
| 1286 | state $ten = Sidef::Types::Number::Number::_set_int(10); | ||
| 1287 | $base //= $ten; | ||
| 1288 | Sidef::Types::Number::Number::digits2num($base, $self); | ||
| 1289 | } | ||
| 1290 | |||
| 1291 | *from_digits = \&digits2num; | ||
| 1292 | |||
| 1293 | sub cfrac2num { | ||
| 1294 | my ($self) = @_; | ||
| 1295 | |||
| 1296 | my $res = $self->[-1]; | ||
| 1297 | my $end = $#{$self}; | ||
| 1298 | |||
| 1299 | for my $k (1 .. $end) { | ||
| 1300 | $res = $res->inv->add($self->[$end - $k]); | ||
| 1301 | } | ||
| 1302 | |||
| 1303 | $res; | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | *from_continued_fraction = \&cfrac2num; | ||
| 1307 | |||
| 1308 | sub _min_max_by { | ||
| 1309 | my ($self, $block, $order) = @_; | ||
| 1310 | |||
| 1311 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1312 | |||
| 1313 | @$self || return undef; | ||
| 1314 | |||
| 1315 | my $minmax = $self->[0]; | ||
| 1316 | my $old_key = $block->run($minmax); | ||
| 1317 | |||
| 1318 | foreach my $i (1 .. $#$self) { | ||
| 1319 | |||
| 1320 | my $value = $self->[$i]; | ||
| 1321 | my $new_key = $block->run($value); | ||
| 1322 | |||
| 1323 | if (CORE::int($new_key cmp $old_key) == $order) { | ||
| 1324 | $minmax = $value; | ||
| 1325 | $old_key = $new_key; | ||
| 1326 | } | ||
| 1327 | } | ||
| 1328 | |||
| 1329 | $minmax; | ||
| 1330 | } | ||
| 1331 | |||
| 1332 | sub max_by { | ||
| 1333 | @_ = (@_[0, 1], 1); | ||
| 1334 | goto &_min_max_by; | ||
| 1335 | } | ||
| 1336 | |||
| 1337 | sub min_by { | ||
| 1338 | @_ = (@_[0, 1], -1); | ||
| 1339 | goto &_min_max_by; | ||
| 1340 | } | ||
| 1341 | |||
| 1342 | sub swap { | ||
| 1343 | my ($self, $i, $j) = @_; | ||
| 1344 | @$self[$i, $j] = @$self[$j, $i]; | ||
| 1345 | $self; | ||
| 1346 | } | ||
| 1347 | |||
| 1348 | sub change_to { | ||
| 1349 | my ($self, $arg) = @_; | ||
| 1350 | @$self = @$arg; | ||
| 1351 | $self; | ||
| 1352 | } | ||
| 1353 | |||
| 1354 | sub _flatten { | ||
| 1355 | my %addr; # keeps track of seen objects | ||
| 1356 | |||
| 1357 | my $sub = sub { | ||
| 1358 | my ($obj) = @_; | ||
| 1359 | |||
| 1360 | my $class = ref($obj); | ||
| 1361 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 1362 | |||
| 1363 | exists($addr{$refaddr}) | ||
| 1364 | and return @{$addr{$refaddr}}; | ||
| 1365 | |||
| 1366 | my @flat; | ||
| 1367 | $addr{$refaddr} = \@flat; | ||
| 1368 | |||
| 1369 | foreach my $item (@$obj) { | ||
| 1370 | CORE::push(@flat, ((ref($item) eq $class or UNIVERSAL::isa($item, __PACKAGE__)) ? $item->flatten : $item)); | ||
| 1371 | } | ||
| 1372 | |||
| 1373 | @flat; | ||
| 1374 | }; | ||
| 1375 | |||
| 1376 | no warnings 'redefine'; | ||
| 1377 | local *Sidef::Types::Array::Array::flatten = $sub; | ||
| 1378 | $sub->($_[0]); | ||
| 1379 | } | ||
| 1380 | |||
| 1381 | sub flatten { | ||
| 1382 | my ($self) = @_; | ||
| 1383 | bless [$self->_flatten]; | ||
| 1384 | } | ||
| 1385 | |||
| 1386 | *flat = \&flatten; | ||
| 1387 | |||
| 1388 | sub exists { | ||
| 1389 | my ($self, $index) = @_; | ||
| 1390 | exists($self->[$index]) | ||
| 1391 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1392 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 1393 | } | ||
| 1394 | |||
| 1395 | *has_index = \&exists; | ||
| 1396 | |||
| 1397 | sub defined { | ||
| 1398 | my ($self, $index) = @_; | ||
| 1399 | defined($self->[$index]) | ||
| 1400 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 1401 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 1402 | } | ||
| 1403 | |||
| 1404 | sub items { | ||
| 1405 | my ($self, @indices) = @_; | ||
| 1406 | bless([map { exists($self->[$_]) ? $self->[$_] : undef } @indices]); | ||
| 1407 | } | ||
| 1408 | |||
| 1409 | sub item { | ||
| 1410 | my ($self, $index) = @_; | ||
| 1411 | exists($self->[$index]) ? $self->[$index] : undef; | ||
| 1412 | } | ||
| 1413 | |||
| 1414 | sub fetch { | ||
| 1415 | my ($self, $index, $default) = @_; | ||
| 1416 | exists($self->[$index]) ? $self->[$index] : $default; | ||
| 1417 | } | ||
| 1418 | |||
| 1419 | sub dig { | ||
| 1420 | my ($self, $key, @keys) = @_; | ||
| 1421 | |||
| 1422 | my $value = $self->fetch($key) // return undef; | ||
| 1423 | |||
| 1424 | foreach my $key (@keys) { | ||
| 1425 | $value = $value->fetch($key) // return undef; | ||
| 1426 | } | ||
| 1427 | |||
| 1428 | $value; | ||
| 1429 | } | ||
| 1430 | |||
| 1431 | sub _slice { | ||
| 1432 | my ($self, $pos1, $len) = @_; | ||
| 1433 | |||
| 1434 | $pos1 = defined($pos1) ? CORE::int($pos1) : 0; | ||
| 1435 | $len = defined($len) ? CORE::int($len) : undef; | ||
| 1436 | |||
| 1437 | my $curlen = @$self; | ||
| 1438 | |||
| 1439 | my $pos2 = 0; | ||
| 1440 | |||
| 1441 | if ($pos1 < 0) { | ||
| 1442 | $pos1 += $curlen; | ||
| 1443 | } | ||
| 1444 | |||
| 1445 | if ($pos1 > 0 and $pos1 > $curlen) { | ||
| 1446 | return; | ||
| 1447 | } | ||
| 1448 | |||
| 1449 | if (defined($len)) { | ||
| 1450 | if ($len < 0) { | ||
| 1451 | $pos2 = $curlen + $len; | ||
| 1452 | } | ||
| 1453 | elsif ($pos1 < 0) { | ||
| 1454 | $pos2 = $pos1 + $len; | ||
| 1455 | } | ||
| 1456 | elsif ($len > $curlen - $pos1) { | ||
| 1457 | $pos2 = $curlen; | ||
| 1458 | } | ||
| 1459 | else { | ||
| 1460 | $pos2 = $pos1 + $len; | ||
| 1461 | } | ||
| 1462 | } | ||
| 1463 | else { | ||
| 1464 | $pos2 = $curlen; | ||
| 1465 | } | ||
| 1466 | |||
| 1467 | if ($pos2 < 0) { | ||
| 1468 | if ($pos1 < 0) { | ||
| 1469 | return; | ||
| 1470 | } | ||
| 1471 | $pos2 = 0; | ||
| 1472 | } | ||
| 1473 | elsif ($pos1 < 0) { | ||
| 1474 | $pos1 = 0; | ||
| 1475 | } | ||
| 1476 | |||
| 1477 | if ($pos2 < $pos1) { | ||
| 1478 | $pos2 = $pos1; | ||
| 1479 | } | ||
| 1480 | if ($pos2 > $curlen) { | ||
| 1481 | $pos2 = $curlen; | ||
| 1482 | } | ||
| 1483 | |||
| 1484 | @$self[$pos1 .. $pos2 - 1]; | ||
| 1485 | } | ||
| 1486 | |||
| 1487 | sub slice { | ||
| 1488 | bless [_slice(@_)]; | ||
| 1489 | } | ||
| 1490 | |||
| 1491 | sub _ft { | ||
| 1492 | my ($self, $pos1, $pos2) = @_; | ||
| 1493 | |||
| 1494 | $pos1 = defined($pos1) ? CORE::int($pos1) : 0; | ||
| 1495 | $pos2 = defined($pos2) ? CORE::int($pos2) : undef; | ||
| 1496 | |||
| 1497 | my $curlen = scalar(@$self); | ||
| 1498 | |||
| 1499 | if ($pos1 < 0) { | ||
| 1500 | $pos1 += $curlen; | ||
| 1501 | } | ||
| 1502 | |||
| 1503 | if ($pos1 > 0 and $pos1 > $curlen) { | ||
| 1504 | return; | ||
| 1505 | } | ||
| 1506 | |||
| 1507 | if (defined($pos2)) { | ||
| 1508 | if ($pos2 < 0) { | ||
| 1509 | $pos2 += $curlen; | ||
| 1510 | } | ||
| 1511 | elsif ($pos1 < 0) { | ||
| 1512 | $pos2 = $pos1 + $pos2; | ||
| 1513 | } | ||
| 1514 | } | ||
| 1515 | else { | ||
| 1516 | $pos2 = $curlen - 1; | ||
| 1517 | } | ||
| 1518 | |||
| 1519 | if ($pos2 < 0) { | ||
| 1520 | if ($pos1 < 0) { | ||
| 1521 | return; | ||
| 1522 | } | ||
| 1523 | } | ||
| 1524 | elsif ($pos1 < 0) { | ||
| 1525 | $pos1 = 0; | ||
| 1526 | } | ||
| 1527 | |||
| 1528 | if ($pos2 >= $curlen) { | ||
| 1529 | $pos2 = $curlen - 1; | ||
| 1530 | } | ||
| 1531 | |||
| 1532 | @$self[$pos1 .. $pos2]; | ||
| 1533 | } | ||
| 1534 | |||
| 1535 | sub ft { | ||
| 1536 | bless [_ft(@_)]; | ||
| 1537 | } | ||
| 1538 | |||
| 1539 | sub each { | ||
| 1540 | my ($self, $block) = @_; | ||
| 1541 | |||
| 1542 | foreach my $item (@$self) { | ||
| 1543 | $block->run($item); | ||
| 1544 | } | ||
| 1545 | |||
| 1546 | $self; | ||
| 1547 | } | ||
| 1548 | |||
| 1549 | *for = \&each; | ||
| 1550 | *foreach = \&each; | ||
| 1551 | |||
| 1552 | sub each_2d { | ||
| 1553 | my ($self, $block) = @_; | ||
| 1554 | |||
| 1555 | foreach my $item (@$self) { | ||
| 1556 | $block->run(@$item); | ||
| 1557 | } | ||
| 1558 | |||
| 1559 | $self; | ||
| 1560 | } | ||
| 1561 | |||
| 1562 | sub each_slice { | ||
| 1563 | my ($self, $n, $block) = @_; | ||
| 1564 | |||
| 1565 | $n = CORE::int($n); | ||
| 1566 | |||
| 1567 | my @copy = @$self; | ||
| 1568 | while (my @slice = CORE::splice(@copy, 0, $n)) { | ||
| 1569 | $block->run(@slice); | ||
| 1570 | } | ||
| 1571 | |||
| 1572 | $self; | ||
| 1573 | } | ||
| 1574 | |||
| 1575 | sub slice_before { | ||
| 1576 | my ($self, $block) = @_; | ||
| 1577 | |||
| 1578 | my @new; | ||
| 1579 | my $i = 0; | ||
| 1580 | foreach my $item (@$self) { | ||
| 1581 | if ($block->run($item)) { | ||
| 1582 | ++$i if @new; | ||
| 1583 | } | ||
| 1584 | push @{$new[$i]}, $item; | ||
| 1585 | } | ||
| 1586 | |||
| 1587 | bless([map { bless($_) } @new]); | ||
| 1588 | } | ||
| 1589 | |||
| 1590 | sub slice_after { | ||
| 1591 | my ($self, $block) = @_; | ||
| 1592 | |||
| 1593 | my @new; | ||
| 1594 | my $i = 0; | ||
| 1595 | foreach my $item (@$self) { | ||
| 1596 | CORE::push(@{$new[$i]}, $item); | ||
| 1597 | ++$i if $block->run($item); | ||
| 1598 | } | ||
| 1599 | |||
| 1600 | bless [map { bless($_) } @new]; | ||
| 1601 | } | ||
| 1602 | |||
| 1603 | sub squeeze { | ||
| 1604 | my ($self) = @_; | ||
| 1605 | |||
| 1606 | my @result; | ||
| 1607 | |||
| 1608 | foreach my $item (@$self) { | ||
| 1609 | if (!@result || $result[-1] ne $item) { | ||
| 1610 | CORE::push(@result, $item); | ||
| 1611 | } | ||
| 1612 | } | ||
| 1613 | |||
| 1614 | bless \@result; | ||
| 1615 | } | ||
| 1616 | |||
| 1617 | *squeeze_dup = \&squeeze; | ||
| 1618 | |||
| 1619 | sub squeeze_by { | ||
| 1620 | my ($self, $block) = @_; | ||
| 1621 | |||
| 1622 | my @result; | ||
| 1623 | |||
| 1624 | foreach my $item (@$self) { | ||
| 1625 | if (@result && $block->run($result[-1], $item)) { | ||
| 1626 | next; | ||
| 1627 | } | ||
| 1628 | CORE::push(@result, $item); | ||
| 1629 | } | ||
| 1630 | |||
| 1631 | bless \@result; | ||
| 1632 | } | ||
| 1633 | |||
| 1634 | sub chunk_while { | ||
| 1635 | my ($self, $block) = @_; | ||
| 1636 | |||
| 1637 | my @chunks; | ||
| 1638 | my @current; | ||
| 1639 | |||
| 1640 | foreach my $item (@$self) { | ||
| 1641 | if (@current && !$block->run($current[-1], $item)) { | ||
| 1642 | CORE::push(@chunks, bless [CORE::splice(@current)]); | ||
| 1643 | } | ||
| 1644 | CORE::push(@current, $item); | ||
| 1645 | } | ||
| 1646 | |||
| 1647 | if (@current) { | ||
| 1648 | CORE::push(@chunks, bless \@current); | ||
| 1649 | } | ||
| 1650 | |||
| 1651 | bless \@chunks; | ||
| 1652 | } | ||
| 1653 | |||
| 1654 | sub each_cons { | ||
| 1655 | my ($self, $n, $block) = @_; | ||
| 1656 | |||
| 1657 | $n = CORE::int($n); | ||
| 1658 | |||
| 1659 | my @values; | ||
| 1660 | my $count = 0; | ||
| 1661 | |||
| 1662 | foreach my $item (@$self) { | ||
| 1663 | if (++$count > $n) { | ||
| 1664 | CORE::shift(@values); | ||
| 1665 | --$count; | ||
| 1666 | } | ||
| 1667 | |||
| 1668 | CORE::push(@values, $item); | ||
| 1669 | |||
| 1670 | if ($count == $n) { | ||
| 1671 | $block->run(@values); | ||
| 1672 | } | ||
| 1673 | } | ||
| 1674 | |||
| 1675 | $self; | ||
| 1676 | } | ||
| 1677 | |||
| 1678 | sub map_cons { | ||
| 1679 | my ($self, $n, $block) = @_; | ||
| 1680 | |||
| 1681 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 1682 | |||
| 1683 | $n = CORE::int($n); | ||
| 1684 | |||
| 1685 | my @result; | ||
| 1686 | my @values; | ||
| 1687 | my $count = 0; | ||
| 1688 | |||
| 1689 | foreach my $item (@$self) { | ||
| 1690 | if (++$count > $n) { | ||
| 1691 | CORE::shift(@values); | ||
| 1692 | --$count; | ||
| 1693 | } | ||
| 1694 | |||
| 1695 | CORE::push(@values, $item); | ||
| 1696 | |||
| 1697 | if ($count == $n) { | ||
| 1698 | CORE::push(@result, $block->run(@values)); | ||
| 1699 | } | ||
| 1700 | } | ||
| 1701 | |||
| 1702 | bless \@result; | ||
| 1703 | } | ||
| 1704 | |||
| 1705 | *cons = \&map_cons; | ||
| 1706 | |||
| 1707 | sub map_slice { | ||
| 1708 | my ($self, $n, $block) = @_; | ||
| 1709 | |||
| 1710 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 1711 | |||
| 1712 | $n = CORE::int($n); | ||
| 1713 | |||
| 1714 | my @result; | ||
| 1715 | my @copy = @$self; | ||
| 1716 | |||
| 1717 | while (my @slice = CORE::splice(@copy, 0, $n)) { | ||
| 1718 | CORE::push(@result, $block->run(@slice)); | ||
| 1719 | } | ||
| 1720 | |||
| 1721 | bless \@result; | ||
| 1722 | } | ||
| 1723 | |||
| 1724 | *slices = \&map_slice; | ||
| 1725 | |||
| 1726 | sub each_index { | ||
| 1727 | my ($self, $block) = @_; | ||
| 1728 | |||
| 1729 | foreach my $i (0 .. $#$self) { | ||
| 1730 | $block->run(Sidef::Types::Number::Number::_set_int($i)); | ||
| 1731 | } | ||
| 1732 | |||
| 1733 | $self; | ||
| 1734 | } | ||
| 1735 | |||
| 1736 | *each_k = \&each_index; | ||
| 1737 | *each_key = \&each_index; | ||
| 1738 | |||
| 1739 | sub each_kv { | ||
| 1740 | my ($self, $block) = @_; | ||
| 1741 | |||
| 1742 | foreach my $i (0 .. $#$self) { | ||
| 1743 | $block->run(Sidef::Types::Number::Number::_set_int($i), $self->[$i]); | ||
| 1744 | } | ||
| 1745 | |||
| 1746 | $self; | ||
| 1747 | } | ||
| 1748 | |||
| 1749 | sub expand { | ||
| 1750 | my ($self, $block) = @_; | ||
| 1751 | |||
| 1752 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1753 | |||
| 1754 | my @new; | ||
| 1755 | my @copy = @$self; | ||
| 1756 | |||
| 1757 | foreach my $item (@copy) { | ||
| 1758 | my $res = $block->run($item); | ||
| 1759 | |||
| 1760 | if (ref($res) eq __PACKAGE__ or UNIVERSAL::isa($res, __PACKAGE__)) { | ||
| 1761 | CORE::push(@copy, @$res); | ||
| 1762 | } | ||
| 1763 | else { | ||
| 1764 | CORE::push(@new, $res); | ||
| 1765 | } | ||
| 1766 | } | ||
| 1767 | |||
| 1768 | bless \@new; | ||
| 1769 | } | ||
| 1770 | |||
| 1771 | *expand_by = \&expand; | ||
| 1772 | |||
| 1773 | sub recmap { | ||
| 1774 | my ($self, $block) = @_; | ||
| 1775 | |||
| 1776 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1777 | |||
| 1778 | my @copy = @$self; | ||
| 1779 | |||
| 1780 | foreach my $item (@copy) { | ||
| 1781 | my $res = $block->run($item); | ||
| 1782 | |||
| 1783 | if (ref($res) eq __PACKAGE__ or UNIVERSAL::isa($res, __PACKAGE__)) { | ||
| 1784 | CORE::push(@copy, @$res); | ||
| 1785 | } | ||
| 1786 | } | ||
| 1787 | |||
| 1788 | bless \@copy; | ||
| 1789 | } | ||
| 1790 | |||
| 1791 | sub map { | ||
| 1792 | my ($self, $block) = @_; | ||
| 1793 | |||
| 1794 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1795 | |||
| 1796 | my @array; | ||
| 1797 | foreach my $item (@$self) { | ||
| 1798 | CORE::push(@array, $block->run($item)); | ||
| 1799 | } | ||
| 1800 | |||
| 1801 | bless \@array, ref($self); | ||
| 1802 | } | ||
| 1803 | |||
| 1804 | *collect = \↦ | ||
| 1805 | |||
| 1806 | sub map_2d { | ||
| 1807 | my ($self, $block) = @_; | ||
| 1808 | |||
| 1809 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 1810 | |||
| 1811 | my @array; | ||
| 1812 | foreach my $item (@$self) { | ||
| 1813 | CORE::push(@array, $block->run(@$item)); | ||
| 1814 | } | ||
| 1815 | |||
| 1816 | bless \@array, ref($self); | ||
| 1817 | } | ||
| 1818 | |||
| 1819 | sub map_kv { | ||
| 1820 | my ($self, $block) = @_; | ||
| 1821 | |||
| 1822 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 1823 | |||
| 1824 | my @arr; | ||
| 1825 | foreach my $i (0 .. $#$self) { | ||
| 1826 | CORE::push(@arr, $block->run(Sidef::Types::Number::Number::_set_int($i), $self->[$i])); | ||
| 1827 | } | ||
| 1828 | |||
| 1829 | bless \@arr, ref($self); | ||
| 1830 | } | ||
| 1831 | |||
| 1832 | *collect_kv = \&map_kv; | ||
| 1833 | |||
| 1834 | sub flat_map { | ||
| 1835 | my ($self, $block) = @_; | ||
| 1836 | |||
| 1837 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 1838 | |||
| 1839 | my @array; | ||
| 1840 | foreach my $item (@$self) { | ||
| 1841 | CORE::push(@array, @{scalar $block->run($item)}); | ||
| 1842 | } | ||
| 1843 | |||
| 1844 | bless \@array; | ||
| 1845 | } | ||
| 1846 | |||
| 1847 | sub grep { | ||
| 1848 | my ($self, $block) = @_; | ||
| 1849 | |||
| 1850 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1851 | |||
| 1852 | my @array; | ||
| 1853 | foreach my $item (@$self) { | ||
| 1854 | CORE::push(@array, $item) if $block->run($item); | ||
| 1855 | } | ||
| 1856 | |||
| 1857 | bless \@array, ref($self); | ||
| 1858 | } | ||
| 1859 | |||
| 1860 | *select = \&grep; | ||
| 1861 | |||
| 1862 | sub grep_2d { | ||
| 1863 | my ($self, $block) = @_; | ||
| 1864 | |||
| 1865 | my @array; | ||
| 1866 | foreach my $item (@$self) { | ||
| 1867 | CORE::push(@array, $item) if $block->run(@$item); | ||
| 1868 | } | ||
| 1869 | |||
| 1870 | bless \@array, ref($self); | ||
| 1871 | } | ||
| 1872 | |||
| 1873 | sub grep_kv { | ||
| 1874 | my ($self, $block) = @_; | ||
| 1875 | |||
| 1876 | my @array; | ||
| 1877 | foreach my $i (0 .. $#$self) { | ||
| 1878 | CORE::push(@array, $self->[$i]) if $block->run(Sidef::Types::Number::Number::_set_int($i), $self->[$i]); | ||
| 1879 | } | ||
| 1880 | |||
| 1881 | bless \@array, ref($self); | ||
| 1882 | } | ||
| 1883 | |||
| 1884 | *select_kv = \&grep_kv; | ||
| 1885 | |||
| 1886 | sub group { | ||
| 1887 | my ($self, $block) = @_; | ||
| 1888 | |||
| 1889 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1890 | |||
| 1891 | my %hash; | ||
| 1892 | foreach my $item (@$self) { | ||
| 1893 | CORE::push(@{$hash{$block->run($item)}}, $item); | ||
| 1894 | } | ||
| 1895 | |||
| 1896 | foreach my $value (CORE::values(%hash)) { | ||
| 1897 | bless $value; | ||
| 1898 | } | ||
| 1899 | |||
| 1900 | Sidef::Types::Hash::Hash->new(\%hash); | ||
| 1901 | } | ||
| 1902 | |||
| 1903 | *group_by = \&group; | ||
| 1904 | |||
| 1905 | sub stack { | ||
| 1906 | my ($self, $block) = @_; | ||
| 1907 | |||
| 1908 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1909 | @$self || return bless []; | ||
| 1910 | |||
| 1911 | my @result = bless [$self->[0]]; | ||
| 1912 | my $prev_value = $block->run($self->[0]); | ||
| 1913 | |||
| 1914 | foreach my $i (1 .. $#{$self}) { | ||
| 1915 | |||
| 1916 | my $item = $self->[$i]; | ||
| 1917 | my $curr_value = $block->run($item); | ||
| 1918 | |||
| 1919 | if (!($curr_value eq $prev_value)) { | ||
| 1920 | CORE::push(@result, bless []); | ||
| 1921 | } | ||
| 1922 | |||
| 1923 | CORE::push(@{$result[-1]}, $item); | ||
| 1924 | $prev_value = $curr_value; | ||
| 1925 | } | ||
| 1926 | |||
| 1927 | bless \@result; | ||
| 1928 | } | ||
| 1929 | |||
| 1930 | *stack_by = \&stack; | ||
| 1931 | |||
| 1932 | sub run_length { | ||
| 1933 | my ($self, $block) = @_; | ||
| 1934 | |||
| 1935 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 1936 | @$self || return bless []; | ||
| 1937 | |||
| 1938 | my @result = bless [$self->[0], 1]; | ||
| 1939 | my $prev_value = $block->run($self->[0]); | ||
| 1940 | |||
| 1941 | foreach my $i (1 .. $#{$self}) { | ||
| 1942 | |||
| 1943 | my $item = $self->[$i]; | ||
| 1944 | my $curr_value = $block->run($item); | ||
| 1945 | |||
| 1946 | if ($curr_value eq $prev_value) { | ||
| 1947 | ++$result[-1][1]; | ||
| 1948 | } | ||
| 1949 | else { | ||
| 1950 | CORE::push(@result, bless [$item, 1]); | ||
| 1951 | } | ||
| 1952 | |||
| 1953 | $prev_value = $curr_value; | ||
| 1954 | } | ||
| 1955 | |||
| 1956 | foreach my $pair (@result) { | ||
| 1957 | $pair->[1] = Sidef::Types::Number::Number::_set_int($pair->[1]); | ||
| 1958 | } | ||
| 1959 | |||
| 1960 | bless \@result; | ||
| 1961 | } | ||
| 1962 | |||
| 1963 | *run_length_by = \&run_length; | ||
| 1964 | |||
| 1965 | sub match { | ||
| 1966 | my ($self, $regex) = @_; | ||
| 1967 | |||
| 1968 | my %addr; | ||
| 1969 | |||
| 1970 | my $sub = sub { | ||
| 1971 | my ($obj) = @_; | ||
| 1972 | |||
| 1973 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 1974 | |||
| 1975 | exists($addr{$refaddr}) | ||
| 1976 | && return Sidef::Types::Bool::Bool::FALSE; | ||
| 1977 | |||
| 1978 | undef $addr{$refaddr}; | ||
| 1979 | |||
| 1980 | foreach my $item (@$obj) { | ||
| 1981 | if (defined(my $sub = UNIVERSAL::can($item, 'match'))) { | ||
| 1982 | $sub->($item, $regex) | ||
| 1983 | && return Sidef::Types::Bool::Bool::TRUE; | ||
| 1984 | } | ||
| 1985 | elsif ($regex->run($item)) { | ||
| 1986 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 1987 | } | ||
| 1988 | } | ||
| 1989 | |||
| 1990 | Sidef::Types::Bool::Bool::FALSE; | ||
| 1991 | }; | ||
| 1992 | |||
| 1993 | no warnings 'redefine'; | ||
| 1994 | local *Sidef::Types::Array::Array::match = $sub; | ||
| 1995 | $sub->($_[0]); | ||
| 1996 | } | ||
| 1997 | |||
| 1998 | sub iter { | ||
| 1999 | my ($self) = @_; | ||
| 2000 | |||
| 2001 | my $i = 0; | ||
| 2002 | Sidef::Types::Block::Block->new( | ||
| 2003 | code => sub { | ||
| 2004 | $self->[$i++]; | ||
| 2005 | } | ||
| 2006 | ); | ||
| 2007 | } | ||
| 2008 | |||
| 2009 | sub freq_by { | ||
| 2010 | my ($self, $block) = @_; | ||
| 2011 | |||
| 2012 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2013 | |||
| 2014 | my %hash; | ||
| 2015 | foreach my $item (@$self) { | ||
| 2016 | my $r = $hash{$block->run($item)} //= { | ||
| 2017 | count => 0, | ||
| 2018 | items => [], | ||
| 2019 | }; | ||
| 2020 | CORE::push(@{$r->{items}}, $item); | ||
| 2021 | ++$r->{count}; | ||
| 2022 | } | ||
| 2023 | |||
| 2024 | my %freq; | ||
| 2025 | foreach my $key (CORE::keys(%hash)) { | ||
| 2026 | my $r = $hash{$key}; | ||
| 2027 | my $n = Sidef::Types::Number::Number::_set_int($r->{count}); | ||
| 2028 | @freq{@{$r->{items}}} = ($n) x scalar(@{$r->{items}}); | ||
| 2029 | } | ||
| 2030 | |||
| 2031 | Sidef::Types::Hash::Hash->new(\%freq); | ||
| 2032 | } | ||
| 2033 | |||
| 2034 | sub freq { | ||
| 2035 | my ($self, $block) = @_; | ||
| 2036 | |||
| 2037 | if (defined($block)) { | ||
| 2038 | goto &freq_by; | ||
| 2039 | } | ||
| 2040 | |||
| 2041 | my %hash; | ||
| 2042 | foreach my $item (@$self) { | ||
| 2043 | ++$hash{$item}; | ||
| 2044 | } | ||
| 2045 | |||
| 2046 | foreach my $key (CORE::keys(%hash)) { | ||
| 2047 | $hash{$key} = Sidef::Types::Number::Number::_set_int($hash{$key}); | ||
| 2048 | } | ||
| 2049 | |||
| 2050 | Sidef::Types::Hash::Hash->new(\%hash); | ||
| 2051 | } | ||
| 2052 | |||
| 2053 | sub _huffman_from_code_lengths { | ||
| 2054 | my ($code_lengths_table) = @_; | ||
| 2055 | |||
| 2056 | # This algorithm is based on the pseudocode in RFC 1951 (Section 3.2.2) | ||
| 2057 | # (Steps are numbered as in the RFC) | ||
| 2058 | |||
| 2059 | my @code_lengths = map { [$_, $code_lengths_table->{$_}] } CORE::sort { $a <=> $b } CORE::keys %$code_lengths_table; | ||
| 2060 | |||
| 2061 | # Step 1: Count the number of codes for each length | ||
| 2062 | my $max_length = List::Util::max(map { $_->[1] } @code_lengths) // 0; | ||
| 2063 | my @length_counts = (0) x ($max_length + 1); | ||
| 2064 | |||
| 2065 | foreach my $length (map { $_->[1] } @code_lengths) { | ||
| 2066 | |||
| 2067 | # Treat undef or negative lengths as 0 (unused) | ||
| 2068 | if (defined($length) and $length > 0) { | ||
| 2069 | ++$length_counts[$length]; | ||
| 2070 | } | ||
| 2071 | } | ||
| 2072 | |||
| 2073 | # Step 2: Generate the starting numerical value for each length | ||
| 2074 | my $code = 0; | ||
| 2075 | $length_counts[0] = 0; | ||
| 2076 | my @next_code = (0) x ($max_length + 1); | ||
| 2077 | |||
| 2078 | foreach my $bits (1 .. $max_length) { | ||
| 2079 | $code = ($code + $length_counts[$bits - 1]) << 1; | ||
| 2080 | $next_code[$bits] = $code; | ||
| 2081 | } | ||
| 2082 | |||
| 2083 | # Step 3: Assign numerical values to all codes | ||
| 2084 | my %dict; | ||
| 2085 | foreach my $pair (@code_lengths) { | ||
| 2086 | my ($key, $length) = @$pair; | ||
| 2087 | |||
| 2088 | # Skip zero-length codes (unused symbols) | ||
| 2089 | if (defined($length) and $length != 0) { | ||
| 2090 | |||
| 2091 | # Format the integer code as a binary string with $length bits | ||
| 2092 | my $binary_code = sprintf('%0*b', $length, $next_code[$length]); | ||
| 2093 | |||
| 2094 | $dict{$key} = bless(\$binary_code, 'Sidef::Types::String::String'); | ||
| 2095 | |||
| 2096 | # Increment the code for the next symbol of this length | ||
| 2097 | ++$next_code[$length]; | ||
| 2098 | } | ||
| 2099 | } | ||
| 2100 | |||
| 2101 | Sidef::Types::Hash::Hash->new(\%dict); | ||
| 2102 | } | ||
| 2103 | |||
| 2104 | sub _huffman_walk_tree { | ||
| 2105 | my ($node, $code, $h) = @_; | ||
| 2106 | |||
| 2107 | # Node structure: [ content, frequency ] | ||
| 2108 | # If content is a reference, it's an internal node: [left_child, right_child] | ||
| 2109 | # If content is a scalar, it's a leaf: 'symbol' | ||
| 2110 | |||
| 2111 | my $c = $node->[0] // return $h; | ||
| 2112 | |||
| 2113 | if (ref $c) { | ||
| 2114 | |||
| 2115 | # Recurse: Left adds '0', Right adds '1' | ||
| 2116 | __SUB__->($c->[$_], $code . $_, $h) for ('0', '1'); | ||
| 2117 | } | ||
| 2118 | else { | ||
| 2119 | # Leaf node found | ||
| 2120 | $h->{$c} = $code; | ||
| 2121 | } | ||
| 2122 | |||
| 2123 | return $h; | ||
| 2124 | } | ||
| 2125 | |||
| 2126 | sub _heap_push { | ||
| 2127 | my ($heap, $item) = @_; | ||
| 2128 | CORE::push(@$heap, $item); | ||
| 2129 | my $i = $#$heap; | ||
| 2130 | while ($i > 0) { | ||
| 2131 | my $p = ($i - 1) >> 1; | ||
| 2132 | last if ($heap->[$p][1] <= $heap->[$i][1]); | ||
| 2133 | @{$heap}[$p, $i] = @{$heap}[$i, $p]; | ||
| 2134 | $i = $p; | ||
| 2135 | } | ||
| 2136 | } | ||
| 2137 | |||
| 2138 | sub _heap_pop { | ||
| 2139 | my ($heap) = @_; | ||
| 2140 | return CORE::pop(@$heap) if (@$heap == 1); | ||
| 2141 | my $top = $heap->[0]; | ||
| 2142 | $heap->[0] = CORE::pop @$heap; | ||
| 2143 | my $n = scalar @$heap; | ||
| 2144 | my $i = 0; | ||
| 2145 | while (1) { | ||
| 2146 | my $s = $i; | ||
| 2147 | my $l = 2 * $i + 1; | ||
| 2148 | my $r = $l + 1; | ||
| 2149 | $s = $l if ($l < $n && $heap->[$l][1] < $heap->[$s][1]); | ||
| 2150 | $s = $r if ($r < $n && $heap->[$r][1] < $heap->[$s][1]); | ||
| 2151 | last if $s == $i; | ||
| 2152 | @{$heap}[$i, $s] = @{$heap}[$s, $i]; | ||
| 2153 | $i = $s; | ||
| 2154 | } | ||
| 2155 | return $top; | ||
| 2156 | } | ||
| 2157 | |||
| 2158 | sub huffman { | ||
| 2159 | my ($self) = @_; | ||
| 2160 | |||
| 2161 | # 1. Count Frequencies | ||
| 2162 | my %freq; | ||
| 2163 | foreach my $item (@$self) { | ||
| 2164 | ++$freq{$item}; | ||
| 2165 | } | ||
| 2166 | |||
| 2167 | my @symbols = CORE::sort CORE::keys %freq; | ||
| 2168 | |||
| 2169 | # 2. Initialize Heap | ||
| 2170 | # Structure: [ [symbol_or_children], frequency ] | ||
| 2171 | my @heap; | ||
| 2172 | foreach my $k (@symbols) { | ||
| 2173 | _heap_push(\@heap, [$k, $freq{$k}]); | ||
| 2174 | } | ||
| 2175 | |||
| 2176 | # 3. Build Huffman Tree | ||
| 2177 | while (@heap > 1) { | ||
| 2178 | my $x = _heap_pop(\@heap); | ||
| 2179 | my $y = _heap_pop(\@heap); | ||
| 2180 | _heap_push(\@heap, [[$x, $y], $x->[1] + $y->[1]]); | ||
| 2181 | } | ||
| 2182 | |||
| 2183 | if (@heap == 1 && !ref $heap[0][0]) { | ||
| 2184 | @heap = ([[$heap[0]], $heap[0][1]]); | ||
| 2185 | } | ||
| 2186 | |||
| 2187 | # 4. Generate Codes | ||
| 2188 | my $h = _huffman_walk_tree($heap[0], '', {}); | ||
| 2189 | |||
| 2190 | my %code_lengths; | ||
| 2191 | foreach my $i (@symbols) { | ||
| 2192 | $code_lengths{$i} = CORE::length($h->{$i}); | ||
| 2193 | } | ||
| 2194 | |||
| 2195 | _huffman_from_code_lengths(\%code_lengths); | ||
| 2196 | } | ||
| 2197 | |||
| 2198 | sub first_by { | ||
| 2199 | my ($self, $block) = @_; | ||
| 2200 | |||
| 2201 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2202 | |||
| 2203 | foreach my $val (@$self) { | ||
| 2204 | return $val if $block->run($val); | ||
| 2205 | } | ||
| 2206 | |||
| 2207 | return undef; | ||
| 2208 | } | ||
| 2209 | |||
| 2210 | *find = \&first_by; | ||
| 2211 | |||
| 2212 | sub first { | ||
| 2213 | my ($self, $arg) = @_; | ||
| 2214 | |||
| 2215 | if (defined $arg) { | ||
| 2216 | |||
| 2217 | if (ref($arg) eq 'Sidef::Types::Block::Block') { | ||
| 2218 | goto &first_by; | ||
| 2219 | } | ||
| 2220 | |||
| 2221 | my $end = $#$self; | ||
| 2222 | |||
| 2223 | $arg = CORE::int($arg) || return bless([], ref($self)); | ||
| 2224 | $arg += $end + 1 if $arg < 0; | ||
| 2225 | $arg -= 1; | ||
| 2226 | |||
| 2227 | return bless([@$self[0 .. ($arg > $end ? $end : $arg)]], ref($self)); | ||
| 2228 | } | ||
| 2229 | |||
| 2230 | @$self ? $self->[0] : undef; | ||
| 2231 | } | ||
| 2232 | |||
| 2233 | *head = \&first; | ||
| 2234 | |||
| 2235 | sub last_by { | ||
| 2236 | my ($self, $block) = @_; | ||
| 2237 | |||
| 2238 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2239 | |||
| 2240 | for (my $i = $#$self ; $i >= 0 ; --$i) { | ||
| 2241 | return $self->[$i] if $block->run($self->[$i]); | ||
| 2242 | } | ||
| 2243 | |||
| 2244 | return undef; | ||
| 2245 | } | ||
| 2246 | |||
| 2247 | sub last { | ||
| 2248 | my ($self, $arg) = @_; | ||
| 2249 | |||
| 2250 | if (defined $arg) { | ||
| 2251 | |||
| 2252 | if (ref($arg) eq 'Sidef::Types::Block::Block') { | ||
| 2253 | goto &last_by; | ||
| 2254 | } | ||
| 2255 | |||
| 2256 | my $end = $#$self; | ||
| 2257 | |||
| 2258 | $arg = CORE::int($arg) || return bless([], ref($self)); | ||
| 2259 | $arg += $end + 1 if $arg < 0; | ||
| 2260 | |||
| 2261 | my $from = $end - $arg + 1; | ||
| 2262 | |||
| 2263 | return bless([@$self[($from < 0 ? 0 : $from) .. $end]], ref($self)); | ||
| 2264 | } | ||
| 2265 | |||
| 2266 | @$self ? $self->[-1] : undef; | ||
| 2267 | } | ||
| 2268 | |||
| 2269 | *tail = \&last; | ||
| 2270 | |||
| 2271 | sub skip_by { | ||
| 2272 | my ($self, $block) = @_; | ||
| 2273 | |||
| 2274 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2275 | |||
| 2276 | my @array; | ||
| 2277 | foreach my $item (@$self) { | ||
| 2278 | $block->run($item) || CORE::push(@array, $item); | ||
| 2279 | } | ||
| 2280 | |||
| 2281 | bless \@array, ref($self); | ||
| 2282 | } | ||
| 2283 | |||
| 2284 | sub skip { | ||
| 2285 | my ($self, $n) = @_; | ||
| 2286 | |||
| 2287 | if (defined($n) and ref($n) eq 'Sidef::Types::Block::Block') { | ||
| 2288 | goto &skip_by; | ||
| 2289 | } | ||
| 2290 | |||
| 2291 | $n = defined($n) ? CORE::int($n) : 1; | ||
| 2292 | $n == 0 and return $self->clone; | ||
| 2293 | |||
| 2294 | $self->last(-$n); | ||
| 2295 | } | ||
| 2296 | |||
| 2297 | *skip_first = \&skip; | ||
| 2298 | |||
| 2299 | sub skip_last { | ||
| 2300 | my ($self, $n) = @_; | ||
| 2301 | |||
| 2302 | if (defined($n) and ref($n) eq 'Sidef::Types::Block::Block') { | ||
| 2303 | goto &skip_by; | ||
| 2304 | } | ||
| 2305 | |||
| 2306 | $n = defined($n) ? CORE::int($n) : 1; | ||
| 2307 | $n == 0 and return $self->clone; | ||
| 2308 | |||
| 2309 | $self->first(-$n); | ||
| 2310 | } | ||
| 2311 | |||
| 2312 | sub any { | ||
| 2313 | my ($self, $block) = @_; | ||
| 2314 | |||
| 2315 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2316 | |||
| 2317 | foreach my $val (@$self) { | ||
| 2318 | $block->run($val) | ||
| 2319 | && return (Sidef::Types::Bool::Bool::TRUE); | ||
| 2320 | } | ||
| 2321 | |||
| 2322 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 2323 | } | ||
| 2324 | |||
| 2325 | sub all { | ||
| 2326 | my ($self, $block) = @_; | ||
| 2327 | |||
| 2328 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2329 | |||
| 2330 | foreach my $val (@$self) { | ||
| 2331 | $block->run($val) | ||
| 2332 | || return (Sidef::Types::Bool::Bool::FALSE); | ||
| 2333 | } | ||
| 2334 | |||
| 2335 | (Sidef::Types::Bool::Bool::TRUE); | ||
| 2336 | } | ||
| 2337 | |||
| 2338 | sub none { | ||
| 2339 | my ($self, $block) = @_; | ||
| 2340 | |||
| 2341 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2342 | |||
| 2343 | foreach my $val (@$self) { | ||
| 2344 | $block->run($val) | ||
| 2345 | && return (Sidef::Types::Bool::Bool::FALSE); | ||
| 2346 | } | ||
| 2347 | |||
| 2348 | (Sidef::Types::Bool::Bool::TRUE); | ||
| 2349 | } | ||
| 2350 | |||
| 2351 | sub bindex_by { | ||
| 2352 | my ($self, $obj) = @_; | ||
| 2353 | |||
| 2354 | my $left = 0; | ||
| 2355 | my $right = $#$self; | ||
| 2356 | my ($middle, $item, $cmp); | ||
| 2357 | |||
| 2358 | while ($left <= $right) { | ||
| 2359 | |||
| 2360 | $middle = (($right + $left) >> 1); | ||
| 2361 | $item = $self->[$middle]; | ||
| 2362 | $cmp = CORE::int($obj->run($item)) || return Sidef::Types::Number::Number::_set_int($middle); | ||
| 2363 | |||
| 2364 | if ($cmp > 0) { | ||
| 2365 | $right = $middle - 1; | ||
| 2366 | } | ||
| 2367 | else { | ||
| 2368 | $left = $middle + 1; | ||
| 2369 | } | ||
| 2370 | } | ||
| 2371 | |||
| 2372 | Sidef::Types::Number::Number::MONE; | ||
| 2373 | } | ||
| 2374 | |||
| 2375 | *bsearch_index_by = \&bindex_by; | ||
| 2376 | |||
| 2377 | sub bindex { | ||
| 2378 | my ($self, $obj) = @_; | ||
| 2379 | |||
| 2380 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2381 | goto &bindex_by; | ||
| 2382 | } | ||
| 2383 | |||
| 2384 | my $left = 0; | ||
| 2385 | my $right = $#$self; | ||
| 2386 | my ($middle, $item, $cmp); | ||
| 2387 | |||
| 2388 | while ($left <= $right) { | ||
| 2389 | $middle = (($right + $left) >> 1); | ||
| 2390 | $item = $self->[$middle]; | ||
| 2391 | $cmp = CORE::int($item cmp $obj) || return Sidef::Types::Number::Number::_set_int($middle); | ||
| 2392 | |||
| 2393 | if ($cmp > 0) { | ||
| 2394 | $right = $middle - 1; | ||
| 2395 | } | ||
| 2396 | else { | ||
| 2397 | $left = $middle + 1; | ||
| 2398 | } | ||
| 2399 | } | ||
| 2400 | |||
| 2401 | Sidef::Types::Number::Number::MONE; | ||
| 2402 | } | ||
| 2403 | |||
| 2404 | *bsearch_index = \&bindex; | ||
| 2405 | |||
| 2406 | sub bsearch { | ||
| 2407 | my ($self, $obj) = @_; | ||
| 2408 | my $index = $self->bindex($obj); | ||
| 2409 | $index->is_mone ? undef : $self->[CORE::int($index)]; | ||
| 2410 | } | ||
| 2411 | |||
| 2412 | *bsearch_by = \&bsearch; | ||
| 2413 | |||
| 2414 | sub bindex_ge_by { | ||
| 2415 | my ($self, $obj) = @_; | ||
| 2416 | |||
| 2417 | my $left = 0; | ||
| 2418 | my $right = $#$self; | ||
| 2419 | my ($middle, $item, $cmp); | ||
| 2420 | |||
| 2421 | while (1) { | ||
| 2422 | |||
| 2423 | $middle = (($right + $left) >> 1); | ||
| 2424 | $item = $self->[$middle]; | ||
| 2425 | $cmp = CORE::int($obj->run($item)) || return Sidef::Types::Number::Number::_set_int($middle); | ||
| 2426 | |||
| 2427 | if ($cmp < 0) { | ||
| 2428 | $left = $middle + 1; | ||
| 2429 | if ($left > $right) { | ||
| 2430 | ++$middle; | ||
| 2431 | last; | ||
| 2432 | } | ||
| 2433 | } | ||
| 2434 | else { | ||
| 2435 | $right = $middle - 1; | ||
| 2436 | $left > $right && last; | ||
| 2437 | } | ||
| 2438 | } | ||
| 2439 | |||
| 2440 | Sidef::Types::Number::Number::_set_int($middle); | ||
| 2441 | } | ||
| 2442 | |||
| 2443 | sub bindex_ge { | ||
| 2444 | my ($self, $obj) = @_; | ||
| 2445 | |||
| 2446 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2447 | goto &bindex_ge_by; | ||
| 2448 | } | ||
| 2449 | |||
| 2450 | my $left = 0; | ||
| 2451 | my $right = $#$self; | ||
| 2452 | my ($middle, $item, $cmp); | ||
| 2453 | |||
| 2454 | while (1) { | ||
| 2455 | |||
| 2456 | $middle = (($right + $left) >> 1); | ||
| 2457 | $item = $self->[$middle]; | ||
| 2458 | $cmp = CORE::int($item cmp $obj) || return Sidef::Types::Number::Number::_set_int($middle); | ||
| 2459 | |||
| 2460 | if ($cmp < 0) { | ||
| 2461 | $left = $middle + 1; | ||
| 2462 | if ($left > $right) { | ||
| 2463 | ++$middle; | ||
| 2464 | last; | ||
| 2465 | } | ||
| 2466 | } | ||
| 2467 | else { | ||
| 2468 | $right = $middle - 1; | ||
| 2469 | $left > $right && last; | ||
| 2470 | } | ||
| 2471 | } | ||
| 2472 | |||
| 2473 | Sidef::Types::Number::Number::_set_int($middle); | ||
| 2474 | } | ||
| 2475 | |||
| 2476 | sub bindex_le_by { | ||
| 2477 | my ($self, $obj) = @_; | ||
| 2478 | |||
| 2479 | my $left = 0; | ||
| 2480 | my $right = $#$self; | ||
| 2481 | my ($middle, $item, $cmp); | ||
| 2482 | |||
| 2483 | while (1) { | ||
| 2484 | |||
| 2485 | $middle = (($right + $left) >> 1); | ||
| 2486 | $item = $self->[$middle]; | ||
| 2487 | $cmp = CORE::int($obj->run($item)) || return Sidef::Types::Number::Number::_set_int($middle); | ||
| 2488 | |||
| 2489 | if ($cmp < 0) { | ||
| 2490 | $left = $middle + 1; | ||
| 2491 | $left > $right && last; | ||
| 2492 | } | ||
| 2493 | else { | ||
| 2494 | $right = $middle - 1; | ||
| 2495 | if ($left > $right) { | ||
| 2496 | --$middle; | ||
| 2497 | last; | ||
| 2498 | } | ||
| 2499 | } | ||
| 2500 | } | ||
| 2501 | |||
| 2502 | Sidef::Types::Number::Number::_set_int($middle); | ||
| 2503 | } | ||
| 2504 | |||
| 2505 | sub bindex_le { | ||
| 2506 | my ($self, $obj) = @_; | ||
| 2507 | |||
| 2508 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2509 | goto &bindex_le_by; | ||
| 2510 | } | ||
| 2511 | |||
| 2512 | my $left = 0; | ||
| 2513 | my $right = $#$self; | ||
| 2514 | my ($middle, $item, $cmp); | ||
| 2515 | |||
| 2516 | while (1) { | ||
| 2517 | |||
| 2518 | $middle = (($right + $left) >> 1); | ||
| 2519 | $item = $self->[$middle]; | ||
| 2520 | $cmp = CORE::int($item cmp $obj) || return Sidef::Types::Number::Number::_set_int($middle); | ||
| 2521 | |||
| 2522 | if ($cmp < 0) { | ||
| 2523 | $left = $middle + 1; | ||
| 2524 | $left > $right && last; | ||
| 2525 | } | ||
| 2526 | else { | ||
| 2527 | $right = $middle - 1; | ||
| 2528 | if ($left > $right) { | ||
| 2529 | --$middle; | ||
| 2530 | last; | ||
| 2531 | } | ||
| 2532 | } | ||
| 2533 | } | ||
| 2534 | |||
| 2535 | Sidef::Types::Number::Number::_set_int($middle); | ||
| 2536 | } | ||
| 2537 | |||
| 2538 | sub bindex_min_by { | ||
| 2539 | my ($self, $block) = @_; | ||
| 2540 | |||
| 2541 | my $left = 0; | ||
| 2542 | my $right = $#$self; | ||
| 2543 | my ($middle, $item, $cmp); | ||
| 2544 | |||
| 2545 | while ($left < $right) { | ||
| 2546 | |||
| 2547 | $middle = (($right + $left) >> 1); | ||
| 2548 | $item = $self->[$middle]; | ||
| 2549 | $cmp = CORE::int($block->run($item)); | ||
| 2550 | |||
| 2551 | if ($cmp < 0) { | ||
| 2552 | $left = $middle + 1; | ||
| 2553 | } | ||
| 2554 | else { | ||
| 2555 | $right = $middle; | ||
| 2556 | } | ||
| 2557 | } | ||
| 2558 | |||
| 2559 | Sidef::Types::Number::Number::_set_int($left); | ||
| 2560 | } | ||
| 2561 | |||
| 2562 | sub bindex_min { | ||
| 2563 | my ($self, $obj) = @_; | ||
| 2564 | |||
| 2565 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2566 | goto &bindex_min_by; | ||
| 2567 | } | ||
| 2568 | |||
| 2569 | my $left = 0; | ||
| 2570 | my $right = $#$self; | ||
| 2571 | my ($middle, $item, $cmp); | ||
| 2572 | |||
| 2573 | while ($left < $right) { | ||
| 2574 | |||
| 2575 | $middle = (($right + $left) >> 1); | ||
| 2576 | $item = $self->[$middle]; | ||
| 2577 | $cmp = CORE::int($item cmp $obj); | ||
| 2578 | |||
| 2579 | if ($cmp < 0) { | ||
| 2580 | $left = $middle + 1; | ||
| 2581 | } | ||
| 2582 | else { | ||
| 2583 | $right = $middle; | ||
| 2584 | } | ||
| 2585 | } | ||
| 2586 | |||
| 2587 | Sidef::Types::Number::Number::_set_int($left); | ||
| 2588 | } | ||
| 2589 | |||
| 2590 | sub bindex_max_by { | ||
| 2591 | my ($self, $block) = @_; | ||
| 2592 | |||
| 2593 | my $left = 0; | ||
| 2594 | my $right = $#$self; | ||
| 2595 | my ($middle, $item, $cmp); | ||
| 2596 | |||
| 2597 | while ($left < $right) { | ||
| 2598 | |||
| 2599 | $middle = 1 + (($right + $left) >> 1); | ||
| 2600 | $item = $self->[$middle]; | ||
| 2601 | $cmp = CORE::int($block->run($item)); | ||
| 2602 | |||
| 2603 | if ($cmp > 0) { | ||
| 2604 | $right = $middle - 1; | ||
| 2605 | } | ||
| 2606 | else { | ||
| 2607 | $left = $middle; | ||
| 2608 | } | ||
| 2609 | } | ||
| 2610 | |||
| 2611 | Sidef::Types::Number::Number::_set_int($right); | ||
| 2612 | } | ||
| 2613 | |||
| 2614 | sub bindex_max { | ||
| 2615 | my ($self, $obj) = @_; | ||
| 2616 | |||
| 2617 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2618 | goto &bindex_max_by; | ||
| 2619 | } | ||
| 2620 | |||
| 2621 | my $left = 0; | ||
| 2622 | my $right = $#$self; | ||
| 2623 | my ($middle, $item, $cmp); | ||
| 2624 | |||
| 2625 | while ($left < $right) { | ||
| 2626 | |||
| 2627 | $middle = 1 + (($right + $left) >> 1); | ||
| 2628 | $item = $self->[$middle]; | ||
| 2629 | $cmp = CORE::int($item cmp $obj); | ||
| 2630 | |||
| 2631 | if ($cmp > 0) { | ||
| 2632 | $right = $middle - 1; | ||
| 2633 | } | ||
| 2634 | else { | ||
| 2635 | $left = $middle; | ||
| 2636 | } | ||
| 2637 | } | ||
| 2638 | |||
| 2639 | Sidef::Types::Number::Number::_set_int($right); | ||
| 2640 | } | ||
| 2641 | |||
| 2642 | sub bsearch_min { | ||
| 2643 | my ($self, $obj) = @_; | ||
| 2644 | my $index = $self->bindex_min($obj); | ||
| 2645 | $index->is_mone ? undef : $self->[CORE::int($index)]; | ||
| 2646 | } | ||
| 2647 | |||
| 2648 | sub bsearch_max { | ||
| 2649 | my ($self, $obj) = @_; | ||
| 2650 | my $index = $self->bindex_max($obj); | ||
| 2651 | $index->is_mone ? undef : $self->[CORE::int($index)]; | ||
| 2652 | } | ||
| 2653 | |||
| 2654 | sub bsearch_le { | ||
| 2655 | my ($self, $obj) = @_; | ||
| 2656 | my $index = $self->bindex_le($obj); | ||
| 2657 | $index->is_mone ? undef : $self->[CORE::int($index)]; | ||
| 2658 | } | ||
| 2659 | |||
| 2660 | *bsearch_le_by = \&bsearch_le; | ||
| 2661 | |||
| 2662 | sub bsearch_ge { | ||
| 2663 | my ($self, $obj) = @_; | ||
| 2664 | my $index = $self->bindex_ge($obj); | ||
| 2665 | $index->is_mone ? undef : $self->[CORE::int($index)]; | ||
| 2666 | } | ||
| 2667 | |||
| 2668 | *bsearch_ge_by = \&bsearch_ge; | ||
| 2669 | |||
| 2670 | sub index { | ||
| 2671 | my ($self, $obj) = @_; | ||
| 2672 | |||
| 2673 | if (@_ > 1) { | ||
| 2674 | |||
| 2675 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2676 | foreach my $i (0 .. $#$self) { | ||
| 2677 | $obj->run($self->[$i]) | ||
| 2678 | && return Sidef::Types::Number::Number::_set_int($i); | ||
| 2679 | } | ||
| 2680 | return Sidef::Types::Number::Number::MONE; | ||
| 2681 | } | ||
| 2682 | |||
| 2683 | foreach my $i (0 .. $#$self) { | ||
| 2684 | $self->[$i] eq $obj | ||
| 2685 | and return Sidef::Types::Number::Number::_set_int($i); | ||
| 2686 | } | ||
| 2687 | |||
| 2688 | return Sidef::Types::Number::Number::MONE; | ||
| 2689 | } | ||
| 2690 | |||
| 2691 | @$self | ||
| 2692 | ? Sidef::Types::Number::Number::ZERO | ||
| 2693 | : Sidef::Types::Number::Number::MONE; | ||
| 2694 | } | ||
| 2695 | |||
| 2696 | *index_by = \&index; | ||
| 2697 | *first_index = \&index; | ||
| 2698 | *first_index_by = \&index; | ||
| 2699 | |||
| 2700 | sub rindex { | ||
| 2701 | my ($self, $obj) = @_; | ||
| 2702 | |||
| 2703 | if (@_ > 1) { | ||
| 2704 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2705 | for (my $i = $#$self ; $i >= 0 ; $i--) { | ||
| 2706 | $obj->run($self->[$i]) | ||
| 2707 | && return Sidef::Types::Number::Number::_set_int($i); | ||
| 2708 | } | ||
| 2709 | |||
| 2710 | return Sidef::Types::Number::Number::MONE; | ||
| 2711 | } | ||
| 2712 | |||
| 2713 | for (my $i = $#$self ; $i >= 0 ; $i--) { | ||
| 2714 | $self->[$i] eq $obj | ||
| 2715 | and return Sidef::Types::Number::Number::_set_int($i); | ||
| 2716 | } | ||
| 2717 | |||
| 2718 | return Sidef::Types::Number::Number::MONE; | ||
| 2719 | } | ||
| 2720 | |||
| 2721 | $self->end; | ||
| 2722 | } | ||
| 2723 | |||
| 2724 | *rindex_by = \&rindex; | ||
| 2725 | *last_index = \&rindex; | ||
| 2726 | *last_index_by = \&rindex; | ||
| 2727 | |||
| 2728 | sub pairmap { | ||
| 2729 | my ($self, $block) = @_; | ||
| 2730 | |||
| 2731 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 2732 | |||
| 2733 | my $end = @$self; | ||
| 2734 | |||
| 2735 | my @array; | ||
| 2736 | for (my $i = 1 ; $i < $end ; $i += 2) { | ||
| 2737 | CORE::push(@array, $block->run(@$self[$i - 1, $i])); | ||
| 2738 | } | ||
| 2739 | |||
| 2740 | bless \@array; | ||
| 2741 | } | ||
| 2742 | |||
| 2743 | *pair_map = \&pairmap; | ||
| 2744 | |||
| 2745 | sub shuffle { | ||
| 2746 | my ($self) = @_; | ||
| 2747 | bless [List::Util::shuffle(@$self)], ref($self); | ||
| 2748 | } | ||
| 2749 | |||
| 2750 | sub weighted_shuffle_by { | ||
| 2751 | my ($self, $block) = @_; | ||
| 2752 | |||
| 2753 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2754 | |||
| 2755 | #<<< | ||
| 2756 | my @weights = map { | ||
| 2757 | Sidef::Types::Number::Number::__numify__(${Sidef::Types::Number::Number->new(scalar $block->run($_))}) | ||
| 2758 | } @$self; | ||
| 2759 | #>>> | ||
| 2760 | |||
| 2761 | my @vals = @$self; | ||
| 2762 | my $total = List::Util::sum(@weights); | ||
| 2763 | |||
| 2764 | my @shuffled; | ||
| 2765 | while (@vals > 1) { | ||
| 2766 | my $select = CORE::int(CORE::rand($total)); | ||
| 2767 | |||
| 2768 | my $idx = 0; | ||
| 2769 | while ($select >= $weights[$idx]) { | ||
| 2770 | $select -= $weights[$idx++]; | ||
| 2771 | } | ||
| 2772 | |||
| 2773 | CORE::push(@shuffled, CORE::splice(@vals, $idx, 1)); | ||
| 2774 | $total -= CORE::splice(@weights, $idx, 1); | ||
| 2775 | } | ||
| 2776 | |||
| 2777 | CORE::push(@shuffled, @vals) if @vals; | ||
| 2778 | bless \@shuffled, ref($self); | ||
| 2779 | } | ||
| 2780 | |||
| 2781 | sub best_shuffle { | ||
| 2782 | my ($s) = @_; | ||
| 2783 | my ($t) = $s->shuffle; | ||
| 2784 | |||
| 2785 | my $end = $#$s; | ||
| 2786 | |||
| 2787 | foreach my $i (0 .. $end) { | ||
| 2788 | foreach my $j (0 .. $end) { | ||
| 2789 | $i != $j | ||
| 2790 | && !($t->[$i] eq $s->[$j]) | ||
| 2791 | && !($t->[$j] eq $s->[$i]) | ||
| 2792 | && do { | ||
| 2793 | @$t[$i, $j] = @$t[$j, $i]; | ||
| 2794 | last; | ||
| 2795 | } | ||
| 2796 | } | ||
| 2797 | } | ||
| 2798 | |||
| 2799 | $t; | ||
| 2800 | } | ||
| 2801 | |||
| 2802 | *bshuffle = \&best_shuffle; | ||
| 2803 | |||
| 2804 | sub accumulate_by { | ||
| 2805 | my ($self, $block) = @_; | ||
| 2806 | |||
| 2807 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 2808 | |||
| 2809 | my @acc; | ||
| 2810 | my $prev; | ||
| 2811 | |||
| 2812 | foreach my $item (@$self) { | ||
| 2813 | if (defined($prev)) { | ||
| 2814 | CORE::push(@acc, $prev = $prev->add($block->run($item))); | ||
| 2815 | } | ||
| 2816 | else { | ||
| 2817 | CORE::push(@acc, $prev = $block->run($item)); | ||
| 2818 | } | ||
| 2819 | } | ||
| 2820 | |||
| 2821 | bless \@acc, ref($self); | ||
| 2822 | } | ||
| 2823 | |||
| 2824 | *acc_by = \&accumulate_by; | ||
| 2825 | |||
| 2826 | sub accumulate { | ||
| 2827 | my ($self, $block) = @_; | ||
| 2828 | |||
| 2829 | if (defined($block)) { | ||
| 2830 | goto &accumulate_by; | ||
| 2831 | } | ||
| 2832 | |||
| 2833 | my @acc; | ||
| 2834 | my $prev; | ||
| 2835 | |||
| 2836 | foreach my $item (@$self) { | ||
| 2837 | if (defined($prev)) { | ||
| 2838 | CORE::push(@acc, $prev = $prev->add($item)); | ||
| 2839 | } | ||
| 2840 | else { | ||
| 2841 | CORE::push(@acc, $prev = $item); | ||
| 2842 | } | ||
| 2843 | } | ||
| 2844 | |||
| 2845 | bless \@acc, ref($self); | ||
| 2846 | } | ||
| 2847 | |||
| 2848 | *acc = \&accumulate; | ||
| 2849 | |||
| 2850 | sub differences { | ||
| 2851 | my ($self, $n) = @_; | ||
| 2852 | |||
| 2853 | if (defined($n)) { | ||
| 2854 | $n = CORE::int($n); | ||
| 2855 | } | ||
| 2856 | else { | ||
| 2857 | $n = 1; | ||
| 2858 | } | ||
| 2859 | |||
| 2860 | my @diffs = @$self; | ||
| 2861 | |||
| 2862 | foreach my $i (1 .. $n) { | ||
| 2863 | my @tmp; | ||
| 2864 | my $prev = shift(@diffs); | ||
| 2865 | |||
| 2866 | while (@diffs) { | ||
| 2867 | my $curr = shift(@diffs); | ||
| 2868 | CORE::push(@tmp, $curr->sub($prev)); | ||
| 2869 | $prev = $curr; | ||
| 2870 | } | ||
| 2871 | @diffs = @tmp; | ||
| 2872 | } | ||
| 2873 | |||
| 2874 | bless \@diffs, ref($self); | ||
| 2875 | } | ||
| 2876 | |||
| 2877 | *deltas = \&differences; | ||
| 2878 | *diffs = \&differences; | ||
| 2879 | *nth_differences = \&differences; | ||
| 2880 | |||
| 2881 | sub solve_seq { | ||
| 2882 | my ($self, $offset) = @_; | ||
| 2883 | $offset //= Sidef::Types::Number::Number::ZERO; | ||
| 2884 | Sidef::Types::Number::Number::_solve_seq($offset, @$self); | ||
| 2885 | } | ||
| 2886 | |||
| 2887 | sub solve_rec_seq { | ||
| 2888 | my ($self) = @_; | ||
| 2889 | Sidef::Types::Number::Number::_solve_rec_seq(@$self); | ||
| 2890 | } | ||
| 2891 | |||
| 2892 | *find_linear_recurrence = \&solve_rec_seq; | ||
| 2893 | |||
| 2894 | sub binsplit { | ||
| 2895 | my ($self, $block) = @_; | ||
| 2896 | Sidef::Types::Number::Number::_binsplit([@$self], $block); | ||
| 2897 | } | ||
| 2898 | |||
| 2899 | sub reduce { | ||
| 2900 | my ($self, $obj, $initial) = @_; | ||
| 2901 | |||
| 2902 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 2903 | |||
| 2904 | my ($from, $x) = ( | ||
| 2905 | defined($initial) | ||
| 2906 | ? (0, $initial) | ||
| 2907 | : (1, $self->[0]) | ||
| 2908 | ); | ||
| 2909 | |||
| 2910 | foreach my $i ($from .. $#$self) { | ||
| 2911 | $x = $obj->run($x, $self->[$i]); | ||
| 2912 | } | ||
| 2913 | |||
| 2914 | return $x; | ||
| 2915 | } | ||
| 2916 | |||
| 2917 | $self->reduce_operator("$obj", $initial); | ||
| 2918 | } | ||
| 2919 | |||
| 2920 | *inject = \&reduce; | ||
| 2921 | |||
| 2922 | sub map_reduce { | ||
| 2923 | my ($self, $obj, $initial) = @_; | ||
| 2924 | |||
| 2925 | my ($from, $x) = ( | ||
| 2926 | defined($initial) | ||
| 2927 | ? (0, $initial) | ||
| 2928 | : (1, $self->[0]) | ||
| 2929 | ); | ||
| 2930 | |||
| 2931 | my @list = ($x); | ||
| 2932 | foreach my $i ($from .. $#$self) { | ||
| 2933 | $x = $obj->run($x, $self->[$i]); | ||
| 2934 | CORE::push(@list, $x); | ||
| 2935 | } | ||
| 2936 | |||
| 2937 | bless \@list, ref($self); | ||
| 2938 | } | ||
| 2939 | |||
| 2940 | *reduce_map = \&map_reduce; | ||
| 2941 | |||
| 2942 | sub length { | ||
| 2943 | Sidef::Types::Number::Number::_set_int(scalar @{$_[0]}); | ||
| 2944 | } | ||
| 2945 | |||
| 2946 | *len = \&length; # alias | ||
| 2947 | *size = \&length; | ||
| 2948 | |||
| 2949 | sub end { | ||
| 2950 | Sidef::Types::Number::Number::_set_int($#{$_[0]}); | ||
| 2951 | } | ||
| 2952 | |||
| 2953 | *offset = \&end; | ||
| 2954 | |||
| 2955 | sub resize { | ||
| 2956 | my ($self, $num) = @_; | ||
| 2957 | $#$self = $num; | ||
| 2958 | $self; | ||
| 2959 | } | ||
| 2960 | |||
| 2961 | *resize_to = \&resize; | ||
| 2962 | |||
| 2963 | sub pick { | ||
| 2964 | my ($self, $amount) = @_; | ||
| 2965 | |||
| 2966 | if (defined $amount) { | ||
| 2967 | $amount = CORE::int($amount); | ||
| 2968 | |||
| 2969 | my $len = @$self; | ||
| 2970 | if ($amount >= $len) { | ||
| 2971 | return bless([List::Util::shuffle(@$self)], ref($self)); | ||
| 2972 | } | ||
| 2973 | |||
| 2974 | my @result; | ||
| 2975 | for (my ($i, $amount_left) = (0, $len) ; $amount > 0 ; ++$i, --$amount_left) { | ||
| 2976 | my $rand = CORE::int(CORE::rand($amount_left)); | ||
| 2977 | if ($rand < $amount) { | ||
| 2978 | CORE::push(@result, $self->[$i]); | ||
| 2979 | $amount--; | ||
| 2980 | } | ||
| 2981 | } | ||
| 2982 | |||
| 2983 | return bless([List::Util::shuffle(@result)], ref($self)); | ||
| 2984 | } | ||
| 2985 | |||
| 2986 | $self->[CORE::rand(scalar @$self)]; | ||
| 2987 | } | ||
| 2988 | |||
| 2989 | sub rand { | ||
| 2990 | my ($self, $amount) = @_; | ||
| 2991 | |||
| 2992 | if (defined $amount) { | ||
| 2993 | $amount = CORE::int($amount); | ||
| 2994 | |||
| 2995 | my $len = @$self; | ||
| 2996 | return ( | ||
| 2997 | $len > 0 | ||
| 2998 | ? bless([map { $self->[CORE::rand($len)] } 1 .. $amount], ref($self)) | ||
| 2999 | : bless([], ref($self)) | ||
| 3000 | ); | ||
| 3001 | } | ||
| 3002 | |||
| 3003 | $self->[CORE::rand(scalar @$self)]; | ||
| 3004 | } | ||
| 3005 | |||
| 3006 | *sample = \&rand; | ||
| 3007 | |||
| 3008 | sub range { | ||
| 3009 | my ($self) = @_; | ||
| 3010 | Sidef::Types::Range::RangeNumber->new(Sidef::Types::Number::Number::ZERO, | ||
| 3011 | Sidef::Types::Number::Number::_set_int($#$self), | ||
| 3012 | Sidef::Types::Number::Number::ONE, | ||
| 3013 | ); | ||
| 3014 | } | ||
| 3015 | |||
| 3016 | sub indices_by { | ||
| 3017 | my ($self, $block) = @_; | ||
| 3018 | |||
| 3019 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3020 | |||
| 3021 | my @indices; | ||
| 3022 | foreach my $i (0 .. $#$self) { | ||
| 3023 | if ($block->run($self->[$i])) { | ||
| 3024 | CORE::push(@indices, Sidef::Types::Number::Number::_set_int($i)); | ||
| 3025 | } | ||
| 3026 | } | ||
| 3027 | |||
| 3028 | bless \@indices; | ||
| 3029 | } | ||
| 3030 | |||
| 3031 | *keys_by = \&indices_by; | ||
| 3032 | |||
| 3033 | sub indices_of { | ||
| 3034 | my ($self, $arg) = @_; | ||
| 3035 | |||
| 3036 | my @indices; | ||
| 3037 | foreach my $i (0 .. $#$self) { | ||
| 3038 | if ($self->[$i] eq $arg) { | ||
| 3039 | CORE::push(@indices, Sidef::Types::Number::Number::_set_int($i)); | ||
| 3040 | } | ||
| 3041 | } | ||
| 3042 | |||
| 3043 | bless \@indices; | ||
| 3044 | } | ||
| 3045 | |||
| 3046 | *keys_of = \&indices_of; | ||
| 3047 | |||
| 3048 | sub indices { | ||
| 3049 | my ($self, $arg) = @_; | ||
| 3050 | |||
| 3051 | if (defined($arg)) { | ||
| 3052 | goto &indices_by; | ||
| 3053 | } | ||
| 3054 | |||
| 3055 | bless [map { Sidef::Types::Number::Number::_set_int($_) } 0 .. $#$self]; | ||
| 3056 | } | ||
| 3057 | |||
| 3058 | *keys = \&indices; | ||
| 3059 | |||
| 3060 | sub pairs { | ||
| 3061 | my ($self) = @_; | ||
| 3062 | bless [map { Sidef::Types::Array::Pair->new(Sidef::Types::Number::Number::_set_int($_), $self->[$_]) } 0 .. $#$self]; | ||
| 3063 | } | ||
| 3064 | |||
| 3065 | *kv = \&pairs; | ||
| 3066 | *zip_indices = \&pairs; | ||
| 3067 | |||
| 3068 | sub insert { | ||
| 3069 | my ($self, $i, @objects) = @_; | ||
| 3070 | |||
| 3071 | $i = CORE::int($i); | ||
| 3072 | |||
| 3073 | if ($#$self < $i) { | ||
| 3074 | $#$self = $i - 1; | ||
| 3075 | } | ||
| 3076 | |||
| 3077 | CORE::splice(@$self, $i, 0, @objects); | ||
| 3078 | $self; | ||
| 3079 | } | ||
| 3080 | |||
| 3081 | sub binsert { | ||
| 3082 | my ($self, $obj) = @_; | ||
| 3083 | |||
| 3084 | my $left = 0; | ||
| 3085 | my $right = $#$self; | ||
| 3086 | my ($middle, $item, $cmp); | ||
| 3087 | |||
| 3088 | if ($right < 0) { | ||
| 3089 | CORE::push(@$self, $obj); | ||
| 3090 | return $self; | ||
| 3091 | } | ||
| 3092 | |||
| 3093 | while (1) { | ||
| 3094 | $middle = (($right + $left) >> 1); | ||
| 3095 | $item = $self->[$middle]; | ||
| 3096 | $cmp = CORE::int($item cmp $obj) || last; | ||
| 3097 | |||
| 3098 | if ($cmp < 0) { | ||
| 3099 | $left = $middle + 1; | ||
| 3100 | if ($left > $right) { | ||
| 3101 | ++$middle; | ||
| 3102 | last; | ||
| 3103 | } | ||
| 3104 | } | ||
| 3105 | else { | ||
| 3106 | $right = $middle - 1; | ||
| 3107 | $left > $right && last; | ||
| 3108 | } | ||
| 3109 | } | ||
| 3110 | |||
| 3111 | CORE::splice(@$self, $middle, 0, $obj); | ||
| 3112 | $self; | ||
| 3113 | } | ||
| 3114 | |||
| 3115 | sub compact { | ||
| 3116 | my ($self) = @_; | ||
| 3117 | bless [grep { defined($_) } @$self], ref($self); | ||
| 3118 | } | ||
| 3119 | |||
| 3120 | sub unique { | ||
| 3121 | my ($self, $block) = @_; | ||
| 3122 | |||
| 3123 | if (defined($block)) { | ||
| 3124 | return $self->uniq_by($block); | ||
| 3125 | } | ||
| 3126 | |||
| 3127 | my @sorted = do { | ||
| 3128 | |||
| 3129 | my @arr; | ||
| 3130 | my $numeric = 1; | ||
| 3131 | |||
| 3132 | foreach my $i (0 .. $#$self) { | ||
| 3133 | |||
| 3134 | CORE::push(@arr, [$i, $self->[$i]]); | ||
| 3135 | |||
| 3136 | if ($numeric and (ref($self->[$i]) ne 'Sidef::Types::Number::Number' or ref(${$self->[$i]}) eq 'Math::MPC')) { | ||
| 3137 | $numeric = 0; | ||
| 3138 | } | ||
| 3139 | } | ||
| 3140 | |||
| 3141 | if ($numeric) { | ||
| 3142 | return $self->iuniq; | ||
| 3143 | } | ||
| 3144 | |||
| 3145 | CORE::sort { $a->[1] cmp $b->[1] } @arr; | ||
| 3146 | }; | ||
| 3147 | |||
| 3148 | my @unique; | ||
| 3149 | my $max = $#sorted; | ||
| 3150 | |||
| 3151 | for (my $i = 0 ; $i <= $max ; $i++) { | ||
| 3152 | $unique[$sorted[$i][0]] = $sorted[$i][1]; | ||
| 3153 | ++$i while ($i < $max and $sorted[$i][1] eq $sorted[$i + 1][1]); | ||
| 3154 | } | ||
| 3155 | |||
| 3156 | bless [grep { defined($_) } @unique], ref($self); | ||
| 3157 | } | ||
| 3158 | |||
| 3159 | *uniq = \&unique; | ||
| 3160 | *distinct = \&unique; | ||
| 3161 | |||
| 3162 | sub iuniq { | ||
| 3163 | my ($self) = @_; | ||
| 3164 | |||
| 3165 | my @sorted = do { | ||
| 3166 | my @arr; | ||
| 3167 | foreach my $i (0 .. $#$self) { | ||
| 3168 | CORE::push(@arr, [$i, $self->[$i], ${$self->[$i]}]); | ||
| 3169 | } | ||
| 3170 | CORE::sort { $a->[2] <=> $b->[2] } @arr; | ||
| 3171 | }; | ||
| 3172 | |||
| 3173 | my @unique; | ||
| 3174 | my $max = $#sorted; | ||
| 3175 | |||
| 3176 | for (my $i = 0 ; $i <= $max ; $i++) { | ||
| 3177 | $unique[$sorted[$i][0]] = $sorted[$i][1]; | ||
| 3178 | ++$i while ($i < $max and $sorted[$i][2] == $sorted[$i + 1][2]); | ||
| 3179 | } | ||
| 3180 | |||
| 3181 | bless [grep { defined($_) } @unique], ref($self); | ||
| 3182 | } | ||
| 3183 | |||
| 3184 | sub last_unique { | ||
| 3185 | my ($self) = @_; | ||
| 3186 | |||
| 3187 | my @sorted = do { | ||
| 3188 | my @arr; | ||
| 3189 | foreach my $i (0 .. $#$self) { | ||
| 3190 | CORE::push(@arr, [$i, $self->[$i]]); | ||
| 3191 | } | ||
| 3192 | CORE::sort { $a->[1] cmp $b->[1] } @arr; | ||
| 3193 | }; | ||
| 3194 | |||
| 3195 | my @unique; | ||
| 3196 | my $max = $#sorted; | ||
| 3197 | |||
| 3198 | for (my $i = 0 ; $i <= $max ; $i++) { | ||
| 3199 | ++$i while ($i < $max and $sorted[$i][1] eq $sorted[$i + 1][1]); | ||
| 3200 | $unique[$sorted[$i][0]] = $sorted[$i][1]; | ||
| 3201 | } | ||
| 3202 | |||
| 3203 | bless [grep { defined($_) } @unique], ref($self); | ||
| 3204 | } | ||
| 3205 | |||
| 3206 | *last_uniq = \&last_unique; | ||
| 3207 | |||
| 3208 | sub uniq_by { | ||
| 3209 | my ($self, $block) = @_; | ||
| 3210 | |||
| 3211 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3212 | |||
| 3213 | my @sorted = do { | ||
| 3214 | my @arr; | ||
| 3215 | my $i = -1; | ||
| 3216 | foreach my $item (@$self) { | ||
| 3217 | CORE::push(@arr, [++$i, $item, scalar $block->run($item)]); | ||
| 3218 | } | ||
| 3219 | CORE::sort { $a->[2] cmp $b->[2] } @arr; | ||
| 3220 | }; | ||
| 3221 | |||
| 3222 | my @unique; | ||
| 3223 | my $max = $#sorted; | ||
| 3224 | |||
| 3225 | for (my $i = 0 ; $i <= $max ; $i++) { | ||
| 3226 | $unique[$sorted[$i][0]] = $sorted[$i][1]; | ||
| 3227 | ++$i while ($i < $max and $sorted[$i][2] eq $sorted[$i + 1][2]); | ||
| 3228 | } | ||
| 3229 | |||
| 3230 | bless [grep { defined } @unique], ref($self); | ||
| 3231 | } | ||
| 3232 | |||
| 3233 | *unique_by = \&uniq_by; | ||
| 3234 | |||
| 3235 | sub last_uniq_by { | ||
| 3236 | my ($self, $block) = @_; | ||
| 3237 | |||
| 3238 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3239 | |||
| 3240 | my @sorted = do { | ||
| 3241 | my @arr; | ||
| 3242 | my $i = -1; | ||
| 3243 | foreach my $item (@$self) { | ||
| 3244 | CORE::push(@arr, [++$i, $item, scalar $block->run($item)]); | ||
| 3245 | } | ||
| 3246 | CORE::sort { $a->[2] cmp $b->[2] } @arr; | ||
| 3247 | }; | ||
| 3248 | |||
| 3249 | my @unique; | ||
| 3250 | my $max = $#sorted; | ||
| 3251 | |||
| 3252 | for (my $i = 0 ; $i <= $max ; $i++) { | ||
| 3253 | ++$i while ($i < $max and $sorted[$i][2] eq $sorted[$i + 1][2]); | ||
| 3254 | $unique[$sorted[$i][0]] = $sorted[$i][1]; | ||
| 3255 | } | ||
| 3256 | |||
| 3257 | bless [grep { defined } @unique], ref($self); | ||
| 3258 | } | ||
| 3259 | |||
| 3260 | *last_unique_by = \&last_uniq_by; | ||
| 3261 | |||
| 3262 | sub abbrev { | ||
| 3263 | my ($self, $pattern) = @_; | ||
| 3264 | |||
| 3265 | if (defined($pattern)) { | ||
| 3266 | if (ref($pattern) eq 'Sidef::Types::Regex::Regex') { | ||
| 3267 | $pattern = $pattern->get_value; | ||
| 3268 | } | ||
| 3269 | else { | ||
| 3270 | $pattern = qr/\Q$pattern\E/; | ||
| 3271 | } | ||
| 3272 | } | ||
| 3273 | |||
| 3274 | my (%seen, %table); | ||
| 3275 | foreach my $item (@$self) { | ||
| 3276 | my $word = "$item"; | ||
| 3277 | my $length = CORE::length($word) || next; | ||
| 3278 | |||
| 3279 | for (my $len = $length ; $len >= 1 ; --$len) { | ||
| 3280 | my $abbrev = substr($word, 0, $len); | ||
| 3281 | |||
| 3282 | if (defined($pattern)) { | ||
| 3283 | ($abbrev =~ $pattern) || next; | ||
| 3284 | } | ||
| 3285 | |||
| 3286 | my $count = ++$seen{$abbrev}; | ||
| 3287 | |||
| 3288 | if ($count == 1) { | ||
| 3289 | $table{$abbrev} = $item; | ||
| 3290 | } | ||
| 3291 | elsif ($count == 2) { | ||
| 3292 | CORE::delete($table{$abbrev}); | ||
| 3293 | } | ||
| 3294 | else { | ||
| 3295 | last; | ||
| 3296 | } | ||
| 3297 | } | ||
| 3298 | } | ||
| 3299 | |||
| 3300 | foreach my $item (@$self) { | ||
| 3301 | my $word = "$item"; | ||
| 3302 | |||
| 3303 | if (defined($pattern)) { | ||
| 3304 | ($word =~ $pattern) || next; | ||
| 3305 | } | ||
| 3306 | |||
| 3307 | $table{$word} = $item; | ||
| 3308 | } | ||
| 3309 | |||
| 3310 | Sidef::Types::Hash::Hash->new(\%table); | ||
| 3311 | } | ||
| 3312 | |||
| 3313 | *abbreviations = \&abbrev; | ||
| 3314 | |||
| 3315 | sub uniq_prefs { | ||
| 3316 | my ($self, $block) = @_; | ||
| 3317 | |||
| 3318 | my $tail = {}; # some unique value | ||
| 3319 | my $callback = defined($block); | ||
| 3320 | |||
| 3321 | my %table; | ||
| 3322 | foreach my $sub_array (@$self) { | ||
| 3323 | my $ref = \%table; | ||
| 3324 | foreach my $item (@$sub_array) { | ||
| 3325 | $ref = $ref->{$item} //= {}; | ||
| 3326 | } | ||
| 3327 | $ref->{$tail} = $sub_array; | ||
| 3328 | } | ||
| 3329 | |||
| 3330 | my @abbrev; | ||
| 3331 | sub { | ||
| 3332 | my ($hash) = @_; | ||
| 3333 | |||
| 3334 | foreach my $key (my @keys = CORE::sort(keys(%{$hash}))) { | ||
| 3335 | next if $key eq $tail; | ||
| 3336 | __SUB__->($hash->{$key}); | ||
| 3337 | |||
| 3338 | if ($#keys > 0) { | ||
| 3339 | my $count = 0; | ||
| 3340 | my $ref = delete $hash->{$key}; | ||
| 3341 | while (my ($key) = CORE::each %{$ref}) { | ||
| 3342 | if ($key eq $tail) { | ||
| 3343 | |||
| 3344 | if ($callback) { | ||
| 3345 | $block->run(@{$ref->{$key}}[0 .. $#{$ref->{$key}} - $count]); | ||
| 3346 | } | ||
| 3347 | else { | ||
| 3348 | CORE::push(@abbrev, bless([@{$ref->{$key}}[0 .. $#{$ref->{$key}} - $count]])); | ||
| 3349 | } | ||
| 3350 | |||
| 3351 | last; | ||
| 3352 | } | ||
| 3353 | $ref = $ref->{$key}; | ||
| 3354 | $count++; | ||
| 3355 | } | ||
| 3356 | } | ||
| 3357 | } | ||
| 3358 | } | ||
| 3359 | ->(\%table); | ||
| 3360 | |||
| 3361 | bless \@abbrev; | ||
| 3362 | } | ||
| 3363 | |||
| 3364 | *unique_prefixes = \&uniq_prefs; | ||
| 3365 | |||
| 3366 | sub contains { | ||
| 3367 | my ($self, $obj, @extra) = @_; | ||
| 3368 | |||
| 3369 | if (ref($obj) eq 'Sidef::Types::Block::Block') { | ||
| 3370 | foreach my $item (@$self) { | ||
| 3371 | if ($obj->run($item)) { | ||
| 3372 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 3373 | } | ||
| 3374 | } | ||
| 3375 | |||
| 3376 | return (Sidef::Types::Bool::Bool::FALSE); | ||
| 3377 | } | ||
| 3378 | |||
| 3379 | my $end = $#$self; | ||
| 3380 | |||
| 3381 | foreach my $i (0 .. $end) { | ||
| 3382 | if ($self->[$i] eq $obj) { | ||
| 3383 | |||
| 3384 | my $ok = 1; | ||
| 3385 | my $j = $i; | ||
| 3386 | |||
| 3387 | foreach my $obj (@extra) { | ||
| 3388 | if (++$j <= $end and $self->[$j] eq $obj) { | ||
| 3389 | ## ok | ||
| 3390 | } | ||
| 3391 | else { | ||
| 3392 | $ok = 0; | ||
| 3393 | last; | ||
| 3394 | } | ||
| 3395 | } | ||
| 3396 | |||
| 3397 | $ok && return (Sidef::Types::Bool::Bool::TRUE); | ||
| 3398 | } | ||
| 3399 | } | ||
| 3400 | |||
| 3401 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 3402 | } | ||
| 3403 | |||
| 3404 | *has = \&contains; | ||
| 3405 | *contain = \&contains; | ||
| 3406 | *include = \&contains; | ||
| 3407 | *includes = \&contains; | ||
| 3408 | |||
| 3409 | sub contains_type { | ||
| 3410 | my ($self, $obj) = @_; | ||
| 3411 | |||
| 3412 | my $ref = ref($obj); | ||
| 3413 | |||
| 3414 | foreach my $item (@$self) { | ||
| 3415 | if (ref($item) eq $ref || UNIVERSAL::isa($item, $ref)) { | ||
| 3416 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 3417 | } | ||
| 3418 | } | ||
| 3419 | |||
| 3420 | return (Sidef::Types::Bool::Bool::FALSE); | ||
| 3421 | } | ||
| 3422 | |||
| 3423 | sub contains_any { | ||
| 3424 | my ($self, $array) = @_; | ||
| 3425 | |||
| 3426 | if (ref($array) ne __PACKAGE__) { | ||
| 3427 | $array = $array->to_a; | ||
| 3428 | } | ||
| 3429 | |||
| 3430 | foreach my $item (@$array) { | ||
| 3431 | if ($self->contains($item)) { | ||
| 3432 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 3433 | } | ||
| 3434 | } | ||
| 3435 | |||
| 3436 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 3437 | } | ||
| 3438 | |||
| 3439 | sub contains_all { | ||
| 3440 | my ($self, $array) = @_; | ||
| 3441 | |||
| 3442 | if (ref($array) ne __PACKAGE__) { | ||
| 3443 | $array = $array->to_a; | ||
| 3444 | } | ||
| 3445 | |||
| 3446 | foreach my $item (@$array) { | ||
| 3447 | unless ($self->contains($item)) { | ||
| 3448 | return (Sidef::Types::Bool::Bool::FALSE); | ||
| 3449 | } | ||
| 3450 | } | ||
| 3451 | |||
| 3452 | (Sidef::Types::Bool::Bool::TRUE); | ||
| 3453 | } | ||
| 3454 | |||
| 3455 | sub shift { | ||
| 3456 | my ($self, $num) = @_; | ||
| 3457 | |||
| 3458 | if (defined $num) { | ||
| 3459 | return bless([CORE::splice(@$self, 0, $num)], ref($self)); | ||
| 3460 | } | ||
| 3461 | |||
| 3462 | shift(@$self); | ||
| 3463 | } | ||
| 3464 | |||
| 3465 | *drop_first = \&shift; | ||
| 3466 | *drop_left = \&shift; | ||
| 3467 | |||
| 3468 | sub shift_while { | ||
| 3469 | my ($self, $block) = @_; | ||
| 3470 | |||
| 3471 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3472 | |||
| 3473 | while (@$self and $block->run($self->[0])) { | ||
| 3474 | CORE::shift(@$self); | ||
| 3475 | } | ||
| 3476 | |||
| 3477 | $self; | ||
| 3478 | } | ||
| 3479 | |||
| 3480 | sub pop { | ||
| 3481 | my ($self, $num) = @_; | ||
| 3482 | |||
| 3483 | if (defined $num) { | ||
| 3484 | $num = CORE::int($num); | ||
| 3485 | $num = $num > $#$self ? 0 : @$self - $num; | ||
| 3486 | return bless([CORE::splice(@$self, $num)], ref($self)); | ||
| 3487 | } | ||
| 3488 | |||
| 3489 | pop(@$self); | ||
| 3490 | } | ||
| 3491 | |||
| 3492 | *drop_last = \&pop; | ||
| 3493 | *drop_right = \&pop; | ||
| 3494 | |||
| 3495 | sub pop_while { | ||
| 3496 | my ($self, $block) = @_; | ||
| 3497 | |||
| 3498 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3499 | |||
| 3500 | while (@$self and $block->run($self->[-1])) { | ||
| 3501 | CORE::pop(@$self); | ||
| 3502 | } | ||
| 3503 | |||
| 3504 | $self; | ||
| 3505 | } | ||
| 3506 | |||
| 3507 | sub pop_rand { | ||
| 3508 | my ($self) = @_; | ||
| 3509 | $#$self > -1 || return undef; | ||
| 3510 | CORE::splice(@$self, CORE::rand(scalar @$self), 1); | ||
| 3511 | } | ||
| 3512 | |||
| 3513 | sub delete_index { | ||
| 3514 | my ($self, $offset) = @_; | ||
| 3515 | CORE::splice(@$self, $offset, 1); | ||
| 3516 | } | ||
| 3517 | |||
| 3518 | *pop_at = \&delete_index; | ||
| 3519 | *delete_at = \&delete_index; | ||
| 3520 | |||
| 3521 | sub splice { | ||
| 3522 | my ($self, $offset, $length, @objects) = @_; | ||
| 3523 | |||
| 3524 | $offset = defined($offset) ? CORE::int($offset) : 0; | ||
| 3525 | $length = defined($length) ? CORE::int($length) : scalar(@$self); | ||
| 3526 | |||
| 3527 | bless [CORE::splice(@$self, $offset, $length, @objects)], ref($self); | ||
| 3528 | } | ||
| 3529 | |||
| 3530 | sub take_right { | ||
| 3531 | my ($self, $amount) = @_; | ||
| 3532 | |||
| 3533 | my $end = $#$self; | ||
| 3534 | $amount = CORE::int($amount); | ||
| 3535 | $amount = $end > ($amount - 1) ? $amount - 1 : $end; | ||
| 3536 | |||
| 3537 | bless [@$self[$end - $amount .. $end]], ref($self); | ||
| 3538 | } | ||
| 3539 | |||
| 3540 | sub take_left { | ||
| 3541 | my ($self, $amount) = @_; | ||
| 3542 | |||
| 3543 | my $end = $#$self; | ||
| 3544 | $amount = CORE::int($amount); | ||
| 3545 | $amount = $end > ($amount - 1) ? $amount - 1 : $end; | ||
| 3546 | |||
| 3547 | bless [@$self[0 .. $amount]], ref($self); | ||
| 3548 | } | ||
| 3549 | |||
| 3550 | sub is_empty { | ||
| 3551 | my ($self) = @_; | ||
| 3552 | ($#$self < 0) | ||
| 3553 | ? (Sidef::Types::Bool::Bool::TRUE) | ||
| 3554 | : (Sidef::Types::Bool::Bool::FALSE); | ||
| 3555 | } | ||
| 3556 | |||
| 3557 | sub clear { | ||
| 3558 | my ($self) = @_; | ||
| 3559 | @$self = (); | ||
| 3560 | $self; | ||
| 3561 | } | ||
| 3562 | |||
| 3563 | sub sort { | ||
| 3564 | my ($self, $block) = @_; | ||
| 3565 | |||
| 3566 | if (defined $block) { | ||
| 3567 | return bless [CORE::sort { scalar $block->run($a, $b) } @$self], ref($self); | ||
| 3568 | } | ||
| 3569 | |||
| 3570 | my $numeric = 1; | ||
| 3571 | |||
| 3572 | foreach my $item (@$self) { | ||
| 3573 | if (ref($item) ne 'Sidef::Types::Number::Number' or ref($$item) eq 'Math::MPC') { | ||
| 3574 | $numeric = 0; | ||
| 3575 | last; | ||
| 3576 | } | ||
| 3577 | } | ||
| 3578 | |||
| 3579 | if ($numeric) { | ||
| 3580 | return $self->isort; | ||
| 3581 | } | ||
| 3582 | |||
| 3583 | bless [CORE::sort { $a cmp $b } @$self], ref($self); | ||
| 3584 | } | ||
| 3585 | |||
| 3586 | sub isort { | ||
| 3587 | my ($self) = @_; | ||
| 3588 | bless [CORE::sort { $$a <=> $$b } @$self], ref($self); | ||
| 3589 | } | ||
| 3590 | |||
| 3591 | sub sort_by { | ||
| 3592 | my ($self, $block) = @_; | ||
| 3593 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3594 | my @keys = map { scalar $block->run($_) } @$self; | ||
| 3595 | bless [@{$self}[CORE::sort { $keys[$a] cmp $keys[$b] } 0 .. $#$self]], ref($self); | ||
| 3596 | } | ||
| 3597 | |||
| 3598 | sub isort_by { | ||
| 3599 | my ($self, $block) = @_; | ||
| 3600 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 3601 | my @keys = map { ${$block->run($_)} } @$self; | ||
| 3602 | bless [@{$self}[CORE::sort { $keys[$a] <=> $keys[$b] } 0 .. $#$self]], ref($self); | ||
| 3603 | } | ||
| 3604 | |||
| 3605 | foreach my $name ( | ||
| 3606 | qw( | ||
| 3607 | derangements | ||
| 3608 | permutations | ||
| 3609 | circular_permutations | ||
| 3610 | ) | ||
| 3611 | ) { | ||
| 3612 | |||
| 3613 | no strict 'refs'; | ||
| 3614 | |||
| 3615 | *{__PACKAGE__ . '::' . $name} = sub { | ||
| 3616 | my ($self, $block) = @_; | ||
| 3617 | |||
| 3618 | require Algorithm::Combinatorics; | ||
| 3619 | my $iter = &{'Algorithm::Combinatorics::' . $name}([@$self]); | ||
| 3620 | |||
| 3621 | if (defined($block)) { | ||
| 3622 | while (defined(my $arr = $iter->next)) { | ||
| 3623 | $block->run(@$arr); | ||
| 3624 | } | ||
| 3625 | return $self; | ||
| 3626 | } | ||
| 3627 | |||
| 3628 | my @result; | ||
| 3629 | while (defined(my $arr = $iter->next)) { | ||
| 3630 | push @result, bless [@$arr]; | ||
| 3631 | } | ||
| 3632 | |||
| 3633 | bless \@result; | ||
| 3634 | }; | ||
| 3635 | } | ||
| 3636 | |||
| 3637 | *complete_permutations = \&derangements; | ||
| 3638 | |||
| 3639 | foreach my $name ( | ||
| 3640 | qw( | ||
| 3641 | variations | ||
| 3642 | variations_with_repetition | ||
| 3643 | combinations | ||
| 3644 | combinations_with_repetition | ||
| 3645 | subsets | ||
| 3646 | ) | ||
| 3647 | ) { | ||
| 3648 | |||
| 3649 | no strict 'refs'; | ||
| 3650 | |||
| 3651 | *{__PACKAGE__ . '::' . $name} = sub { | ||
| 3652 | my ($self, $k, $block) = @_; | ||
| 3653 | |||
| 3654 | require Algorithm::Combinatorics; | ||
| 3655 | |||
| 3656 | if (not defined($block) and ref($k) eq 'Sidef::Types::Block::Block') { | ||
| 3657 | ($block, $k) = ($k, undef); | ||
| 3658 | } | ||
| 3659 | |||
| 3660 | my $iter = do { | ||
| 3661 | local $SIG{__WARN__} = sub { }; | ||
| 3662 | &{'Algorithm::Combinatorics::' . $name}([@$self], defined($k) ? CORE::int($k) : ()); | ||
| 3663 | }; | ||
| 3664 | |||
| 3665 | if (defined($block)) { | ||
| 3666 | while (defined(my $arr = $iter->next)) { | ||
| 3667 | $block->run(@$arr); | ||
| 3668 | } | ||
| 3669 | return $self; | ||
| 3670 | } | ||
| 3671 | |||
| 3672 | my @result; | ||
| 3673 | while (defined(my $arr = $iter->next)) { | ||
| 3674 | push @result, bless [@$arr]; | ||
| 3675 | } | ||
| 3676 | |||
| 3677 | bless \@result; | ||
| 3678 | }; | ||
| 3679 | } | ||
| 3680 | |||
| 3681 | *tuples = \&variations; | ||
| 3682 | *tuples_with_repetition = \&variations_with_repetition; | ||
| 3683 | |||
| 3684 | sub partitions { | ||
| 3685 | my ($self, $k, $block) = @_; | ||
| 3686 | |||
| 3687 | require Algorithm::Combinatorics; | ||
| 3688 | |||
| 3689 | if (not defined($block) and ref($k) eq 'Sidef::Types::Block::Block') { | ||
| 3690 | ($block, $k) = ($k, undef); | ||
| 3691 | } | ||
| 3692 | |||
| 3693 | my $iter = do { | ||
| 3694 | local $SIG{__WARN__} = sub { }; | ||
| 3695 | Algorithm::Combinatorics::partitions([@$self], defined($k) ? CORE::int($k) : ()); | ||
| 3696 | }; | ||
| 3697 | |||
| 3698 | if (defined($block)) { | ||
| 3699 | while (defined(my $arr = $iter->next)) { | ||
| 3700 | $block->run(map { bless $_ } @$arr); | ||
| 3701 | } | ||
| 3702 | return $self; | ||
| 3703 | } | ||
| 3704 | |||
| 3705 | my @result; | ||
| 3706 | while (defined(my $arr = $iter->next)) { | ||
| 3707 | push @result, bless [map { bless $_ } @$arr]; | ||
| 3708 | } | ||
| 3709 | |||
| 3710 | bless \@result; | ||
| 3711 | } | ||
| 3712 | |||
| 3713 | sub ordered_partitions { | ||
| 3714 | my ($self, $k, $block) = @_; | ||
| 3715 | |||
| 3716 | require Algorithm::Combinatorics; | ||
| 3717 | |||
| 3718 | if (not defined($block) and ref($k) eq 'Sidef::Types::Block::Block') { | ||
| 3719 | ($block, $k) = ($k, undef); | ||
| 3720 | } | ||
| 3721 | |||
| 3722 | my $iter = do { | ||
| 3723 | local $SIG{__WARN__} = sub { }; | ||
| 3724 | Algorithm::Combinatorics::subsets([0 .. $#{$self} - 1], (defined($k) ? (CORE::int($k) - 1) : ())); | ||
| 3725 | }; | ||
| 3726 | |||
| 3727 | if (defined($block)) { | ||
| 3728 | while (defined(my $indices = $iter->next)) { | ||
| 3729 | $block->run(@{$self->segment(@$indices)}); | ||
| 3730 | } | ||
| 3731 | return $self; | ||
| 3732 | } | ||
| 3733 | |||
| 3734 | my @result; | ||
| 3735 | while (defined(my $indices = $iter->next)) { | ||
| 3736 | push @result, $self->segment(@$indices); | ||
| 3737 | } | ||
| 3738 | |||
| 3739 | bless \@result; | ||
| 3740 | } | ||
| 3741 | |||
| 3742 | sub nth_permutation { | ||
| 3743 | my ($self, $n) = @_; | ||
| 3744 | |||
| 3745 | my @perm; | ||
| 3746 | my @arr = @$self; | ||
| 3747 | |||
| 3748 | if (ref($n) ne 'Sidef::Types::Number::Number') { | ||
| 3749 | $n = Sidef::Types::Number::Number->new($n); | ||
| 3750 | } | ||
| 3751 | |||
| 3752 | $n = $n->int; | ||
| 3753 | $n = Sidef::Types::Number::Number::_any2mpz($$n) // return undef; | ||
| 3754 | |||
| 3755 | my $sgn = Math::GMPz::Rmpz_sgn($n); | ||
| 3756 | |||
| 3757 | if ($sgn < 0) { | ||
| 3758 | Math::GMPz::Rmpz_neg($n, $n); | ||
| 3759 | @arr = CORE::reverse(@arr); | ||
| 3760 | } | ||
| 3761 | elsif ($sgn == 0) { | ||
| 3762 | return bless \@arr; | ||
| 3763 | } | ||
| 3764 | |||
| 3765 | state $f = Math::GMPz::Rmpz_init_nobless(); | ||
| 3766 | state $q = Math::GMPz::Rmpz_init_nobless(); | ||
| 3767 | |||
| 3768 | Math::GMPz::Rmpz_fac_ui($f, scalar(@arr)); # f = factorial(len) | ||
| 3769 | |||
| 3770 | while (my $len = scalar(@arr)) { | ||
| 3771 | Math::GMPz::Rmpz_divexact_ui($f, $f, $len); # f = f/len | ||
| 3772 | Math::GMPz::Rmpz_divmod($q, $n, $n, $f); # q = n//f ;; n = n%f | ||
| 3773 | Math::GMPz::Rmpz_mod_ui($q, $q, $len); # q = q%len | ||
| 3774 | CORE::push(@perm, CORE::splice(@arr, Math::GMPz::Rmpz_get_ui($q), 1)); | ||
| 3775 | } | ||
| 3776 | |||
| 3777 | bless \@perm; | ||
| 3778 | } | ||
| 3779 | |||
| 3780 | *nth_perm = \&nth_permutation; | ||
| 3781 | |||
| 3782 | sub random_permutation { | ||
| 3783 | my ($self) = @_; | ||
| 3784 | $self->nth_permutation($self->len->factorial->irand); | ||
| 3785 | } | ||
| 3786 | |||
| 3787 | *rand_perm = \&random_permutation; | ||
| 3788 | |||
| 3789 | sub next_permutation { | ||
| 3790 | my ($self) = @_; | ||
| 3791 | |||
| 3792 | my $k = $#$self; | ||
| 3793 | return Sidef::Types::Bool::Bool::FALSE if ($k < 0); | ||
| 3794 | |||
| 3795 | my $i = $k - 1; | ||
| 3796 | while ($i >= 0 and $self->[$i]->ge($self->[$i + 1])) { | ||
| 3797 | --$i; | ||
| 3798 | } | ||
| 3799 | |||
| 3800 | if ($i == -1) { | ||
| 3801 | @$self = CORE::reverse(@$self); | ||
| 3802 | return Sidef::Types::Bool::Bool::FALSE; | ||
| 3803 | } | ||
| 3804 | |||
| 3805 | if ($self->[$i + 1]->gt($self->[$k])) { | ||
| 3806 | @{$self}[$i + 1 .. $k] = CORE::reverse(@{$self}[$i + 1 .. $k]); | ||
| 3807 | } | ||
| 3808 | |||
| 3809 | my $j = $i + 1; | ||
| 3810 | while ($self->[$i]->ge($self->[$j])) { | ||
| 3811 | ++$j; | ||
| 3812 | } | ||
| 3813 | @{$self}[$i, $j] = @{$self}[$j, $i]; | ||
| 3814 | return Sidef::Types::Bool::Bool::TRUE; | ||
| 3815 | } | ||
| 3816 | |||
| 3817 | sub _unique_permutations { | ||
| 3818 | my ($array, $callback) = @_; | ||
| 3819 | |||
| 3820 | sub { | ||
| 3821 | my ($items, $current_perm) = @_; | ||
| 3822 | |||
| 3823 | if (!@$items) { | ||
| 3824 | $callback->($current_perm); | ||
| 3825 | return; | ||
| 3826 | } | ||
| 3827 | |||
| 3828 | my %level_seen; | ||
| 3829 | for my $i (0 .. $#$items) { | ||
| 3830 | my $item = $items->[$i]; | ||
| 3831 | |||
| 3832 | # Skip iterations for duplicate elements in the same level | ||
| 3833 | next if $level_seen{$item}++; | ||
| 3834 | |||
| 3835 | my @new_items = @$items; | ||
| 3836 | CORE::splice(@new_items, $i, 1); | ||
| 3837 | |||
| 3838 | my @new_perm = (@$current_perm, $item); | ||
| 3839 | __SUB__->(\@new_items, \@new_perm); | ||
| 3840 | } | ||
| 3841 | } | ||
| 3842 | ->($array, []); | ||
| 3843 | } | ||
| 3844 | |||
| 3845 | sub unique_permutations { | ||
| 3846 | my ($self, $block) = @_; | ||
| 3847 | |||
| 3848 | my $vals = $self->sort; | ||
| 3849 | |||
| 3850 | if (!defined($block)) { | ||
| 3851 | my @results; | ||
| 3852 | _unique_permutations( | ||
| 3853 | $vals, | ||
| 3854 | sub { | ||
| 3855 | my ($perm) = @_; | ||
| 3856 | push @results, bless $perm; | ||
| 3857 | } | ||
| 3858 | ); | ||
| 3859 | return bless \@results; | ||
| 3860 | } | ||
| 3861 | |||
| 3862 | my $break = 1; | ||
| 3863 | foreach my $n (1, 2) { | ||
| 3864 | if ($n == 1) { | ||
| 3865 | $block->run(@$vals); | ||
| 3866 | } | ||
| 3867 | $break = 0; | ||
| 3868 | last; | ||
| 3869 | } | ||
| 3870 | |||
| 3871 | while (!$break and $vals->next_permutation) { | ||
| 3872 | $block->run(@$vals); | ||
| 3873 | } | ||
| 3874 | |||
| 3875 | $block; | ||
| 3876 | } | ||
| 3877 | |||
| 3878 | *uniq_permutations = \&unique_permutations; | ||
| 3879 | |||
| 3880 | sub perm2num { | ||
| 3881 | my ($self) = @_; | ||
| 3882 | Sidef::Types::Number::Number::_set_int(Math::Prime::Util::GMP::permtonum([map { CORE::int($_) } @$self]) // return undef); | ||
| 3883 | } | ||
| 3884 | |||
| 3885 | sub det_bareiss { | ||
| 3886 | my ($self) = @_; | ||
| 3887 | $self->to_matrix->det_bareiss; | ||
| 3888 | } | ||
| 3889 | |||
| 3890 | # Reduced row echelon form | ||
| 3891 | sub rref { | ||
| 3892 | my ($self) = @_; | ||
| 3893 | $self->to_matrix->rref; | ||
| 3894 | } | ||
| 3895 | |||
| 3896 | *reduced_row_echelon_form = \&rref; | ||
| 3897 | |||
| 3898 | sub gauss_jordan_invert { | ||
| 3899 | my ($self) = @_; | ||
| 3900 | $self->to_matrix->gauss_jordan_invert; | ||
| 3901 | } | ||
| 3902 | |||
| 3903 | sub gauss_jordan_solve { | ||
| 3904 | my ($self, $vector) = @_; | ||
| 3905 | $self->to_matrix->gauss_jordan_solve($vector); | ||
| 3906 | } | ||
| 3907 | |||
| 3908 | sub matrix_solve { | ||
| 3909 | my ($self, $vector) = @_; | ||
| 3910 | $self->to_matrix->solve($vector); | ||
| 3911 | } | ||
| 3912 | |||
| 3913 | *msolve = \&matrix_solve; | ||
| 3914 | |||
| 3915 | sub invert { | ||
| 3916 | my ($self) = @_; | ||
| 3917 | $self->to_matrix->invert; | ||
| 3918 | } | ||
| 3919 | |||
| 3920 | *inv = \&invert; | ||
| 3921 | *inverse = \&invert; | ||
| 3922 | |||
| 3923 | sub determinant { | ||
| 3924 | my ($self) = @_; | ||
| 3925 | $self->to_matrix->det; | ||
| 3926 | } | ||
| 3927 | |||
| 3928 | *det = \&determinant; | ||
| 3929 | |||
| 3930 | sub matrix_add { | ||
| 3931 | my ($m1, $m2) = @_; | ||
| 3932 | $m1->wise_operator('+', $m2); | ||
| 3933 | } | ||
| 3934 | |||
| 3935 | *madd = \&matrix_add; | ||
| 3936 | |||
| 3937 | sub matrix_sub { | ||
| 3938 | my ($m1, $m2) = @_; | ||
| 3939 | $m1->wise_operator('-', $m2); | ||
| 3940 | } | ||
| 3941 | |||
| 3942 | *msub = \&matrix_sub; | ||
| 3943 | |||
| 3944 | sub matrix_mul { | ||
| 3945 | my ($m1, $m2) = @_; | ||
| 3946 | $m1->to_matrix->mul($m2); | ||
| 3947 | } | ||
| 3948 | |||
| 3949 | *mmul = \&matrix_mul; | ||
| 3950 | |||
| 3951 | sub matrix_div { | ||
| 3952 | my ($m1, $m2) = @_; | ||
| 3953 | $m1->matrix_mul($m2->to_matrix->inv); | ||
| 3954 | } | ||
| 3955 | |||
| 3956 | *mdiv = \&matrix_div; | ||
| 3957 | |||
| 3958 | sub scalar_add { | ||
| 3959 | my ($self, $scalar) = @_; | ||
| 3960 | $self->scalar_operator('+', $scalar); | ||
| 3961 | } | ||
| 3962 | |||
| 3963 | *sadd = \&scalar_add; | ||
| 3964 | |||
| 3965 | sub scalar_sub { | ||
| 3966 | my ($self, $scalar) = @_; | ||
| 3967 | $self->scalar_operator('-', $scalar); | ||
| 3968 | } | ||
| 3969 | |||
| 3970 | *ssub = \&scalar_sub; | ||
| 3971 | |||
| 3972 | sub scalar_mul { | ||
| 3973 | my ($self, $scalar) = @_; | ||
| 3974 | $self->scalar_operator('*', $scalar); | ||
| 3975 | } | ||
| 3976 | |||
| 3977 | *smul = \&scalar_mul; | ||
| 3978 | |||
| 3979 | sub scalar_div { | ||
| 3980 | my ($self, $scalar) = @_; | ||
| 3981 | $self->scalar_operator('/', $scalar); | ||
| 3982 | } | ||
| 3983 | |||
| 3984 | *sdiv = \&scalar_div; | ||
| 3985 | |||
| 3986 | sub matrix_pow { | ||
| 3987 | my ($A, $pow) = @_; | ||
| 3988 | $A->to_matrix->pow($pow); | ||
| 3989 | } | ||
| 3990 | |||
| 3991 | *mpow = \&matrix_pow; | ||
| 3992 | |||
| 3993 | sub _pipeline_op_call { | ||
| 3994 | my ($obj, $callback) = @_; | ||
| 3995 | |||
| 3996 | my @args; | ||
| 3997 | |||
| 3998 | if (ref($callback) eq __PACKAGE__) { | ||
| 3999 | @args = @$callback; | ||
| 4000 | $callback = CORE::shift(@args); | ||
| 4001 | } | ||
| 4002 | |||
| 4003 | if (ref($callback) eq 'Sidef::Types::Block::Block') { | ||
| 4004 | return $callback->call($obj, @args); | ||
| 4005 | } | ||
| 4006 | elsif (ref($callback) eq 'Sidef::Types::String::String') { | ||
| 4007 | return $obj->$$callback(@args); | ||
| 4008 | } | ||
| 4009 | |||
| 4010 | die "[ERROR] Invalid callback object: expected Block or String, but got <<", ref($callback), ">>"; | ||
| 4011 | } | ||
| 4012 | |||
| 4013 | sub pipeline_cross_op { | ||
| 4014 | my ($self, @callbacks) = @_; | ||
| 4015 | |||
| 4016 | my @list; | ||
| 4017 | foreach my $item (@$self) { | ||
| 4018 | foreach my $callback (@callbacks) { | ||
| 4019 | push @list, _pipeline_op_call($item, $callback); | ||
| 4020 | } | ||
| 4021 | } | ||
| 4022 | |||
| 4023 | bless \@list, ref($self); | ||
| 4024 | } | ||
| 4025 | |||
| 4026 | sub pipeline_map_op { | ||
| 4027 | my ($self, $callback, @args) = @_; | ||
| 4028 | |||
| 4029 | if (@args) { | ||
| 4030 | $callback = bless [$callback, @args]; | ||
| 4031 | } | ||
| 4032 | |||
| 4033 | my @list; | ||
| 4034 | |||
| 4035 | foreach my $item (@$self) { | ||
| 4036 | push @list, _pipeline_op_call($item, $callback); | ||
| 4037 | } | ||
| 4038 | |||
| 4039 | bless \@list, ref($self); | ||
| 4040 | } | ||
| 4041 | |||
| 4042 | sub pipeline_zip_op { | ||
| 4043 | my ($self, @callbacks) = @_; | ||
| 4044 | |||
| 4045 | my $argc = scalar(@callbacks); | ||
| 4046 | my @copy = @$self; | ||
| 4047 | |||
| 4048 | my @list; | ||
| 4049 | |||
| 4050 | while (1) { | ||
| 4051 | |||
| 4052 | (my @tmp = CORE::splice(@copy, 0, $argc)) == $argc or last; | ||
| 4053 | |||
| 4054 | for my $i (0 .. $argc - 1) { | ||
| 4055 | push @list, _pipeline_op_call($tmp[$i], $callbacks[$i]); | ||
| 4056 | } | ||
| 4057 | } | ||
| 4058 | |||
| 4059 | bless \@list, ref($self); | ||
| 4060 | } | ||
| 4061 | |||
| 4062 | sub cartesian { | ||
| 4063 | my ($self, $block) = @_; | ||
| 4064 | |||
| 4065 | if (!defined($block) and Sidef::Types::Number::Number::HAS_PRIME_UTIL) { | ||
| 4066 | my @result; | ||
| 4067 | |||
| 4068 | Math::Prime::Util::forsetproduct( | ||
| 4069 | sub { | ||
| 4070 | push @result, bless [@_]; # don't bless \@_ | ||
| 4071 | }, | ||
| 4072 | map { [@$_] } @$self | ||
| 4073 | ); | ||
| 4074 | |||
| 4075 | return bless \@result; | ||
| 4076 | } | ||
| 4077 | |||
| 4078 | require Algorithm::Loops; | ||
| 4079 | |||
| 4080 | my $iter = Algorithm::Loops::NestedLoops([map { [@$_] } @$self]); | ||
| 4081 | |||
| 4082 | if (defined($block)) { | ||
| 4083 | while (my @arr = $iter->()) { | ||
| 4084 | $block->run(@arr); | ||
| 4085 | } | ||
| 4086 | return $self; | ||
| 4087 | } | ||
| 4088 | |||
| 4089 | my @result; | ||
| 4090 | while (my @arr = $iter->()) { | ||
| 4091 | push @result, bless \@arr; | ||
| 4092 | } | ||
| 4093 | bless \@result; | ||
| 4094 | } | ||
| 4095 | |||
| 4096 | sub zip { | ||
| 4097 | my ($self, $block) = @_; | ||
| 4098 | |||
| 4099 | my @arrays = @$self; | ||
| 4100 | my $min = List::Util::min(map { scalar @$_ } @arrays); | ||
| 4101 | |||
| 4102 | my @new_array; | ||
| 4103 | foreach my $i (0 .. $min - 1) { | ||
| 4104 | |||
| 4105 | my @tmp = (map { $_->[$i] } @arrays); | ||
| 4106 | |||
| 4107 | if (defined($block)) { | ||
| 4108 | $block->run(@tmp); | ||
| 4109 | } | ||
| 4110 | else { | ||
| 4111 | CORE::push(@new_array, bless \@tmp); | ||
| 4112 | } | ||
| 4113 | } | ||
| 4114 | |||
| 4115 | defined($block) ? $self : bless \@new_array; | ||
| 4116 | } | ||
| 4117 | |||
| 4118 | *transpose = \&zip; | ||
| 4119 | |||
| 4120 | sub zip_by { | ||
| 4121 | my ($self, $block) = @_; | ||
| 4122 | |||
| 4123 | $block //= Sidef::Types::Block::Block::ARRAY_IDENTITY; | ||
| 4124 | |||
| 4125 | my @arrays = @$self; | ||
| 4126 | my $min = List::Util::min(map { scalar @$_ } @arrays); | ||
| 4127 | |||
| 4128 | my @new_array; | ||
| 4129 | foreach my $i (0 .. $min - 1) { | ||
| 4130 | CORE::push(@new_array, $block->run(map { $_->[$i] } @arrays)); | ||
| 4131 | } | ||
| 4132 | |||
| 4133 | bless \@new_array; | ||
| 4134 | } | ||
| 4135 | |||
| 4136 | sub unzip_by { | ||
| 4137 | my ($self, $block) = @_; | ||
| 4138 | |||
| 4139 | my @matrix; | ||
| 4140 | foreach my $i (0 .. $#$self) { | ||
| 4141 | |||
| 4142 | my @tmp = $block->run($self->[$i]); | ||
| 4143 | |||
| 4144 | if (@tmp < @matrix) { | ||
| 4145 | $#tmp = $#matrix; | ||
| 4146 | } | ||
| 4147 | |||
| 4148 | foreach my $j (0 .. $#tmp) { | ||
| 4149 | $matrix[$j][$i] = $tmp[$j]; | ||
| 4150 | } | ||
| 4151 | } | ||
| 4152 | |||
| 4153 | bless $_ for @matrix; | ||
| 4154 | bless \@matrix; | ||
| 4155 | } | ||
| 4156 | |||
| 4157 | sub pack { | ||
| 4158 | my ($self, $format) = @_; | ||
| 4159 | Sidef::Types::String::String->new(CORE::pack("$format", @$self)); | ||
| 4160 | } | ||
| 4161 | |||
| 4162 | sub push { | ||
| 4163 | my ($self, @args) = @_; | ||
| 4164 | CORE::push(@$self, @args); | ||
| 4165 | $self; | ||
| 4166 | } | ||
| 4167 | |||
| 4168 | *append = \&push; | ||
| 4169 | |||
| 4170 | sub unshift { | ||
| 4171 | my ($self, @args) = @_; | ||
| 4172 | CORE::unshift(@$self, @args); | ||
| 4173 | $self; | ||
| 4174 | } | ||
| 4175 | |||
| 4176 | *prepend = \&unshift; | ||
| 4177 | |||
| 4178 | sub rotate { | ||
| 4179 | my ($self, $num) = @_; | ||
| 4180 | |||
| 4181 | my $len = $#$self + 1; | ||
| 4182 | return bless([]) if $len == 0; | ||
| 4183 | $num = CORE::int($num) % $len; | ||
| 4184 | return bless([@$self], ref($self)) if $num == 0; | ||
| 4185 | |||
| 4186 | my @array = @$self; | ||
| 4187 | CORE::unshift(@array, CORE::splice(@array, $num)); | ||
| 4188 | bless \@array, ref($self); | ||
| 4189 | } | ||
| 4190 | |||
| 4191 | sub join { | ||
| 4192 | my ($self, $delim, $block) = @_; | ||
| 4193 | $delim = defined($delim) ? "$delim" : ''; | ||
| 4194 | |||
| 4195 | if (defined $block) { | ||
| 4196 | return Sidef::Types::String::String->new(CORE::join($delim, map { scalar $block->run($_) } @$self)); | ||
| 4197 | } | ||
| 4198 | |||
| 4199 | Sidef::Types::String::String->new(CORE::join($delim, @$self)); | ||
| 4200 | } | ||
| 4201 | |||
| 4202 | sub join_bytes { | ||
| 4203 | my ($self, $encoding) = @_; | ||
| 4204 | state $x = require Encode; | ||
| 4205 | $encoding = defined($encoding) ? "$encoding" : 'UTF-8'; | ||
| 4206 | Sidef::Types::String::String->new(eval { Encode::decode($encoding, CORE::pack('C*', @$self)) } // return); | ||
| 4207 | } | ||
| 4208 | |||
| 4209 | *chrs = \&join_bytes; | ||
| 4210 | *decode = \&join_bytes; | ||
| 4211 | |||
| 4212 | sub join_insert { | ||
| 4213 | my ($self, $obj) = @_; | ||
| 4214 | |||
| 4215 | my @array; | ||
| 4216 | foreach my $item (@$self) { | ||
| 4217 | CORE::push(@array, $item, $obj); | ||
| 4218 | } | ||
| 4219 | |||
| 4220 | CORE::pop(@array); | ||
| 4221 | bless \@array; | ||
| 4222 | } | ||
| 4223 | |||
| 4224 | sub reverse { | ||
| 4225 | my ($self) = @_; | ||
| 4226 | bless [CORE::reverse @$self], ref($self); | ||
| 4227 | } | ||
| 4228 | |||
| 4229 | *flip = \&reverse; | ||
| 4230 | |||
| 4231 | sub delete_first { | ||
| 4232 | my ($self, $obj) = @_; | ||
| 4233 | |||
| 4234 | foreach my $i (0 .. $#$self) { | ||
| 4235 | if ($self->[$i] eq $obj) { | ||
| 4236 | CORE::splice(@$self, $i, 1); | ||
| 4237 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 4238 | } | ||
| 4239 | } | ||
| 4240 | |||
| 4241 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 4242 | } | ||
| 4243 | |||
| 4244 | *remove_first = \&delete_first; | ||
| 4245 | |||
| 4246 | sub delete_last { | ||
| 4247 | my ($self, $obj) = @_; | ||
| 4248 | |||
| 4249 | for (my $i = $#$self ; $i >= 0 ; $i--) { | ||
| 4250 | if ($self->[$i] eq $obj) { | ||
| 4251 | CORE::splice(@$self, $i, 1); | ||
| 4252 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 4253 | } | ||
| 4254 | } | ||
| 4255 | |||
| 4256 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 4257 | } | ||
| 4258 | |||
| 4259 | *remove_last = \&delete_last; | ||
| 4260 | |||
| 4261 | sub delete { | ||
| 4262 | my ($self, $obj) = @_; | ||
| 4263 | |||
| 4264 | for (my $i = $#$self ; $i >= 0 ; --$i) { | ||
| 4265 | if ($self->[$i] eq $obj) { | ||
| 4266 | CORE::splice(@$self, $i, 1); | ||
| 4267 | } | ||
| 4268 | } | ||
| 4269 | |||
| 4270 | $self; | ||
| 4271 | } | ||
| 4272 | |||
| 4273 | *remove = \&delete; | ||
| 4274 | |||
| 4275 | sub delete_if { | ||
| 4276 | my ($self, $block) = @_; | ||
| 4277 | |||
| 4278 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 4279 | |||
| 4280 | for (my $i = 0 ; $i <= $#$self ; $i++) { | ||
| 4281 | if ($block->run($self->[$i])) { | ||
| 4282 | CORE::splice(@$self, $i--, 1); | ||
| 4283 | } | ||
| 4284 | } | ||
| 4285 | |||
| 4286 | $self; | ||
| 4287 | } | ||
| 4288 | |||
| 4289 | *remove_if = \&delete_if; | ||
| 4290 | *remove_by = \&delete_if; | ||
| 4291 | *delete_by = \&delete_if; | ||
| 4292 | |||
| 4293 | sub extract_by { | ||
| 4294 | my ($self, $block) = @_; | ||
| 4295 | |||
| 4296 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 4297 | |||
| 4298 | my @extracted; | ||
| 4299 | for (my $i = 0 ; $i <= $#$self ; $i++) { | ||
| 4300 | if ($block->run($self->[$i])) { | ||
| 4301 | CORE::push(@extracted, CORE::splice(@$self, $i--, 1)); | ||
| 4302 | } | ||
| 4303 | } | ||
| 4304 | |||
| 4305 | bless \@extracted; | ||
| 4306 | } | ||
| 4307 | |||
| 4308 | sub extract_first_by { | ||
| 4309 | my ($self, $block) = @_; | ||
| 4310 | |||
| 4311 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 4312 | |||
| 4313 | foreach my $i (0 .. $#$self) { | ||
| 4314 | if ($block->run($self->[$i])) { | ||
| 4315 | return CORE::splice(@$self, $i--, 1); | ||
| 4316 | } | ||
| 4317 | } | ||
| 4318 | |||
| 4319 | return undef; | ||
| 4320 | } | ||
| 4321 | |||
| 4322 | sub extract_last_by { | ||
| 4323 | my ($self, $block) = @_; | ||
| 4324 | |||
| 4325 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 4326 | |||
| 4327 | for (my $i = $#$self ; $i >= 0 ; --$i) { | ||
| 4328 | if ($block->run($self->[$i])) { | ||
| 4329 | return CORE::splice(@$self, $i, 1); | ||
| 4330 | } | ||
| 4331 | } | ||
| 4332 | |||
| 4333 | return undef; | ||
| 4334 | } | ||
| 4335 | |||
| 4336 | sub delete_first_if { | ||
| 4337 | my ($self, $block) = @_; | ||
| 4338 | |||
| 4339 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 4340 | |||
| 4341 | foreach my $i (0 .. $#$self) { | ||
| 4342 | if ($block->run($self->[$i])) { | ||
| 4343 | CORE::splice(@$self, $i, 1); | ||
| 4344 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 4345 | } | ||
| 4346 | } | ||
| 4347 | |||
| 4348 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 4349 | } | ||
| 4350 | |||
| 4351 | *remove_first_if = \&delete_first_if; | ||
| 4352 | *remove_first_by = \&delete_first_if; | ||
| 4353 | *delete_first_by = \&delete_first_if; | ||
| 4354 | |||
| 4355 | sub delete_last_if { | ||
| 4356 | my ($self, $block) = @_; | ||
| 4357 | |||
| 4358 | $block //= Sidef::Types::Block::Block::IDENTITY; | ||
| 4359 | |||
| 4360 | for (my $i = $#$self ; $i >= 0 ; --$i) { | ||
| 4361 | if ($block->run($self->[$i])) { | ||
| 4362 | CORE::splice(@$self, $i, 1); | ||
| 4363 | return (Sidef::Types::Bool::Bool::TRUE); | ||
| 4364 | } | ||
| 4365 | } | ||
| 4366 | |||
| 4367 | (Sidef::Types::Bool::Bool::FALSE); | ||
| 4368 | } | ||
| 4369 | |||
| 4370 | *remove_last_if = \&delete_last_if; | ||
| 4371 | *remove_last_by = \&delete_last_if; | ||
| 4372 | *delete_last_by = \&delete_last_if; | ||
| 4373 | |||
| 4374 | sub to_list { @{$_[0]} } | ||
| 4375 | |||
| 4376 | sub getopt { | ||
| 4377 | my ($self, %opts) = @_; | ||
| 4378 | |||
| 4379 | @$self or return Sidef::Types::Array::Array->new; | ||
| 4380 | |||
| 4381 | state $x = require Getopt::Long; | ||
| 4382 | Getopt::Long::Configure('no_ignore_case'); | ||
| 4383 | |||
| 4384 | my @argv = map { "$_" } @$self; | ||
| 4385 | my @opts = CORE::keys %opts; | ||
| 4386 | |||
| 4387 | my %parsed; | ||
| 4388 | Getopt::Long::GetOptionsFromArray(\@argv, \%parsed, @opts); | ||
| 4389 | |||
| 4390 | my %lookup = map { | ||
| 4391 | my ($name) = Getopt::Long::ParseOptionSpec($_, \my %info); | ||
| 4392 | defined($name) ? ($name => {obj => $opts{$_}, type => $info{$name}[0]}) : (); | ||
| 4393 | } @opts; | ||
| 4394 | |||
| 4395 | foreach my $key (CORE::keys %parsed) { | ||
| 4396 | |||
| 4397 | my $rec = $lookup{$key}; | ||
| 4398 | my $obj = $rec->{obj}; | ||
| 4399 | my $type = $rec->{type}; | ||
| 4400 | |||
| 4401 | if (ref($obj) eq 'REF' or ref($obj) eq 'SCALAR') { | ||
| 4402 | my $ref = ref($$obj); | ||
| 4403 | |||
| 4404 | # Determine the type for undefined references | ||
| 4405 | if ($ref eq '') { | ||
| 4406 | if ($type eq 'i' or $type eq 'f') { | ||
| 4407 | $ref = 'Sidef::Types::Number::Number'; | ||
| 4408 | } | ||
| 4409 | elsif ($type eq '') { | ||
| 4410 | $ref = 'Sidef::Types::Bool::Bool'; | ||
| 4411 | } | ||
| 4412 | else { | ||
| 4413 | $ref = 'Sidef::Types::String::String'; | ||
| 4414 | } | ||
| 4415 | } | ||
| 4416 | |||
| 4417 | if ( $ref eq 'Sidef::Types::String::String' | ||
| 4418 | or $ref eq 'Sidef::Types::Number::Number') { | ||
| 4419 | $$obj = $ref->new($parsed{$key}); | ||
| 4420 | } | ||
| 4421 | elsif ($ref eq 'Sidef::Types::Bool::Bool') { | ||
| 4422 | $$obj = | ||
| 4423 | $parsed{$key} | ||
| 4424 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 4425 | : Sidef::Types::Bool::Bool::FALSE; | ||
| 4426 | } | ||
| 4427 | else { | ||
| 4428 | if ($type eq '') { | ||
| 4429 | $$obj = $ref->new( | ||
| 4430 | $parsed{$key} | ||
| 4431 | ? Sidef::Types::Bool::Bool::TRUE | ||
| 4432 | : Sidef::Types::Bool::Bool::FALSE | ||
| 4433 | ); | ||
| 4434 | } | ||
| 4435 | elsif ($type eq 'i' or $type eq 'f') { | ||
| 4436 | $$obj = $ref->new(Sidef::Types::Number::Number->new($parsed{$key})); | ||
| 4437 | } | ||
| 4438 | else { | ||
| 4439 | $$obj = $ref->new(Sidef::Types::String::String->new($parsed{$key})); | ||
| 4440 | } | ||
| 4441 | } | ||
| 4442 | } | ||
| 4443 | else { | ||
| 4444 | $obj->call(); | ||
| 4445 | } | ||
| 4446 | } | ||
| 4447 | |||
| 4448 | bless [map { Sidef::Types::String::String->new($_) } @argv]; | ||
| 4449 | } | ||
| 4450 | |||
| 4451 | sub _dump { | ||
| 4452 | my %addr; # keeps track of dumped objects | ||
| 4453 | |||
| 4454 | my $sub = sub { | ||
| 4455 | my ($obj) = @_; | ||
| 4456 | |||
| 4457 | my $refaddr = Scalar::Util::refaddr($obj); | ||
| 4458 | |||
| 4459 | exists($addr{$refaddr}) | ||
| 4460 | and return $addr{$refaddr}; | ||
| 4461 | |||
| 4462 | $addr{$refaddr} = "Array(#`($refaddr)...)"; | ||
| 4463 | |||
| 4464 | my $s; | ||
| 4465 | |||
| 4466 | '[' . CORE::join(', ', map { ref($_) && ($s = UNIVERSAL::can($_, 'dump')) ? $s->($_) : ($_ // 'nil') } @$obj) . ']'; | ||
| 4467 | }; | ||
| 4468 | |||
| 4469 | no warnings 'redefine'; | ||
| 4470 | local *Sidef::Types::Array::Array::dump = $sub; | ||
| 4471 | $sub->($_[0]); | ||
| 4472 | } | ||
| 4473 | |||
| 4474 | sub dump { | ||
| 4475 | Sidef::Types::String::String->new($_[0]->_dump); | ||
| 4476 | } | ||
| 4477 | |||
| 4478 | *to_s = \&dump; | ||
| 4479 | *to_str = \&dump; | ||
| 4480 | |||
| 4481 | sub to_hash { | ||
| 4482 | my ($self) = @_; | ||
| 4483 | Sidef::Types::Hash::Hash->new(@$self); | ||
| 4484 | } | ||
| 4485 | |||
| 4486 | *to_h = \&to_hash; | ||
| 4487 | |||
| 4488 | sub to_a { | ||
| 4489 | ref($_[0]) ? $_[0] : __PACKAGE__->new($_[0]); | ||
| 4490 | } | ||
| 4491 | |||
| 4492 | *to_array = \&to_a; | ||
| 4493 | |||
| 4494 | sub to_set { | ||
| 4495 | my ($self) = @_; | ||
| 4496 | Sidef::Types::Set::Set->new(@$self); | ||
| 4497 | } | ||
| 4498 | |||
| 4499 | sub to_bag { | ||
| 4500 | my ($self) = @_; | ||
| 4501 | Sidef::Types::Set::Bag->new(@$self); | ||
| 4502 | } | ||
| 4503 | |||
| 4504 | sub to_matrix { | ||
| 4505 | my ($self) = @_; | ||
| 4506 | Sidef::Types::Array::Matrix->new(@$self); | ||
| 4507 | } | ||
| 4508 | |||
| 4509 | *to_m = \&to_matrix; | ||
| 4510 | |||
| 4511 | sub to_vector { | ||
| 4512 | my ($self) = @_; | ||
| 4513 | Sidef::Types::Array::Vector->new(@$self); | ||
| 4514 | } | ||
| 4515 | |||
| 4516 | *to_v = \&to_vector; | ||
| 4517 | |||
| 4518 | { | ||
| 4519 | no strict 'refs'; | ||
| 4520 | |||
| 4521 | *{__PACKAGE__ . '::' . '&'} = \∧ | ||
| 4522 | *{__PACKAGE__ . '::' . '*'} = \&mul; | ||
| 4523 | *{__PACKAGE__ . '::' . '**'} = \&mpow; | ||
| 4524 | *{__PACKAGE__ . '::' . '<<'} = \&push; | ||
| 4525 | *{__PACKAGE__ . '::' . '«'} = \&push; | ||
| 4526 | *{__PACKAGE__ . '::' . '>>'} = \&pop; | ||
| 4527 | *{__PACKAGE__ . '::' . '»'} = \&pop; | ||
| 4528 | *{__PACKAGE__ . '::' . '|Z>'} = \&pipeline_zip_op; | ||
| 4529 | *{__PACKAGE__ . '::' . '|X>'} = \&pipeline_cross_op; | ||
| 4530 | *{__PACKAGE__ . '::' . '|>>'} = \&pipeline_map_op; | ||
| 4531 | *{__PACKAGE__ . '::' . '|'} = \∨ | ||
| 4532 | *{__PACKAGE__ . '::' . '^'} = \&xor; | ||
| 4533 | *{__PACKAGE__ . '::' . '+'} = \&add; | ||
| 4534 | *{__PACKAGE__ . '::' . '-'} = \&diff; | ||
| 4535 | *{__PACKAGE__ . '::' . '=='} = \&eq; | ||
| 4536 | *{__PACKAGE__ . '::' . '<'} = \< | ||
| 4537 | *{__PACKAGE__ . '::' . '<='} = \≤ | ||
| 4538 | *{__PACKAGE__ . '::' . '≤'} = \≤ | ||
| 4539 | *{__PACKAGE__ . '::' . '>'} = \> | ||
| 4540 | *{__PACKAGE__ . '::' . '≥'} = \≥ | ||
| 4541 | *{__PACKAGE__ . '::' . '>='} = \≥ | ||
| 4542 | *{__PACKAGE__ . '::' . '!='} = \≠ | ||
| 4543 | *{__PACKAGE__ . '::' . '≠'} = \≠ | ||
| 4544 | *{__PACKAGE__ . '::' . '<=>'} = \&cmp; | ||
| 4545 | *{__PACKAGE__ . '::' . '/'} = \÷ | ||
| 4546 | *{__PACKAGE__ . '::' . '÷'} = \÷ | ||
| 4547 | *{__PACKAGE__ . '::' . '...'} = \&to_list; | ||
| 4548 | *{__PACKAGE__ . '::' . '∋'} = \&contains; | ||
| 4549 | *{__PACKAGE__ . '::' . '∌'} = sub { $_[0]->contains($_[1])->not }; | ||
| 4550 | } | ||
| 4551 | |||
| 4552 | 1 |