Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace all Tie::IxHash w/ Hash::Ordered #21

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion cpanfile
Expand Up @@ -16,6 +16,6 @@ requires 'Storable';
requires 'strict';
requires 'Test::Most';
requires 'Text::Table::Tiny';
requires 'Tie::IxHash';
requires 'Hash::Ordered';
requires 'Try::Tiny';
requires 'warnings';
23 changes: 11 additions & 12 deletions lib/Data/Frame.pm
Expand Up @@ -4,8 +4,8 @@ package Data::Frame;
use strict;
use warnings;

use Tie::IxHash;
use Tie::IxHash::Extension;
use Hash::Ordered;
use Hash::Ordered::Extension;
use PDL::Lite;
use Data::Perl ();
use List::AllUtils;
Expand Down Expand Up @@ -37,7 +37,7 @@ with 'MooX::Traits';

sub _trait_namespace { 'Data::Frame::Role' } # override for MooX::Traits

has _columns => ( is => 'ro', default => sub { Tie::IxHash->new; } );
has _columns => ( is => 'ro', default => sub { Hash::Ordered->new; } );

has _row_names => ( is => 'rw', predicate => 1 );

Expand Down Expand Up @@ -128,7 +128,7 @@ Returns the count of the number of columns in the C<Data::Frame>.
=cut
sub number_of_columns {
my ($self) = @_;
$self->_columns->Length;
scalar $self->_columns->keys;
}

=method number_of_rows
Expand Down Expand Up @@ -160,7 +160,7 @@ sub nth_column {
confess "requires index" unless defined $index;
confess "column index out of bounds" if $index >= $self->number_of_columns;
# fine if $index < 0 because negative indices are supported
$self->_columns->Values( $index );
( $self->_columns->values )[ $index ];
}

=method column_names
Expand All @@ -182,10 +182,10 @@ sub column_names {
try {
$self->_columns->RenameKeys( @colnames );
} catch {
confess "incorrect number of column names" if /@{[ Tie::IxHash::ERROR_KEY_LENGTH_MISMATCH ]}/;
confess "incorrect number of column names" if /@{[ Hash::Ordered::ERROR_KEY_LENGTH_MISMATCH ]}/;
};
}
[ $self->_columns->Keys ];
[ $self->_columns->keys ];
}

=method row_names
Expand Down Expand Up @@ -253,8 +253,8 @@ Returns the column with the name C<$column_name>.
=cut
sub column {
my ($self, $colname) = @_;
confess "column $colname does not exist" unless $self->_columns->EXISTS( $colname );
$self->_columns->FETCH( $colname );
confess "column $colname does not exist" unless $self->_columns->exists( $colname );
$self->_columns->get( $colname );
}

sub _column_validate {
Expand Down Expand Up @@ -297,15 +297,14 @@ C<$data>.
sub add_column {
my ($self, $name, $data) = @_;
confess "column $name already exists"
if $self->_columns->EXISTS( $name );
if $self->_columns->exists( $name );

# TODO apply column role to data
$data = PDL::SV->new( $data ) if ref $data eq 'ARRAY';

$self->_column_validate( $name => $data);


$self->_columns->Push( $name => $data );
$self->_columns->set( $name => $data );
}

=method select_rows
Expand Down
11 changes: 6 additions & 5 deletions lib/Tie/IxHash/Extension.pm → lib/Hash/Ordered/Extension.pm
@@ -1,20 +1,21 @@
package Tie::IxHash::Extension;
package Hash::Ordered::Extension;

use strict;
use warnings;
use List::AllUtils;

{
package Tie::IxHash;
package Hash::Ordered;

use constant ERROR_KEY_LENGTH_MISMATCH => "incorrect number of keys";

sub RenameKeys {
my ($self, @names) = @_;
die ERROR_KEY_LENGTH_MISMATCH if @names != $self->Length;
my @values = $self->Values;
die ERROR_KEY_LENGTH_MISMATCH if @names != $self->keys;
my @values = $self->values;
my @new_kv = List::AllUtils::mesh( @names, @values );
$self->Splice(0, $self->Length, @new_kv);
$self->clear;
$self->push(@new_kv);
}


Expand Down
24 changes: 16 additions & 8 deletions lib/PDL/Factor.pm
Expand Up @@ -5,11 +5,12 @@ use warnings;

use Moo;
use PDL::Lite;
use Tie::IxHash;
use Tie::IxHash::Extension;
use Hash::Ordered;
use Hash::Ordered::Extension;
use Data::Rmap qw(rmap);
use Storable qw(dclone);
use Scalar::Util qw(blessed);
use List::AllUtils qw( first_index );
use Test::Deep::NoTest qw(eq_deeply);

extends 'PDL';
Expand Down Expand Up @@ -45,20 +46,27 @@ around new => sub {
}
my %opt = @args;

my $levels = Tie::IxHash->new;
my $levels = Hash::Ordered->new;
my $enum = $opt{integer} // dclone($data);
if( exists $opt{levels} ) {
# add the levels first if given levels option
for my $l (@{ $opt{levels} } ) {
$levels->Push( $l => 1 );
$levels->set( $l => 1 );
}
# TODO what if the levels passed in are not unique?
# TODO what if the integer enum data outside the range of level indices?
} else {
rmap {
my $v = $_;
$levels->Push($v => 1); # add value to hash if it doesn't exist
$_ = $levels->Indices($v); # assign index of level
$levels->set($v => 1); # add value to hash if it doesn't exist

# Ugly replacement for first_index
my ( $i, $idx ) = ( 0, 0 );
for my $key ( $levels->keys ) {
$idx = $i if $key eq $v;
$i++;
}
$_ = $idx;
} $enum;
}

Expand Down Expand Up @@ -86,7 +94,7 @@ around string => sub {
my ($self, %opt) = @_;
my $ret = $orig->(@_);
if( exists $opt{with_levels} ) {
my @level_string = grep { defined } $self->{_levels}->Keys();
my @level_string = grep { defined } $self->{_levels}->keys();
$ret .= "\n";
$ret .= "Levels: @level_string";
}
Expand Down Expand Up @@ -118,7 +126,7 @@ sub equal {
}
} else {
# TODO hacky. need to test this more
my $key_idx = $self->_levels->Indices($other);
my $key_idx = first_index { $_ == $other } $self->_levels->values;
return $self->{PDL} == $key_idx;
}
}
Expand Down
14 changes: 7 additions & 7 deletions lib/PDL/Role/Enumerable.pm
Expand Up @@ -3,15 +3,15 @@ package PDL::Role::Enumerable;
use strict;
use warnings;

use Tie::IxHash;
use Tie::IxHash::Extension;
use Hash::Ordered;
use Hash::Ordered::Extension;
use Moo::Role;
use Try::Tiny;
use List::AllUtils ();

with qw(PDL::Role::Stringifiable);

has _levels => ( is => 'rw', default => sub { Tie::IxHash->new; } );
has _levels => ( is => 'rw', default => sub { Hash::Ordered->new; } );

sub element_stringify_max_width {
my ($self, $element) = @_;
Expand All @@ -23,12 +23,12 @@ sub element_stringify_max_width {

sub element_stringify {
my ($self, $element) = @_;
( $self->_levels->Keys )[ $element ];
( $self->_levels->keys )[ $element ];
}

sub number_of_levels {
my ($self) = @_;
$self->_levels->Length;
scalar $self->_levels->keys;
}

sub levels {
Expand All @@ -37,10 +37,10 @@ sub levels {
try {
$self->_levels->RenameKeys( @levels );
} catch {
die "incorrect number of levels" if /@{[ Tie::IxHash::ERROR_KEY_LENGTH_MISMATCH ]}/;
die "incorrect number of levels" if /@{[ Hash::Ordered::ERROR_KEY_LENGTH_MISMATCH ]}/;
};
}
[ $self->_levels->Keys ];
[ $self->_levels->keys ];
}

around qw(slice uniq dice) => sub {
Expand Down