############ 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(); }
In reply to VectorSpace by smalhotra
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |