sparse multi-dimensional arrays

Discussion in 'Ruby' started by Bill Birkett, Oct 3, 2006.

  1. Bill Birkett

    Bill Birkett Guest

    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 - <>
    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
    Bill Birkett, Oct 3, 2006
    #1
    1. Advertising

  2. On Tue, Oct 03, 2006 at 10:58:22PM +0900, Bill Birkett wrote:
    > 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]
    Logan Capaldo, Oct 3, 2006
    #2
    1. Advertising

  3. Bill Birkett

    Nathan Grant Guest

    Nathan Grant, Oct 3, 2006
    #3
  4. 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


    Bill Birkett wrote:
    > 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
    Jason Nordwick, Oct 3, 2006
    #4
  5. 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 Grant wrote:
    >> $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/
    >
    Jason Nordwick, Oct 3, 2006
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Alf P. Steinbach
    Replies:
    0
    Views:
    435
    Alf P. Steinbach
    Aug 18, 2003
  2. John Harrison
    Replies:
    4
    Views:
    6,923
    Default User
    Aug 19, 2003
  3. Icosahedron
    Replies:
    8
    Views:
    652
    Vivek
    Aug 21, 2003
  4. Charles Hixson

    Multi-dimensioned sparse array ?

    Charles Hixson, Nov 19, 2003, in forum: Ruby
    Replies:
    2
    Views:
    139
    Charles Hixson
    Nov 19, 2003
  5. Wirianto Djunaidi
    Replies:
    2
    Views:
    200
    Wirianto Djunaidi
    Apr 29, 2008
Loading...

Share This Page