tags:

views:

429

answers:

4

Is there any standard or "most usual" way to represent multidimensional sparse arrays in Haskell (without sacrificing performance too much)?

Something like map< int, map< int, MyClass> > in C++, for example. I've Googled and found just some old academic papers and other people asking for this too.

Thanks!

+6  A: 

How about a Data.Map (Int,Int) MyClass?

newacct
+3  A: 

There's HsJudy, which seems to be well-tailored for sparse key sets.

Judy bindings (a C library that implements fast sparse dynamic arrays) for Haskell presenting APIs conforming as much as possible to the existent Haskell library interfaces, like Data.Map and Data.Array.MArray. This binding for the Judy library includes all its four types: mapping from words to bits (Judy1), from words to values (JudyL), from strings to values (JudyHS) and from array-of-bytes to values (JudyHS).

But I'd probably go with a Data.Map.Map (Int, Int) MyClass until running into scalability or usability issues.

ephemient
Thanks a lot! (to you and the other poster who suggested Data.Map.I guess I probably will run into scalability problems, but I'll try anyway :-)
Jay
+3  A: 

Data.Map (Int,Int) MyClass is an excellent suggestion; try that first.

If you run into space problems with that, try IntMap (IntMap MyClass). IntMaps (in module Data.IntMap) are Maps with Ints as keys; being specialised they are more efficient than generic maps. It might or might not make a significant difference.

There is also the Scalable, adaptive persistent container types project which might be of use to you. Those containers (including maps) use significantly less space than normal maps but they are slightly more complicated (although still reasonably easy in use).

Martijn
Thank you! This will be really useful for me (I'm working with some numerical algorithms on large but sparse arrays).
Jay