cleanup context variables
authorMischa POSLAWSKY <perl@shiar.org>
Sun, 15 Nov 2009 06:06:01 +0000 (07:06 +0100)
committerMischa POSLAWSKY <perl@shiar.org>
Sun, 15 Nov 2009 06:06:01 +0000 (07:06 +0100)
lib/List/Index.pm
t/10-ranges.t

index daeb707f9835c58e8160edf05440f59acd600690..6309d2e0d3413926b6063098444e17f471f796f6 100644 (file)
@@ -27,14 +27,17 @@ sub ranges {
        my $pages    = $options->{pages   } || 1 + int $#rows / $pagesize;
 
        $pagesize = $pages >= $#rows ? 1 : @rows / $pages;
-       my $lookbehind = -$context;
-       my $lookahead  =  $context;
+       my $shrunk = 0;
+       my $enlarged = 0;
 
        my @links = ('');
        for (my $offset = $pagesize + .5; $offset < @rows; $offset += $pagesize) {
                my $link = substr $rows[$offset], 0, $length;
                if ($context) {
-                       my $penalty = 0;
+                       my $lookbehind = -$context + $shrunk;
+                       my $lookahead  =  $context - $enlarged;
+                       $shrunk = $enlarged = 0;
+
                        # take a value slightly before the current offset
                        if ((my $before = $offset + $lookbehind) > 0) {
                                # see how much of it matches the current link
@@ -46,15 +49,13 @@ sub ranges {
                                # truncate link upto where the earlier value starts to differ
                                if ($trim < length $link) {
                                        substr($link, $trim) = '';
-                                       for (reverse $before .. $offset) {
-                                               $rows[$offset - $penalty] =~ /^\Q$link/ or last;
-                                               $penalty++;
+                                       for (reverse $before .. $offset - 1) {
+                                               $rows[$_] =~ /^\Q$link/ or last;
+                                               $enlarged++;
                                        }
                                }
                        }
 
-                       $lookbehind = -$context;
-
                        # take a value after the current offset
                        if ((my $after = $offset + $lookahead) < $#rows) {
                                # see how much of it matches the current link
@@ -67,15 +68,13 @@ sub ranges {
                                if ($trim < length $link) {
                                        $link = substr $rows[$after], 0, $trim;
                                        # advance lookbehind offset on the next page
-                                       $penalty = 0;
-                                       for ($offset .. $after) {
+                                       $enlarged = 0;
+                                       for ($offset + 1 .. $after) {
+                                               $shrunk++;
                                                last if $rows[$_] =~ /^\Q$link/;
-                                               $lookbehind++;
                                        }
                                }
                        }
-
-                       $lookahead = $context - $penalty;
                }
 
                push @links, $link unless $links[-1] eq $link;
index efe64b37d64c29d6fd732501b2e93e1233b22ed8..a3949453e05ef770b19bdadb884ff6a09feeb283 100644 (file)
@@ -133,12 +133,12 @@ subtest 'distribution' => sub {
        my $index = List::Index->new({ pagesize => 10 }) or return;
 
        is_deeply(
-               $index->ranges(\@data, { context => 8 }),
+               $index->ranges(\@data, { context => 7 }),
                [qw(-g h i-)],
                'large context'
        );
        is_deeply(
-               $index->ranges(\@data, { context => 7 }),
+               $index->ranges(\@data, { context => 6 }),
                # after 2nd page is enlarged by lookbehind to 'h', limit subsequent lookahead
                # to prevent the page from getting too large (17 entries if forwarded to 'i')
                [qw(-g h-hm hn-)],