Subject: | Set::IntSpan::Fast contains_all_range func |
Date: | Tue, 27 Nov 2007 08:05:25 +1100 |
To: | bug-set-intspan-fast [...] rt.cpan.org |
From: | Kevin Ryde <user42 [...] zip.com.au> |
It'd be nice to have a "contains_all_range" function in
Set::IntSpan::Fast, vaguely matching add_range and remove_range.
I've got a use loading consecutively numbered database records into
memory "on-demand", recording in a set what ranges have been loaded so
far. When wanting to display a range I thought to quickly test whether
it's covered by the set already, often yes in my case, before figuring
what non-covered portions have to be loaded or calculated.
It might be as simple as follows (tested, err, a little bit :-),
basically "contains($low)" with an extra test on whether the found span
extends to the high too, if I understand the array format correctly.
=item C<contains_all_range( $low, $high )>
Return true if all the numbers in the range C<$low> to C<$high> (inclusive)
are in the set.
=cut
sub contains_all_range {
my ($self, $lo, $hi) = @_;
if ($lo > $hi) { croak "Range limits must be in ascending order"; }
my $pos = $self->_find_pos ($lo + 1);
return ($pos & 1) && $hi < $self->[$pos];
}