From c6b7152b5b448744cce44c7ae52cf530c1e19bba Mon Sep 17 00:00:00 2001 From: Mischa POSLAWSKY Date: Thu, 12 Nov 2009 19:25:32 +0100 Subject: [PATCH] XXX: omit duplicates (overlaps) --- lib/List/Index.pm | 6 ++++-- t/10-ranges.t | 6 +++--- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/List/Index.pm b/lib/List/Index.pm index bf4e04d..57027fa 100644 --- a/lib/List/Index.pm +++ b/lib/List/Index.pm @@ -24,7 +24,7 @@ sub ranges { $pagesize = @$self / $pages; my $offset = $pagesize; - my @links = (['']); + my @links = (''); while ($offset < @$self) { my $link = substr $self->[$offset], 0, $length; if ($context) { @@ -57,10 +57,12 @@ sub ranges { } } - push @links, [$link]; + push @links, $link; $offset += $pagesize; } + use List::MoreUtils 'uniq'; + @links = map { [$_] } uniq @links; for my $i (0 .. $#links - 1) { my ($link, $lastchar) = $links[$i + 1]->[0] =~ /(.*)(.)/; $link .= $lastchar le 'a' ? '.' : chr( ord($lastchar) - 1 ); diff --git a/t/10-ranges.t b/t/10-ranges.t index 66edf76..3667034 100644 --- a/t/10-ranges.t +++ b/t/10-ranges.t @@ -25,7 +25,7 @@ is_deeply($index->ranges({pagesize => @uniform / 2.1}), [map { [split /-/, $_, 2 { ok(my $index = List::Index->new(['aa'..'zz', 1..193]), 'non-alphabetic values (uniform)'); is_deeply($index->ranges, [map { [split /-/, $_, 2] } qw( - -. . . . + -. . a-b c-dr ds-fn fo-hk hl-jg jh-k l-m n-o p-qr qs-sn so-uk ul-wg wh-x y- )], 'default ranges'); is_deeply($index->ranges({pagesize => 300}), [map { [split /-/, $_, 2] } qw( @@ -44,10 +44,10 @@ is_deeply($index->ranges({pagesize => 2}), [ map { [split /-/, $_, 2] } qw(-a b c d-ea. eaa-) ], 'default context'); # context should be 1 is_deeply($index->ranges({pagesize => 2, context => 2}), [ - map { [split /-/, $_, 2] } qw(-a b-c d-c d e-) + map { [split /-/, $_, 2] } qw(-a b-c d e-) ], 'overlap'); # first item equals second due to large context is_deeply($index->ranges({pagesize => 2, length => 1}), [ - map { [split /-/, $_, 2] } qw(-a b-a b-c d e-) + map { [split /-/, $_, 2] } qw(-a b-c d e-) ], 'single char'); #pp($index->ranges({pagesize => 2, context => 2, length => 1})); -- 2.30.0