diff options
Diffstat (limited to 'lib/Data/MultiValued/RangeContainer.pm')
-rw-r--r-- | lib/Data/MultiValued/RangeContainer.pm | 139 |
1 files changed, 90 insertions, 49 deletions
diff --git a/lib/Data/MultiValued/RangeContainer.pm b/lib/Data/MultiValued/RangeContainer.pm index 0bfe8cd..8e369e1 100644 --- a/lib/Data/MultiValued/RangeContainer.pm +++ b/lib/Data/MultiValued/RangeContainer.pm @@ -1,4 +1,10 @@ package Data::MultiValued::RangeContainer; +{ + $Data::MultiValued::RangeContainer::VERSION = '0.0.1'; +} +{ + $Data::MultiValued::RangeContainer::DIST = 'Data-MultiValued'; +} use Moose; use Moose::Util::TypeConstraints; use MooseX::Types::Moose qw(Num Str Any Undef ArrayRef); @@ -7,24 +13,6 @@ use Data::MultiValued::Exceptions; # ABSTRACT: container for ranged values -=head1 DESCRIPTION - -Please don't use this module directly, use L<Data::MultiValued::Ranges>. - -This module implements the storage for ranged data. It's similar to -L<Array::IntSpan>, but simpler (and slower). - -A range is defined by a pair of numbers, C<from> and C<to>, and it -contains C<< Num $x : $min <= $x < $max >>. C<undef> is treated as -"inf" (negative infinity if used as C<from> or C<at>, positive -infinity if used as C<to>). - -The internal representation of a range is a hash with three keys, -C<from> C<to> C<value>. - -=head1 METHODS - -=cut has _storage => ( is => 'rw', @@ -39,15 +27,6 @@ has _storage => ( default => sub { [ ] }, ); -=head2 C<get> - - my $value = $obj->get({ at => $point }); - -Retrieves the range that includes the given point. Throws a -L<Data::MultiValued::Exceptions::RangeNotFound> exception if no range -includes the point. - -=cut sub get { my ($self,$args) = @_; @@ -117,16 +96,6 @@ sub _partition_slots { return \@before,\@overlap,\@after; } -=head2 C<get_or_create> - - $obj->get_or_create({ from => $min, to => $max }); - -Retrieves the range that has the given extremes. If no such range -exists, creates a new range, splicing any existing overlapping range, -and returns it. Throws L<Data::MultiValued::Exceptions::BadRange> if -C<< $min > $max >>. - -=cut sub get_or_create { my ($self,$args) = @_; @@ -151,18 +120,6 @@ sub get_or_create { return $range; } -=head2 C<clear> - - $obj->clear({ from => $min, to => $max }); - -Removes the range that has the given extremes. If no such range -exists, splices any existing overlapping range so that C<< -$obj->get({at => $point }) >> for any C<< $min <= $point < $max >> -will die. - -Throws L<Data::MultiValued::Exceptions::BadRange> if C<< $min > $max >>. - -=cut sub clear { my ($self,$args) = @_; @@ -268,4 +225,88 @@ sub _splice_slot { return $new; } + +sub all_ranges { + my ($self) = @_; + + return map { [ $_->{from}, $_->{to} ] } @{$self->_storage}; +} + 1; + +__END__ +=pod + +=head1 NAME + +Data::MultiValued::RangeContainer - container for ranged values + +=head1 VERSION + +version 0.0.1 + +=head1 DESCRIPTION + +Please don't use this module directly, use L<Data::MultiValued::Ranges>. + +This module implements the storage for ranged data. It's similar to +L<Array::IntSpan>, but simpler (and slower). + +A range is defined by a pair of numbers, C<from> and C<to>, and it +contains C<< Num $x : $min <= $x < $max >>. C<undef> is treated as +"inf" (negative infinity if used as C<from> or C<at>, positive +infinity if used as C<to>). + +The internal representation of a range is a hash with three keys, +C<from> C<to> C<value>. + +=head1 METHODS + +=head2 C<get> + + my $value = $obj->get({ at => $point }); + +Retrieves the range that includes the given point. Throws a +L<Data::MultiValued::Exceptions::RangeNotFound> exception if no range +includes the point. + +=head2 C<get_or_create> + + $obj->get_or_create({ from => $min, to => $max }); + +Retrieves the range that has the given extremes. If no such range +exists, creates a new range, splicing any existing overlapping range, +and returns it. Throws L<Data::MultiValued::Exceptions::BadRange> if +C<< $min > $max >>. + +=head2 C<clear> + + $obj->clear({ from => $min, to => $max }); + +Removes the range that has the given extremes. If no such range +exists, splices any existing overlapping range so that C<< +$obj->get({at => $point }) >> for any C<< $min <= $point < $max >> +will die. + +Throws L<Data::MultiValued::Exceptions::BadRange> if C<< $min > $max >>. + +=head2 C<all_ranges> + + my @ranges = $obj->all_ranges; + +Returns all the ranges defined in this object, as a list of 2-elements +arrayrefs. + +=head1 AUTHOR + +Gianni Ceccarelli <dakkar@thenautilus.net> + +=head1 COPYRIGHT AND LICENSE + +This software is copyright (c) 2011 by Net-a-porter.com. + +This is free software; you can redistribute it and/or modify it under +the same terms as the Perl 5 programming language system itself. + +=cut + |