From 0a1f26b15c1e76a9336014a520bb5aea38521b71 Mon Sep 17 00:00:00 2001 From: stphnlyd Date: Fri, 21 Jun 2019 00:09:21 +0800 Subject: [PATCH] github #5 : add a PDL::SV::uniqind() method --- Changes | 1 + lib/PDL/SV.pm | 20 +++++++++++++++++--- t/11-pdl_sv.t | 7 +++++-- 3 files changed, 23 insertions(+), 5 deletions(-) diff --git a/Changes b/Changes index 450d070..3c2fa05 100644 --- a/Changes +++ b/Changes @@ -2,6 +2,7 @@ Revision history of Alt-Data-Frame-ButMore 0.0052_01 TBD - Add a Data::Frame::drop_bad() method. + - Add a PDL::SV::uniqind() method. - Add a "faithfuld" example. 0.0051 2019-06-01 diff --git a/lib/PDL/SV.pm b/lib/PDL/SV.pm index e346977..5086c66 100644 --- a/lib/PDL/SV.pm +++ b/lib/PDL/SV.pm @@ -194,16 +194,30 @@ sub glue { BAD values are not considered unique and are ignored. +=head2 uniqind() + +Return the indices of all uniq elements of a piddle. + =cut sub uniq { - my $self = shift; + my ($self) = @_; my $class = ref($self); my @uniq = List::AllUtils::uniq( grep { defined $_ } @{ $self->_effective_internal } ); - my $new = $class->new( \@uniq ); - return $new; + return $class->new( \@uniq ); +} + +## Please see file perltidy.ERR +sub uniqind { + my ($self) = @_; + + my $effective_internal = $self->_effective_internal; + my @uniqind = List::AllUtils::uniq_by { $effective_internal->[$_] } + grep { defined $effective_internal->[$_] } + ( 0 .. $#$effective_internal ); + return pdl( \@uniqind ); } =head2 sever diff --git a/t/11-pdl_sv.t b/t/11-pdl_sv.t index ebb804f..f4ec942 100644 --- a/t/11-pdl_sv.t +++ b/t/11-pdl_sv.t @@ -47,8 +47,11 @@ subtest at => sub { }; subtest uniq => sub { - my $p1 = PDL::SV->new( [qw(foo bar baz foo bar)] )->setbadat(1); - pdl_is( $p1->uniq, PDL::SV->new( [qw(foo baz bar)] ), 'uniq' ); + my $p1 = + PDL::SV->new( [qw(foo bar baz foo bar)] )->setbadat(1)->setbadat(2); + pdl_is( $p1->uniq, PDL::SV->new( [qw(foo bar)] ), 'uniq' ); + + pdl_is( $p1->uniqind, pdl( [ 0, 4 ] ), 'uniqind' ); }; subtest sever => sub {