From: Mathieu Bouchard Date: 2006-03-14T07:27:15+09:00 Subject: Re: how to introduce reference objects into ruby On Tue, 14 Mar 2006, Geert Fannes wrote: > and if it is choosen to be smaller to have a better fit, this becomes > virtually the same as storing each element separately. But I could be > wrong since I never tried this. Have you experience with this? Ok, I haven't tried it, and doing it efficiently enough might mean using a clustering algorithm that will pack similar columns and rows closer together so that they take less space. If you don't do it, the method of storing a pair of hashes of hashes is more efficient. There are questions of graph density that I haven't investigated (what's the expectancy of space taken by the data given a certain probability that a certain pair of vertices has an edge between them...). BTW, if you make your matrix element assignment operator do two assignments (one to the row-major representation and one to the column-major representation), then you might not need references. In Ruby, there's the []= and [] operators which you can define together. The []= can play both the role of subscripting and assignment without needing any intermediate references. Also, Ruby's subscript operator can take any number of arguments. The right-handed value is always the last in the list of args. class MyMatrix ... def []=(i,j,value) @rows[i][j]=value @columns[i][j]=value end end mm=MyMatrix.new mm[3,5] = 42 _ _ __ ___ _____ ________ _____________ _____________________ ... | Mathieu Bouchard - t�l:+1.514.383.3801 - http://artengine.ca/matju | Freelance Digital Arts Engineer, Montr�al QC Canada