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
# 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
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;
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-)],