| 1 | ######################################################################## | ||
| 2 | # housekeeping | ||
| 3 | ######################################################################## | ||
| 4 | package LinkedList::Single::Example::SortedMerge v2.0.0; | ||
| 5 | use v5.40; | ||
| 6 | |||
| 7 | use Carp qw( croak ); | ||
| 8 | |||
| 9 | use LinkedList::Single qw( v2 ); | ||
| 10 | |||
| 11 | ######################################################################## | ||
| 12 | # an item is [ $key, @payload ]: one node of the list. the comparator | ||
| 13 | # sees the keys only, and returns <0, 0, >0 like <=>; the default is | ||
| 14 | # numeric. a merge is stable: when the keys are equal the item from the | ||
| 15 | # left list goes first. | ||
| 16 | # | ||
| 17 | # two ways to merge are provided, chosen by new( how => ... ): | ||
| 18 | # | ||
| 19 | # relink move the nodes of the inputs onto the output through | ||
| 20 | # Fragments. nothing is built; each input is emptied as | ||
| 21 | # its nodes leave it. | ||
| 22 | # copy push each item's data onto a new list through a tail | ||
| 23 | # cursor, then empty the inputs. | ||
| 24 | # | ||
| 25 | # neither can do what a pointer-based merge inside the module could | ||
| 26 | # (see benchmarks/50-SortedMerge/raw-nodes): LinkedList::Single has no operation that | ||
| 27 | # moves a node by pointer, and its Fragments move a node's *contents* | ||
| 28 | # into a new array. what each costs is in LinkedList::Single::Example::SortedMerge::POD. | ||
| 29 | ######################################################################## | ||
| 30 | |||
| 31 | sub new( $class, %arg ) | ||
| 32 | { | ||
| 33 | my $cmp = $arg{ cmp } // sub( $x, $y ) { $x <=> $y }; | ||
| 34 | my $how = $arg{ how } // 'relink'; | ||
| 35 | |||
| 36 | # with no comparator given, sort_via_array can use perl's own | ||
| 37 | # numeric comparison instead of calling $cmp for every pair. | ||
| 38 | |||
| 39 | my $default = ! defined $arg{ cmp }; | ||
| 40 | |||
| 41 | $how =~ m{ \A (?: relink | copy ) \z }x | ||
| 42 | or croak "Botched new: how must be 'relink' or 'copy', not '$how'"; | ||
| 43 | |||
| 44 | bless | ||
| 45 | { | ||
| 46 | cmp => $cmp | ||
| 47 | , how => $how | ||
| 48 | , default => $default | ||
| 49 | } | ||
| 50 | => $class | ||
| 51 | } | ||
| 52 | |||
| 53 | ######################################################################## | ||
| 54 | # merge two sorted lists into a new one. both inputs are consumed | ||
| 55 | # (they test false afterwards). | ||
| 56 | ######################################################################## | ||
| 57 | |||
| 58 | sub merge( $self, $x, $y ) | ||
| 59 | { | ||
| 60 | $self->{ how } eq 'relink' | ||
| 61 | ? _merge_relink( $self->{ cmp }, $x, $y ) | ||
| 62 | : _merge_copy( $self->{ cmp }, $x, $y ) | ||
| 63 | } | ||
| 64 | |||
| 65 | ######################################################################## | ||
| 66 | # relink: take the front node of whichever list goes next. | ||
| 67 | # | ||
| 68 | # the output is built at a cursor parked on its sentinel, so adding to | ||
| 69 | # it never walks it (push_onto walks to the sentinel of whatever it is | ||
| 70 | # given; for a cursor already standing on one that is no walk at all). | ||
| 71 | # when one input runs out the other is moved whole, in one cut. | ||
| 72 | ######################################################################## | ||
| 73 | |||
| 74 | sub _merge_relink( $cmp, $x, $y ) | ||
| 75 | { | ||
| 76 | my $out = LinkedList::Single->new; | ||
| 77 | my $tail = $out->cursor; | ||
| 78 | |||
| 79 | while( $x && $y ) | ||
| 80 | { | ||
| 81 | my $from | ||
| 82 | = $cmp->( ( $x->data )[ 0 ], ( $y->data )[ 0 ] ) <= 0 | ||
| 83 | ? $x | ||
| 84 | : $y | ||
| 85 | ; | ||
| 86 | |||
| 87 | _move_front( $from, $tail ); | ||
| 88 | } | ||
| 89 | |||
| 90 | for my $rest ( $x, $y ) | ||
| 91 | { | ||
| 92 | $rest | ||
| 93 | and $rest->fragment->push_onto( $tail ); | ||
| 94 | } | ||
| 95 | |||
| 96 | $out | ||
| 97 | } | ||
| 98 | |||
| 99 | # one node from the front of $src onto the sentinel $tail stands on. | ||
| 100 | # | ||
| 101 | # $rest everything after the first node (cut O(1): the cursor | ||
| 102 | # is on the first node, the cut is at the next) | ||
| 103 | # $one the first node alone, which empties $src | ||
| 104 | # | ||
| 105 | # then the rest goes back into the empty $src, and the node is added | ||
| 106 | # to the output, where its own sentinel becomes the new end. | ||
| 107 | |||
| 108 | sub _move_front( $src, $tail ) | ||
| 109 | { | ||
| 110 | my $rest = $src->cursor->next->fragment; | ||
| 111 | my $one = $src->fragment; | ||
| 112 | |||
| 113 | $rest | ||
| 114 | and $rest->push_onto( $src ); | ||
| 115 | |||
| 116 | $one->push_onto( $tail ); | ||
| 117 | |||
| 118 | $tail->advance; | ||
| 119 | |||
| 120 | return | ||
| 121 | } | ||
| 122 | |||
| 123 | ######################################################################## | ||
| 124 | # copy: read both inputs with cursors, push onto a new list. | ||
| 125 | ######################################################################## | ||
| 126 | |||
| 127 | sub _merge_copy( $cmp, $x, $y ) | ||
| 128 | { | ||
| 129 | my $out = LinkedList::Single->new; | ||
| 130 | my $tail = $out->cursor; | ||
| 131 | my $xc = $x->cursor; | ||
| 132 | my $yc = $y->cursor; | ||
| 133 | |||
| 134 | while( $xc && $yc ) | ||
| 135 | { | ||
| 136 | my $from | ||
| 137 | = $cmp->( ( $xc->data )[ 0 ], ( $yc->data )[ 0 ] ) <= 0 | ||
| 138 | ? $xc | ||
| 139 | : $yc | ||
| 140 | ; | ||
| 141 | |||
| 142 | $tail->push( $from->data )->advance; | ||
| 143 | |||
| 144 | $from->advance; | ||
| 145 | } | ||
| 146 | |||
| 147 | for my $rest ( $xc, $yc ) | ||
| 148 | { | ||
| 149 | while( $rest ) | ||
| 150 | { | ||
| 151 | $tail->push( $rest->data )->advance; | ||
| 152 | |||
| 153 | $rest->advance; | ||
| 154 | } | ||
| 155 | } | ||
| 156 | |||
| 157 | # the inputs were consumed: empty them, as relink does. | ||
| 158 | |||
| 159 | $x->fragment; | ||
| 160 | $y->fragment; | ||
| 161 | |||
| 162 | $out | ||
| 163 | } | ||
| 164 | |||
| 165 | ######################################################################## | ||
| 166 | # split a list in two. the first $n nodes stay in the list given, the | ||
| 167 | # rest go to a new list. $n must be less than the length of the list. | ||
| 168 | # | ||
| 169 | # the cut is one cursor walk of $n nodes and one O(1) fragment. | ||
| 170 | ######################################################################## | ||
| 171 | |||
| 172 | sub _split( $list, $n ) | ||
| 173 | { | ||
| 174 | my $right = LinkedList::Single->new; | ||
| 175 | |||
| 176 | $list->cursor->next( $n )->fragment->push_onto( $right ); | ||
| 177 | |||
| 178 | ( $list, $right ) | ||
| 179 | } | ||
| 180 | |||
| 181 | # into two halves; the left gets the extra node when the length is odd. | ||
| 182 | |||
| 183 | sub split_half( $self, $list ) | ||
| 184 | { | ||
| 185 | my $count = $list->count; | ||
| 186 | |||
| 187 | $count > 1 | ||
| 188 | or croak "Botched split_half: a list of $count nodes has no halves"; | ||
| 189 | |||
| 190 | _split( $list, int( ( $count + 1 ) / 2 ) ) | ||
| 191 | } | ||
| 192 | |||
| 193 | ######################################################################## | ||
| 194 | # merge sort: split, sort each half, merge. the recursion is log2(n) | ||
| 195 | # deep; the length is passed down so no list is counted twice. | ||
| 196 | # | ||
| 197 | # the argument is consumed: use the list returned. | ||
| 198 | ######################################################################## | ||
| 199 | |||
| 200 | sub merge_sort( $self, $list ) | ||
| 201 | { | ||
| 202 | _sort( $self, $list, $list->count ) | ||
| 203 | } | ||
| 204 | |||
| 205 | sub _sort( $self, $list, $count ) | ||
| 206 | { | ||
| 207 | $count > 1 | ||
| 208 | or return $list; | ||
| 209 | |||
| 210 | my $half = int( ( $count + 1 ) / 2 ); | ||
| 211 | my( $left, $right ) = _split( $list, $half ); | ||
| 212 | |||
| 213 | $self->merge | ||
| 214 | ( | ||
| 215 | _sort( $self, $left, $half ) | ||
| 216 | , _sort( $self, $right, $count - $half ) | ||
| 217 | ) | ||
| 218 | } | ||
| 219 | |||
| 220 | ######################################################################## | ||
| 221 | # sort through an array: take the items out, let perl's own sort order | ||
| 222 | # them, and build a new list. nothing here is clever, and it is the | ||
| 223 | # practical competitor to a merge sort on the list: perl's sort is C, | ||
| 224 | # and each item crosses the list boundary only twice. | ||
| 225 | # | ||
| 226 | # the order of equal keys is perl's: its sort has been a stable merge | ||
| 227 | # sort since 5.8, though the documentation stops short of promising it, | ||
| 228 | # and the tests check it. the argument is consumed, like merge_sort's. | ||
| 229 | ######################################################################## | ||
| 230 | |||
| 231 | sub sort_via_array( $self, $list ) | ||
| 232 | { | ||
| 233 | my $cmp = $self->{ cmp }; | ||
| 234 | my $item = ( scalar $list->extract ) // []; | ||
| 235 | my $out = LinkedList::Single->new; | ||
| 236 | my $tail = $out->cursor; | ||
| 237 | |||
| 238 | my @sorted | ||
| 239 | = $self->{ default } | ||
| 240 | ? sort { $a->[ 0 ] <=> $b->[ 0 ] } $item->@* | ||
| 241 | : sort { $cmp->( $a->[ 0 ], $b->[ 0 ] ) } $item->@* | ||
| 242 | ; | ||
| 243 | |||
| 244 | $tail->push( $_->@* )->advance | ||
| 245 | for @sorted; | ||
| 246 | |||
| 247 | $list->fragment; | ||
| 248 | |||
| 249 | $out | ||
| 250 | } | ||
| 251 | |||
| 252 | ######################################################################## | ||
| 253 | # test support: items in and out. | ||
| 254 | ######################################################################## | ||
| 255 | |||
| 256 | # a list of the items, in order. | ||
| 257 | |||
| 258 | sub build( $self, @item ) | ||
| 259 | { | ||
| 260 | my $list = LinkedList::Single->new; | ||
| 261 | my $tail = $list->cursor; | ||
| 262 | |||
| 263 | $tail->push( $_->@* )->advance | ||
| 264 | for @item; | ||
| 265 | |||
| 266 | $list | ||
| 267 | } | ||
| 268 | |||
| 269 | # arrayref of the items in a list: [ $key, @payload ], in order. | ||
| 270 | |||
| 271 | sub contents( $self, $list ) | ||
| 272 | { | ||
| 273 | ( scalar $list->extract ) // [] | ||
| 274 | } | ||
| 275 | |||
| 276 | 1 | ||
| 277 | __END__ |