summaryrefslogtreecommitdiff
path: root/Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm
diff options
context:
space:
mode:
Diffstat (limited to 'Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm')
-rw-r--r--Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm40
1 files changed, 32 insertions, 8 deletions
diff --git a/Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm b/Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm
index 33864da..474626f 100644
--- a/Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm
+++ b/Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm
@@ -103,28 +103,52 @@ sub set_or_create {
return $range;
}
+sub clear {
+ my ($self,$args) = @_;
+
+ my $from = $args->{from};
+ my $to = $args->{to};
+
+ Data::MultiValued::Exceptions::BadRange->throw({
+ from => $from,
+ to => $to,
+ }) if _cmp($from,$to,0,1)>0;
+
+ return $self->_clear_slot($from,$to);
+}
+
sub _create_slot {
my ($self,$from,$to) = @_;
- my $new = {
+ $self->_splice_slot($from,$to,{
from => $from,
to => $to,
value => undef,
- };
+ });
+}
+
+sub _clear_slot {
+ my ($self,$from,$to) = @_;
+
+ $self->_splice_slot($from,$to);
+}
+
+sub _splice_slot {
+ my ($self,$from,$to,$new) = @_;
if (!@{$self->_storage}) { # empty
- push @{$self->_storage},$new;
+ push @{$self->_storage},$new if $new;
return $new;
}
my ($before,$overlap,$after) = $self->_partition_slots($from,$to);
if (!@$before && !@$overlap) {
- unshift @{$self->_storage},$new;
+ unshift @{$self->_storage},$new if $new;
return $new;
}
if (!@$after && !@$overlap) {
- push @{$self->_storage},$new;
+ push @{$self->_storage},$new if $new;
return $new;
}
@@ -134,7 +158,7 @@ sub _create_slot {
my $last_to_replace = $overlap->[-1],
my $how_many = @$overlap;
- my @replacement = ($new);
+ my @replacement = $new ? ($new) : ();
if ($how_many > 0) { # we have to splice
my $first = $self->_storage->[$first_to_replace];
@@ -148,8 +172,8 @@ sub _create_slot {
value => $first->{value},
}
}
- if (_cmp($last->{from},$to,0,1)<0
- && _cmp($last->{to},$to,1,1)>=0) {
+ if (_cmp($last->{from},$to,0,1)<=0
+ && _cmp($last->{to},$to,1,1)>0) {
push @replacement, {
from => $to,
to => $last->{to},