| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | |||
| 5 | package LinkedList::Single v1.0.0; | ||
| 6 | use v5.24; | ||
| 7 | |||
| 8 | use Symbol; | ||
| 9 | |||
| 10 | use Carp qw( carp croak confess ); | ||
| 11 | use Scalar::Util qw( blessed refaddr reftype looks_like_number ); | ||
| 12 | use Storable qw( dclone ); | ||
| 13 | |||
| 14 | use overload | ||
| 15 | ( | ||
| 16 | q{bool} => | ||
| 17 | sub | ||
| 18 | { | ||
| 19 | # the list handler is true if the current | ||
| 20 | # node is not empty (i.e., is not at the | ||
| 21 | # final node). | ||
| 22 | # | ||
| 23 | # this allows for: | ||
| 24 | # | ||
| 25 | # $listh->head; while( $listh ){ ... } | ||
| 26 | |||
| 27 | my $listh = shift; | ||
| 28 | |||
| 29 | $$listh # && @{ $$listh } | ||
| 30 | }, | ||
| 31 | |||
| 32 | q{+} => | ||
| 33 | sub | ||
| 34 | { | ||
| 35 | # return a node at the given offset. | ||
| 36 | # update the list to that point. | ||
| 37 | |||
| 38 | my ( $listh, $offset ) | ||
| 39 | = $_[2] | ||
| 40 | ? @_[1,0] | ||
| 41 | : @_[0,1] | ||
| 42 | ; | ||
| 43 | |||
| 44 | my $node = $$listh || $listh->head_node; | ||
| 45 | |||
| 46 | # i.e., $offset == 0, gets ( 1 .. 0 ) for no change. | ||
| 47 | # | ||
| 48 | # note: this might want to return undef if that node | ||
| 49 | # does not exist -- i.e., more like perly array result. | ||
| 50 | # | ||
| 51 | # note the side effect on += if that change is made: | ||
| 52 | # should $listh be updated to undef or left as-is? | ||
| 53 | |||
| 54 | for ( 1 .. $offset ) | ||
| 55 | { | ||
| 56 | $node | ||
| 57 | or return undef; | ||
| 58 | |||
| 59 | $node = $node->[0]; | ||
| 60 | } | ||
| 61 | |||
| 62 | $node | ||
| 63 | }, | ||
| 64 | |||
| 65 | q{+=} => | ||
| 66 | sub | ||
| 67 | { | ||
| 68 | my ( $listh, $offset ) | ||
| 69 | = $_[2] | ||
| 70 | ? @_[1,0] | ||
| 71 | : @_[0,1] | ||
| 72 | ; | ||
| 73 | |||
| 74 | $$listh = $listh + $offset; | ||
| 75 | |||
| 76 | $listh | ||
| 77 | }, | ||
| 78 | |||
| 79 | q{""} => | ||
| 80 | sub | ||
| 81 | { | ||
| 82 | my ( $listh ) | ||
| 83 | = $_[2] | ||
| 84 | ? $_[1] | ||
| 85 | : $_[0] | ||
| 86 | ; | ||
| 87 | |||
| 88 | $listh->id_string | ||
| 89 | }, | ||
| 90 | ); | ||
| 91 | |||
| 92 | ######################################################################## | ||
| 93 | # package variables | ||
| 94 | ######################################################################## | ||
| 95 | |||
| 96 | our @CARP_NOT = ( __PACKAGE__ ); | ||
| 97 | |||
| 98 | # inside-out data for the heads of the lists. | ||
| 99 | |||
| 100 | my %rootz = (); | ||
| 101 | |||
| 102 | my $verbose = $ENV{ VERBOSE } // ''; | ||
| 103 | |||
| 104 | ######################################################################## | ||
| 105 | # utility subs | ||
| 106 | ######################################################################## | ||
| 107 | |||
| 108 | ######################################################################## | ||
| 109 | # the issue here was fixed in later versions of Perl. | ||
| 110 | # this can be removed to allow for automtic deletion. | ||
| 111 | ######################################################################## | ||
| 112 | # | ||
| 113 | # perl's recursive cleanups croaks after 100 levels, kinda limits the | ||
| 114 | # list size. fix is converting it to iterative by replacing the | ||
| 115 | # head node. | ||
| 116 | # | ||
| 117 | # nasty business: simplest solution gets sig11's | ||
| 118 | # in 5.8 & 5.10.1 with lists 2**15 long or more. | ||
| 119 | # probelem is that destroy pukes after | ||
| 120 | # returning. only fix so far is keeping the | ||
| 121 | # heads alive permenantly (i.e., mem leak is | ||
| 122 | # feature) | ||
| 123 | # | ||
| 124 | # $head = $head->[0] | ||
| 125 | # while $head->[0]; | ||
| 126 | # | ||
| 127 | # fix is expanding the list in place, which | ||
| 128 | # takes a bit more work. | ||
| 129 | # | ||
| 130 | # @$head = @{ $head->[0] } | ||
| 131 | # while $head->[0]; | ||
| 132 | # | ||
| 133 | # weird thing is that it blows up after DESTROY returns, | ||
| 134 | # not when the entry is deleted from $rootz{ $key }. | ||
| 135 | # | ||
| 136 | # net result: truncate works fine with the fast method, | ||
| 137 | # DESTROY has to expand out the contents to make it work. | ||
| 138 | # | ||
| 139 | # note that doing this without the separate $node | ||
| 140 | # variable gets the sigfault ( i.e., | ||
| 141 | # | ||
| 142 | # $head->[0] = @{ $head->[0] } | ||
| 143 | # | ||
| 144 | # blows up). | ||
| 145 | # | ||
| 146 | # see t/03*.t for example of testing this particular | ||
| 147 | # issue. | ||
| 148 | # | ||
| 149 | #my $cleanup | ||
| 150 | #= sub | ||
| 151 | #{ | ||
| 152 | # no-op this for testing whether cleanup is still required. | ||
| 153 | # | ||
| 154 | # my $node = shift | ||
| 155 | # or return; | ||
| 156 | # | ||
| 157 | # $node = $node->[0] | ||
| 158 | # while $node->[0]; | ||
| 159 | # | ||
| 160 | # return | ||
| 161 | #}; | ||
| 162 | ######################################################################## | ||
| 163 | |||
| 164 | my $get_node_data | ||
| 165 | = sub | ||
| 166 | { | ||
| 167 | my $node = shift | ||
| 168 | or croak 'Bogus get_node_data: false node'; | ||
| 169 | |||
| 170 | my @data = @{ $node }[ 1 .. $#$node ]; | ||
| 171 | |||
| 172 | wantarray | ||
| 173 | ? @data | ||
| 174 | : \@data | ||
| 175 | }; | ||
| 176 | |||
| 177 | my $set_node_data | ||
| 178 | = sub | ||
| 179 | { | ||
| 180 | my $node = shift | ||
| 181 | or croak 'Bogus set_node_data: false node'; | ||
| 182 | |||
| 183 | @_ or carp "Oddity: setting empty node data" | ||
| 184 | if $verbose; | ||
| 185 | |||
| 186 | # return the previous data for posterity | ||
| 187 | |||
| 188 | wantarray | ||
| 189 | ? splice @$node, 1, $#$node, @_ | ||
| 190 | : [ splice @$node, 1, $#$node, @_ ] | ||
| 191 | }; | ||
| 192 | |||
| 193 | my $list_next | ||
| 194 | = sub | ||
| 195 | { | ||
| 196 | my $listh = shift; | ||
| 197 | |||
| 198 | my $node = $$listh | ||
| 199 | or croak "Bogus list_next: list handler does not reference a node"; | ||
| 200 | |||
| 201 | # return handles assignment in list context | ||
| 202 | # more gracefully by not passing back scalar | ||
| 203 | # undef, remains false in a list context. | ||
| 204 | |||
| 205 | $node->[0] | ||
| 206 | or | ||
| 207 | return | ||
| 208 | }; | ||
| 209 | |||
| 210 | ######################################################################## | ||
| 211 | # extract the handler, tail & count args for map, reduce, etc. | ||
| 212 | # @CARP_NOT prevents these from reporting errors as being from the | ||
| 213 | # calling subs. | ||
| 214 | |||
| 215 | my $handler_args | ||
| 216 | = sub | ||
| 217 | { | ||
| 218 | my ( $listh, $handler, %argz ) = @_; | ||
| 219 | |||
| 220 | blessed $listh | ||
| 221 | or croak "Bogus list handler: un-blessed '$listh'"; | ||
| 222 | |||
| 223 | # handler can safely be false, avoid undefs. | ||
| 224 | |||
| 225 | $handler //= ''; | ||
| 226 | |||
| 227 | if( exists $argz{ count } ) | ||
| 228 | { | ||
| 229 | my $count = $argz{ count }; | ||
| 230 | |||
| 231 | if( looks_like_number $count ) | ||
| 232 | { | ||
| 233 | $count < 0 | ||
| 234 | and croak "Botched count: negative value '$count'"; | ||
| 235 | |||
| 236 | # zero count will never produce any result. | ||
| 237 | |||
| 238 | $count | ||
| 239 | or return; | ||
| 240 | } | ||
| 241 | elsif( $count ) | ||
| 242 | { | ||
| 243 | croak "Botched count: non-numeric value '$count'"; | ||
| 244 | } | ||
| 245 | |||
| 246 | ( $listh, $handler, $count ) | ||
| 247 | } | ||
| 248 | else | ||
| 249 | { | ||
| 250 | ( $listh, $handler ) | ||
| 251 | } | ||
| 252 | }; | ||
| 253 | |||
| 254 | ######################################################################## | ||
| 255 | # public interface | ||
| 256 | ######################################################################## | ||
| 257 | |||
| 258 | # entry with a link-to-empty-next. | ||
| 259 | # | ||
| 260 | # the nested arrayref is the first | ||
| 261 | # node on the list. this is required | ||
| 262 | # for unshift to add the first node | ||
| 263 | # after the head. | ||
| 264 | |||
| 265 | sub construct | ||
| 266 | { | ||
| 267 | my $proto = shift; | ||
| 268 | |||
| 269 | # i.e., a reference to the root node referencing the head node, | ||
| 270 | # which begins life as the tail node and is thus empty. | ||
| 271 | |||
| 272 | my $listh = bless \( [ [] ] ), ref $proto || $proto; | ||
| 273 | |||
| 274 | $rootz{ refaddr $listh } = $$listh; | ||
| 275 | |||
| 276 | $listh | ||
| 277 | } | ||
| 278 | |||
| 279 | sub initialize | ||
| 280 | { | ||
| 281 | # this may not always be called from new(), need | ||
| 282 | # perform some add'l housekeeping. | ||
| 283 | # | ||
| 284 | # this cleans up any existing by de-referencing the | ||
| 285 | # head node, leaves the list at the head (vs. root). | ||
| 286 | # | ||
| 287 | # this leaves $listh referencing the head node, which is | ||
| 288 | # what people usually want. | ||
| 289 | |||
| 290 | my $listh = shift; | ||
| 291 | my $node = $listh->root_node; | ||
| 292 | ( $node ) = @$node = ( [] ); | ||
| 293 | $$listh = $node; | ||
| 294 | |||
| 295 | if( @_ ) | ||
| 296 | { | ||
| 297 | # start a at the new head == tail node, expand it | ||
| 298 | # into a new tail node + data. | ||
| 299 | |||
| 300 | ( $node ) = @$node = ( [], $_ ) | ||
| 301 | for @_; | ||
| 302 | } | ||
| 303 | |||
| 304 | return | ||
| 305 | } | ||
| 306 | |||
| 307 | sub new | ||
| 308 | { | ||
| 309 | my $listh = &construct; | ||
| 310 | |||
| 311 | $listh->initialize( @_ ); | ||
| 312 | |||
| 313 | $listh | ||
| 314 | } | ||
| 315 | |||
| 316 | sub clone | ||
| 317 | { | ||
| 318 | state $pkg = __PACKAGE__; | ||
| 319 | |||
| 320 | # clone the contents of $listh into a new list. | ||
| 321 | # | ||
| 322 | # reacall that the linked lists looks like a nested- | ||
| 323 | # struct-from-hell to Perl, at which point dclone will | ||
| 324 | # happily & quickly create a new copy of it all. | ||
| 325 | # | ||
| 326 | # note that assigning $rootz{ r/a $new } will overwrite | ||
| 327 | # the root, discarding any existing linked list manged | ||
| 328 | # by $clone. | ||
| 329 | |||
| 330 | my $listh = shift; | ||
| 331 | |||
| 332 | my $clone | ||
| 333 | = do | ||
| 334 | { | ||
| 335 | if( @_ ) | ||
| 336 | { | ||
| 337 | my $dest = shift | ||
| 338 | or croak "Bogus clone: false clone-into value"; | ||
| 339 | |||
| 340 | blessed $dest | ||
| 341 | or croak "Bogus clone: '$dest' is not an object"; | ||
| 342 | |||
| 343 | $dest->isa( __PACKAGE__ ) | ||
| 344 | or croak "Bogus clone: '$dest' is not a '$pkg'"; | ||
| 345 | |||
| 346 | $dest | ||
| 347 | } | ||
| 348 | else | ||
| 349 | { | ||
| 350 | $listh->new | ||
| 351 | } | ||
| 352 | }; | ||
| 353 | |||
| 354 | # replacing both the root and current node | ||
| 355 | # will release the linked list. | ||
| 356 | |||
| 357 | $$clone = $$listh; | ||
| 358 | |||
| 359 | $rootz{ refaddr $clone } | ||
| 360 | = $rootz{ refaddr $listh }; | ||
| 361 | |||
| 362 | $clone | ||
| 363 | } | ||
| 364 | |||
| 365 | sub DESTROY | ||
| 366 | { | ||
| 367 | my $head = delete $rootz{ refaddr shift }; | ||
| 368 | |||
| 369 | my $node = $head->[0]; | ||
| 370 | |||
| 371 | # $cleanup->( $node ); | ||
| 372 | |||
| 373 | $#$head = -1; | ||
| 374 | |||
| 375 | return | ||
| 376 | } | ||
| 377 | |||
| 378 | # if $rootz{ $key } isn't removed then the | ||
| 379 | # node = node->next approach works just fine. | ||
| 380 | # so, truncate can use the faster aproach. | ||
| 381 | |||
| 382 | sub truncate | ||
| 383 | { | ||
| 384 | my $listh = shift; | ||
| 385 | |||
| 386 | my $node = $$listh; | ||
| 387 | |||
| 388 | my $next = splice @$node, 0, 1, []; | ||
| 389 | |||
| 390 | # $cleanup->( $next ); | ||
| 391 | |||
| 392 | $listh | ||
| 393 | } | ||
| 394 | |||
| 395 | sub replace | ||
| 396 | { | ||
| 397 | my $listh = shift; | ||
| 398 | |||
| 399 | $listh->head->truncate; | ||
| 400 | |||
| 401 | $listh->initialize( @_ ); | ||
| 402 | |||
| 403 | $listh | ||
| 404 | } | ||
| 405 | |||
| 406 | ######################################################################## | ||
| 407 | # basic information: the current node referenced by the list handler | ||
| 408 | # | ||
| 409 | # calling node without an argument returns the current one, with one | ||
| 410 | # sets the node. this allows for tell/reset-style stacking of node | ||
| 411 | # positions. | ||
| 412 | |||
| 413 | sub curr_node | ||
| 414 | { | ||
| 415 | # yes, this is a metadata leak. | ||
| 416 | # doing it this way makes it simpler to | ||
| 417 | # deal with things like map and friends | ||
| 418 | # using methods. | ||
| 419 | |||
| 420 | my $listh = shift; | ||
| 421 | my $node = $$listh | ||
| 422 | or return; | ||
| 423 | |||
| 424 | wantarray | ||
| 425 | ? @$node | ||
| 426 | : $node | ||
| 427 | } | ||
| 428 | |||
| 429 | sub node | ||
| 430 | { | ||
| 431 | my $listh = shift; | ||
| 432 | |||
| 433 | @_ | ||
| 434 | ? $$listh = shift | ||
| 435 | : $$listh | ||
| 436 | } | ||
| 437 | |||
| 438 | ######################################################################## | ||
| 439 | # hide extra data in the root node after the first-node ref. | ||
| 440 | # | ||
| 441 | # splice with $#$head works since 1 .. end == length - 1 == offset. | ||
| 442 | |||
| 443 | sub set_meta | ||
| 444 | { | ||
| 445 | my $listh = shift; | ||
| 446 | my $root = $listh->root; | ||
| 447 | |||
| 448 | @$root = ( $root->[0], @_ ); | ||
| 449 | |||
| 450 | $listh | ||
| 451 | } | ||
| 452 | |||
| 453 | sub add_meta | ||
| 454 | { | ||
| 455 | my $listh = shift; | ||
| 456 | my $root = $listh->root; | ||
| 457 | |||
| 458 | push @$root, @_; | ||
| 459 | |||
| 460 | $listh | ||
| 461 | } | ||
| 462 | |||
| 463 | sub get_meta | ||
| 464 | { | ||
| 465 | my $root = $_[0]->root; | ||
| 466 | |||
| 467 | wantarray | ||
| 468 | ? @{ $root }[ 1 .. $#$root ] | ||
| 469 | : [ @{ $root }[ 1 .. $#$root ] ] | ||
| 470 | } | ||
| 471 | |||
| 472 | sub id_string | ||
| 473 | { | ||
| 474 | my $listh = shift; | ||
| 475 | my $root = $listh->root_node; | ||
| 476 | |||
| 477 | $root->[1] // join '=' => blessed $listh, refaddr $listh | ||
| 478 | } | ||
| 479 | |||
| 480 | ######################################################################## | ||
| 481 | # node/list status | ||
| 482 | |||
| 483 | sub has_nodes | ||
| 484 | { | ||
| 485 | # i.e., is the list populated? | ||
| 486 | |||
| 487 | my $root = $_[0]->root; | ||
| 488 | |||
| 489 | !! @{ $root->[0] } | ||
| 490 | } | ||
| 491 | |||
| 492 | sub has_next | ||
| 493 | { | ||
| 494 | # i.e., while( $node->has_next ){ ... } | ||
| 495 | # true if the next node is not the empty sentinel. | ||
| 496 | |||
| 497 | my $listh = shift; | ||
| 498 | |||
| 499 | !! $listh->$list_next | ||
| 500 | } | ||
| 501 | |||
| 502 | sub has_data | ||
| 503 | { | ||
| 504 | # i.e., if( $listh->has_data ) { process the node's data } | ||
| 505 | # note that an "empty" node might have a next on and just | ||
| 506 | # no data at that node. | ||
| 507 | |||
| 508 | my $listh = shift; | ||
| 509 | |||
| 510 | @{ $$listh } > 1 | ||
| 511 | } | ||
| 512 | |||
| 513 | sub is_empty | ||
| 514 | { | ||
| 515 | # Q: does the current node have data? | ||
| 516 | # A: it will if there is more than one element. | ||
| 517 | # | ||
| 518 | # $node is syntatic sugar, replacing "$#$$listh" | ||
| 519 | |||
| 520 | my $listh = shift; | ||
| 521 | my $node = $$listh; | ||
| 522 | |||
| 523 | $#$node > 0 | ||
| 524 | } | ||
| 525 | |||
| 526 | sub clear_node_data | ||
| 527 | { | ||
| 528 | my $listh = shift; | ||
| 529 | |||
| 530 | # any data to replace the current data is | ||
| 531 | # left on the stack. | ||
| 532 | |||
| 533 | my $node = $listh | ||
| 534 | or croak "Bogus node_data: listh handler '$listh' does not reference a node"; | ||
| 535 | |||
| 536 | splice @$node, 1, $#$node | ||
| 537 | } | ||
| 538 | |||
| 539 | sub node_data | ||
| 540 | { | ||
| 541 | my $listh = shift; | ||
| 542 | |||
| 543 | # any data to replace the current data is | ||
| 544 | # left on the stack. | ||
| 545 | |||
| 546 | my $node = $$listh | ||
| 547 | or croak "Bogus node_data: listh handler '$listh' does not reference a node"; | ||
| 548 | |||
| 549 | @_ | ||
| 550 | ? $set_node_data->( $node, @_ ) | ||
| 551 | : $get_node_data->( $node ) | ||
| 552 | } | ||
| 553 | |||
| 554 | sub next_data | ||
| 555 | { | ||
| 556 | my $listh = shift; | ||
| 557 | my $node = $$listh | ||
| 558 | or croak "Bogus next_data: listh handler '$listh' does not reference a node"; | ||
| 559 | |||
| 560 | local $$listh = $node->[0]; | ||
| 561 | |||
| 562 | $listh->node_data( @_ ) | ||
| 563 | } | ||
| 564 | |||
| 565 | sub clear_data | ||
| 566 | { | ||
| 567 | my $listh = shift; | ||
| 568 | |||
| 569 | my $node = $$listh | ||
| 570 | or return; | ||
| 571 | |||
| 572 | @$node = ( $node->[0] ); | ||
| 573 | |||
| 574 | $listh | ||
| 575 | } | ||
| 576 | |||
| 577 | sub list_data | ||
| 578 | { | ||
| 579 | my $listh = shift; | ||
| 580 | my $node = $$listh; | ||
| 581 | |||
| 582 | my @return = (); | ||
| 583 | |||
| 584 | while( @$node ) | ||
| 585 | { | ||
| 586 | my ( $node, @data ) = @$node; | ||
| 587 | push @return, \@data; | ||
| 588 | } | ||
| 589 | |||
| 590 | wantarray | ||
| 591 | ? @return | ||
| 592 | : \@return | ||
| 593 | } | ||
| 594 | |||
| 595 | ######################################################################## | ||
| 596 | # access the list head. | ||
| 597 | # | ||
| 598 | # root is mainly useful for testing, | ||
| 599 | # head_node for externally walking the | ||
| 600 | # list (i.e., when OO calls are too expensive). | ||
| 601 | # | ||
| 602 | # new_head is surgery: replace the head node. | ||
| 603 | # leaves most sanity checks in the | ||
| 604 | # caller's hands. | ||
| 605 | # | ||
| 606 | # mainly useful for cross-linked lists. | ||
| 607 | |||
| 608 | sub new_root | ||
| 609 | { | ||
| 610 | my $listh = shift; | ||
| 611 | |||
| 612 | # called without arguments is an error: no reason to | ||
| 613 | # do it and it can cause real pain if not caught. | ||
| 614 | |||
| 615 | my $root = shift | ||
| 616 | or confess "Bogus new_root: false root (use truncate instead?)"; | ||
| 617 | |||
| 618 | 'ARRAY' eq reftype $root | ||
| 619 | or confess "Bogus new_root: non-arrayref root"; | ||
| 620 | |||
| 621 | $root->[0] ||= []; | ||
| 622 | |||
| 623 | $$listh = $root->[0]; | ||
| 624 | |||
| 625 | my $key = refaddr $listh; | ||
| 626 | |||
| 627 | # $cleanup->( delete $rootz{ $key } ); | ||
| 628 | |||
| 629 | $rootz{ $key } = $root; | ||
| 630 | |||
| 631 | $listh | ||
| 632 | } | ||
| 633 | |||
| 634 | sub new_head | ||
| 635 | { | ||
| 636 | my ( $listh, $head ) = @_; | ||
| 637 | |||
| 638 | my $root = $listh->root_node; | ||
| 639 | |||
| 640 | $head = splice @$root, 0, 1, $head; | ||
| 641 | |||
| 642 | # $cleanup->( $head ); | ||
| 643 | |||
| 644 | $listh->head | ||
| 645 | } | ||
| 646 | |||
| 647 | sub root_node | ||
| 648 | { | ||
| 649 | my $listh = shift; | ||
| 650 | my $root = $rootz{ refaddr $listh }; | ||
| 651 | |||
| 652 | wantarray | ||
| 653 | ? @{ $root } | ||
| 654 | : $root | ||
| 655 | } | ||
| 656 | |||
| 657 | sub head_node | ||
| 658 | { | ||
| 659 | my $listh = shift; | ||
| 660 | |||
| 661 | my $root = $listh->root_node; | ||
| 662 | |||
| 663 | wantarray | ||
| 664 | ? @{ $root->[0] } | ||
| 665 | : $root->[0] | ||
| 666 | } | ||
| 667 | |||
| 668 | sub root | ||
| 669 | { | ||
| 670 | my $listh = shift; | ||
| 671 | $$listh = $listh->root_node; | ||
| 672 | |||
| 673 | $listh | ||
| 674 | } | ||
| 675 | |||
| 676 | sub head | ||
| 677 | { | ||
| 678 | my $listh = shift; | ||
| 679 | $$listh = $listh->head_node; | ||
| 680 | |||
| 681 | $listh | ||
| 682 | } | ||
| 683 | |||
| 684 | ######################################################################## | ||
| 685 | # walk the list. | ||
| 686 | |||
| 687 | sub next | ||
| 688 | { | ||
| 689 | my $listh = shift; | ||
| 690 | my $node = $$listh | ||
| 691 | or croak "Bogus next: list handler does not reference a node"; | ||
| 692 | |||
| 693 | if( @_ ) | ||
| 694 | { | ||
| 695 | my $offset = shift; | ||
| 696 | |||
| 697 | looks_like_number $offset | ||
| 698 | or croak "Bogus next: '$offset' is not a number"; | ||
| 699 | |||
| 700 | if( $offset > 0 ) | ||
| 701 | { | ||
| 702 | # testing @$node avoids walking off the | ||
| 703 | # end of the list, leaves $listh at the | ||
| 704 | # tail rather than undef. | ||
| 705 | # | ||
| 706 | # this also means that while( $listh->next ) | ||
| 707 | # can lead to an infinite loop w/o test for | ||
| 708 | # has_next or is_tail or has_data. | ||
| 709 | |||
| 710 | while( @$node && $offset-- ) | ||
| 711 | { | ||
| 712 | $node = $node->[0]; | ||
| 713 | } | ||
| 714 | |||
| 715 | $$listh = $node; | ||
| 716 | } | ||
| 717 | elsif( $offset ) | ||
| 718 | { | ||
| 719 | croak "Bogus next: negative offset '$offset'"; | ||
| 720 | } | ||
| 721 | else | ||
| 722 | { | ||
| 723 | # offset zero, do nothing. | ||
| 724 | } | ||
| 725 | } | ||
| 726 | else | ||
| 727 | { | ||
| 728 | # skip all of the extra work for the | ||
| 729 | # default case of advancing one node. | ||
| 730 | |||
| 731 | $$listh = $node->[0] | ||
| 732 | } | ||
| 733 | |||
| 734 | # note that $listh may reference the tail at | ||
| 735 | # this point but will not be false. | ||
| 736 | |||
| 737 | $listh | ||
| 738 | } | ||
| 739 | |||
| 740 | sub each | ||
| 741 | { | ||
| 742 | my $listh = shift; | ||
| 743 | my $node = $$listh | ||
| 744 | or return; | ||
| 745 | |||
| 746 | # leaves $listh parked at the tail rather than | ||
| 747 | # walking off the list. | ||
| 748 | |||
| 749 | @$node | ||
| 750 | or return; | ||
| 751 | |||
| 752 | ( $$listh, my @valz ) = @$node; | ||
| 753 | |||
| 754 | wantarray | ||
| 755 | ? @valz | ||
| 756 | : \@valz | ||
| 757 | } | ||
| 758 | |||
| 759 | ######################################################################## | ||
| 760 | # Notes: | ||
| 761 | # | ||
| 762 | # * depending on location w/ tail or list having any data | ||
| 763 | # to begin with these may produce an empty list. | ||
| 764 | # | ||
| 765 | # * result lists begin at the root (vs. head) node since add puts the | ||
| 766 | # next node after the current one. | ||
| 767 | |||
| 768 | my $first | ||
| 769 | = sub | ||
| 770 | { | ||
| 771 | my ( $node, $select ) = @_; | ||
| 772 | |||
| 773 | for(;;) | ||
| 774 | { | ||
| 775 | # only ways out are end of list (undef), end of | ||
| 776 | # count (false), or locating a value (true). | ||
| 777 | |||
| 778 | my ( $next, @buffer ) = @$node | ||
| 779 | or return undef; | ||
| 780 | |||
| 781 | $select->( @buffer ) | ||
| 782 | and return $node; | ||
| 783 | |||
| 784 | $node = $next; | ||
| 785 | } | ||
| 786 | }; | ||
| 787 | |||
| 788 | sub first | ||
| 789 | { | ||
| 790 | state $default | ||
| 791 | = sub | ||
| 792 | { | ||
| 793 | # i.e., find the first node that | ||
| 794 | # contains a true data value. | ||
| 795 | |||
| 796 | $_ and return $_ for @_; | ||
| 797 | return | ||
| 798 | }; | ||
| 799 | |||
| 800 | my ( $listh, $select ) = &$handler_args | ||
| 801 | or return; | ||
| 802 | |||
| 803 | my $node = $$listh | ||
| 804 | or return; | ||
| 805 | |||
| 806 | @$node | ||
| 807 | or return; | ||
| 808 | |||
| 809 | $node = $first->( $node->[0], $select || $default ) | ||
| 810 | or return; | ||
| 811 | |||
| 812 | $$listh = $node; | ||
| 813 | |||
| 814 | $listh | ||
| 815 | } | ||
| 816 | |||
| 817 | sub map | ||
| 818 | { | ||
| 819 | state $default | ||
| 820 | = sub | ||
| 821 | { | ||
| 822 | my $buffer = shift; | ||
| 823 | |||
| 824 | @$buffer | ||
| 825 | }; | ||
| 826 | |||
| 827 | my ( $listh, $xform, $count ) = &$handler_args; | ||
| 828 | |||
| 829 | $xform ||= $default; | ||
| 830 | |||
| 831 | # start at the root, not the head since $result->add puts | ||
| 832 | # the data into a new node after the current one. | ||
| 833 | |||
| 834 | my $result = $listh->new->root; | ||
| 835 | my $node = $$listh; | ||
| 836 | my @buffer = (); | ||
| 837 | |||
| 838 | while( ( $node, @buffer ) = @$node ) | ||
| 839 | { | ||
| 840 | $result->add( $xform->( \@buffer ) )->next; | ||
| 841 | |||
| 842 | --$count | ||
| 843 | or last; | ||
| 844 | } | ||
| 845 | |||
| 846 | $result->head | ||
| 847 | } | ||
| 848 | |||
| 849 | sub copy | ||
| 850 | { | ||
| 851 | # i.e., map with default xform | ||
| 852 | |||
| 853 | my $listh = shift; | ||
| 854 | |||
| 855 | $listh->map( '' => @_ ) | ||
| 856 | } | ||
| 857 | |||
| 858 | my $node_nonempty | ||
| 859 | = sub | ||
| 860 | { | ||
| 861 | # i.e., true if the node has any data. | ||
| 862 | |||
| 863 | !! @_ | ||
| 864 | }; | ||
| 865 | |||
| 866 | sub prune | ||
| 867 | { | ||
| 868 | my ( $listh, $select, $count ) = &$handler_args; | ||
| 869 | |||
| 870 | my $node = $listh->root_node; | ||
| 871 | |||
| 872 | while( my $next = $node->[0] ) | ||
| 873 | { | ||
| 874 | $node->[0] | ||
| 875 | = $first-> | ||
| 876 | ( | ||
| 877 | $next, | ||
| 878 | $select || $node_nonempty | ||
| 879 | ) | ||
| 880 | or do | ||
| 881 | { | ||
| 882 | $node->[0] = []; | ||
| 883 | last; | ||
| 884 | }; | ||
| 885 | |||
| 886 | --$count | ||
| 887 | or last; | ||
| 888 | } | ||
| 889 | continue | ||
| 890 | { | ||
| 891 | $node = $node->[0]; | ||
| 892 | } | ||
| 893 | |||
| 894 | $$listh = $node; | ||
| 895 | |||
| 896 | $listh | ||
| 897 | } | ||
| 898 | |||
| 899 | sub grep | ||
| 900 | { | ||
| 901 | my ( $listh, $select, $count ) = &$handler_args; | ||
| 902 | |||
| 903 | $count //= 0; | ||
| 904 | |||
| 905 | my $node = $$listh; | ||
| 906 | my $result = $listh->new->root; | ||
| 907 | my @buffer = (); | ||
| 908 | |||
| 909 | $select ||= $node_nonempty; | ||
| 910 | |||
| 911 | while( ( $node, @buffer ) = @$node ) | ||
| 912 | { | ||
| 913 | $select->( @buffer ) | ||
| 914 | and | ||
| 915 | $result->add( @buffer )->next; | ||
| 916 | |||
| 917 | --$count | ||
| 918 | or last; | ||
| 919 | } | ||
| 920 | |||
| 921 | # in a void context replace the existing list in place. | ||
| 922 | |||
| 923 | defined wantarray | ||
| 924 | ? $result->head | ||
| 925 | : $result->head->clone( $listh ) | ||
| 926 | } | ||
| 927 | |||
| 928 | ##sub reduce | ||
| 929 | ##{ | ||
| 930 | ## my ( $listh, $node, $combine, $count ) = &$handler_args; | ||
| 931 | ## | ||
| 932 | ## my $result = undef; | ||
| 933 | ## my @buffer = (); | ||
| 934 | ## | ||
| 935 | ## while( ( $node, @buffer ) = @$node ) | ||
| 936 | ## { | ||
| 937 | ## $result = $combine->( $result, \@buffer ); | ||
| 938 | ## | ||
| 939 | ## --$count | ||
| 940 | ## or last; | ||
| 941 | ## } | ||
| 942 | ## | ||
| 943 | ## $result | ||
| 944 | ##} | ||
| 945 | ## | ||
| 946 | ##sub grep | ||
| 947 | ##{ | ||
| 948 | ## my ( $listh, $node, $handler, $count ) = &$handler_args; | ||
| 949 | ## | ||
| 950 | ## my $result = $listh->new->root; | ||
| 951 | ## my @buffer = (); | ||
| 952 | ## | ||
| 953 | ## if( $count ) | ||
| 954 | ## { | ||
| 955 | ## for( 1 .. $count ) | ||
| 956 | ## { | ||
| 957 | ## @$node | ||
| 958 | ## or last; | ||
| 959 | ## | ||
| 960 | ## ( $node, @buffer ) = @$node; | ||
| 961 | ## | ||
| 962 | ## $handler->( @buffer ) | ||
| 963 | ## and | ||
| 964 | ## $result->add( @buffer ); | ||
| 965 | ## } | ||
| 966 | ## } | ||
| 967 | ## elsif( defined $node ) | ||
| 968 | ## { | ||
| 969 | ## # search zero nodes: return empty result. | ||
| 970 | ## } | ||
| 971 | ## else | ||
| 972 | ## { | ||
| 973 | ## while( @$node ) | ||
| 974 | ## { | ||
| 975 | ## ( $node, @buffer ) = @$node; | ||
| 976 | ## | ||
| 977 | ## $handler->( @buffer ) | ||
| 978 | ## and | ||
| 979 | ## $result->add( @buffer ); | ||
| 980 | ## } | ||
| 981 | ## } | ||
| 982 | ## | ||
| 983 | ## $result | ||
| 984 | ##} | ||
| 985 | ## | ||
| 986 | ##sub sort | ||
| 987 | ##{ | ||
| 988 | ## my ( $listh, $node, $handler, $count ) = &$handler_args; | ||
| 989 | ## | ||
| 990 | ##} | ||
| 991 | ## | ||
| 992 | ######################################################################## | ||
| 993 | # keep this section at the end to avoid uses with CORE::* functions. | ||
| 994 | ######################################################################## | ||
| 995 | # modify the list | ||
| 996 | # | ||
| 997 | # add uses a relative position (e.g., for insertion sort), others | ||
| 998 | # use the head (or last) node. | ||
| 999 | |||
| 1000 | sub add | ||
| 1001 | { | ||
| 1002 | my $listh = shift; | ||
| 1003 | my $node = $$listh; | ||
| 1004 | |||
| 1005 | # insert after the current node. | ||
| 1006 | |||
| 1007 | $node->[0] = [ $node->[0], @_ ]; | ||
| 1008 | |||
| 1009 | $listh | ||
| 1010 | } | ||
| 1011 | |||
| 1012 | # shift and cut do the same basic thing, question | ||
| 1013 | # is whether it's done mid-list or at the head. | ||
| 1014 | # pop could work this way if it weren't so bloody | ||
| 1015 | # expensive to find/maintain the end of a list. | ||
| 1016 | # | ||
| 1017 | # note that shift has one bit of extra work in that | ||
| 1018 | # it has to replace $$listh when it currently references | ||
| 1019 | # the first node. | ||
| 1020 | |||
| 1021 | sub cut | ||
| 1022 | { | ||
| 1023 | # no need to modify $$listh here since the | ||
| 1024 | # node after the current one is always removed. | ||
| 1025 | |||
| 1026 | my $listh = shift; | ||
| 1027 | my $node = $$listh; | ||
| 1028 | |||
| 1029 | # nothing to cut if we are at the end-of-list. | ||
| 1030 | # or the node prior to it. | ||
| 1031 | |||
| 1032 | @{ $node->[0] } | ||
| 1033 | or return; | ||
| 1034 | |||
| 1035 | if( defined wantarray ) | ||
| 1036 | { | ||
| 1037 | ( $node->[0], my @valz ) = @{ $node->[0] }; | ||
| 1038 | |||
| 1039 | wantarray | ||
| 1040 | ? @valz | ||
| 1041 | : \@valz | ||
| 1042 | } | ||
| 1043 | else | ||
| 1044 | { | ||
| 1045 | # just discard the data if the | ||
| 1046 | # user doesn't want it. | ||
| 1047 | |||
| 1048 | $node->[0] = $node->[0][0]; | ||
| 1049 | } | ||
| 1050 | } | ||
| 1051 | |||
| 1052 | ######################################################################## | ||
| 1053 | # put these last to avoid having to use CORE::* | ||
| 1054 | # everywhere else. | ||
| 1055 | |||
| 1056 | sub splice | ||
| 1057 | { | ||
| 1058 | my $listh = shift; | ||
| 1059 | my $count = shift || 0; | ||
| 1060 | |||
| 1061 | looks_like_number $count | ||
| 1062 | or croak "Bogus splice: non-numeric count '$count'"; | ||
| 1063 | |||
| 1064 | $count < 0 | ||
| 1065 | and croak "Bogus splice: negative count '$count'"; | ||
| 1066 | |||
| 1067 | # short circut if there is nothing to do. | ||
| 1068 | |||
| 1069 | $count > 0 || @_ | ||
| 1070 | or return; | ||
| 1071 | |||
| 1072 | my $node = $$listh | ||
| 1073 | or confess "Bogus splice: empty list handler"; | ||
| 1074 | |||
| 1075 | my $dead = ''; | ||
| 1076 | |||
| 1077 | if( $count > 0 ) | ||
| 1078 | { | ||
| 1079 | my $tail = $node; | ||
| 1080 | |||
| 1081 | for( 1 .. $count ) | ||
| 1082 | { | ||
| 1083 | @$tail or last; | ||
| 1084 | |||
| 1085 | $tail = $tail->[0]; | ||
| 1086 | } | ||
| 1087 | |||
| 1088 | # this is the start of the chain that gets removed. | ||
| 1089 | # keep it alive for a few steps to see if the caller | ||
| 1090 | # wants it back or we should clean it up. | ||
| 1091 | # | ||
| 1092 | # after that, splice the node out of the list. | ||
| 1093 | |||
| 1094 | $dead = $node->[0]; | ||
| 1095 | $node->[0] = delete $tail->[0]; | ||
| 1096 | $tail->[0] = []; | ||
| 1097 | } | ||
| 1098 | |||
| 1099 | # at this point $dead is either false or | ||
| 1100 | # a runt linked list lacking its terminating | ||
| 1101 | # node. | ||
| 1102 | # | ||
| 1103 | # insert anything on the stack after the | ||
| 1104 | # current node. | ||
| 1105 | |||
| 1106 | for( @_ ) | ||
| 1107 | { | ||
| 1108 | $node = $node->[0] = [ $node->[0], $_ ]; | ||
| 1109 | } | ||
| 1110 | |||
| 1111 | # nothing to return or clean up if there | ||
| 1112 | # wasn't anything removed. | ||
| 1113 | |||
| 1114 | $dead or return; | ||
| 1115 | |||
| 1116 | # if the caller wants anything back then | ||
| 1117 | # clean up the dead chain and hand it back. | ||
| 1118 | # | ||
| 1119 | # alternative: array of data? | ||
| 1120 | |||
| 1121 | if( defined wantarray ) | ||
| 1122 | { | ||
| 1123 | # hand back a linked list with $dead as the head node. | ||
| 1124 | |||
| 1125 | my $new = $listh->new; | ||
| 1126 | |||
| 1127 | my $head = $$new; | ||
| 1128 | |||
| 1129 | @$head = @$dead; | ||
| 1130 | |||
| 1131 | return $new | ||
| 1132 | } | ||
| 1133 | else | ||
| 1134 | { | ||
| 1135 | # $cleanup->( $dead ); | ||
| 1136 | |||
| 1137 | return | ||
| 1138 | } | ||
| 1139 | } | ||
| 1140 | |||
| 1141 | ######################################################################## | ||
| 1142 | # aside: push can be very expensive. | ||
| 1143 | # but, then, so is maintaining a separate | ||
| 1144 | # node-before-the-tail entry. | ||
| 1145 | # | ||
| 1146 | # successive pushes are quite fast, due to | ||
| 1147 | # leaving $$listh on the newly added node, | ||
| 1148 | # which leaves the while loop running only | ||
| 1149 | # once per push. | ||
| 1150 | |||
| 1151 | sub push | ||
| 1152 | { | ||
| 1153 | my $listh = shift; | ||
| 1154 | my $node = $$listh; | ||
| 1155 | |||
| 1156 | $node = $node->[0] | ||
| 1157 | while @$node; | ||
| 1158 | |||
| 1159 | # at this point we're at the list tail: the | ||
| 1160 | # empty placeholder arrayref. populate it in | ||
| 1161 | # place with a new tail. | ||
| 1162 | |||
| 1163 | @$node = ( [], @_ ); | ||
| 1164 | |||
| 1165 | $$listh = $node; | ||
| 1166 | |||
| 1167 | $listh | ||
| 1168 | } | ||
| 1169 | |||
| 1170 | sub unshift | ||
| 1171 | { | ||
| 1172 | my $root = $_[0]->root_node; | ||
| 1173 | |||
| 1174 | $root->[0] = [ $root->[0], @_ ]; | ||
| 1175 | |||
| 1176 | $_[0] | ||
| 1177 | } | ||
| 1178 | |||
| 1179 | sub shift | ||
| 1180 | { | ||
| 1181 | my $listh = shift; | ||
| 1182 | my $root = $listh->root_node; | ||
| 1183 | |||
| 1184 | # need to replace $listh contents if it | ||
| 1185 | # referrs to the head we are removing! | ||
| 1186 | |||
| 1187 | $$listh = '' | ||
| 1188 | if $$listh == $root->[0]; | ||
| 1189 | |||
| 1190 | if( defined wantarray ) | ||
| 1191 | { | ||
| 1192 | my @valz = @{ $root->[0] }; | ||
| 1193 | |||
| 1194 | $root->[0] = shift @valz; | ||
| 1195 | |||
| 1196 | $$listh ||= $root->[0]; | ||
| 1197 | |||
| 1198 | wantarray | ||
| 1199 | ? @valz | ||
| 1200 | : \@valz | ||
| 1201 | } | ||
| 1202 | else | ||
| 1203 | { | ||
| 1204 | # get this over with for cases where | ||
| 1205 | # the user doesn't want the data. | ||
| 1206 | |||
| 1207 | $root->[0][0] | ||
| 1208 | and $root->[0] = $root->[0][0]; | ||
| 1209 | |||
| 1210 | $$listh ||= $root->[0]; | ||
| 1211 | |||
| 1212 | return | ||
| 1213 | } | ||
| 1214 | } | ||
| 1215 | |||
| 1216 | # keep require happy | ||
| 1217 | |||
| 1218 | 1 | ||
| 1219 | |||
| 1220 | __END__ |