From 97973476dd225680059f6b58a12cef3ef950ffde Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Sun, 15 Nov 2009 07:06:01 +0100 Subject: [PATCH] cleanup context variables --- lib/List/Index.pm | 25 ++++++++++++------------- t/10-ranges.t | 4 ++-- 2 files changed, 14 insertions(+), 15 deletions(-) diff --git a/lib/List/Index.pm b/lib/List/Index.pm index daeb707..6309d2e 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -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; diff --git a/t/10-ranges.t b/t/10-ranges.t index efe64b3..a394945 100644 --- a/t/10-ranges.t +++ b/t/10-ranges.t @@ -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-)], -- 2.30.0