Multi dimensional arrays in Propeller?
Jimmy W.
Posts: 112
OK, I swear I have looked!
I am looking to define a multi dimensional array and the compiler expects an end line after the first endbracket, is there any way to create a multi dimensional array in spin?
· long OWControllerDev[noparse][[/noparse]16][noparse][[/noparse]4]
· long OWTempDev[noparse][[/noparse]16][noparse][[/noparse]2][noparse][[/noparse]4]
OR, define a bunch of vars?
· long OWControllerDev1[noparse][[/noparse]4]...long OWControllerDev16[noparse][[/noparse]4]
I am hoping there is some way for the former and not to have to do the latter!
I am looking to define a multi dimensional array and the compiler expects an end line after the first endbracket, is there any way to create a multi dimensional array in spin?
· long OWControllerDev[noparse][[/noparse]16][noparse][[/noparse]4]
· long OWTempDev[noparse][[/noparse]16][noparse][[/noparse]2][noparse][[/noparse]4]
OR, define a bunch of vars?
· long OWControllerDev1[noparse][[/noparse]4]...long OWControllerDev16[noparse][[/noparse]4]
I am hoping there is some way for the former and not to have to do the latter!
Comments
SPIN has no "data structures"
Heres to declaring 472 variables woo!
EDIT: Nothing happened here......
Posted from my blackberry on the drive home and when I got home and re-read it. Hope no one saw it.
EDIT 2: looks like·I was too late for the ever·present and quick responding deSilva
PS: deSilva·you rock [noparse]:)[/noparse]
Post Edited (Jimmy W.) : 12/26/2007 4:40:06 AM GMT
(a) Also: No
(b) There is no use of guessing. I think there will be no other way but you thoroughly read through chapter 4 of the Manual. You will discover many extremely usefull things. The general rule is: There is a rich control structure and many - also unusual - operators; however there is no data structure (only the concept of contiguous memory cells of either 1, 2 or 4 bytes each), and there is a sometimes tricky memory allocation concept, split into DAT and VAR sections.
Have fun!
For example, to create a 3 x 4 array, you do a "long array[3*4]" in the VAR section.
To access element <i,j>, you use "array[ i*3+j ]" where i ranges from 0 to 2 and j ranges from 0 to 3.
Post Edited (Mike Green) : 12/26/2007 7:24:06 AM GMT
- Row Major Order
- Column Major Order
array[i*n+j] or array[i+j*n]
The alternative is an "array" of addresses to other "arrays", but this takes up far to much memory overhead for most uses. The advantage of this is that you can create dynamic arrays and/or linked lists. I suppose if it weren't something time critical you could implement this using memory off the propeller chip...
More interesting, an easy way to implement a balanced binary tree:
don't use (or at least as part of the tree) the first element of the array. Then for each node, the children of the node are array[index*2] and array[index*2+1]. So array[0] isn't part of the tree (use it for something else like the count of used nodes or something), the root is array. The children of the root (array) is array[1*2] (array) and array[1*2+1] (array 3). The children of array is array[2*2] (array) and array[2*2+1] (array).