Sparse multi-dimensional arrays

I did like to convert some programs I wrote in Perl to Ruby. I’m
working with sparse three dimensional data. Here’s an example in
Perl. The array @a has just two elements. The rest are undefined.

#!/usr/bin/perl -w

$a[0][0][0] = 0;
$a[100][100][100] = 100;

print “$a[0][0][0]\n”;
print “$a[100][100][100]\n”;

The output:

0
100

Is there an easy way to implement this sort of data structure in
Ruby? The Array class seems limited to one dimension. I looked at the
Matrix class, but there doesn’t appear to be any way of assigning
values to the individual matrix elements (which seems pretty strange,
so maybe I’m overlooking the obvious).

-Bill

William B. Birkett - [email protected]
Print Quality Consultant
Doppelganger, LLC - http://www.doplganger.com/

48799 Meadow Drive, Plymouth, MI 48170 (USA)
Office: (734) 927-4232 FAX: (734) 468-0580
Cell: (734) 516-4790

On Tue, Oct 03, 2006 at 10:58:22PM +0900, Bill Birkett wrote:

print “$a[100][100][100]\n”;
so maybe I’m overlooking the obvious).

It’s pretty easy if you know the dimensions before hand, a bit more
hackery pokery if you don’t.
a = Array.new(101) { Array.new(101) { Array.new(101) } }
a[0][0][0] = 0
a[100][100][100] = 100

puts a[0][0][0]
puts a[100][100][100]

Bill Birkett wrote:

print “$a[100][100][100]\n”;
so maybe I’m overlooking the obvious).
The solution depends on what you are trying to accomplish. If your aim
is to
minimize storage space, then use a nested hash, with numeric indices as
keys. If your aim is simply to create an n-dimensional array and you
don’t
care about storage size, then follow the advice of another poster:

#!/usr/bin/ruby -w

dim_size = 100

arr = Array.new(dim_size) { Array.new(dim_size) { Array.new(dim_size,42)
} }

puts arr[39][56][24]

42

Close:

a={}
a[[0,0,0]], a[[100,100,100]] = 0, 100
puts “#{a[[0,0,0]]}”
puts “#{a[[100,100,100]]}”

But you might like this better (my Ruby isn’t very good, so please
excuse me if I overcomplicate these definitions):

class SparseMatrix < Hash
alias :oldget :[]
alias :oldset :[]=
def; self.oldget(i); end
def[]=(*i); v=i.pop; self.oldset(i,v); end
end

a = SparseMatrix.new
a[0,0,0], a[100,100,100] = 0, 100

irb(main):017:0> a
=> {[0, 0, 0]=>0, [100, 100, 100]=>100}
irb(main):018:0> puts “#{a[0,0,0]}”
0
=> nil
irb(main):019:0> puts “#{a[100,100,100]}”
100
=> nil
irb(main):020:0> puts “#{a[2,2,2]}”

=> nil
irb(main):021:0>

-j

$a[0][0][0] = 0;
$a[100][100][100] = 100;

print “$a[0][0][0]\n”;
print “$a[100][100][100]\n”;

The output:

0
100

I saw this on inquirylabs.com recently [1]

hsh = Hash.new(&(p=lambda{|h,k| h[k] = Hash.new(&p)}))

hsh[0][0][0] = 0 >> 0
hsh[100][100][100] = 100 >> 100

HTH

Nathan

[1]
http://blog.inquirylabs.com/2006/09/20/ruby-hashes-of-arbitrary-depth/

irb(main):005:0> hsh[1]
=> {}
irb(main):006:0> hsh[2]
=> {}
irb(main):007:0> hsh[3]
=> {}
irb(main):008:0> hsh
=> {1=>{}, 2=>{}, 3=>{}}

ugh.

-j

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs