Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

VectorSpace

by smalhotra (Scribe)
on Feb 25, 2005 at 15:47 UTC ( [id://434526]=sourcecode: print w/replies, xml ) Need Help??
Category: Utility Scripts
Author/Contact Info
Description: I needed something to iterate over every combination of a set of points, essentially to traverse a vector space. Since I couldn't find anything readily, I decided to roll my own. It's posted here in case anyone needs to do the same (or when I need to recall it). I didn't post the base class, Object but it is essentially soomething that supports new(), set(), and get() methods.
############ VectorSpace Utility #######################
package VectorSpace;
use Object;
@VectorSpace::ISA = qw(Object);
sub init {
    my ($self) = @_;
    $self->set(space => []);
    return $self;
}
sub add_dimension {
    my ($self, $dim_name, $data) = @_;
    my $space = $self->get('space');
    my $count = ref $data ? $#$data + 1 : $data;
    my $dimension = { name => $dim_name,
                      count => $count,
                      data => $data };
    push(@$space, $dimension);
    return $dimension;
}

sub initialize_iterator {
    my ($self) = @_;
    my $point = [];
    for (@{$self->get('space')}) {
        push (@$point, 0);
    }
    $self->set( position => 0 );
    $self->set( point => $point );
}
sub point {
    my ($self) = @_;
    my $point = $self->get('point');
    my $space = $self->get('space');
    my @res;
    for (0..$#$point) {
        push(@res, $space->[$_]->{data}->[$point->[$_]]);
    }
    return \@res;
}
sub increment_iterator {
    my ($self) = @_;
    my $point = $self->get('point');
    my $pos = $self->get('position');
    my $space = $self->get('space');

    return $self->_next_point($pos);
}
sub _next_point {
    my ($self, $pos) = @_;
    my $point = $self->get('point');
    my $space = $self->get('space');

    if ($pos > $#$point) {
        ## end of iteration
        return;
    }
    $point->[$pos]++;
    if ($point->[$pos] >= $space->[$pos]->{count}) {
        $point->[$pos] = 0;
        return $self->_next_point($pos+1);
    }
    return 1;
}
##################  End VectorSpace Utility #############
## Sample Usage ##
    use Data::Dumper;
    my $vec = VectorSpace->new( );
    $vec->add_dimension( length => [1..3] );
    $vec->add_dimension( height => [1..3] );
    print Dumper $vec;
    $vec->initialize_iterator();
    my $c;
    while (1) {
        my $data = $vec->point();
        print Dumper $data;
        last if ! $vec->increment_iterator();
    }
Replies are listed 'Best First'.
Re: VectorSpace
by moot (Chaplain) on Feb 26, 2005 at 04:11 UTC
    I'm not fully sure what your code is doing, but you might want to look into Class::Accessor and Class::MethodMaker for your base class, and perhaps overload for some of those foo_iterator and add_foo methods.
      All, My sys admin is reinstalling perl modules after aix 5.2 upgrade and he gets this error for the IO module. Can some one please help.. cc_r -c -D_ALL_SOURCE -D_ANSI_C_SOURCE -D_POSIX_SOURCE -qmaxmem=16384 -qnoansialias -DUSE_NATIVE_DLOPEN -DNEED_PTHREAD_INIT -q32 -D_LARGE_FILES -qlonglong -O -DVERSION=\"1.20\" -DXS_VERSION=\"1.20\" "-I/usr/opt/perl5/lib/5.8.0/aix-thread-multi/CORE" -DI_POLL IO.c 1506-507 (W) No licenses available. Contact your program supplier to add additional users. Compilation will proceed shortly. "/usr/opt/perl5/lib/5.8.0/aix-thread-multi/CORE/config.h", line 2820.9: 1506-236 (W) Macro name I_POLL has been redefined. "IO.xs", line 201.35: 1506-280 (W) Function argument assignment between types "struct sv*" and "long long*" is not allowed. "IO.xs", line 208.22: 1506-045 (S) Undeclared identifier sv_undef. "IO.xs", line 219.44: 1506-280 (W) Function argument assignment between types "struct sv*" and "long long*" is not allowed. "IO.xs", line 252.22: 1506-045 (S) Undeclared identifier sv_undef. make: 1254-004 The error code from the last command is 1. Stop. c1e0n143:/mnt/perlModules/IO-1.20#

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: sourcecode [id://434526]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2024-04-23 08:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found