Re: How to tie



Patricia Shanahan wrote:
I think your best approach is going to be to write an interface
representing your sparse arrays. For example, if they are two
dimensional doubles:

public interface SparseArray{
double get(long i, long j);
void set(double val, long i, long j);
}

and then write various implementations of the interface. The interface
may need additional methods for constructing views, such as rows,
columns, subblocks etc.

Most of the code would work in terms of the interface.

Unfortunately, Java lacks the operator overloading that would let you
represent get and set using array notation. That lack gets in the way of
easy translation from e.g. subscripted matrix notation to code.

One possible implementation of SparseArray is to use a
Map <Pair <Integer, Integer>, T>.

You'll need to write your own Pair <U, U>, or you could use java.awt.Dimension if you're willing to settle for int indexes instead of long.

====
package example;
public interface SparseArray <X, T>
{
T get( X i, X j );
void set( T val, X i, X j );
}

====
package example;
import java.awt.Dimension;
public class Sparse2D <T> implements SparseArray <Integer, T>
{
private final Map <Dimension, T> internal = new HashMap <Dimension, T> ();

public T get( Integer i, Integer j )
{
return internal.get( new Dimension( j, i ));
}
public void set( T val, Integer i, Integer j )
{
internal.put( new Dimension( j, i ), val );
}
}

====

Substitute 'new Pair <Long, Long> ( i, j )' for the Dimension constructions to implement a SparseArray<Long, T>.

I reverse i and j in the Dimension constructors because of the documented interpretation of the 'width' and 'height' members of Dimension. This wouldn't apply to Pair.

--
Lew
.



Relevant Pages

  • Re: char (*ptr)[]
    ... > Does your compiler accept this declaration? ... > dimension is required. ... void f) ...
    (comp.lang.c)
  • Kants thought experiment : is chirality a dimension?
    ... a universe containing only of ONE hand ... and otherwise void. ... Since there is no intrinsic difference in shape ... considered to be an "extra" dimension in its own right in which case ...
    (sci.physics)
  • Re: For crying out loud...BRING BACK THE DALEKS AND GALLIFREY!!!!!
    ... I didn't think the Daleks were from another dimension. ... had avoided the destruction of the Daleks in the Time War by escaping ... to the void between dimensions in their spherical craft, ...
    (rec.arts.drwho)
  • Re: Doomsday Paradox
    ... All the daleks and Cybermen that got 'sucked' into the portal were the ones ... that had 'come through' from the void... ... If the void has no time, dimension ... then they could be frozen in time floating around in that ...
    (rec.arts.drwho)