diff options
author | Gianni Ceccarelli <dakkar@thenautilus.net> | 2011-11-10 12:53:24 +0000 |
---|---|---|
committer | Gianni Ceccarelli <dakkar@thenautilus.net> | 2011-11-10 15:04:15 +0000 |
commit | 9b3887ba26cfa17344567d9a1b89921892d02dda (patch) | |
tree | b9e8d8c63cfb158cb5a9e6c9f71946e35ffdbcee /Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm | |
parent | ugly serialization helper role (diff) | |
download | data-multivalued-9b3887ba26cfa17344567d9a1b89921892d02dda.tar.gz data-multivalued-9b3887ba26cfa17344567d9a1b89921892d02dda.tar.bz2 data-multivalued-9b3887ba26cfa17344567d9a1b89921892d02dda.zip |
'clear' almost completely implemneted
Diffstat (limited to 'Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm')
-rw-r--r-- | Data-MultiValued/lib/Data/MultiValued/RangeContainer.pm | 40 |
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}, |