LBVXS::_add_inplace() moved to XS.xs
[language-befunge-vector-xs.git] / lib / Language / Befunge / Vector / XS.pm
blob662046f9957242ac11072e69e739b0b97691dfa3
2 # This file is part of Language::Befunge::Vector::XS.
3 # Copyright (c) 2008 Jerome Quelin, all rights reserved.
5 # This program is free software; you can redistribute it and/or modify
6 # it under the same terms as Perl itself.
10 package Language::Befunge::Vector::XS;
12 use strict;
13 use warnings;
15 use overload
16 '=' => \&copy,
17 '+' => \&_add,
18 '-' => \&_substract,
19 'neg' => \&_invert,
20 '+=' => \&_add_inplace,
21 '-=' => \&_substract_inplace,
22 '<=>' => \&_compare,
23 '""' => \&as_string;
25 our $VERSION = '0.0.1';
27 require XSLoader;
28 XSLoader::load('Language::Befunge::Vector::XS', $VERSION);
30 # Preloaded methods go here.
32 sub as_string {
33 my $self = shift;
34 local $" = ',';
35 return "(@$self)";
38 #sub copy { my $vec = shift; return bless [@$vec], ref $vec; }
39 sub get_all_components {$_[0]}
40 sub clear {$_[0]}
41 sub set_component {$_[0]}
42 sub bounds_check {$_[0]}
43 sub _add {$_[0]}
44 sub _substract {$_[0]}
45 sub _invert {$_[0]}
48 __END__
50 =head1 NAME
52 Language::Befunge::Vector::XS - an opaque, N-dimensional vector class.
56 =head1 SYNOPSIS
58 my $v1 = Language::Befunge::Vector::XS->new($x, $y, ...);
59 my $v2 = Language::Befunge::Vector::XS->new_zeroes($dims);
63 =head1 DESCRIPTION
65 This class abstracts normal vector manipulation. It lets you pass
66 around one argument to your functions, rather than N arguments, one
67 per dimension. This means much of your code doesn't have to care
68 how many dimensions you're working with.
70 You can do vector arithmetic, test for equality, or even stringify
71 the vector to a string like I<"(1,2,3)">.
73 It has exactly the same api as C<Language::Befunge::Vector>, but LBVXS
74 is written in XS for speed reasons.
77 =head1 CONSTRUCTORS
79 =head2 my $vec = LBV::XS->new( $x [, $y, ...] )
81 Create a new vector. The arguments are the actual vector data; one
82 integer per dimension.
85 =head2 my $vec = LBV::XS->new_zeroes($dims);
87 Create a new vector of dimension C<$dims>, set to the origin (all zeroes). C<<
88 LBV->new_zeroes(2) >> is exactly equivalent to B<< LBV->new(0,0) >>.
91 =head2 my $vec = $v->copy;
93 Return a new LBV object, which has the same dimensions and coordinates
94 as $v.
98 =head1 PUBLIC METHODS
100 =head2 my $str = $vec->as_string;
102 Return the stringified form of C<$vec>. For instance, a Befunge vector
103 might look like C<(1,2)>.
105 This method is also applied to stringification, ie when one forces
106 string context (C<"$vec">).
109 =head2 my $dims = $vec->get_dims;
111 Return the number of dimensions, an integer.
114 =head2 my $val = $vec->get_component($dim);
116 Get the value for dimension C<$dim>.
119 =head2 my @vals = $vec->get_all_components;
121 Get the values for all dimensions, in order from 0..N.
124 =head2 $vec->clear;
126 Set the vector back to the origin, all 0's.
129 =head2 $vec->set_component($dim, $value);
131 Set the value for dimension C<$dim> to C<$value>.
134 =head2 my $is_within = $vec->bounds_check($begin, $end);
136 Check whether C<$vec> is within the box defined by C<$begin> and C<$end>.
137 Return 1 if vector is contained within the box, and 0 otherwise.
141 =head1 MATHEMATICAL OPERATIONS
143 =head2 Standard operations
145 One can do some maths on the vectors. Addition and substraction work as
146 expected:
148 my $v = $v1 + $v2;
149 my $v = $v1 - $v2;
151 Either operation return a new LBV object, which is the result of C<$v1>
152 plus / minus C<$v2>.
154 The inversion is also supported:
155 my $v2 = -$v1;
157 will subtracts C<$v1> from the origin, and effectively, gives the
158 inverse of the original vector. The new vector is the same distance from
159 the origin, in the opposite direction.
162 =head2 Inplace operations
164 LBV objects also supports inplace mathematical operations:
166 $v1 += $v2;
167 $v1 -= $v2;
169 effectively adds / substracts C<$v2> to / from C<$v1>, and stores the
170 result back into C<$v1>.
173 =head2 Comparison
175 Finally, LBV objects can be tested for equality, ie whether two vectors
176 both point at the same spot.
178 print "same" if $v1 == $v2;
179 print "differ" if $v1 != $v2;
182 =head1 SEE ALSO
184 L<Language::Befunge::Vector>
187 =head1 AUTHOR
189 Jerome Quelin, E<lt>jquelin@cpan.orgE<gt>
191 Development is discussed on E<lt>language-befunge@mongueurs.netE<gt>
194 =head1 COPYRIGHT & LICENSE
196 Copyright (c) 2008 Jerome Quelin, all rights reserved.
198 This program is free software; you can redistribute it and/or modify
199 it under the same terms as Perl itself.
202 =cut