Language::Befunge::Storage::Generic::Sparse - a generic N-dimensional LaheySpace.


Language-Befunge documentation Contained in the Language-Befunge distribution.

Index


Code Index:

NAME

Top

Language::Befunge::Storage::Generic::Sparse - a generic N-dimensional LaheySpace.

SYNOPSIS

Top

    # create a 3-dimensional LaheySpace.
    my $torus = Language::Befunge::Storage::Generic::Sparse->new(3);
    $torus->clear();
    $torus->store(<<"EOF");
    12345
    67890
    \fabcde
    fghij
    EOF

Note you usually don't need to use this module directly. Language::Befunge::Interpreter can optionally use it.

DESCRIPTION

Top

This module implements an N-dimensional storage space, as a sparse hash. The values in the hash are keyed by coordinate strings, as created by stringifying a Vector object.

CONSTRUCTOR

Top

new( dimensions )

Creates a new Lahey Space.

PUBLIC METHODS

Top

clear( )

Clear the torus.

get_value( vector )

Return the number stored in the torus at the specified location. If the value hasn't yet been set, it defaults to the ordinal value of a space (ie, #32).

/!\ As in Funge, code and data share the same playfield, the number returned can be either an instruction or a data (or even both... Eh, that's Funge! :o) ).

set_value( vector, value )

Write the supplied value in the torus at the specified location.

/!\ As in Funge, code and data share the same playfield, the number stored can be either an instruction or a data (or even both... Eh, that's Funge! :o) ).

EXTERNAL METHODS

Top

Several methods are inherited from the Language::Befunge::Storage base class. These methods are:

    store
    store_binary
    get_char
    get_dims
    rectangle
    expand
    min
    max
    labels_lookup
    _labels_try

Please see the documentation of that module for more information.

BUGS

Top

None known. Please inform me if you find one.

SEE ALSO

Top

<Language::Befunge::Storage>, Language::Befunge::Storage::2D::Sparse, Language::Befunge.

AUTHOR

Top

Mark Glines, <infinoid@cpan.org> Jerome Quelin, <jquelin@cpan.org>

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

COPYRIGHT & LICENSE

Top


Language-Befunge documentation Contained in the Language-Befunge distribution.

#
# This file is part of Language::Befunge.
# 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.
#
#

package Language::Befunge::Storage::Generic::Sparse;
require 5.010;
use strict;
use warnings;
use Carp;
use Language::Befunge::Vector;
use Language::Befunge::IP;
use base 'Language::Befunge::Storage';

# -- CONSTRUCTOR


#
# new( dimensions )
#
# Creates a new Lahey Space.
#
sub new {
    my $package = shift;
    my $dimensions = shift;
    my %args = @_;
    my $usage = "Usage: $package->new(\$dimensions, Wrapping => \$wrapping)";
    croak $usage unless defined $dimensions;
    croak $usage unless $dimensions > 0;
    croak $usage unless exists $args{Wrapping};
    my $self  = {
        nd  => $dimensions,
        wrapping => $args{Wrapping},
    };
    bless $self, $package;
    $self->clear();
    return $self;
}


# -- PUBLIC METHODS

#
# clear(  )
#
# Clear the torus.
#
sub clear {
    my $self = shift;
    $$self{min} = Language::Befunge::Vector->new_zeroes($$self{nd});
    $$self{max} = Language::Befunge::Vector->new_zeroes($$self{nd});
    $$self{torus} = {};
}


#
# my $val = get_value( vector )
#
# Return the number stored in the torus at the specified location. If
# the value hasn't yet been set, it defaults to the ordinal value of a
# space (ie, #32).
#
# B</!\> As in Funge, code and data share the same playfield, the
# number returned can be either an instruction B<or> a data (or even
# both... Eh, that's Funge! :o) ).
#
sub get_value {
    my ($self, $v) = @_;
    my $str = "$v";
    return 32 unless exists $$self{torus}{$str};
    return $$self{torus}{$str};
}


#
# set_value( vector, value )
#
# Write the supplied value in the torus at the specified location.
#
# B</!\> As in Funge, code and data share the same playfield, the
# number stored can be either an instruction B<or> a data (or even
# both... Eh, that's Funge! :o) ).
#
sub set_value {
    my ($self, $v, $val) = @_;

    # update min/max
    $self->expand($v);
    my $str = "$v";
    $$self{torus}{$str} = $val;
}


1;
__END__