DR-Tarantool
view release on metacpan - search on metacpan
view release on metacpan or search on metacpan
lib/DR/Tarantool/Iterator.pm view on Meta::CPAN
$old_iter->sort(sub { $_[0]->name cmp $_[1]->name });
# $iter1 is sorted, too, but $iter2 is not
=cut
sub clone {
my $self = shift;
my %opts;
if (@_ == 1) {
%opts = (clone_items => shift);
} else {
%opts = @_;
}
my %pre = (
data => $self->data,
item_class => $self->item_class,
item_constructor => $self->item_constructor
);
my $clone_items = delete $opts{clone_items};
my $items = $clone_items ? [ @{ $self->{items} } ] : $self->{items};
$self = $self->new( $items, %pre, %opts );
$self;
}
=head2 count
Return the number of tuples available through the iterator.
=cut
sub count {
my ($self) = @_;
return scalar @{ $self->{items} };
}
=head2 item
Return one tuple from the iterator by its index
(or croak an error if the index is out of range).
=cut
sub item {
my ($self, $no) = @_;
my $item = $self->raw_item( $no );
if (my $class = $self->item_class) {
if (my $m = $self->item_constructor) {
return $class->$m( $item, $no, $self );
}
return bless $item => $class if ref $item;
return bless \$item => $class;
}
return $self->{items}[ $no ];
}
=head2 raw_item
Return one raw tuple from the iterator by its index
(or croak error if the index is out of range).
In other words, this method ignores B<item_class> and B<item_constructor>.
=cut
sub raw_item {
my ($self, $no) = @_;
my $exists = $self->exists($no);
croak "wrong item number format: " . (defined($no) ? $no : 'undef')
unless defined $exists;
croak 'wrong item number: ' . $no unless $exists;
if ($no >= 0) {
croak "iterator doesn't contain item with number $no"
unless $no < $self->count;
} else {
croak "iterator doesn't contain item with number $no"
unless $no >= -$self->count;
}
return $self->{items}[ $no ];
}
=head2 raw_sort(&)
Sort the contents referred to by the iterator (changes the current
iterator object).
The compare function receives two B<raw> objects:
$iter->raw_sort(sub { $_[0]->field cmp $_[1]->field });
=cut
sub raw_sort {
my ($self, $cb) = @_;
my $items = $self->{items};
@$items = sort { &$cb($a, $b) } @$items;
return $self;
}
=head2 sort(&)
Sort the contents referred to by the iterator (changes the current object).
The compare function receives two constructed objects:
$iter->sort(sub { $_[0]->field <=> $_[1]->field });
=cut
view all matches for this distributionview release on metacpan - search on metacpan
( run in 1.497 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )