| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | |||
| 5 | package LinkedList::Single v2.1.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 looks_like_number ); | ||
| 132 | use Sub::Name qw( subname ); | ||
| 133 | |||
| 134 | our @CARP_NOT = ( __PACKAGE__, qw( Object::Pad ) ); | ||
| 135 | |||
| 136 | ############################################################ | ||
| 137 | # sorting: the nodes are relinked in place, no node is built | ||
| 138 | # and no data is copied. the helpers work on bare nodes and | ||
| 139 | # live here with the other code that unwraps them. | ||
| 140 | ############################################################ | ||
| 141 | |||
| 142 | # merge two runs of nodes, each a ( head, tail, count ) triple, into | ||
| 143 | # one: returns ( head, tail ) of the result. the tail's next is left | ||
| 144 | # for the caller. | ||
| 145 | # | ||
| 146 | # $comp_gen->( @data ) returns a predicate that is true for a node | ||
| 147 | # whose data sorts after @data. the generator is called once for each | ||
| 148 | # node of the right run, which is placed in front of the first node | ||
| 149 | # of the left run that sorts after it. nodes that compare equal keep | ||
| 150 | # their order: the left one goes first. | ||
| 151 | |||
| 152 | my $merge_runs | ||
| 153 | = subname merge_runs | ||
| 154 | => sub( $comp_gen, $x, $xt, $nx, $y, $yt, $ny ) | ||
| 155 | { | ||
| 156 | my $out = []; | ||
| 157 | my $tail = $out; | ||
| 158 | |||
| 159 | while( $ny ) | ||
| 160 | { | ||
| 161 | my $pred = $comp_gen->( $y->@[ 1 .. $y->$#* ] ); | ||
| 162 | |||
| 163 | while( $nx && ! $pred->( $x->@[ 1 .. $x->$#* ] ) ) | ||
| 164 | { | ||
| 165 | my $next = $x->[0]; | ||
| 166 | |||
| 167 | $tail->[0] = $x; | ||
| 168 | $tail = $x; | ||
| 169 | $x = $next; | ||
| 170 | --$nx; | ||
| 171 | } | ||
| 172 | |||
| 173 | # the left run is used up: the rest of the right run | ||
| 174 | # is already in order and goes on whole. | ||
| 175 | |||
| 176 | if( ! $nx ) | ||
| 177 | { | ||
| 178 | $tail->[0] = $y; | ||
| 179 | |||
| 180 | return ( $out->[0], $yt ); | ||
| 181 | } | ||
| 182 | |||
| 183 | my $next = $y->[0]; | ||
| 184 | |||
| 185 | $tail->[0] = $y; | ||
| 186 | $tail = $y; | ||
| 187 | $y = $next; | ||
| 188 | --$ny; | ||
| 189 | } | ||
| 190 | |||
| 191 | $tail->[0] = $x; | ||
| 192 | |||
| 193 | ( $out->[0], $xt ) | ||
| 194 | }; | ||
| 195 | |||
| 196 | # merge sort $count nodes starting at $head, returns ( head, tail ) of | ||
| 197 | # the sorted run. only the nodes in the run are touched; the next of | ||
| 198 | # the tail is left for the caller. | ||
| 199 | |||
| 200 | my $sort_run | ||
| 201 | = subname sort_run | ||
| 202 | => sub( $comp_gen, $head, $count ) | ||
| 203 | { | ||
| 204 | $count > 1 | ||
| 205 | or return ( $head, $head ); | ||
| 206 | |||
| 207 | my $half = int( ( $count + 1 ) / 2 ); | ||
| 208 | my $last = $head; | ||
| 209 | |||
| 210 | $last = $last->[0] for 2 .. $half; | ||
| 211 | |||
| 212 | my $right = $last->[0]; | ||
| 213 | |||
| 214 | my ( $xh, $xt ) = __SUB__->( $comp_gen, $head, $half ); | ||
| 215 | my ( $yh, $yt ) = __SUB__->( $comp_gen, $right, $count - $half ); | ||
| 216 | |||
| 217 | $merge_runs->( $comp_gen, $xh, $xt, $half, $yh, $yt, $count - $half ) | ||
| 218 | }; | ||
| 219 | |||
| 220 | ############################################################ | ||
| 221 | # definition | ||
| 222 | ############################################################ | ||
| 223 | |||
| 224 | field $node; # notice the lack of a default | ||
| 225 | |||
| 226 | method object_type() | ||
| 227 | { | ||
| 228 | # classes can override this to provide more | ||
| 229 | # info (e.g., "List::Circular"). | ||
| 230 | # | ||
| 231 | # return $type to simplify handling in a scalar | ||
| 232 | # context. | ||
| 233 | |||
| 234 | my ( $type ) = $self->META->name =~ m{ (\w+) $}x; | ||
| 235 | |||
| 236 | $type | ||
| 237 | } | ||
| 238 | |||
| 239 | ############################################################ | ||
| 240 | # construct the object | ||
| 241 | ############################################################ | ||
| 242 | |||
| 243 | ADJUST :params ( :$whence = undef ) | ||
| 244 | { | ||
| 245 | my $name = $self->META->name; | ||
| 246 | |||
| 247 | # this could be spread across the classes, simpler to | ||
| 248 | # have it here. note that if-logic needs to proceed | ||
| 249 | # from most-to-least derived. | ||
| 250 | |||
| 251 | if( $self->isa( $list_c ) ) | ||
| 252 | { | ||
| 253 | $whence | ||
| 254 | and croak "Botched $name: extraneous 'whence' argument"; | ||
| 255 | |||
| 256 | $node = $sentinel_node->(); | ||
| 257 | } | ||
| 258 | elsif( $self->isa( $curs_c ) ) | ||
| 259 | { | ||
| 260 | # cursors require and object, not a bare node. | ||
| 261 | # note that this intentionally will create a | ||
| 262 | # cursor to an empty list in order to support | ||
| 263 | # the sentinel and *_lazy methods. | ||
| 264 | |||
| 265 | blessed $whence | ||
| 266 | or croak "Botched $name: '$whence' is not an object"; | ||
| 267 | |||
| 268 | $whence->DOES( $nmgr_r ) | ||
| 269 | or croak "Botched $name: '$whence' does not $nmgr_r"; | ||
| 270 | |||
| 271 | $node = $whence->node; | ||
| 272 | } | ||
| 273 | elsif( $self->isa( $frag_c ) ) | ||
| 274 | { | ||
| 275 | if( $whence ) | ||
| 276 | { | ||
| 277 | state $sent_type = reftype $sentinel_node->(); | ||
| 278 | |||
| 279 | # fragments require a bare node, normally | ||
| 280 | # as a by-product of detach. | ||
| 281 | |||
| 282 | blessed $whence | ||
| 283 | and croak "Botched $name: '$whence' is not a bare node."; | ||
| 284 | |||
| 285 | reftype $whence eq $sent_type | ||
| 286 | or croak "Botched $name: '$whence' is not '$sent_type'"; | ||
| 287 | |||
| 288 | $node = [ splice $whence->@* ]; | ||
| 289 | } | ||
| 290 | else | ||
| 291 | { | ||
| 292 | # empty fragment is reasonable, | ||
| 293 | # e.g., generate_lazy(). | ||
| 294 | |||
| 295 | $node = $sentinel_node->(); | ||
| 296 | } | ||
| 297 | } | ||
| 298 | else | ||
| 299 | { | ||
| 300 | croak "Unknown class: none of $list_c, $curs_c, $frag_c"; | ||
| 301 | } | ||
| 302 | } | ||
| 303 | |||
| 304 | method cursor() | ||
| 305 | { | ||
| 306 | $curs_c->new( whence => $self ) | ||
| 307 | } | ||
| 308 | |||
| 309 | method fragment() | ||
| 310 | { | ||
| 311 | $frag_c->new( whence => $self->node ) | ||
| 312 | } | ||
| 313 | |||
| 314 | method list() | ||
| 315 | { | ||
| 316 | $self | ||
| 317 | ->fragment | ||
| 318 | ->push_onto | ||
| 319 | ( | ||
| 320 | $list_c->new | ||
| 321 | ) | ||
| 322 | } | ||
| 323 | |||
| 324 | # sort the nodes from $node through the sentinel in place, with | ||
| 325 | # $comp_gen if there is one and by the first datum otherwise. | ||
| 326 | # | ||
| 327 | # the sentinel is the same node afterwards, since cursors that are | ||
| 328 | # sitting on it have to stay on the end of the list. the node itself | ||
| 329 | # is also the first node afterwards, since a list or a cursor holds it: | ||
| 330 | # its contents are copied into a new node that is sorted with the | ||
| 331 | # others, and at the end the node and the first node of the sorted | ||
| 332 | # chain exchange places. | ||
| 333 | # | ||
| 334 | # nothing is lost if the comparator dies: the chain is put back in its | ||
| 335 | # original order before the exception is passed on. | ||
| 336 | |||
| 337 | method sort_nodes( $comp_gen = undef ) | ||
| 338 | { | ||
| 339 | my @chain = ( $node ); | ||
| 340 | |||
| 341 | while( $chain[-1]->[0]->@* ) | ||
| 342 | { | ||
| 343 | push @chain, $chain[-1]->[0]; | ||
| 344 | } | ||
| 345 | |||
| 346 | # nothing to sort with a single node. | ||
| 347 | |||
| 348 | @chain > 1 | ||
| 349 | or return $self; | ||
| 350 | |||
| 351 | my $sentinel = $chain[-1]->[0]; | ||
| 352 | |||
| 353 | $chain[0] = [ $node->@* ]; | ||
| 354 | |||
| 355 | my $relink | ||
| 356 | = sub( @link ) | ||
| 357 | { | ||
| 358 | $link[ $_ - 1 ]->[0] = $link[ $_ ] for 1 .. $#link; | ||
| 359 | $link[-1]->[0] = $sentinel; | ||
| 360 | }; | ||
| 361 | |||
| 362 | my ( $head, $tail ); | ||
| 363 | |||
| 364 | try | ||
| 365 | { | ||
| 366 | if( $comp_gen ) | ||
| 367 | { | ||
| 368 | ( $head, $tail ) | ||
| 369 | = $sort_run->( $comp_gen, $chain[0], scalar @chain ); | ||
| 370 | } | ||
| 371 | else | ||
| 372 | { | ||
| 373 | # no comparator: perl's sort, which is stable, on the | ||
| 374 | # first datum, numeric if the first node looks it. | ||
| 375 | |||
| 376 | my @sorted | ||
| 377 | = looks_like_number $chain[0]->[1] | ||
| 378 | ? sort { $a->[1] <=> $b->[1] } @chain | ||
| 379 | : sort { $a->[1] cmp $b->[1] } @chain | ||
| 380 | ; | ||
| 381 | |||
| 382 | $relink->( @sorted ); | ||
| 383 | |||
| 384 | ( $head, $tail ) = @sorted[ 0, -1 ]; | ||
| 385 | } | ||
| 386 | |||
| 387 | $tail->[0] = $sentinel; | ||
| 388 | } | ||
| 389 | catch( $err ) | ||
| 390 | { | ||
| 391 | $relink->( @chain ); | ||
| 392 | |||
| 393 | $node->@* = $chain[0]->@*; | ||
| 394 | |||
| 395 | die $err; | ||
| 396 | }; | ||
| 397 | |||
| 398 | # the first node of the sorted chain, $head, is $node's new | ||
| 399 | # contents. if it is the copy, $node is done. if not, $head is | ||
| 400 | # an original node that nothing links to now: it takes the | ||
| 401 | # place of the copy, which goes, so every original node is | ||
| 402 | # still in the list. only these two nodes change their data. | ||
| 403 | |||
| 404 | $node->@* = $head->@*; | ||
| 405 | |||
| 406 | if( $head != $chain[0] ) | ||
| 407 | { | ||
| 408 | my $before = $node; | ||
| 409 | |||
| 410 | $before = $before->[0] | ||
| 411 | while $before->[0] != $chain[0]; | ||
| 412 | |||
| 413 | $before->[0] = $head; | ||
| 414 | $head->@* = $chain[0]->@*; | ||
| 415 | } | ||
| 416 | |||
| 417 | $self | ||
| 418 | } | ||
| 419 | |||
| 420 | # remove the $count nodes after this one by relinking, returning | ||
| 421 | # them as a fragment. fewer than $count are taken if that is all | ||
| 422 | # there is; none leaves an empty fragment. | ||
| 423 | # | ||
| 424 | # fragment takes the whole tail after this node (the first node's | ||
| 425 | # contents move into the fragment's head node, which leaves the | ||
| 426 | # old first node as an empty orphan), then the run is cut at | ||
| 427 | # $count nodes and the remainder is linked back after this node. | ||
| 428 | # all of the other nodes keep their identity, as do the cursors | ||
| 429 | # on them, and so does the list's sentinel. | ||
| 430 | |||
| 431 | method detach_nodes( $count = 1 ) | ||
| 432 | { | ||
| 433 | $count > 0 | ||
| 434 | or croak "Botched detach: count '$count' is not positive"; | ||
| 435 | |||
| 436 | $node->@* | ||
| 437 | or croak 'Bogus detach at sentinel'; | ||
| 438 | |||
| 439 | my $frag = $self->next->fragment; | ||
| 440 | |||
| 441 | $frag | ||
| 442 | or return $frag; | ||
| 443 | |||
| 444 | my $last = $frag->node; | ||
| 445 | |||
| 446 | for( 2 .. $count ) | ||
| 447 | { | ||
| 448 | $last->[0]->@* | ||
| 449 | or last; | ||
| 450 | |||
| 451 | $last = $last->[0]; | ||
| 452 | } | ||
| 453 | |||
| 454 | my $after = $last->[0]; | ||
| 455 | |||
| 456 | $last->[0] = $sentinel_node->(); | ||
| 457 | $node->[0] = $after; | ||
| 458 | |||
| 459 | $frag | ||
| 460 | } | ||
| 461 | |||
| 462 | # splice the nodes of the fragment $frag in after this one by | ||
| 463 | # relinking: no node is built and none changes its data. the | ||
| 464 | # fragment is left empty. this node must not be the sentinel. | ||
| 465 | |||
| 466 | method attach_nodes( $frag ) | ||
| 467 | { | ||
| 468 | $node->@* | ||
| 469 | or croak 'Bogus attach at sentinel'; | ||
| 470 | |||
| 471 | $frag | ||
| 472 | or return $self; | ||
| 473 | |||
| 474 | my $tail = $frag->tail_node; | ||
| 475 | |||
| 476 | $tail->[0] = $node->[0]; | ||
| 477 | $node->[0] = $frag->node; | ||
| 478 | $frag->node = $sentinel_node->(); | ||
| 479 | |||
| 480 | $self | ||
| 481 | } | ||
| 482 | |||
| 483 | ############################################################ | ||
| 484 | # introspection | ||
| 485 | ############################################################ | ||
| 486 | # calling them 'car/cdr' seemed a bit obscure; 'next' and | ||
| 487 | # 'data' seemed more approachable. | ||
| 488 | # | ||
| 489 | # these are data leaks to some extent; workable so long as | ||
| 490 | # caller's don't inspect the contents of what's returned | ||
| 491 | # for the node. | ||
| 492 | |||
| 493 | method node :lvalue | ||
| 494 | { | ||
| 495 | # ideally "my method node", but that doesn't work | ||
| 496 | # in O::P yet. sans the private method is is where | ||
| 497 | # the internal data structure leaks out. | ||
| 498 | # | ||
| 499 | # use to transfer node value between objects: | ||
| 500 | # $node = $whence->node. | ||
| 501 | |||
| 502 | $node | ||
| 503 | } | ||
| 504 | |||
| 505 | method next_node :lvalue | ||
| 506 | { | ||
| 507 | $node->[0] | ||
| 508 | }; | ||
| 509 | |||
| 510 | method tail_node() | ||
| 511 | { | ||
| 512 | $node->@* | ||
| 513 | or croak 'Bogus tail_node at sentinel'; | ||
| 514 | |||
| 515 | my $tail = $node; | ||
| 516 | |||
| 517 | for | ||
| 518 | ( | ||
| 519 | my $next = $tail->[0] | ||
| 520 | ; $next->@* | ||
| 521 | ; $next = $next->[0] | ||
| 522 | ) | ||
| 523 | { | ||
| 524 | $tail = $next; | ||
| 525 | } | ||
| 526 | |||
| 527 | |||
| 528 | $tail | ||
| 529 | } | ||
| 530 | |||
| 531 | method is_sentinel() | ||
| 532 | { | ||
| 533 | ! $node->@* | ||
| 534 | } | ||
| 535 | |||
| 536 | method count() | ||
| 537 | { | ||
| 538 | # sentinel short-curcuits the ++$i, leaves | ||
| 539 | # the count at zero. | ||
| 540 | |||
| 541 | my $i = 0; | ||
| 542 | my $curr = $node; | ||
| 543 | |||
| 544 | ++$i while ( $curr ) = $curr->@*; | ||
| 545 | |||
| 546 | $i | ||
| 547 | } | ||
| 548 | |||
| 549 | ############################################################ | ||
| 550 | # node data | ||
| 551 | |||
| 552 | method data() | ||
| 553 | { | ||
| 554 | $node->@* | ||
| 555 | or croak 'Bogus data: at sentinel'; | ||
| 556 | |||
| 557 | my $wa = wantarray | ||
| 558 | // croak 'Botched data: used in void context'; | ||
| 559 | |||
| 560 | $wa | ||
| 561 | ? $node->@[ 1 .. $#$node ] | ||
| 562 | : [ $node->@[ 1 .. $#$node ] ] | ||
| 563 | } | ||
| 564 | |||
| 565 | method next_data() | ||
| 566 | { | ||
| 567 | my $next = $node->[0] | ||
| 568 | or croak 'Bogus next_data at sentinel'; | ||
| 569 | |||
| 570 | my $wa = wantarray | ||
| 571 | // croak 'Botched next_data: used in void context'; | ||
| 572 | |||
| 573 | $wa | ||
| 574 | ? $next->@[ 1 .. $#$next ] | ||
| 575 | : [ $next->@[ 1 .. $#$next ] ] | ||
| 576 | } | ||
| 577 | |||
| 578 | method set_data | ||
| 579 | { | ||
| 580 | # this can set the sentinal node, | ||
| 581 | # effectivly making it a push. | ||
| 582 | |||
| 583 | $node->@* = ( $node->[0] || $sentinel_node->(), @_ ); | ||
| 584 | |||
| 585 | $self | ||
| 586 | } | ||
| 587 | |||
| 588 | method push_data | ||
| 589 | { | ||
| 590 | push $node->@*, @_; | ||
| 591 | |||
| 592 | $self | ||
| 593 | } | ||
| 594 | |||
| 595 | ############################################################ | ||
| 596 | # obvious locations along the list. | ||
| 597 | # head lives with the list as that is the only place | ||
| 598 | # we are guaranteed to find it. | ||
| 599 | |||
| 600 | method next( $count = 1 ) | ||
| 601 | { | ||
| 602 | my $curs = $self->cursor; | ||
| 603 | |||
| 604 | $count | ||
| 605 | and do | ||
| 606 | { | ||
| 607 | $count > 0 | ||
| 608 | or croak "Botched next: negative count '$count'"; | ||
| 609 | |||
| 610 | my $next = $node; | ||
| 611 | my $a = ''; | ||
| 612 | |||
| 613 | for( 1 .. $count ) | ||
| 614 | { | ||
| 615 | $a = $next->[0] or last; | ||
| 616 | $next = $a; | ||
| 617 | } | ||
| 618 | |||
| 619 | $curs->node = $next; | ||
| 620 | }; | ||
| 621 | |||
| 622 | $curs | ||
| 623 | } | ||
| 624 | |||
| 625 | method tail() | ||
| 626 | { | ||
| 627 | my $curs = $self->cursor; | ||
| 628 | $curs->node = $curs->tail_node; | ||
| 629 | |||
| 630 | $curs | ||
| 631 | } | ||
| 632 | |||
| 633 | method sentinel() | ||
| 634 | { | ||
| 635 | # this may not advance at all for an empty list or | ||
| 636 | # cursor at the sentinel already. | ||
| 637 | # | ||
| 638 | # bit less work than tail since it dosn't have to | ||
| 639 | # check the following node, only the current one. | ||
| 640 | |||
| 641 | my $curs = $self->cursor; | ||
| 642 | $curs->node | ||
| 643 | = do | ||
| 644 | { | ||
| 645 | my $sent = $node; | ||
| 646 | $sent = $sent->[0] while $sent->@*; | ||
| 647 | $sent | ||
| 648 | }; | ||
| 649 | |||
| 650 | $curs | ||
| 651 | } | ||
| 652 | |||
| 653 | ############################################################ | ||
| 654 | # modify the sequnce of nodes | ||
| 655 | # | ||
| 656 | # insert has a bit more overhead due to copying the values, | ||
| 657 | # useully not enough differnce to notice. | ||
| 658 | # | ||
| 659 | # push/pop make sense on a cursor and live here; shift and | ||
| 660 | # unshift only make sense on a list and live there. | ||
| 661 | |||
| 662 | method push | ||
| 663 | { | ||
| 664 | if( $node->@* ) | ||
| 665 | { | ||
| 666 | $self->tail->append( @_ ) | ||
| 667 | } | ||
| 668 | else | ||
| 669 | { | ||
| 670 | # push at the sentinel still works, just | ||
| 671 | # moves the sentinel out one node.. | ||
| 672 | |||
| 673 | $node->@* = ( $sentinel_node->(), @_ ); | ||
| 674 | } | ||
| 675 | |||
| 676 | $self | ||
| 677 | } | ||
| 678 | |||
| 679 | method pop() | ||
| 680 | { | ||
| 681 | $self | ||
| 682 | or croak "Bogus pop at sentinel"; | ||
| 683 | |||
| 684 | $self->tail->delete | ||
| 685 | } | ||
| 686 | |||
| 687 | method insert | ||
| 688 | { | ||
| 689 | # insert a new node prior to this one. | ||
| 690 | # the [] allocate new storage with the | ||
| 691 | # value of $ref as the next node and | ||
| 692 | # the new data passed in. | ||
| 693 | # | ||
| 694 | # this is more expensive than append due to | ||
| 695 | # copying $node->@*. | ||
| 696 | # generally, prefer append; but this is | ||
| 697 | # allowable at the sentinel and makes sense | ||
| 698 | # for a list. | ||
| 699 | |||
| 700 | $node->@* = ( [ $node->@* ], @_ ); | ||
| 701 | $self | ||
| 702 | } | ||
| 703 | |||
| 704 | method reverse | ||
| 705 | { | ||
| 706 | $node->@* | ||
| 707 | or return $self; | ||
| 708 | |||
| 709 | # effectively $self->unshift( $tmp->shift ) | ||
| 710 | # without all of the method call overhead. | ||
| 711 | # | ||
| 712 | # $next buffers $tmp->next while $head gets | ||
| 713 | # moved to the new list. | ||
| 714 | |||
| 715 | my $head = $self->fragment->node; | ||
| 716 | my $next = ''; | ||
| 717 | |||
| 718 | while( $head->@* ) | ||
| 719 | { | ||
| 720 | $next = splice $head->@*, 0, 1, $node; | ||
| 721 | $node = $head; | ||
| 722 | $head = $next | ||
| 723 | } | ||
| 724 | |||
| 725 | $self | ||
| 726 | } | ||
| 727 | |||
| 728 | method generate_lazy( $generator ) | ||
| 729 | { | ||
| 730 | # ignore $self this just generates a fragment. | ||
| 731 | |||
| 732 | my $frag = $frag_c->new; | ||
| 733 | |||
| 734 | try | ||
| 735 | { | ||
| 736 | # push whatever the generator hands us, which | ||
| 737 | # may be empty, until it does a 'die "\n"', | ||
| 738 | # interruping the assignment, leaving $node as | ||
| 739 | # the sentinel. | ||
| 740 | |||
| 741 | for( my $node = $frag->node ;; ) | ||
| 742 | { | ||
| 743 | ( $node ) = | ||
| 744 | ( | ||
| 745 | $node->@* = ( $sentinel_node->(), $generator->() ) | ||
| 746 | ) | ||
| 747 | } | ||
| 748 | } | ||
| 749 | catch( $err ) | ||
| 750 | { | ||
| 751 | # anything other than a newline is a 'real' | ||
| 752 | # exception and has to be passed on. | ||
| 753 | |||
| 754 | $err eq "\n" | ||
| 755 | or croak "Failed append_lazy: $err"; | ||
| 756 | } | ||
| 757 | |||
| 758 | $frag | ||
| 759 | } | ||
| 760 | |||
| 761 | method append_lazy( $generator ) | ||
| 762 | { | ||
| 763 | # can't append_to an empty list/sentinel: if $self | ||
| 764 | # is false then use insert. | ||
| 765 | |||
| 766 | $self | ||
| 767 | ? $self->generate_lazy( $generator )->append_to( $self ) | ||
| 768 | : $self->generate_lazy( $generator )->insert_at( $self ) | ||
| 769 | } | ||
| 770 | |||
| 771 | method insert_lazy( $generator ) | ||
| 772 | { | ||
| 773 | # unshift_lazy == $list->head->insert_lazy | ||
| 774 | |||
| 775 | $self->generate_lazy( $generator )->insert_at( $self ) | ||
| 776 | } | ||
| 777 | |||
| 778 | method push_lazy( $generator ) | ||
| 779 | { | ||
| 780 | $self->sentinel->insert_lazy( $generator ) | ||
| 781 | } | ||
| 782 | |||
| 783 | method push_list( @valz ) | ||
| 784 | { | ||
| 785 | $self->push_lazy | ||
| 786 | ( | ||
| 787 | sub | ||
| 788 | { | ||
| 789 | @valz or die "\n"; | ||
| 790 | shift @valz | ||
| 791 | } | ||
| 792 | ) | ||
| 793 | } | ||
| 794 | |||
| 795 | method transfer_node( $thence ) | ||
| 796 | { | ||
| 797 | $thence->node->@* = splice $node->@*; | ||
| 798 | $self | ||
| 799 | } | ||
| 800 | |||
| 801 | ############################################################ | ||
| 802 | # | ||
| 803 | # delete == 'delete key' == remove $node node. | ||
| 804 | # drop == backspace == remove next node. | ||
| 805 | # | ||
| 806 | # $list->head->delete == shift; | ||
| 807 | # $list->tail->drop == pop; | ||
| 808 | # | ||
| 809 | # the head is extraneous since lists cannot move, but you | ||
| 810 | # get the point. | ||
| 811 | # | ||
| 812 | ############################################################ | ||
| 813 | |||
| 814 | method delete() | ||
| 815 | { | ||
| 816 | # delete the current node, replacing it with the | ||
| 817 | # contents of the next node. | ||
| 818 | # | ||
| 819 | # Note: this may leave the current node as the | ||
| 820 | # sentinel. | ||
| 821 | # | ||
| 822 | # Note: drop is more efficient since it doesn't | ||
| 823 | # copy both node contents, only the next ref. | ||
| 824 | # | ||
| 825 | # Note: delete on a list == shift; the difference | ||
| 826 | # being that "shift" is always at the head. | ||
| 827 | |||
| 828 | my $next = $node->[0] | ||
| 829 | or croak 'Bogus delete at sentinel'; | ||
| 830 | |||
| 831 | if( defined wantarray ) | ||
| 832 | { | ||
| 833 | # 1 .. $#$node == skip the next ref, only data. | ||
| 834 | ( | ||
| 835 | splice $node->@*, 0, 1+$#$node, $next->@* | ||
| 836 | )[1..$#$node] | ||
| 837 | } | ||
| 838 | else | ||
| 839 | { | ||
| 840 | $node->@* = $next->@* | ||
| 841 | } | ||
| 842 | } | ||
| 843 | |||
| 844 | # sanity check: is the 1..$self->count any worse than | ||
| 845 | # using push (in map)? if not then all we need is map | ||
| 846 | # with a default of "sub{ @_ }". benchmark this with | ||
| 847 | # some really large lists before leaving it in. | ||
| 848 | |||
| 849 | # bulk extract the list contents into a single flat | ||
| 850 | # list or list of arrayrefs. | ||
| 851 | |||
| 852 | method flat_list() | ||
| 853 | { | ||
| 854 | defined( my $wa = wantarray ) | ||
| 855 | or croak 'Bogus extract: used in void context'; | ||
| 856 | |||
| 857 | $self | ||
| 858 | or return; | ||
| 859 | |||
| 860 | my @data = (); | ||
| 861 | my $node = $self->node; | ||
| 862 | my @found | ||
| 863 | = map | ||
| 864 | { | ||
| 865 | ( $node, @data ) = $node->@*; | ||
| 866 | @data | ||
| 867 | } | ||
| 868 | ( 1 .. $self->count ); | ||
| 869 | |||
| 870 | return | ||
| 871 | $wa | ||
| 872 | ? @found | ||
| 873 | : \@found | ||
| 874 | } | ||
| 875 | |||
| 876 | method extract() | ||
| 877 | { | ||
| 878 | # bulk extract the list contents. | ||
| 879 | # cursors allow extracting only a portion. | ||
| 880 | |||
| 881 | defined( my $wa = wantarray ) | ||
| 882 | or croak 'Bogus extract: used in void context'; | ||
| 883 | |||
| 884 | # the empty list is sufficient to notice that | ||
| 885 | # nothing was extracted. | ||
| 886 | |||
| 887 | $node->@* | ||
| 888 | or return; | ||
| 889 | |||
| 890 | my $curr = $self->node; | ||
| 891 | my @found | ||
| 892 | = map | ||
| 893 | { | ||
| 894 | ( $curr, my @data ) = $curr->@*; | ||
| 895 | \@data | ||
| 896 | } | ||
| 897 | ( 1 .. $self->count ); | ||
| 898 | |||
| 899 | return | ||
| 900 | $wa | ||
| 901 | ? @found | ||
| 902 | : \@found | ||
| 903 | } | ||
| 904 | |||
| 905 | method push_args( @argz ) | ||
| 906 | { | ||
| 907 | $self | ||
| 908 | ->generate_lazy | ||
| 909 | ( | ||
| 910 | sub | ||
| 911 | { | ||
| 912 | @argz | ||
| 913 | ? shift @argz | ||
| 914 | : die "\n" | ||
| 915 | } | ||
| 916 | ) | ||
| 917 | ->push_onto( $self ) | ||
| 918 | } | ||
| 919 | } | ||
| 920 | |||
| 921 | role LinkedList::Single::Header | ||
| 922 | { | ||
| 923 | use Carp qw( croak ); | ||
| 924 | use Scalar::Util qw( blessed ); | ||
| 925 | use Symbol qw( qualify_to_ref ); | ||
| 926 | |||
| 927 | ############################################################ | ||
| 928 | # list ops | ||
| 929 | # | ||
| 930 | # yes, cursor and head are identical: the only node | ||
| 931 | # available is the head but having a consistent name | ||
| 932 | # for it seems worthwhile and provides a saner error | ||
| 933 | # message. | ||
| 934 | # | ||
| 935 | # extensions to this could add a ( class => ... ) | ||
| 936 | # argument; for the moment this simply uses the | ||
| 937 | # hardwired class. | ||
| 938 | |||
| 939 | method head() | ||
| 940 | { | ||
| 941 | # head of an empty list is meaningless: there is | ||
| 942 | # no data node to return. | ||
| 943 | |||
| 944 | $self | ||
| 945 | or croak 'Bogus head: empty list'; | ||
| 946 | |||
| 947 | $curs_c->new( whence => $self ) | ||
| 948 | } | ||
| 949 | |||
| 950 | method shift() | ||
| 951 | { | ||
| 952 | $self | ||
| 953 | or croak 'Bogus shift: empty list'; | ||
| 954 | |||
| 955 | $self->delete | ||
| 956 | } | ||
| 957 | |||
| 958 | method unshift | ||
| 959 | { | ||
| 960 | $self->insert( @_ ) | ||
| 961 | } | ||
| 962 | |||
| 963 | method unshift_lazy( $generator ) | ||
| 964 | { | ||
| 965 | $self->cursor->insert_lazy( $generator ) | ||
| 966 | } | ||
| 967 | |||
| 968 | method unshift_list( @node_valz ) | ||
| 969 | { | ||
| 970 | @node_valz | ||
| 971 | or return $self; | ||
| 972 | |||
| 973 | $self | ||
| 974 | ->generate_lazy | ||
| 975 | ( | ||
| 976 | sub | ||
| 977 | { | ||
| 978 | @node_valz | ||
| 979 | or die "\n"; | ||
| 980 | |||
| 981 | shift @node_valz | ||
| 982 | } | ||
| 983 | ) | ||
| 984 | ->insert_at | ||
| 985 | ( | ||
| 986 | $self | ||
| 987 | ) | ||
| 988 | } | ||
| 989 | } | ||
| 990 | |||
| 991 | role LinkedList::Single::Iterator; | ||
| 992 | { | ||
| 993 | ############################################################ | ||
| 994 | # housekeeping | ||
| 995 | ############################################################ | ||
| 996 | |||
| 997 | use Carp qw( croak carp ); | ||
| 998 | |||
| 999 | our @CARP_NOT = ( __PACKAGE__, qw( Object::Pad ) ); | ||
| 1000 | |||
| 1001 | ############################################################ | ||
| 1002 | # methods | ||
| 1003 | ############################################################ | ||
| 1004 | |||
| 1005 | method advance() | ||
| 1006 | { | ||
| 1007 | # the assignment may leave $self on the sentinal (i.e., | ||
| 1008 | # false), which is intentional if we want to | ||
| 1009 | # indicate that the list has been consumed by a cursor. | ||
| 1010 | # | ||
| 1011 | # e.g., | ||
| 1012 | # | ||
| 1013 | # while( $cursor ) { ... ; $cursor->advance } | ||
| 1014 | # | ||
| 1015 | # for( my $curs = $list->cursor ; $curs ; $curs->advance ) | ||
| 1016 | # { ... } | ||
| 1017 | # | ||
| 1018 | # my $curs = $list->head; | ||
| 1019 | # for(;;) { ... ; $curs->advance->is_sentinel and last } | ||
| 1020 | # | ||
| 1021 | # this dodges a trivial exception and allows the | ||
| 1022 | # test for $self to gracefully stop the processing | ||
| 1023 | |||
| 1024 | my $next = $self->next_node | ||
| 1025 | or croak 'Bogus advance at sentinel'; | ||
| 1026 | |||
| 1027 | # if we are alive at this point then there is a next node. | ||
| 1028 | |||
| 1029 | $self->node = $next; | ||
| 1030 | $self | ||
| 1031 | } | ||
| 1032 | |||
| 1033 | ############################################################ | ||
| 1034 | # manage list contents. | ||
| 1035 | # append and drop make no sense for a list so they live | ||
| 1036 | # down here with the cursor (e.g., push == $list->tail->append). | ||
| 1037 | |||
| 1038 | method append | ||
| 1039 | { | ||
| 1040 | # effectivly push. | ||
| 1041 | |||
| 1042 | $self->is_sentinel | ||
| 1043 | ? $self->set_data( $sentinel_node->(), @_ ) | ||
| 1044 | : $self->node->$append( @_ ) | ||
| 1045 | ; | ||
| 1046 | |||
| 1047 | $self | ||
| 1048 | } | ||
| 1049 | |||
| 1050 | method drop() | ||
| 1051 | { | ||
| 1052 | # remove the node following this one, returning | ||
| 1053 | # the data if requested. | ||
| 1054 | |||
| 1055 | $self->node->$drop_node | ||
| 1056 | } | ||
| 1057 | |||
| 1058 | method detach( $count = 1 ) | ||
| 1059 | { | ||
| 1060 | # remove the $count nodes following this one and return | ||
| 1061 | # them as a Fragment; the work is in NodeMgr. | ||
| 1062 | |||
| 1063 | defined wantarray | ||
| 1064 | or croak 'Bogus detach: used in void context'; | ||
| 1065 | |||
| 1066 | $self->detach_nodes( $count ) | ||
| 1067 | } | ||
| 1068 | |||
| 1069 | method sort( $comp_gen = undef ) | ||
| 1070 | { | ||
| 1071 | # it makes sense to sort both a fragment | ||
| 1072 | # and a cursor. | ||
| 1073 | # | ||
| 1074 | # list acquires this via wrapper. | ||
| 1075 | # | ||
| 1076 | # note that $self may be a cursor/fragment: | ||
| 1077 | # we may not be sorting the entire list. | ||
| 1078 | # | ||
| 1079 | # $comp_gen->( @data ) returns a predicate that is | ||
| 1080 | # true for data that sorts after @data; with none | ||
| 1081 | # the first datum of the first node is used to pick | ||
| 1082 | # a numeric or string comparison. equal items stay | ||
| 1083 | # in their original order. | ||
| 1084 | # | ||
| 1085 | # the nodes are relinked in place by NodeMgr::sort_nodes. | ||
| 1086 | |||
| 1087 | $self | ||
| 1088 | or croak 'Bogus sort at sentinel'; | ||
| 1089 | |||
| 1090 | $self->sort_nodes( $comp_gen ) | ||
| 1091 | } | ||
| 1092 | |||
| 1093 | ############################################################ | ||
| 1094 | # list bulk processing. | ||
| 1095 | # absent any other action, these leave the cursor untouched. | ||
| 1096 | # | ||
| 1097 | # each's handler may return false to stop | ||
| 1098 | # processing; the others may raise an exception. | ||
| 1099 | # | ||
| 1100 | # find the first item on the list matching a filter: | ||
| 1101 | # | ||
| 1102 | # my $curs = $cursor->first( $filter ); | ||
| 1103 | # my @data = $cursor->first( $filter )->data; | ||
| 1104 | # | ||
| 1105 | # find the first item on a list and munge all the remaining | ||
| 1106 | # ones: | ||
| 1107 | # | ||
| 1108 | # $list->cursor->first( $filter )->each( $handler ); | ||
| 1109 | # | ||
| 1110 | # because each is normally used to manage the list contents | ||
| 1111 | # it is passed the cursor object; the others are passed | ||
| 1112 | # $cursor->data. | ||
| 1113 | # | ||
| 1114 | # see also lightweight wrappers in List class. | ||
| 1115 | ############################################################ | ||
| 1116 | |||
| 1117 | method each( $handler ) | ||
| 1118 | { | ||
| 1119 | # caller gets back the cursor on the last | ||
| 1120 | # node processed in case they care. useful | ||
| 1121 | # for duplicating the behavior or first() | ||
| 1122 | # or making second(), etc. | ||
| 1123 | # | ||
| 1124 | # reasonable to call in a void context to | ||
| 1125 | # simply process the list. | ||
| 1126 | |||
| 1127 | $self | ||
| 1128 | or croak 'Bogus each at sentinel'; | ||
| 1129 | |||
| 1130 | my $curs = $self->cursor; | ||
| 1131 | |||
| 1132 | for( ;; ) | ||
| 1133 | { | ||
| 1134 | # handler returns false to abort processing, | ||
| 1135 | # sans exception we end up on the sentinel. | ||
| 1136 | # for consistency with the other handlers, | ||
| 1137 | # the newline exception is also handled here. | ||
| 1138 | |||
| 1139 | try | ||
| 1140 | { | ||
| 1141 | $curs->$handler | ||
| 1142 | or last; | ||
| 1143 | } | ||
| 1144 | catch( $err ) | ||
| 1145 | { | ||
| 1146 | $err eq "\n" | ||
| 1147 | or croak "Failed each: $err"; | ||
| 1148 | |||
| 1149 | carp 'Aborted each.'; | ||
| 1150 | last | ||
| 1151 | } | ||
| 1152 | |||
| 1153 | $curs->advance | ||
| 1154 | or last; | ||
| 1155 | } | ||
| 1156 | |||
| 1157 | # false if the list is completed. | ||
| 1158 | |||
| 1159 | $curs | ||
| 1160 | } | ||
| 1161 | |||
| 1162 | method grep( $filter ) | ||
| 1163 | { | ||
| 1164 | defined wantarray | ||
| 1165 | or croak 'Bogus grep: used in void context'; | ||
| 1166 | |||
| 1167 | $self | ||
| 1168 | or croak 'Bogus grep at sentinel'; | ||
| 1169 | |||
| 1170 | my @found = (); | ||
| 1171 | my @data = (); | ||
| 1172 | |||
| 1173 | for | ||
| 1174 | ( | ||
| 1175 | my $curr = $self->cursor | ||
| 1176 | ; $curr | ||
| 1177 | ; $curr->advance | ||
| 1178 | ) | ||
| 1179 | { | ||
| 1180 | try | ||
| 1181 | { | ||
| 1182 | @data = $curr->data; | ||
| 1183 | |||
| 1184 | $filter->( @data ) | ||
| 1185 | or next; | ||
| 1186 | |||
| 1187 | push @found, @data; | ||
| 1188 | } | ||
| 1189 | catch( $err ) | ||
| 1190 | { | ||
| 1191 | $err eq "\n" | ||
| 1192 | or croak "Failed grep: $err"; | ||
| 1193 | |||
| 1194 | carp 'Aborted grep.'; | ||
| 1195 | last | ||
| 1196 | } | ||
| 1197 | } | ||
| 1198 | |||
| 1199 | wantarray | ||
| 1200 | ? @found | ||
| 1201 | : \@found | ||
| 1202 | } | ||
| 1203 | |||
| 1204 | method map( $handler ) | ||
| 1205 | { | ||
| 1206 | defined wantarray | ||
| 1207 | or croak 'Bogus map: used in void context'; | ||
| 1208 | |||
| 1209 | $self | ||
| 1210 | or croak 'Bogus map at sentinel'; | ||
| 1211 | |||
| 1212 | my @result = (); | ||
| 1213 | |||
| 1214 | for | ||
| 1215 | ( | ||
| 1216 | my $curr = $self->cursor | ||
| 1217 | ; $curr | ||
| 1218 | ; $curr->advance | ||
| 1219 | ) | ||
| 1220 | { | ||
| 1221 | try | ||
| 1222 | { | ||
| 1223 | push @result, $handler->( $curr->data ); | ||
| 1224 | } | ||
| 1225 | catch( $err ) | ||
| 1226 | { | ||
| 1227 | $err eq "\n" | ||
| 1228 | or croak "Failed map: $err"; | ||
| 1229 | |||
| 1230 | last | ||
| 1231 | } | ||
| 1232 | } | ||
| 1233 | |||
| 1234 | wantarray | ||
| 1235 | ? @result | ||
| 1236 | : \@result | ||
| 1237 | } | ||
| 1238 | |||
| 1239 | method first( $select ) | ||
| 1240 | { | ||
| 1241 | defined wantarray | ||
| 1242 | or croak 'Bogus first: used in void context'; | ||
| 1243 | |||
| 1244 | $self | ||
| 1245 | or croak 'Bogus first at sentinel'; | ||
| 1246 | |||
| 1247 | for | ||
| 1248 | ( | ||
| 1249 | my $curr = $self->cursor | ||
| 1250 | ; $curr | ||
| 1251 | ; $curr->advance | ||
| 1252 | ) | ||
| 1253 | { | ||
| 1254 | # caller gets back the first cursor that maches | ||
| 1255 | # or undef at the sentinel. | ||
| 1256 | |||
| 1257 | try | ||
| 1258 | { | ||
| 1259 | $select->( $curr->data ) | ||
| 1260 | and return $curr; | ||
| 1261 | } | ||
| 1262 | catch( $err ) | ||
| 1263 | { | ||
| 1264 | $err eq "\n" | ||
| 1265 | or croak "Failed first: $err"; | ||
| 1266 | |||
| 1267 | last | ||
| 1268 | } | ||
| 1269 | } | ||
| 1270 | } | ||
| 1271 | } | ||
| 1272 | |||
| 1273 | role LinkedList::Single::Transient | ||
| 1274 | { | ||
| 1275 | use Carp qw( carp croak ); | ||
| 1276 | use Sub::Name qw( subname ); | ||
| 1277 | |||
| 1278 | our @CARP_NOT = qw( $nmgr_r $list_c $curs_c $frag_c ); | ||
| 1279 | |||
| 1280 | ############################################################ | ||
| 1281 | # these look odd.. until you see the guts of append_lazy | ||
| 1282 | # and insert_lazy: | ||
| 1283 | # | ||
| 1284 | # $self->generate_lazy( $generator )->insert_at( $self ); | ||
| 1285 | # | ||
| 1286 | # returning $self via $thence. all of these leave the | ||
| 1287 | # fragment empty, false, and generally useless. | ||
| 1288 | |||
| 1289 | my $push_onto | ||
| 1290 | = sub | ||
| 1291 | { | ||
| 1292 | my( $src, $dst ) = @_; | ||
| 1293 | |||
| 1294 | $src->transfer_node( $dst->sentinel ); | ||
| 1295 | $dst | ||
| 1296 | }; | ||
| 1297 | |||
| 1298 | method push_onto( $thence ) | ||
| 1299 | { | ||
| 1300 | $thence->DOES( $nmgr_r ) | ||
| 1301 | or croak "Bogus push: '$thence' does not $nmgr_r"; | ||
| 1302 | |||
| 1303 | $self->$push_onto( $thence ) | ||
| 1304 | } | ||
| 1305 | |||
| 1306 | method append_to( $thence ) | ||
| 1307 | { | ||
| 1308 | $thence->DOES( $nmgr_r ) | ||
| 1309 | or croak "Botched append_to: '$thence' does not $nmgr_r"; | ||
| 1310 | |||
| 1311 | $thence | ||
| 1312 | or croak "Bogus append_to at sentinel"; | ||
| 1313 | |||
| 1314 | # relinks: the nodes of $thence after the current one keep | ||
| 1315 | # their identity, as do those of the fragment. | ||
| 1316 | |||
| 1317 | $thence->attach_nodes( $self ); | ||
| 1318 | |||
| 1319 | $thence | ||
| 1320 | } | ||
| 1321 | |||
| 1322 | method insert_at( $thence ) | ||
| 1323 | { | ||
| 1324 | $thence->DOES( $nmgr_r ) | ||
| 1325 | or croak "Bogus append_to: '$thence' does not $nmgr_r"; | ||
| 1326 | |||
| 1327 | $thence | ||
| 1328 | ? | ||
| 1329 | $thence | ||
| 1330 | ->$push_onto( $self ) | ||
| 1331 | ->$push_onto( $thence ) | ||
| 1332 | : | ||
| 1333 | $self->transfer_node( $thence ) | ||
| 1334 | ; | ||
| 1335 | |||
| 1336 | $thence | ||
| 1337 | } | ||
| 1338 | |||
| 1339 | method consume( $handler ) | ||
| 1340 | { | ||
| 1341 | # consume the fragment, leaving nothing but the | ||
| 1342 | # sentinel when complete. | ||
| 1343 | |||
| 1344 | $self | ||
| 1345 | or croak 'Botched consume: empty fragment'; | ||
| 1346 | |||
| 1347 | while( $self ) | ||
| 1348 | { | ||
| 1349 | try | ||
| 1350 | { | ||
| 1351 | $handler->( $self->data ); | ||
| 1352 | } | ||
| 1353 | catch( $err ) | ||
| 1354 | { | ||
| 1355 | $err eq "\n" | ||
| 1356 | or carp | ||
| 1357 | "Failed consume: $err\n" | ||
| 1358 | , $dump_format->( $self->data ) | ||
| 1359 | ; | ||
| 1360 | |||
| 1361 | last | ||
| 1362 | } | ||
| 1363 | |||
| 1364 | $self->advance | ||
| 1365 | or last; | ||
| 1366 | } | ||
| 1367 | |||
| 1368 | # absent exctptions, this will be false (sentinel). | ||
| 1369 | # caller can check for true $self on return and | ||
| 1370 | # handle the non-empty portion to reclaim the data. | ||
| 1371 | |||
| 1372 | $self | ||
| 1373 | } | ||
| 1374 | } | ||
| 1375 | |||
| 1376 | ######################################################################## | ||
| 1377 | # nothing below this point should unwerap $node. # | ||
| 1378 | ######################################################################## | ||
| 1379 | |||
| 1380 | class LinkedList::Single::List v2.0.0 | ||
| 1381 | :repr( pvobj ) | ||
| 1382 | { | ||
| 1383 | ############################################################ | ||
| 1384 | # once node-twiddling operaitons are pushed into the | ||
| 1385 | # role for sharing with the cursor this is kinda | ||
| 1386 | # boring: the node object is a static entity that | ||
| 1387 | # references the head so that the list is kept alive. | ||
| 1388 | # | ||
| 1389 | # all it really does is house the list and generate | ||
| 1390 | # cursors. | ||
| 1391 | # | ||
| 1392 | # shift & unshift live here since a cursor has no idea | ||
| 1393 | # of where the list's head is; push and pop find the | ||
| 1394 | # tail so they make sense to share. | ||
| 1395 | # | ||
| 1396 | # everything else interesting is in the cursor class | ||
| 1397 | # it knows where on the list to perform the operations. | ||
| 1398 | ############################################################ | ||
| 1399 | |||
| 1400 | use overload | ||
| 1401 | q/bool/ => | ||
| 1402 | sub | ||
| 1403 | { | ||
| 1404 | # list is true if its head node is not the sentinel. | ||
| 1405 | |||
| 1406 | my ( $list ) = @_; | ||
| 1407 | |||
| 1408 | ! $list->is_sentinel | ||
| 1409 | } | ||
| 1410 | ; | ||
| 1411 | |||
| 1412 | use Sub::Name qw( subname ); | ||
| 1413 | use Symbol qw( qualify_to_ref ); | ||
| 1414 | |||
| 1415 | apply LinkedList::Single::NodeMgr; | ||
| 1416 | apply LinkedList::Single::Header; | ||
| 1417 | |||
| 1418 | # trivial wrappers, avoid $list->cursor->... | ||
| 1419 | # for common operations we are likley to | ||
| 1420 | # use with a list. | ||
| 1421 | |||
| 1422 | our @wrappers | ||
| 1423 | = qw | ||
| 1424 | ( | ||
| 1425 | each | ||
| 1426 | map | ||
| 1427 | grep | ||
| 1428 | first | ||
| 1429 | sort | ||
| 1430 | ); | ||
| 1431 | |||
| 1432 | for my $name ( @wrappers ) | ||
| 1433 | { | ||
| 1434 | my $sub = $curs_c->can( $name ) | ||
| 1435 | or die "Bogus $name: $curs_c cannot '$name'"; | ||
| 1436 | |||
| 1437 | *{ qualify_to_ref $name } | ||
| 1438 | = subname $name | ||
| 1439 | => sub | ||
| 1440 | { | ||
| 1441 | my ( $list ) = @_; | ||
| 1442 | |||
| 1443 | splice @_, 0, 1, $list->cursor; | ||
| 1444 | |||
| 1445 | goto &$sub | ||
| 1446 | }; | ||
| 1447 | } | ||
| 1448 | } | ||
| 1449 | |||
| 1450 | class LinkedList::Single::Cursor v2.0.0 | ||
| 1451 | :repr( pvobj ) | ||
| 1452 | { | ||
| 1453 | use overload | ||
| 1454 | q/bool/ => | ||
| 1455 | sub | ||
| 1456 | { | ||
| 1457 | # cursor is true if its head node is not the sentinel. | ||
| 1458 | |||
| 1459 | my ( $curs ) = @_; | ||
| 1460 | |||
| 1461 | ! $curs->is_sentinel | ||
| 1462 | } | ||
| 1463 | ; | ||
| 1464 | |||
| 1465 | apply LinkedList::Single::NodeMgr; | ||
| 1466 | apply LinkedList::Single::Iterator; | ||
| 1467 | } | ||
| 1468 | |||
| 1469 | class LinkedList::Single::Fragment v2.0.0 | ||
| 1470 | { | ||
| 1471 | ############################################################ | ||
| 1472 | # splicing into an existing list requries a | ||
| 1473 | # sequence of nodes that doesn't have a head. | ||
| 1474 | # hence a fragment: a sequence of nodes sans | ||
| 1475 | # the list's un-moveable root node. | ||
| 1476 | # | ||
| 1477 | # note these methods are: | ||
| 1478 | # | ||
| 1479 | # $frag->append_to( $list_or_curs ); | ||
| 1480 | # $frag->insert_at( $list_or_curs ); | ||
| 1481 | # | ||
| 1482 | # this avoids appending one list to another | ||
| 1483 | # and getting multi-headed animal syndrome. | ||
| 1484 | # | ||
| 1485 | # it makes no sense to apply bulk op's to a | ||
| 1486 | # fragment; they live in the cursor class. | ||
| 1487 | ############################################################ | ||
| 1488 | |||
| 1489 | use overload | ||
| 1490 | q/bool/ => | ||
| 1491 | sub | ||
| 1492 | { | ||
| 1493 | # list is true if its head node is not the sentinel. | ||
| 1494 | |||
| 1495 | my ( $list ) = @_; | ||
| 1496 | |||
| 1497 | ! $list->is_sentinel | ||
| 1498 | } | ||
| 1499 | ; | ||
| 1500 | |||
| 1501 | apply LinkedList::Single::NodeMgr; | ||
| 1502 | apply LinkedList::Single::Iterator; | ||
| 1503 | apply LinkedList::Single::Transient; | ||
| 1504 | } | ||
| 1505 | |||
| 1506 | # keep require happy | ||
| 1507 | 1 | ||
| 1508 | __END__ |