我有一个函数binary_range_search
,就像这样调用:
my $brs_iterator = binary_range_search(
target => $range, # eg. [1, 200]
search => $ranges # eg. [ {start => 1, end => 1000},
); # {start => 500, end => 1500} ]
brs_iterator->()
将遍历$ range重叠的所有@ $范围。
我想扩展binary_range_search
以便能够以多个范围作为目标来调用它,例如:
target => $target_ranges # eg. [ [1, 200], [50, 300], ... ]
search => $search_ranges # as above
因此,当$ range-> [0]的搜索耗尽时,它应继续到$ range-> [1],依此类推。这是有问题的功能,原始形式:
sub binary_range_search {
my %options = @_;
my $range = $options{target} || return;
my $ranges = $options{search} || return;
my ( $low, $high ) = ( 0, @{$ranges} - 1 );
while ( $low <= $high ) {
my $try = int( ( $low + $high ) / 2 );
$low = $try + 1, next if $ranges->[$try]{end} < $range->[0];
$high = $try - 1, next if $ranges->[$try]{start} > $range->[1];
my ( $down, $up ) = ($try) x 2;
my %seen = ();
my $brs_iterator = sub {
if ( $ranges->[ $up + 1 ]{end} >= $range->[0]
and $ranges->[ $up + 1 ]{start} <= $range->[1]
and !exists $seen{ $up + 1 } )
{
$seen{ $up + 1 } = undef;
return $ranges->[ ++$up ];
}
elsif ( $ranges->[ $down - 1 ]{end} >= $range->[0]
and $ranges->[ $down + 1 ]{start} <= $range->[1]
and !exists $seen{ $down - 1 }
and $down > 0 )
{
$seen{ $down - 1 } = undef;
return $ranges->[ --$down ];
}
elsif ( !exists $seen{$try} ) {
$seen{$try} = undef;
return $ranges->[$try];
}
else {
return;
}
};
return $brs_iterator;
}
return sub { };
}
这是一种标准的二元搜索策略,直到找到重叠范围。然后它向右移动,排气,向左移动,排气,最后放弃。理想情况下,它可能应该shift
下一个目标范围,并重做搜索,我想(也许通过递归?)。我的问题是,我不知道如何使用迭代器结构。
答案 0 :(得分:2)
如果您想迭代与搜索范围重叠的所有值,则不需要二进制搜索。
首先是习惯性的前沿问题:
use warnings;
use strict;
use Carp;
首先,检查我们是否有target
和search
参数,并且对于每个范围,起点不大于其结束点。否则,我们拒绝继续。
sub binary_range_search {
my %arg = @_;
my @errors;
my $target = $arg{target} || push @errors => "no target";
my $search = $arg{search} || push @errors => "no search";
for (@$target) {
my($start,$end) = @$_;
push @errors => "Target start ($start) is greater than end ($end)"
if $start > $end;
}
for (@$search) {
my($start,$end) = @{$_}{qw/ start end /};
push @errors => "Search start ($start) is greater than end ($end)"
if $start > $end;
}
croak "Invalid use of binary_range_search:\n",
map " - $_\n", @errors
if @errors;
迭代器本身是一个保持以下状态的闭包:
my $i;
my($ta,$tb);
my($sa,$sb);
my $si = 0;
,其中
$i
如果已定义为当前重叠范围的下一个值$ta
和$tb
是当前目标范围的起点和终点$sa
和$sb
与上述类似,但针对当前搜索范围$si
是@$search
的索引,用于定义当前搜索范围我们将分配并返回迭代器$it
。声明和初始化是分开的,因此迭代器可以在必要时调用它自己。
my $it;
$it = sub {
如果不再有目标范围或者没有搜索范围,我们就完成了。
return unless @$target && @$search;
当定义$i
时,意味着我们找到了重叠并通过递增$i
进行迭代,直到它大于当前目标范围或当前搜索范围的结束点。
if (defined $i) {
# iterating within a target range
if ($i > $tb || $i > $sb) {
++$si;
undef $i;
return $it->();
}
else {
return $i++;
}
}
否则,我们需要确定下一个目标范围是否与任何搜索范围重叠。但是,如果$i
未定义且我们已经考虑了所有搜索范围,我们会丢弃当前的目标范围并重新开始。
else {
# does the next target range overlap?
if ($si >= @$search) {
shift @$target;
$si = 0;
return $it->();
}
这里我们提取当前目标范围(始终位于@$target
前面)和当前搜索范围(由$si
索引)的起点和终点。
($ta,$tb) = @{ $target->[0] };
($sa,$sb) = @{ $search->[$si] }{qw/ start end /};
现在测试重叠很简单。对于不相交的搜索范围,我们忽略并继续前进。否则,我们找到重叠中最左边的点并从那里迭代。
if ($sb < $ta || $sa > $tb) {
# disjoint
++$si;
undef $i;
return $it->();
}
elsif ($sa >= $ta) {
$i = $sa;
return $i++;
}
elsif ($ta >= $sa) {
$i = $ta;
return $i++;
}
}
};
最后,我们返回迭代器:
$it;
}
有关您问题中类似的示例
my $it = binary_range_search(
target => [ [1, 200], [50, 300] ],
search => [ { start => 1, end => 1000 },
{ start => 500, end => 1500 },
{ start => 40, end => 60 },
{ start => 250, end => 260 } ],
);
while (defined(my $value = $it->())) {
print "got $value\n";
}
其内部点的输出省略
got 1 [...] got 200 got 40 [...] got 60 got 50 [...] got 300 got 50 [...] got 60 got 250 [...] got 260
答案 1 :(得分:2)
我只是将你的迭代器生成包装在for循环中,并构建了一个迭代器函数数组。
根据上下文,我要么返回一个主迭代器,要么返回一个迭代器函数列表。我不确定你想要什么。
use strict;
use warnings;
my $t = [ [1,200], [400,900] ];
my @r = (
{ start => 1, end => 100 },
{ start => 2, end => 500 },
{ start => 204, end => 500 },
{ start => 208, end => 500 },
{ start => 215, end => 1000 },
{ start => 150, end => 1000 },
{ start => 500, end => 1100 },
);
# Get a master iterator that will process each iterator in turn.
my $brs_iterator = binary_range_search(
targets => $t,
search => \@r,
);
# Get an array of iterators
my @brs_iterator = binary_range_search(
targets => $t,
search => \@r,
);
sub binary_range_search {
my %options = @_;
my $targets = $options{targets} || return;
my $ranges = $options{search} || return;
my @iterators;
TARGET:
for my $target ( @$targets ) {
my ( $low, $high ) = ( 0, $#{$ranges} );
RANGE_CHECK:
while ( $low <= $high ) {
my $try = int( ( $low + $high ) / 2 );
# Remove non-overlapping ranges
$low = $try + 1, next RANGE_CHECK
if $ranges->[$try]{end} < $target->[0];
$high = $try - 1, next RANGE_CHECK
if $ranges->[$try]{start} > $target->[1];
my ( $down, $up ) = ($try) x 2;
my %seen = ();
my $brs_iterator = sub {
if ( exists $ranges->[$up + 1]
and $ranges->[ $up + 1 ]{end} >= $target->[0]
and $ranges->[ $up + 1 ]{start} <= $target->[1]
and !exists $seen{ $up + 1 } )
{
$seen{ $up + 1 } = undef;
return $ranges->[ ++$up ];
}
elsif ( $ranges->[ $down - 1 ]{end} >= $target->[0]
and $ranges->[ $down + 1 ]{start} <= $target->[1]
and !exists $seen{ $down - 1 }
and $down > 0 )
{
$seen{ $down - 1 } = undef;
return $ranges->[ --$down ];
}
elsif ( !exists $seen{$try} ) {
$seen{$try} = undef;
return $ranges->[$try];
}
else {
return;
}
};
push @iterators, $brs_iterator;
next TARGET;
}
}
# In scalar context return master iterator that iterates over the list of range iterators.
# In list context returns a list of range iterators.
return wantarray
? @iterators
: sub {
while( @iterators ) {
if( my $range = $iterators[0]() ) {
return $range;
}
shift @iterators;
}
return;
};
}
答案 2 :(得分:0)
将它分成两个函数,一个循环遍历范围的外部函数,并调用一个实现传统二进制文章的内部函数。
答案 3 :(得分:0)
警告:一个非常c ++偏见的答案:
你需要做的是定义一个新类型的迭代器,它是一对通常的迭代器和一个segmemt iterrator(如果你没有一个段迭代器,它是一对const指针/ ref到段和指向正确段的索引。您必须定义随机访问迭代器的所有概念(差异,整数的添加等)。请记住,至少在c ++语言中,这不是一个真正的随机迭代器,因为添加一个整数并不是真正的恒定时间;这就是生活。