| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | |||
| 5 | package LinkedList::Single v2.0.0; | ||
| 6 | use v5.40; | ||
| 7 | use FindBin::libs; | ||
| 8 | |||
| 9 | use Object::Pad v0.821; | ||
| 10 | |||
| 11 | use Carp qw( croak ); | ||
| 12 | use Data::Dumper qw( Dumper ); | ||
| 13 | use Sub::Name qw( subname ); | ||
| 14 | use Symbol qw( qualify qualify_to_ref ); | ||
| 15 | |||
| 16 | my $nmgr_r = qualify 'NodeMgr'; | ||
| 17 | |||
| 18 | my $list_c = qualify 'List'; | ||
| 19 | my $curs_c = qualify 'Cursor'; | ||
| 20 | my $frag_c = qualify 'Fragment'; | ||
| 21 | |||
| 22 | sub new | ||
| 23 | { | ||
| 24 | $list_c->new | ||
| 25 | } | ||
| 26 | |||
| 27 | ######################################################################## | ||
| 28 | # package varaibles and sanity checks | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | our @CARP_NOT = qw( $nmgr_r $list_c $curs_c $frag_c ); | ||
| 32 | |||
| 33 | ######################################################################## | ||
| 34 | # utility subs | ||
| 35 | ######################################################################## | ||
| 36 | |||
| 37 | ######################################################################## | ||
| 38 | # general purpose | ||
| 39 | |||
| 40 | my $dump_format | ||
| 41 | = sub | ||
| 42 | { | ||
| 43 | local $Data::Dumper::Terse = 1; | ||
| 44 | local $Data::Dumper::Indent = 1; | ||
| 45 | local $Data::Dumper::Sortkeys = 1; | ||
| 46 | |||
| 47 | local $Data::Dumper::Purity = 0; | ||
| 48 | local $Data::Dumper::Deepcopy = 0; | ||
| 49 | local $Data::Dumper::Quotekeys = 0; | ||
| 50 | |||
| 51 | join "\n\t" => '', map { ref $_ ? Dumper $_ : $_ } @_ | ||
| 52 | }; | ||
| 53 | |||
| 54 | ######################################################################## | ||
| 55 | # private methods for unwrapping the node structure that simplify | ||
| 56 | # managing the classes. any modification to the structure in | ||
| 57 | # NodeMgr will need to be reflected in these kwikhaks or pushed | ||
| 58 | # down into NodeMgr calls. | ||
| 59 | |||
| 60 | # some of these are used externally for tesitng | ||
| 61 | |||
| 62 | my $node_data | ||
| 63 | = subname node_data | ||
| 64 | => sub( $node ) | ||
| 65 | { | ||
| 66 | # data for the root node can be used for list | ||
| 67 | # metadata for the list and always exists. | ||
| 68 | # | ||
| 69 | # cursors return whatever's in the current node, | ||
| 70 | # croaking on the sentinel. | ||
| 71 | |||
| 72 | $node->@* | ||
| 73 | or croak 'Bogus $node: sentinel'; | ||
| 74 | |||
| 75 | wantarray // return; | ||
| 76 | wantarray | ||
| 77 | ? $node->@[ 1 .. $#$node ] | ||
| 78 | : [ $node->@[ 1 .. $#$node ] ] | ||
| 79 | }; | ||
| 80 | |||
| 81 | my $drop_node | ||
| 82 | = subname drop | ||
| 83 | => sub( $node ) | ||
| 84 | { | ||
| 85 | # $next keeps the node contents alive long enough | ||
| 86 | # to return the data. | ||
| 87 | |||
| 88 | my $next = $node->[0] | ||
| 89 | or croak 'Bogus drop_node at sentinel node'; | ||
| 90 | |||
| 91 | $next->@* | ||
| 92 | ? $node->[0] = $next->[0] | ||
| 93 | : croak 'Bogus drop next is sentinel' | ||
| 94 | ; | ||
| 95 | |||
| 96 | $next->$node_data | ||
| 97 | }; | ||
| 98 | |||
| 99 | my $append | ||
| 100 | = subname append | ||
| 101 | => sub( $node, @data ) | ||
| 102 | { | ||
| 103 | $node->[0] = [ $node->[0], @data ]; | ||
| 104 | }; | ||
| 105 | |||
| 106 | my $sentinel_node | ||
| 107 | = subname sentinel_node | ||
| 108 | => sub() | ||
| 109 | { | ||
| 110 | [] | ||
| 111 | }; | ||
| 112 | |||
| 113 | ######################################################################## | ||
| 114 | # Object Managlement | ||
| 115 | ######################################################################## | ||
| 116 | |||
| 117 | role LinkedList::Single::NodeMgr | ||
| 118 | { | ||
| 119 | # Note: operations that unwrap the object contents | ||
| 120 | # belong here, shared by any classes that have to | ||
| 121 | # manipulate nodes. things like "$obj->node = $obj->next" | ||
| 122 | # that don't manipulate the object guts abound in | ||
| 123 | # the Cursor class, but nowhere there are $node | ||
| 124 | # internals examined outside of this role. | ||
| 125 | |||
| 126 | ############################################################ | ||
| 127 | # housekeeping | ||
| 128 | ############################################################ | ||
| 129 | |||
| 130 | use Carp qw( croak ); | ||
| 131 | use Scalar::Util qw( blessed reftype ); | ||
| 132 | |||
| 133 | our @CARP_NOT = ( __PACKAGE__, qw( Object::Pad ) ); | ||
| 134 | |||
| 135 | ############################################################ | ||
| 136 | # definition | ||
| 137 | ############################################################ | ||
| 138 | |||
| 139 | field $node; # notice the lack of a default | ||
| 140 | |||
| 141 | method object_type() | ||
| 142 | { | ||
| 143 | # classes can override this to provide more | ||
| 144 | # info (e.g., "List::Circular"). | ||
| 145 | # | ||
| 146 | # return $type to simplify handling in a scalar | ||
| 147 | # context. | ||
| 148 | |||
| 149 | my ( $type ) = $self->META->name =~ m{ (\w+) $}x; | ||
| 150 | |||
| 151 | $type | ||
| 152 | } | ||
| 153 | |||
| 154 | ############################################################ | ||
| 155 | # construct the object | ||
| 156 | ############################################################ | ||
| 157 | |||
| 158 | ADJUST :params ( :$whence = undef ) | ||
| 159 | { | ||
| 160 | my $name = $self->META->name; | ||
| 161 | |||
| 162 | # this could be spread across the classes, simpler to | ||
| 163 | # have it here. note that if-logic needs to proceed | ||
| 164 | # from most-to-least derived. | ||
| 165 | |||
| 166 | if( $self->isa( $list_c ) ) | ||
| 167 | { | ||
| 168 | $whence | ||
| 169 | and croak "Botched $name: extraneous 'whence' argument"; | ||
| 170 | |||
| 171 | $node = $sentinel_node->(); | ||
| 172 | } | ||
| 173 | elsif( $self->isa( $curs_c ) ) | ||
| 174 | { | ||
| 175 | # cursors require and object, not a bare node. | ||
| 176 | # note that this intentionally will create a | ||
| 177 | # cursor to an empty list in order to support | ||
| 178 | # the sentinel and *_lazy methods. | ||
| 179 | |||
| 180 | blessed $whence | ||
| 181 | or croak "Botched $name: '$whence' is not an object"; | ||
| 182 | |||
| 183 | $whence->DOES( $nmgr_r ) | ||
| 184 | or croak "Botched $name: '$whence' does not $nmgr_r"; | ||
| 185 | |||
| 186 | $node = $whence->node; | ||
| 187 | } | ||
| 188 | elsif( $self->isa( $frag_c ) ) | ||
| 189 | { | ||
| 190 | if( $whence ) | ||
| 191 | { | ||
| 192 | state $sent_type = reftype $sentinel_node->(); | ||
| 193 | |||
| 194 | # fragments require a bare node, normally | ||
| 195 | # as a by-product of detach. | ||
| 196 | |||
| 197 | blessed $whence | ||
| 198 | and croak "Botched $name: '$whence' is not a bare node."; | ||
| 199 | |||
| 200 | reftype $whence eq $sent_type | ||
| 201 | or croak "Botched $name: '$whence' is not '$sent_type'"; | ||
| 202 | |||
| 203 | $node = [ splice $whence->@* ]; | ||
| 204 | } | ||
| 205 | else | ||
| 206 | { | ||
| 207 | # empty fragment is reasonable, | ||
| 208 | # e.g., generate_lazy(). | ||
| 209 | |||
| 210 | $node = $sentinel_node->(); | ||
| 211 | } | ||
| 212 | } | ||
| 213 | else | ||
| 214 | { | ||
| 215 | croak "Unknown class: none of $list_c, $curs_c, $frag_c"; | ||
| 216 | } | ||
| 217 | } | ||
| 218 | |||
| 219 | method cursor() | ||
| 220 | { | ||
| 221 | $curs_c->new( whence => $self ) | ||
| 222 | } | ||
| 223 | |||
| 224 | method fragment() | ||
| 225 | { | ||
| 226 | $frag_c->new( whence => $self->node ) | ||
| 227 | } | ||
| 228 | |||
| 229 | method list() | ||
| 230 | { | ||
| 231 | $self | ||
| 232 | ->fragment | ||
| 233 | ->push_onto | ||
| 234 | ( | ||
| 235 | $list_c->new | ||
| 236 | ) | ||
| 237 | } | ||
| 238 | |||
| 239 | ############################################################ | ||
| 240 | # introspection | ||
| 241 | ############################################################ | ||
| 242 | # calling them 'car/cdr' seemed a bit obscure; 'next' and | ||
| 243 | # 'data' seemed more approachable. | ||
| 244 | # | ||
| 245 | # these are data leaks to some extent; workable so long as | ||
| 246 | # caller's don't inspect the contents of what's returned | ||
| 247 | # for the node. | ||
| 248 | |||
| 249 | method node :lvalue | ||
| 250 | { | ||
| 251 | # ideally "my method node", but that doesn't work | ||
| 252 | # in O::P yet. sans the private method is is where | ||
| 253 | # the internal data structure leaks out. | ||
| 254 | # | ||
| 255 | # use to transfer node value between objects: | ||
| 256 | # $node = $whence->node. | ||
| 257 | |||
| 258 | $node | ||
| 259 | } | ||
| 260 | |||
| 261 | method next_node :lvalue | ||
| 262 | { | ||
| 263 | $node->[0] | ||
| 264 | }; | ||
| 265 | |||
| 266 | method tail_node() | ||
| 267 | { | ||
| 268 | $node->@* | ||
| 269 | or croak 'Bogus tail_node at sentinel'; | ||
| 270 | |||
| 271 | my $tail = $node; | ||
| 272 | |||
| 273 | for | ||
| 274 | ( | ||
| 275 | my $next = $tail->[0] | ||
| 276 | ; $next->@* | ||
| 277 | ; $next = $next->[0] | ||
| 278 | ) | ||
| 279 | { | ||
| 280 | $tail = $next; | ||
| 281 | } | ||
| 282 | |||
| 283 | |||
| 284 | $tail | ||
| 285 | } | ||
| 286 | |||
| 287 | method is_sentinel() | ||
| 288 | { | ||
| 289 | ! $node->@* | ||
| 290 | } | ||
| 291 | |||
| 292 | method count() | ||
| 293 | { | ||
| 294 | # sentinel short-curcuits the ++$i, leaves | ||
| 295 | # the count at zero. | ||
| 296 | |||
| 297 | my $i = 0; | ||
| 298 | my $curr = $node; | ||
| 299 | |||
| 300 | ++$i while ( $curr ) = $curr->@*; | ||
| 301 | |||
| 302 | $i | ||
| 303 | } | ||
| 304 | |||
| 305 | ############################################################ | ||
| 306 | # node data | ||
| 307 | |||
| 308 | method data() | ||
| 309 | { | ||
| 310 | $node->@* | ||
| 311 | or croak 'Bogus data: at sentinel'; | ||
| 312 | |||
| 313 | my $wa = wantarray | ||
| 314 | // croak 'Botched data: used in void context'; | ||
| 315 | |||
| 316 | $wa | ||
| 317 | ? $node->@[ 1 .. $#$node ] | ||
| 318 | : [ $node->@[ 1 .. $#$node ] ] | ||
| 319 | } | ||
| 320 | |||
| 321 | method next_data() | ||
| 322 | { | ||
| 323 | my $next = $node->[0] | ||
| 324 | or croak 'Bogus next_data at sentinel'; | ||
| 325 | |||
| 326 | my $wa = wantarray | ||
| 327 | // croak 'Botched next_data: used in void context'; | ||
| 328 | |||
| 329 | $wa | ||
| 330 | ? $next->@[ 1 .. $#$next ] | ||
| 331 | : [ $next->@[ 1 .. $#$next ] ] | ||
| 332 | } | ||
| 333 | |||
| 334 | method set_data | ||
| 335 | { | ||
| 336 | # this can set the sentinal node, | ||
| 337 | # effectivly making it a push. | ||
| 338 | |||
| 339 | $node->@* = ( $node->[0] || $sentinel_node->(), @_ ); | ||
| 340 | |||
| 341 | $self | ||
| 342 | } | ||
| 343 | |||
| 344 | method push_data | ||
| 345 | { | ||
| 346 | push $node->@*, @_; | ||
| 347 | |||
| 348 | $self | ||
| 349 | } | ||
| 350 | |||
| 351 | ############################################################ | ||
| 352 | # obvious locations along the list. | ||
| 353 | # head lives with the list as that is the only place | ||
| 354 | # we are guaranteed to find it. | ||
| 355 | |||
| 356 | method next( $count = 1 ) | ||
| 357 | { | ||
| 358 | my $curs = $self->cursor; | ||
| 359 | |||
| 360 | $count | ||
| 361 | and do | ||
| 362 | { | ||
| 363 | $count > 0 | ||
| 364 | or croak "Botched next: negative count '$count'"; | ||
| 365 | |||
| 366 | my $next = $node; | ||
| 367 | $next = $next->[0] for ( 1 .. $count ); | ||
| 368 | |||
| 369 | $curs->node = $next; | ||
| 370 | }; | ||
| 371 | |||
| 372 | $curs | ||
| 373 | } | ||
| 374 | |||
| 375 | method tail() | ||
| 376 | { | ||
| 377 | my $curs = $self->cursor; | ||
| 378 | $curs->node = $curs->tail_node; | ||
| 379 | |||
| 380 | $curs | ||
| 381 | } | ||
| 382 | |||
| 383 | method sentinel() | ||
| 384 | { | ||
| 385 | # this may not advance at all for an empty list or | ||
| 386 | # cursor at the sentinel already. | ||
| 387 | # | ||
| 388 | # bit less work than tail since it dosn't have to | ||
| 389 | # check the following node, only the current one. | ||
| 390 | |||
| 391 | my $curs = $self->cursor; | ||
| 392 | $curs->node | ||
| 393 | = do | ||
| 394 | { | ||
| 395 | my $sent = $node; | ||
| 396 | $sent = $sent->[0] while $sent->@*; | ||
| 397 | $sent | ||
| 398 | }; | ||
| 399 | |||
| 400 | $curs | ||
| 401 | } | ||
| 402 | |||
| 403 | ############################################################ | ||
| 404 | # modify the sequnce of nodes | ||
| 405 | # | ||
| 406 | # insert has a bit more overhead due to copying the values, | ||
| 407 | # useully not enough differnce to notice. | ||
| 408 | # | ||
| 409 | # push/pop make sense on a cursor and live here; shift and | ||
| 410 | # unshift only make sense on a list and live there. | ||
| 411 | |||
| 412 | method push | ||
| 413 | { | ||
| 414 | if( $node->@* ) | ||
| 415 | { | ||
| 416 | $self->tail->append( @_ ) | ||
| 417 | } | ||
| 418 | else | ||
| 419 | { | ||
| 420 | # push at the sentinel still works, just | ||
| 421 | # moves the sentinel out one node.. | ||
| 422 | |||
| 423 | $node->@* = ( $sentinel_node->(), @_ ); | ||
| 424 | } | ||
| 425 | |||
| 426 | $self | ||
| 427 | } | ||
| 428 | |||
| 429 | method pop() | ||
| 430 | { | ||
| 431 | $self | ||
| 432 | or croak "Bogus pop at sentinel"; | ||
| 433 | |||
| 434 | $self->tail->delete | ||
| 435 | } | ||
| 436 | |||
| 437 | method insert | ||
| 438 | { | ||
| 439 | # insert a new node prior to this one. | ||
| 440 | # the [] allocate new storage with the | ||
| 441 | # value of $ref as the next node and | ||
| 442 | # the new data passed in. | ||
| 443 | # | ||
| 444 | # this is more expensive than append due to | ||
| 445 | # copying $node->@*. | ||
| 446 | # generally, prefer append; but this is | ||
| 447 | # allowable at the sentinel and makes sense | ||
| 448 | # for a list. | ||
| 449 | |||
| 450 | $node->@* = ( [ $node->@* ], @_ ); | ||
| 451 | $self | ||
| 452 | } | ||
| 453 | |||
| 454 | method reverse | ||
| 455 | { | ||
| 456 | $node->@* | ||
| 457 | or return $self; | ||
| 458 | |||
| 459 | # effectively $self->unshift( $tmp->shift ) | ||
| 460 | # without all of the method call overhead. | ||
| 461 | # | ||
| 462 | # $next buffers $tmp->next while $head gets | ||
| 463 | # moved to the new list. | ||
| 464 | |||
| 465 | my $head = $self->fragment->node; | ||
| 466 | my $next = ''; | ||
| 467 | |||
| 468 | while( $head->@* ) | ||
| 469 | { | ||
| 470 | $next = splice $head->@*, 0, 1, $node; | ||
| 471 | $node = $head; | ||
| 472 | $head = $next | ||
| 473 | } | ||
| 474 | |||
| 475 | $self | ||
| 476 | } | ||
| 477 | |||
| 478 | method generate_lazy( $generator ) | ||
| 479 | { | ||
| 480 | # ignore $self this just generates a fragment. | ||
| 481 | |||
| 482 | my $frag = $frag_c->new; | ||
| 483 | |||
| 484 | try | ||
| 485 | { | ||
| 486 | # push whatever the generator hands us, which | ||
| 487 | # may be empty, until it does a 'die "\n"', | ||
| 488 | # interruping the assignment, leaving $node as | ||
| 489 | # the sentinel. | ||
| 490 | |||
| 491 | for( my $node = $frag->node ;; ) | ||
| 492 | { | ||
| 493 | ( $node ) = | ||
| 494 | ( | ||
| 495 | $node->@* = ( $sentinel_node->(), $generator->() ) | ||
| 496 | ) | ||
| 497 | } | ||
| 498 | } | ||
| 499 | catch( $err ) | ||
| 500 | { | ||
| 501 | # anything other than a newline is a 'real' | ||
| 502 | # exception and has to be passed on. | ||
| 503 | |||
| 504 | $err eq "\n" | ||
| 505 | or croak "Failed append_lazy: $err"; | ||
| 506 | } | ||
| 507 | |||
| 508 | $frag | ||
| 509 | } | ||
| 510 | |||
| 511 | method append_lazy( $generator ) | ||
| 512 | { | ||
| 513 | # can't append_to an empty list/sentinel: if $self | ||
| 514 | # is false then use insert. | ||
| 515 | |||
| 516 | $self | ||
| 517 | ? $self->generate_lazy( $generator )->append_to( $self ) | ||
| 518 | : $self->generate_lazy( $generator )->insert_at( $self ) | ||
| 519 | } | ||
| 520 | |||
| 521 | method insert_lazy( $generator ) | ||
| 522 | { | ||
| 523 | # unshift_lazy == $list->head->insert_lazy | ||
| 524 | |||
| 525 | $self->generate_lazy( $generator )->insert_at( $self ) | ||
| 526 | } | ||
| 527 | |||
| 528 | method push_lazy( $generator ) | ||
| 529 | { | ||
| 530 | $self->sentinel->insert_lazy( $generator ) | ||
| 531 | } | ||
| 532 | |||
| 533 | method push_list( @valz ) | ||
| 534 | { | ||
| 535 | $self->push_lazy | ||
| 536 | ( | ||
| 537 | sub | ||
| 538 | { | ||
| 539 | @valz or die "\n"; | ||
| 540 | shift @valz | ||
| 541 | } | ||
| 542 | ) | ||
| 543 | } | ||
| 544 | |||
| 545 | method transfer_node( $thence ) | ||
| 546 | { | ||
| 547 | $thence->node->@* = splice $node->@*; | ||
| 548 | $self | ||
| 549 | } | ||
| 550 | |||
| 551 | ############################################################ | ||
| 552 | # | ||
| 553 | # delete == 'delete key' == remove $node node. | ||
| 554 | # drop == backspace == remove next node. | ||
| 555 | # | ||
| 556 | # $list->head->delete == shift; | ||
| 557 | # $list->tail->drop == pop; | ||
| 558 | # | ||
| 559 | # the head is extraneous since lists cannot move, but you | ||
| 560 | # get the point. | ||
| 561 | # | ||
| 562 | ############################################################ | ||
| 563 | |||
| 564 | method delete() | ||
| 565 | { | ||
| 566 | # delete the current node, replacing it with the | ||
| 567 | # contents of the next node. | ||
| 568 | # | ||
| 569 | # Note: this may leave the current node as the | ||
| 570 | # sentinel. | ||
| 571 | # | ||
| 572 | # Note: drop is more efficient since it doesn't | ||
| 573 | # copy both node contents, only the next ref. | ||
| 574 | # | ||
| 575 | # Note: delete on a list == shift; the difference | ||
| 576 | # being that "shift" is always at the head. | ||
| 577 | |||
| 578 | my $next = $node->[0] | ||
| 579 | or croak 'Bogus delete at sentinel'; | ||
| 580 | |||
| 581 | if( defined wantarray ) | ||
| 582 | { | ||
| 583 | # 1 .. $#$node == skip the next ref, only data. | ||
| 584 | ( | ||
| 585 | splice $node->@*, 0, 1+$#$node, $next->@* | ||
| 586 | )[1..$#$node] | ||
| 587 | } | ||
| 588 | else | ||
| 589 | { | ||
| 590 | $node->@* = $next->@* | ||
| 591 | } | ||
| 592 | } | ||
| 593 | |||
| 594 | # sanity check: is the 1..$self->count any worse than | ||
| 595 | # using push (in map)? if not then all we need is map | ||
| 596 | # with a default of "sub{ @_ }". benchmark this with | ||
| 597 | # some really large lists before leaving it in. | ||
| 598 | |||
| 599 | # bulk extract the list contents into a single flat | ||
| 600 | # list or list of arrayrefs. | ||
| 601 | |||
| 602 | method flat_list() | ||
| 603 | { | ||
| 604 | defined( my $wa = wantarray ) | ||
| 605 | or croak 'Bogus extract: used in void context'; | ||
| 606 | |||
| 607 | $self | ||
| 608 | or return; | ||
| 609 | |||
| 610 | my @data = (); | ||
| 611 | my $node = $self->node; | ||
| 612 | my @found | ||
| 613 | = map | ||
| 614 | { | ||
| 615 | ( $node, @data ) = $node->@*; | ||
| 616 | @data | ||
| 617 | } | ||
| 618 | ( 1 .. $self->count ); | ||
| 619 | |||
| 620 | return | ||
| 621 | $wa | ||
| 622 | ? @found | ||
| 623 | : \@found | ||
| 624 | } | ||
| 625 | |||
| 626 | method extract() | ||
| 627 | { | ||
| 628 | # bulk extract the list contents. | ||
| 629 | # cursors allow extracting only a portion. | ||
| 630 | |||
| 631 | defined( my $wa = wantarray ) | ||
| 632 | or croak 'Bogus extract: used in void context'; | ||
| 633 | |||
| 634 | # the empty list is sufficient to notice that | ||
| 635 | # nothing was extracted. | ||
| 636 | |||
| 637 | $node->@* | ||
| 638 | or return; | ||
| 639 | |||
| 640 | my $curr = $self->node; | ||
| 641 | my @found | ||
| 642 | = map | ||
| 643 | { | ||
| 644 | ( $curr, my @data ) = $curr->@*; | ||
| 645 | \@data | ||
| 646 | } | ||
| 647 | ( 1 .. $self->count ); | ||
| 648 | |||
| 649 | return | ||
| 650 | $wa | ||
| 651 | ? @found | ||
| 652 | : \@found | ||
| 653 | } | ||
| 654 | |||
| 655 | method push_args( @argz ) | ||
| 656 | { | ||
| 657 | $self | ||
| 658 | ->generate_lazy | ||
| 659 | ( | ||
| 660 | sub | ||
| 661 | { | ||
| 662 | @argz | ||
| 663 | ? shift @argz | ||
| 664 | : die "\n" | ||
| 665 | } | ||
| 666 | ) | ||
| 667 | ->push_onto( $self ) | ||
| 668 | } | ||
| 669 | } | ||
| 670 | |||
| 671 | role LinkedList::Single::Header | ||
| 672 | { | ||
| 673 | use Carp qw( croak ); | ||
| 674 | use Scalar::Util qw( blessed ); | ||
| 675 | use Symbol qw( qualify_to_ref ); | ||
| 676 | |||
| 677 | ############################################################ | ||
| 678 | # list ops | ||
| 679 | # | ||
| 680 | # yes, cursor and head are identical: the only node | ||
| 681 | # available is the head but having a consistent name | ||
| 682 | # for it seems worthwhile and provides a saner error | ||
| 683 | # message. | ||
| 684 | # | ||
| 685 | # extensions to this could add a ( class => ... ) | ||
| 686 | # argument; for the moment this simply uses the | ||
| 687 | # hardwired class. | ||
| 688 | |||
| 689 | method head() | ||
| 690 | { | ||
| 691 | # head of an empty list is meaningless: there is | ||
| 692 | # no data node to return. | ||
| 693 | |||
| 694 | $self | ||
| 695 | or croak 'Bogus head: empty list'; | ||
| 696 | |||
| 697 | $curs_c->new( whence => $self ) | ||
| 698 | } | ||
| 699 | |||
| 700 | method shift() | ||
| 701 | { | ||
| 702 | $self | ||
| 703 | or croak 'Bogus shift: empty list'; | ||
| 704 | |||
| 705 | $self->delete | ||
| 706 | } | ||
| 707 | |||
| 708 | method unshift | ||
| 709 | { | ||
| 710 | $self->insert( @_ ) | ||
| 711 | } | ||
| 712 | |||
| 713 | method unshift_lazy( $generator ) | ||
| 714 | { | ||
| 715 | $self->cursor->insert_lazy( $generator ) | ||
| 716 | } | ||
| 717 | |||
| 718 | method unshift_list( @node_valz ) | ||
| 719 | { | ||
| 720 | @node_valz | ||
| 721 | or return $self; | ||
| 722 | |||
| 723 | $self | ||
| 724 | ->generate_lazy | ||
| 725 | ( | ||
| 726 | sub | ||
| 727 | { | ||
| 728 | @node_valz | ||
| 729 | or die "\n"; | ||
| 730 | |||
| 731 | shift @node_valz | ||
| 732 | } | ||
| 733 | ) | ||
| 734 | ->insert_at | ||
| 735 | ( | ||
| 736 | $self | ||
| 737 | ) | ||
| 738 | } | ||
| 739 | } | ||
| 740 | |||
| 741 | role LinkedList::Single::Iterator; | ||
| 742 | { | ||
| 743 | ############################################################ | ||
| 744 | # housekeeping | ||
| 745 | ############################################################ | ||
| 746 | |||
| 747 | use Carp qw( croak carp ); | ||
| 748 | use Scalar::Util qw( looks_like_number ); | ||
| 749 | |||
| 750 | our @CARP_NOT = ( __PACKAGE__, qw( Object::Pad ) ); | ||
| 751 | |||
| 752 | ############################################################ | ||
| 753 | # methods | ||
| 754 | ############################################################ | ||
| 755 | |||
| 756 | method advance() | ||
| 757 | { | ||
| 758 | # the assignment may leave $self on the sentinal (i.e., | ||
| 759 | # false), which is intentional if we want to | ||
| 760 | # indicate that the list has been consumed by a cursor. | ||
| 761 | # | ||
| 762 | # e.g., | ||
| 763 | # | ||
| 764 | # while( $cursor ) { ... ; $cursor->advance } | ||
| 765 | # | ||
| 766 | # for( my $curs = $list->cursor ; $curs ; $curs->advance ) | ||
| 767 | # { ... } | ||
| 768 | # | ||
| 769 | # my $curs = $list->head; | ||
| 770 | # for(;;) { ... ; $curs->advance->is_sentinel and last } | ||
| 771 | # | ||
| 772 | # this dodges a trivial exception and allows the | ||
| 773 | # test for $self to gracefully stop the processing | ||
| 774 | |||
| 775 | my $next = $self->next_node | ||
| 776 | or croak 'Bogus advance at sentinel'; | ||
| 777 | |||
| 778 | # if we are alive at this point then there is a next node. | ||
| 779 | |||
| 780 | $self->node = $next; | ||
| 781 | $self | ||
| 782 | } | ||
| 783 | |||
| 784 | ############################################################ | ||
| 785 | # manage list contents. | ||
| 786 | # append and drop make no sense for a list so they live | ||
| 787 | # down here with the cursor (e.g., push == $list->tail->append). | ||
| 788 | |||
| 789 | method append | ||
| 790 | { | ||
| 791 | # effectivly push. | ||
| 792 | |||
| 793 | $self->is_sentinel | ||
| 794 | ? $self->set_data( $sentinel_node->(), @_ ) | ||
| 795 | : $self->node->$append( @_ ) | ||
| 796 | ; | ||
| 797 | |||
| 798 | $self | ||
| 799 | } | ||
| 800 | |||
| 801 | method drop() | ||
| 802 | { | ||
| 803 | # remove the node following this one, returning | ||
| 804 | # the data if requested. | ||
| 805 | |||
| 806 | $self->node->$drop_node | ||
| 807 | } | ||
| 808 | |||
| 809 | method sort( $comp_gen = undef ) | ||
| 810 | { | ||
| 811 | # it makes sense to sort both a fragment | ||
| 812 | # and a cursor. | ||
| 813 | # | ||
| 814 | # list acquires this via wrapper. | ||
| 815 | |||
| 816 | $comp_gen | ||
| 817 | //= do | ||
| 818 | { | ||
| 819 | my ( $sample ) = $self->data; | ||
| 820 | |||
| 821 | looks_like_number $sample | ||
| 822 | ? sub{ my ($x) = @_; sub{ $x < $_[0] } } | ||
| 823 | : sub{ my ($x) = @_; sub{ $x lt $_[0] } } | ||
| 824 | }; | ||
| 825 | |||
| 826 | # note that $self may be a cursor/fragment: | ||
| 827 | # we may not be sorting the entire list. | ||
| 828 | # | ||
| 829 | # creating the fragment truncates $self to | ||
| 830 | # the first node; the one node simplifies | ||
| 831 | # the insert logic in not having to deal | ||
| 832 | # with the initially emtpy list. | ||
| 833 | |||
| 834 | $self | ||
| 835 | ->next | ||
| 836 | ->fragment | ||
| 837 | ->consume | ||
| 838 | ( | ||
| 839 | sub | ||
| 840 | { | ||
| 841 | $self | ||
| 842 | ->first( $comp_gen->( @_ ) ) | ||
| 843 | ->insert( @_ ) | ||
| 844 | } | ||
| 845 | ) | ||
| 846 | ->push_onto( $self ) | ||
| 847 | |||
| 848 | # push_onto returns $thence ($self) | ||
| 849 | } | ||
| 850 | |||
| 851 | ############################################################ | ||
| 852 | # list bulk processing. | ||
| 853 | # absent any other action, these leave the cursor untouched. | ||
| 854 | # | ||
| 855 | # each's handler may return false to stop | ||
| 856 | # processing; the others may raise an exception. | ||
| 857 | # | ||
| 858 | # find the first item on the list matching a filter: | ||
| 859 | # | ||
| 860 | # my $curs = $cursor->first( $filter ); | ||
| 861 | # my @data = $cursor->first( $filter )->data; | ||
| 862 | # | ||
| 863 | # find the first item on a list and munge all the remaining | ||
| 864 | # ones: | ||
| 865 | # | ||
| 866 | # $list->cursor->first( $filter )->each( $handler ); | ||
| 867 | # | ||
| 868 | # because each is normally used to manage the list contents | ||
| 869 | # it is passed the cursor object; the others are passed | ||
| 870 | # $cursor->data. | ||
| 871 | # | ||
| 872 | # see also lightweight wrappers in List class. | ||
| 873 | ############################################################ | ||
| 874 | |||
| 875 | method each( $handler ) | ||
| 876 | { | ||
| 877 | # caller gets back the cursor on the last | ||
| 878 | # node processed in case they care. useful | ||
| 879 | # for duplicating the behavior or first() | ||
| 880 | # or making second(), etc. | ||
| 881 | # | ||
| 882 | # reasonable to call in a void context to | ||
| 883 | # simply process the list. | ||
| 884 | |||
| 885 | $self | ||
| 886 | or croak 'Bogus each at sentinel'; | ||
| 887 | |||
| 888 | my $curs = $self->cursor; | ||
| 889 | |||
| 890 | for( ;; ) | ||
| 891 | { | ||
| 892 | # handler returns false to abort processing, | ||
| 893 | # sans exception we end up on the sentinel. | ||
| 894 | # for consistency with the other handlers, | ||
| 895 | # the newline exception is also handled here. | ||
| 896 | |||
| 897 | try | ||
| 898 | { | ||
| 899 | $curs->$handler | ||
| 900 | or last; | ||
| 901 | } | ||
| 902 | catch( $err ) | ||
| 903 | { | ||
| 904 | $err eq "\n" | ||
| 905 | or croak "Failed each: $err"; | ||
| 906 | |||
| 907 | carp 'Aborted each.'; | ||
| 908 | last | ||
| 909 | } | ||
| 910 | |||
| 911 | $curs->advance | ||
| 912 | or last; | ||
| 913 | } | ||
| 914 | |||
| 915 | # false if the list is completed. | ||
| 916 | |||
| 917 | $curs | ||
| 918 | } | ||
| 919 | |||
| 920 | method grep( $filter ) | ||
| 921 | { | ||
| 922 | defined wantarray | ||
| 923 | or croak 'Bogus grep: used in void context'; | ||
| 924 | |||
| 925 | $self | ||
| 926 | or croak 'Bogus grep at sentinel'; | ||
| 927 | |||
| 928 | my @found = (); | ||
| 929 | my @data = (); | ||
| 930 | |||
| 931 | for | ||
| 932 | ( | ||
| 933 | my $curr = $self->cursor | ||
| 934 | ; $curr | ||
| 935 | ; $curr->advance | ||
| 936 | ) | ||
| 937 | { | ||
| 938 | try | ||
| 939 | { | ||
| 940 | @data = $curr->data; | ||
| 941 | |||
| 942 | $filter->( @data ) | ||
| 943 | or next; | ||
| 944 | |||
| 945 | push @found, @data; | ||
| 946 | } | ||
| 947 | catch( $err ) | ||
| 948 | { | ||
| 949 | $err eq "\n" | ||
| 950 | or croak "Failed grep: $err"; | ||
| 951 | |||
| 952 | carp 'Aborted grep.'; | ||
| 953 | last | ||
| 954 | } | ||
| 955 | } | ||
| 956 | |||
| 957 | wantarray | ||
| 958 | ? @found | ||
| 959 | : \@found | ||
| 960 | } | ||
| 961 | |||
| 962 | method map( $handler ) | ||
| 963 | { | ||
| 964 | defined wantarray | ||
| 965 | or croak 'Bogus map: used in void context'; | ||
| 966 | |||
| 967 | $self | ||
| 968 | or croak 'Bogus map at sentinel'; | ||
| 969 | |||
| 970 | my @result = (); | ||
| 971 | |||
| 972 | for | ||
| 973 | ( | ||
| 974 | my $curr = $self->cursor | ||
| 975 | ; $curr | ||
| 976 | ; $curr->advance | ||
| 977 | ) | ||
| 978 | { | ||
| 979 | try | ||
| 980 | { | ||
| 981 | push @result, $handler->( $curr->data ); | ||
| 982 | } | ||
| 983 | catch( $err ) | ||
| 984 | { | ||
| 985 | $err eq "\n" | ||
| 986 | or croak "Failed map: $err"; | ||
| 987 | |||
| 988 | last | ||
| 989 | } | ||
| 990 | } | ||
| 991 | |||
| 992 | wantarray | ||
| 993 | ? @result | ||
| 994 | : \@result | ||
| 995 | } | ||
| 996 | |||
| 997 | method first( $select ) | ||
| 998 | { | ||
| 999 | defined wantarray | ||
| 1000 | or croak 'Bogus first: used in void context'; | ||
| 1001 | |||
| 1002 | $self | ||
| 1003 | or croak 'Bogus first at sentinel'; | ||
| 1004 | |||
| 1005 | for | ||
| 1006 | ( | ||
| 1007 | my $curr = $self->cursor | ||
| 1008 | ; $curr | ||
| 1009 | ; $curr->advance | ||
| 1010 | ) | ||
| 1011 | { | ||
| 1012 | # caller gets back the first cursor that maches | ||
| 1013 | # or undef at the sentinel. | ||
| 1014 | |||
| 1015 | try | ||
| 1016 | { | ||
| 1017 | $select->( $curr->data ) | ||
| 1018 | and return $curr; | ||
| 1019 | } | ||
| 1020 | catch( $err ) | ||
| 1021 | { | ||
| 1022 | $err eq "\n" | ||
| 1023 | or croak "Failed first: $err"; | ||
| 1024 | |||
| 1025 | last | ||
| 1026 | } | ||
| 1027 | } | ||
| 1028 | } | ||
| 1029 | } | ||
| 1030 | |||
| 1031 | role LinkedList::Single::Transient | ||
| 1032 | { | ||
| 1033 | use Carp qw( carp croak ); | ||
| 1034 | use Sub::Name qw( subname ); | ||
| 1035 | |||
| 1036 | our @CARP_NOT = qw( $nmgr_r $list_c $curs_c $frag_c ); | ||
| 1037 | |||
| 1038 | ############################################################ | ||
| 1039 | # these look odd.. until you see the guts of append_lazy | ||
| 1040 | # and insert_lazy: | ||
| 1041 | # | ||
| 1042 | # $self->generate_lazy( $generator )->insert_at( $self ); | ||
| 1043 | # | ||
| 1044 | # returning $self via $thence. all of these leave the | ||
| 1045 | # fragment empty, false, and generally useless. | ||
| 1046 | |||
| 1047 | my $push_onto | ||
| 1048 | = sub | ||
| 1049 | { | ||
| 1050 | my( $src, $dst ) = @_; | ||
| 1051 | |||
| 1052 | $src->transfer_node( $dst->sentinel ); | ||
| 1053 | $dst | ||
| 1054 | }; | ||
| 1055 | |||
| 1056 | method push_onto( $thence ) | ||
| 1057 | { | ||
| 1058 | $thence->DOES( $nmgr_r ) | ||
| 1059 | or croak "Bogus push: '$thence' does not $nmgr_r"; | ||
| 1060 | |||
| 1061 | $self->$push_onto( $thence ) | ||
| 1062 | } | ||
| 1063 | |||
| 1064 | method append_to( $thence ) | ||
| 1065 | { | ||
| 1066 | $thence->DOES( $nmgr_r ) | ||
| 1067 | or croak "Botched append_to: '$thence' does not $nmgr_r"; | ||
| 1068 | |||
| 1069 | $thence | ||
| 1070 | or croak "Bogus append_to at sentinel"; | ||
| 1071 | |||
| 1072 | $thence | ||
| 1073 | ->next | ||
| 1074 | ->$push_onto( $self ) | ||
| 1075 | ->$push_onto( $thence ) | ||
| 1076 | } | ||
| 1077 | |||
| 1078 | method insert_at( $thence ) | ||
| 1079 | { | ||
| 1080 | $thence->DOES( $nmgr_r ) | ||
| 1081 | or croak "Bogus append_to: '$thence' does not $nmgr_r"; | ||
| 1082 | |||
| 1083 | $thence | ||
| 1084 | ? | ||
| 1085 | $thence | ||
| 1086 | ->$push_onto( $self ) | ||
| 1087 | ->$push_onto( $thence ) | ||
| 1088 | : | ||
| 1089 | $self->transfer_node( $thence ) | ||
| 1090 | ; | ||
| 1091 | |||
| 1092 | $thence | ||
| 1093 | } | ||
| 1094 | |||
| 1095 | method consume( $handler ) | ||
| 1096 | { | ||
| 1097 | # consume the fragment, leaving nothing but the | ||
| 1098 | # sentinel when complete. | ||
| 1099 | |||
| 1100 | $self | ||
| 1101 | or croak 'Botched consume: empty fragment'; | ||
| 1102 | |||
| 1103 | while( $self ) | ||
| 1104 | { | ||
| 1105 | try | ||
| 1106 | { | ||
| 1107 | $handler->( $self->data ); | ||
| 1108 | } | ||
| 1109 | catch( $err ) | ||
| 1110 | { | ||
| 1111 | $err eq "\n" | ||
| 1112 | or carp | ||
| 1113 | "Failed consume: $err\n" | ||
| 1114 | , $dump_format->( $self->data ) | ||
| 1115 | ; | ||
| 1116 | |||
| 1117 | last | ||
| 1118 | } | ||
| 1119 | |||
| 1120 | $self->advance | ||
| 1121 | or last; | ||
| 1122 | } | ||
| 1123 | |||
| 1124 | # absent exctptions, this will be false (sentinel). | ||
| 1125 | # caller can check for true $self on return and | ||
| 1126 | # handle the non-empty portion to reclaim the data. | ||
| 1127 | |||
| 1128 | $self | ||
| 1129 | } | ||
| 1130 | } | ||
| 1131 | |||
| 1132 | ######################################################################## | ||
| 1133 | # nothing below this point should unwerap $node. # | ||
| 1134 | ######################################################################## | ||
| 1135 | |||
| 1136 | class LinkedList::Single::List v2.0.0 | ||
| 1137 | :repr( pvobj ) | ||
| 1138 | { | ||
| 1139 | ############################################################ | ||
| 1140 | # once node-twiddling operaitons are pushed into the | ||
| 1141 | # role for sharing with the cursor this is kinda | ||
| 1142 | # boring: the node object is a static entity that | ||
| 1143 | # references the head so that the list is kept alive. | ||
| 1144 | # | ||
| 1145 | # all it really does is house the list and generate | ||
| 1146 | # cursors. | ||
| 1147 | # | ||
| 1148 | # shift & unshift live here since a cursor has no idea | ||
| 1149 | # of where the list's head is; push and pop find the | ||
| 1150 | # tail so they make sense to share. | ||
| 1151 | # | ||
| 1152 | # everything else interesting is in the cursor class | ||
| 1153 | # it knows where on the list to perform the operations. | ||
| 1154 | ############################################################ | ||
| 1155 | |||
| 1156 | use overload | ||
| 1157 | q/bool/ => | ||
| 1158 | sub | ||
| 1159 | { | ||
| 1160 | # list is true if its head node is not the sentinel. | ||
| 1161 | |||
| 1162 | my ( $list ) = @_; | ||
| 1163 | |||
| 1164 | ! $list->is_sentinel | ||
| 1165 | } | ||
| 1166 | ; | ||
| 1167 | |||
| 1168 | use Sub::Name qw( subname ); | ||
| 1169 | use Symbol qw( qualify_to_ref ); | ||
| 1170 | |||
| 1171 | apply LinkedList::Single::NodeMgr; | ||
| 1172 | apply LinkedList::Single::Header; | ||
| 1173 | |||
| 1174 | # trivial wrappers, avoid $list->cursor->... | ||
| 1175 | # for common operations we are likley to | ||
| 1176 | # use with a list. | ||
| 1177 | |||
| 1178 | our @wrappers | ||
| 1179 | = qw | ||
| 1180 | ( | ||
| 1181 | each | ||
| 1182 | map | ||
| 1183 | grep | ||
| 1184 | first | ||
| 1185 | sort | ||
| 1186 | ); | ||
| 1187 | |||
| 1188 | for my $name ( @wrappers ) | ||
| 1189 | { | ||
| 1190 | my $sub = $curs_c->can( $name ) | ||
| 1191 | or die "Bogus $name: $curs_c cannot '$name'"; | ||
| 1192 | |||
| 1193 | *{ qualify_to_ref $name } | ||
| 1194 | = subname $name | ||
| 1195 | => sub | ||
| 1196 | { | ||
| 1197 | my ( $list ) = @_; | ||
| 1198 | |||
| 1199 | splice @_, 0, 1, $list->cursor; | ||
| 1200 | |||
| 1201 | goto &$sub | ||
| 1202 | }; | ||
| 1203 | } | ||
| 1204 | } | ||
| 1205 | |||
| 1206 | class LinkedList::Single::Cursor v2.0.0 | ||
| 1207 | :repr( pvobj ) | ||
| 1208 | { | ||
| 1209 | use overload | ||
| 1210 | q/bool/ => | ||
| 1211 | sub | ||
| 1212 | { | ||
| 1213 | # cursor is true if its head node is not the sentinel. | ||
| 1214 | |||
| 1215 | my ( $curs ) = @_; | ||
| 1216 | |||
| 1217 | ! $curs->is_sentinel | ||
| 1218 | } | ||
| 1219 | ; | ||
| 1220 | |||
| 1221 | apply LinkedList::Single::NodeMgr; | ||
| 1222 | apply LinkedList::Single::Iterator; | ||
| 1223 | } | ||
| 1224 | |||
| 1225 | class LinkedList::Single::Fragment v2.0.0 | ||
| 1226 | { | ||
| 1227 | ############################################################ | ||
| 1228 | # splicing into an existing list requries a | ||
| 1229 | # sequence of nodes that doesn't have a head. | ||
| 1230 | # hence a fragment: a sequence of nodes sans | ||
| 1231 | # the list's un-moveable root node. | ||
| 1232 | # | ||
| 1233 | # note these methods are: | ||
| 1234 | # | ||
| 1235 | # $frag->append_to( $list_or_curs ); | ||
| 1236 | # $frag->insert_at( $list_or_curs ); | ||
| 1237 | # | ||
| 1238 | # this avoids appending one list to another | ||
| 1239 | # and getting multi-headed animal syndrome. | ||
| 1240 | # | ||
| 1241 | # it makes no sense to apply bulk op's to a | ||
| 1242 | # fragment; they live in the cursor class. | ||
| 1243 | ############################################################ | ||
| 1244 | |||
| 1245 | use overload | ||
| 1246 | q/bool/ => | ||
| 1247 | sub | ||
| 1248 | { | ||
| 1249 | # list is true if its head node is not the sentinel. | ||
| 1250 | |||
| 1251 | my ( $list ) = @_; | ||
| 1252 | |||
| 1253 | ! $list->is_sentinel | ||
| 1254 | } | ||
| 1255 | ; | ||
| 1256 | |||
| 1257 | apply LinkedList::Single::NodeMgr; | ||
| 1258 | apply LinkedList::Single::Iterator; | ||
| 1259 | apply LinkedList::Single::Transient; | ||
| 1260 | } | ||
| 1261 | |||
| 1262 | # keep require happy | ||
| 1263 | 1 | ||
| 1264 | __END__ |