Jerome Quelin >
Language-Befunge >
Language::Befunge::Vector

Language::Befunge::Vector - an opaque, N-dimensional vector class.

my $v1 = Language::Befunge::Vector->new($x, $y, ...); my $v2 = Language::Befunge::Vector->new_zeroes($dims);

This class abstracts normal vector manipulation. It lets you pass around one argument to your functions, rather than N arguments, one per dimension. This means much of your code doesn't have to care how many dimensions you're working with.

You can do vector arithmetic, test for equality, or even stringify the vector to a string like *"(1,2,3)"*.

Create a new vector. The arguments are the actual vector data; one integer per dimension.

Create a new vector of dimension `$dims`

, set to the origin (all zeroes). `LBV->new_zeroes(2)`

is exactly equivalent to **LBV->new(0,0)**.

Return a new LBV object, which has the same dimensions and coordinates as $v.

Return the stringified form of `$vec`

. For instance, a Befunge vector might look like `(1,2)`

.

This method is also applied to stringification, ie when one forces string context (`"$vec"`

).

Return the number of dimensions, an integer.

Get the value for dimension `$dim`

.

Get the values for all dimensions, in order from 0..N.

Set the vector back to the origin, all 0's.

Set the value for dimension `$dim`

to `$value`

.

Check whether `$vec`

is within the box defined by `$begin`

and `$end`

. Return 1 if vector is contained within the box, and 0 otherwise.

Return the next vector in raster order, or undef if the hypercube space has been fully covered.

To enumerate the entire storage area, the caller should call rasterize on the storage area's "min" value the first time, and keep looping while the return value is defined. To enumerate a smaller rectangle, the caller should pass in the min and max vectors describing the rectangle, and keep looping while the return value is defined.

One can do some maths on the vectors. Addition and substraction work as expected:

my $v = $v1 + $v2; my $v = $v1 - $v2;

Either operation return a new LBV object, which is the result of `$v1`

plus / minus `$v2`

.

The inversion is also supported: my $v2 = -$v1;

will subtracts `$v1`

from the origin, and effectively, gives the inverse of the original vector. The new vector is the same distance from the origin, in the opposite direction.

LBV objects also supports inplace mathematical operations:

$v1 += $v2; $v1 -= $v2;

effectively adds / substracts `$v2`

to / from `$v1`

, and stores the result back into `$v1`

.

Finally, LBV objects can be tested for equality, ie whether two vectors both point at the same spot.

print "same" if $v1 == $v2; print "differ" if $v1 != $v2;

my $ptr = $v1->_xs_rasterize_ptr();

Get a pointer to the C "rasterize" function. Returns undef if LBVXS is not loaded. This is useful for external XS modules, to allow them to call the C function directly for additional speed.

The prototype of the C rasterize function is:

AV *rasterize(AV *vec_array, AV *min_array, AV *max_array);

It operates just like the perl rasterize function, and returns NULL when the end of the loop has been reached.

Jerome Quelin, <jquelin@cpan.org>

Development is discussed on <language-befunge@mongueurs.net>

Copyright (c) 2001-2009 Jerome Quelin, all rights reserved.

This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

syntax highlighting: