sparse multi-dimensional arrays

B

Bill Birkett

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
 
L

Logan Capaldo

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).
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]
 
J

Jason Nordwick

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 :eek:ldget :[]
alias :eek:ldset :[]=
def[](*i); 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
 
J

Jason Nordwick

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


Nathan said:
$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/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top