| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  |  | 
| 2 |  |  |  |  |  |  | package Sort::Half::Maker; | 
| 3 |  |  |  |  |  |  |  | 
| 4 | 2 |  |  | 2 |  | 1891 | use strict; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 95 |  | 
| 5 | 2 |  |  | 2 |  | 13 | use warnings; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 901 |  | 
| 6 |  |  |  |  |  |  |  | 
| 7 |  |  |  |  |  |  | our $VERSION = '0.03'; | 
| 8 |  |  |  |  |  |  |  | 
| 9 |  |  |  |  |  |  | require Exporter; | 
| 10 |  |  |  |  |  |  | our @ISA = qw(Exporter); | 
| 11 |  |  |  |  |  |  | our @EXPORT_OK = qw(make_halfsorter make_halfsort); | 
| 12 |  |  |  |  |  |  |  | 
| 13 |  |  |  |  |  |  | sub make_halfsort { | 
| 14 | 5 |  |  | 5 | 1 | 4269 | my %args = @_; | 
| 15 | 5 |  | 100 | 9 |  | 38 | my $sort_sub = $args{fallback} || sub ($$) { $_[0] cmp $_[1] }; | 
|  | 9 |  |  |  |  | 26 |  | 
| 16 | 5 |  |  |  |  | 8 | my %start_hash; | 
| 17 | 5 | 100 |  |  |  | 18 | if ($args{start}) { | 
| 18 | 4 |  |  |  |  | 6 | my @start = @{$args{start}}; | 
|  | 4 |  |  |  |  | 11 |  | 
| 19 |  |  |  |  |  |  | #@start_hash{@start} = (1..@start); | 
| 20 | 4 |  |  |  |  | 7 | my $idx = 1; | 
| 21 | 4 | 100 |  |  |  | 8 | for (@start) { $start_hash{$_} = $idx unless exists $start_hash{$_}; $idx++ }; | 
|  | 12 |  |  |  |  | 41 |  | 
|  | 12 |  |  |  |  | 21 |  | 
| 22 |  |  |  |  |  |  | } | 
| 23 | 5 |  |  |  |  | 8 | my %end_hash; | 
| 24 | 5 | 100 |  |  |  | 13 | if ($args{end}) { | 
| 25 | 2 |  |  |  |  | 4 | my @end = @{$args{end}}; | 
|  | 2 |  |  |  |  | 5 |  | 
| 26 |  |  |  |  |  |  | #@end_hash{@end} = (1..@end); | 
| 27 | 2 |  |  |  |  | 5 | my $idx = 1; # the same as "@end_hash{@end} = (1..@end)" but keeps the first occurrence | 
| 28 | 2 | 100 |  |  |  | 6 | for (@end) { $end_hash{$_} = $idx unless exists $end_hash{$_}; $idx++ }; | 
|  | 7 |  |  |  |  | 25 |  | 
|  | 7 |  |  |  |  | 13 |  | 
| 29 |  |  |  |  |  |  | } | 
| 30 |  |  |  |  |  |  | return sub ($$) { | 
| 31 | 52 |  |  | 52 |  | 5234 | my ($left, $right) = @_; | 
| 32 | 52 | 100 | 100 |  |  | 244 | if ($start_hash{$left} || $start_hash{$right}) { | 
|  |  | 100 | 100 |  |  |  |  | 
| 33 | 26 |  | 66 |  |  | 66 | my $ia = $start_hash{$left} || keys(%start_hash)+1; | 
| 34 | 26 |  | 66 |  |  | 64 | my $ib = $start_hash{$right} || keys(%start_hash)+1; | 
| 35 | 26 |  |  |  |  | 50 | return $ia <=> $ib; | 
| 36 |  |  |  |  |  |  | } elsif ($end_hash{$left} || $end_hash{$right}) { | 
| 37 | 8 |  | 100 |  |  | 25 | my $ia = $end_hash{$left} || 0; | 
| 38 | 8 |  | 100 |  |  | 18 | my $ib = $end_hash{$right} || 0; | 
| 39 | 8 |  |  |  |  | 22 | return $ia <=> $ib; | 
| 40 |  |  |  |  |  |  | } else { | 
| 41 | 18 |  |  |  |  | 38 | return $sort_sub->($left, $right); | 
| 42 |  |  |  |  |  |  | } | 
| 43 |  |  |  |  |  |  | } | 
| 44 |  |  |  |  |  |  |  | 
| 45 | 5 |  |  |  |  | 34 | } | 
| 46 |  |  |  |  |  |  |  | 
| 47 |  |  |  |  |  |  | #sub make_halfsorter { | 
| 48 |  |  |  |  |  |  | #    my %args = @_; | 
| 49 |  |  |  |  |  |  | #    my $sort_sub = $args{any} || sub { $_[0] cmp $_[1] }; | 
| 50 |  |  |  |  |  |  | #    my %pre_hash; | 
| 51 |  |  |  |  |  |  | #    if ($args{pre}) { | 
| 52 |  |  |  |  |  |  | #        my @pre = @{$args{pre}}; | 
| 53 |  |  |  |  |  |  | #        @pre_hash{@pre} = (1..@pre); | 
| 54 |  |  |  |  |  |  | #    } | 
| 55 |  |  |  |  |  |  | #    my %post_hash; | 
| 56 |  |  |  |  |  |  | #    if ($args{post}) { | 
| 57 |  |  |  |  |  |  | #        my @post = @{$args{post}}; | 
| 58 |  |  |  |  |  |  | #        @post_hash{@post} = (1..@post); | 
| 59 |  |  |  |  |  |  | #    } | 
| 60 |  |  |  |  |  |  | #    return sub { | 
| 61 |  |  |  |  |  |  | #        sort { | 
| 62 |  |  |  |  |  |  | #               my ($left, $right) = map { $_ } $a, $b; | 
| 63 |  |  |  |  |  |  | #               #my ($left, $right) = map { $_ } @_; | 
| 64 |  |  |  |  |  |  | #               if ($pre_hash{$left} || $pre_hash{$right}) { | 
| 65 |  |  |  |  |  |  | #                   my $ia = $pre_hash{$left} || keys(%pre_hash)+1; | 
| 66 |  |  |  |  |  |  | #                   my $ib = $pre_hash{$right} || keys(%pre_hash)+1; | 
| 67 |  |  |  |  |  |  | #                   return $ia <=> $ib; | 
| 68 |  |  |  |  |  |  | #               } elsif ($post_hash{$left} || $post_hash{$right}) { | 
| 69 |  |  |  |  |  |  | #                   my $ia = $post_hash{$left} || 0; | 
| 70 |  |  |  |  |  |  | #                   my $ib = $post_hash{$right} || 0; | 
| 71 |  |  |  |  |  |  | #                   return $ia <=> $ib; | 
| 72 |  |  |  |  |  |  | #               } else { | 
| 73 |  |  |  |  |  |  | #                   return $sort_sub->($left, $right); | 
| 74 |  |  |  |  |  |  | #               } | 
| 75 |  |  |  |  |  |  | #        } @_ | 
| 76 |  |  |  |  |  |  | #    } | 
| 77 |  |  |  |  |  |  | # | 
| 78 |  |  |  |  |  |  | #} | 
| 79 |  |  |  |  |  |  |  | 
| 80 |  |  |  |  |  |  | 1; | 
| 81 |  |  |  |  |  |  |  | 
| 82 |  |  |  |  |  |  | __END__ |