Statistics-RserveClient

 view release on metacpan or  search on metacpan

lib/Statistics/RserveClient/REXP/List.pm  view on Meta::CPAN


our $VERSION = '0.12'; #VERSION

our @ISA = qw(Statistics::RserveClient::REXP::Vector);

my $names    = ();               # protected
my $is_named = FALSE;    # protected

#sub setValues($values, $getNames = FALSE) {
sub setValues(@) {
    my $self     = shift;
    my $values   = shift;
    my $getNames = shift;
    if ( !defined($getNames) ) {
        $getNames = FALSE;
    }

    my $names = undef;
    if ($getNames) {
        $names = array_keys($values);
    }
    $values = array_values($values);
    parent::setValues($values);
    if ($names) {
        $self->setNames($names);
    }
}

#  * Set names
#  * @param unknown_type $names

sub setNames($$) {
    my $self  = shift;
    my $names = shift;
    if ( count( $self->values ) != count($names) ) {
        #throw new LengthException('Invalid names length');
        die(      "Invalid names length: "
                . count( $self->values ) . " != "
                . count($names) );
    }
    $self::names    = $names;
    $self::is_named = TRUE;
}

# * return array list of names
sub getNames($) {
    my $self = shift;
    return ( $self->is_named ) ? $self->names : array();
}

# * return TRUE if the list is named

sub isNamed() {
    my $self = shift;
    return $self->is_named;
}

# * Get the value for a given name entry, if list is not named, get the indexed element
# * @param string $name

sub at($) {
    my $self = shift;
    my $name = shift;

    if ( $self->is_named ) {
        my $i = array_search( $name, $self->names );
        if ( $i < 0 ) {
            return undef;
        }
        return $self::values[$i];
    }
}

# * Return element at the index $i
# * @param int $i
# * @return mixed Statistics::RserveClient::REXP or native value

sub atIndex($) {
    my $self = shift;
    my $i    = shift;
    $i = 0 + $i;
    my $n = count($self::values);
    if ( ( $i < 0 ) || ( $i >= $n ) ) {
        #throw new OutOfBoundsException('Invalid index');
        die("Index out of bounds: i = $i\n");
    }
    return $self::values[$i];
}

sub isList() { return TRUE; }

sub offsetExists($) {
    my $self   = shift;
    my $offset = shift;
    if ( $self->is_named ) {
        return array_search( $offset, $self->names ) >= 0;
    }
    else {
        return isset( $self::names[$offset] );
    }
}

sub offsetGet($) {
    my $self   = shift;
    my $offset = shift;
    return $self->at($offset);
}

sub offsetSet($$) {
    my $self   = shift;
    my $offset = shift;
    my $value  = shift;
    # throw new Exception('assign not implemented');
    die("Assign not implemented.\n");
}

sub offsetUnset($) {
    my $self   = shift;
    my $offset = shift;
    #throw new Exception('unset not implemented');
    die("Unset not implemented.\n");



( run in 1.162 second using v1.01-cache-2.11-cpan-524268b4103 )