NAME
Language::Befunge::Vector::XS - an opaque, N-dimensional vector class.
SYNOPSIS
my $v1 = Language::Befunge::Vector::XS->new($x, $y, ...);
my $v2 = Language::Befunge::Vector::XS->new_zeroes($dims);
DESCRIPTION
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)".
It has exactly the same api as Language::Befunge::Vector
, but LBVXS is written in XS for speed reasons.
CONSTRUCTORS
my $vec = LBV::XS->new( $x [, $y, ...] )
Create a new vector. The arguments are the actual vector data; one integer per dimension.
my $vec = LBV::XS->new_zeroes($dims);
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).
my $vec = $v->copy;
Return a new LBV object, which has the same dimensions and coordinates as $v.
PUBLIC METHODS
my $str = $vec->as_string;
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"
).
my $dims = $vec->get_dims;
Return the number of dimensions, an integer.
my $val = $vec->get_component($dim);
Get the value for dimension $dim
.
my @vals = $vec->get_all_components;
Get the values for all dimensions, in order from 0..N.
$vec->clear;
Set the vector back to the origin, all 0's.
$vec->set_component($dim, $value);
Set the value for dimension $dim
to $value
.
my $is_within = $vec->bounds_check($begin, $end);
Check whether $vec
is within the box defined by $begin
and $end
. Return 1 if vector is contained within the box, and 0 otherwise.
MATHEMATICAL OPERATIONS
Standard operations
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.
Inplace operations
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
.
Comparison
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;
SEE ALSO
AUTHOR
Jerome Quelin, <jquelin@cpan.org>
Development is discussed on <language-befunge@mongueurs.net>
COPYRIGHT & LICENSE
Copyright (c) 2008 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.