File Coverage

blib/lib/Language/Befunge/Storage/Generic/Sparse.pm
Criterion Covered Total %
statement 41 41 100.0
branch 8 8 100.0
condition n/a
subroutine 10 10 100.0
pod 4 4 100.0
total 63 63 100.0


line stmt bran cond sub pod time code
1             #
2             # This file is part of Language::Befunge.
3             # Copyright (c) 2001-2009 Jerome Quelin, all rights reserved.
4             #
5             # This program is free software; you can redistribute it and/or modify
6             # it under the same terms as Perl itself.
7             #
8             #
9              
10             package Language::Befunge::Storage::Generic::Sparse;
11             require 5.010;
12 3     3   38144 use strict;
  3         8  
  3         111  
13 3     3   16 use warnings;
  3         7  
  3         85  
14 3     3   16 use Carp;
  3         6  
  3         225  
15 3     3   693 use Language::Befunge::Vector;
  3         7  
  3         54  
16 3     3   690 use Language::Befunge::IP;
  3         8  
  3         53  
17 3     3   51 use base 'Language::Befunge::Storage';
  3         7  
  3         1747  
18              
19             # -- CONSTRUCTOR
20              
21              
22             #
23             # new( dimensions )
24             #
25             # Creates a new Lahey Space.
26             #
27             sub new {
28 12     12 1 4361 my $package = shift;
29 12         18 my $dimensions = shift;
30 12         35 my %args = @_;
31 12         121 my $usage = "Usage: $package->new(\$dimensions, Wrapping => \$wrapping)";
32 12 100       46 croak $usage unless defined $dimensions;
33 11 100       45 croak $usage unless $dimensions > 0;
34 10 100       40 croak $usage unless exists $args{Wrapping};
35 9         34 my $self = {
36             nd => $dimensions,
37             wrapping => $args{Wrapping},
38             };
39 9         31 bless $self, $package;
40 9         28 $self->clear();
41 9         35 return $self;
42             }
43              
44              
45             # -- PUBLIC METHODS
46              
47             #
48             # clear( )
49             #
50             # Clear the torus.
51             #
52             sub clear {
53 15     15 1 1318 my $self = shift;
54 15         111 $$self{min} = Language::Befunge::Vector->new_zeroes($$self{nd});
55 15         81 $$self{max} = Language::Befunge::Vector->new_zeroes($$self{nd});
56 15         53 $$self{torus} = {};
57             }
58              
59              
60             #
61             # my $val = get_value( vector )
62             #
63             # Return the number stored in the torus at the specified location. If
64             # the value hasn't yet been set, it defaults to the ordinal value of a
65             # space (ie, #32).
66             #
67             # B As in Funge, code and data share the same playfield, the
68             # number returned can be either an instruction B a data (or even
69             # both... Eh, that's Funge! :o) ).
70             #
71             sub get_value {
72 1372     1372 1 1744 my ($self, $v) = @_;
73 1372         3416 my $str = "$v";
74 1372 100       5362 return 32 unless exists $$self{torus}{$str};
75 568         1666 return $$self{torus}{$str};
76             }
77              
78              
79             #
80             # set_value( vector, value )
81             #
82             # Write the supplied value in the torus at the specified location.
83             #
84             # B As in Funge, code and data share the same playfield, the
85             # number stored can be either an instruction B a data (or even
86             # both... Eh, that's Funge! :o) ).
87             #
88             sub set_value {
89 394     394 1 580 my ($self, $v, $val) = @_;
90              
91             # update min/max
92 394         1146 $self->expand($v);
93 394         1090 my $str = "$v";
94 394         2083 $$self{torus}{$str} = $val;
95             }
96              
97              
98             1;
99             __END__