| Author |
Message |
< Erlang ~ Multidimensional Arrays? |
| Tichy |
Posted: Mon Nov 12, 2007 1:16 pm |
|
|
|
Joined: 12 Nov 2007
Posts: 2
|
Hello,
is there an elegant way to deal with multidimensional arrays? I am new to Erlang and still not so used to the "assign once only" variables. It seems to me the way to deal with it with lists is to update the whole list in one recursive operation. But for more dimensions (or more complex data structures) it would probably get really messy, since the [H|T] trick doesn't work? I couldn't yet think of an elegant way to do it for a two-dimensional array.
What I wanted is a kind of cellular automaton, that is every cell is being updated according to the values of it's neighbors (more complicated rules than in a CA, though).
One desperate thought I have in the corner of my mind is to use one process per cell, as presumably at least a process can change it's state without all the other processes having to be changed, too? |
|
|
| Back to top |
|
| a-priori |
Posted: Mon Nov 12, 2007 8:17 pm |
|
|
|
Joined: 12 Nov 2007
Posts: 1
|
| If your simulation's "world" is relatively sparse, you could instead implement it as a list of key-value pairs where the key is the X-Y coordinates and the value is the state of that cell. Erlang's 'lists' module provides the key* functions, such as lists:keysearch/3 and lists:keyreplace/4 for manipulating lists like this. |
|
|
| Back to top |
|
| francesco |
Posted: Tue Nov 13, 2007 7:42 am |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
|
| Back to top |
|
| Tichy |
Posted: Wed Nov 14, 2007 4:02 pm |
|
|
|
Joined: 12 Nov 2007
Posts: 2
|
| Thank you for the replies! |
|
|
| Back to top |
|
| anderst |
Posted: Thu Nov 15, 2007 8:50 am |
|
|
|
User
Joined: 21 Nov 2006
Posts: 37
|
|
| Back to top |
|
| francesco |
Posted: Thu Nov 15, 2007 4:29 pm |
|
|
|
User
Joined: 07 Jul 2006
Posts: 249
Location: London
|
|
| Back to top |
|
| tow21 |
Posted: Sat Mar 22, 2008 6:59 pm |
|
|
|
Joined: 22 Mar 2008
Posts: 1
|
Hello, I just found this thread when looking for info on multidimensional arrays.
Unless I'm completely misreading the documentation, though, the array module in R12 only provides one-dimensional arrays.
a) Am I misreading the docs?
b) If not, is there a well-established way to build something like a multidimensional array so I can avoid reinventing the wheel?
Strictly speaking, I only need 2-D arrays - or rather, I need a data structure that lets me easily express things like: "all the elements between a(5,2) and a(5,7)", or equally, "all the elements between a(2,5) and a(7,5)". Obviously this could be built out of a list of lists, it just seems like overkill. |
|
|
| Back to top |
|
| Bill M. |
Posted: Tue Jun 10, 2008 12:46 am |
|
|
|
User
Joined: 06 Jun 2008
Posts: 24
Location: New York
|
While I haven't done this in Erlang, I've done some of this in C++ (not the array subarray/slicing approach) for large dynamically allocated arrays.
The trick is to create a 2d array interface that uses the one dimensional array as a primitive and use base + offset calculations to index the elements. For slicing you might need to create and return an array. Because you might want to keep bounds and other such information in the array, you may need a programming construct to encapsulate the salient information,
perhaps an Erlang Record (there are no classes, so I'm guessing that is the ticket) The record could have fields like min_row, max_row, min_col, max_col, array_1d.
I'm an Erlang newbie, so a more seasoned Erlang developer might have a better approach, but this is my gut reaction from other experiences.
Regards:
Bill M. |
|
|
| Back to top |
|
|
|