Multidimensional arrays of arbitrary complexity
Multidimensional arrays of arbitrary complexity
- Subject: Multidimensional arrays of arbitrary complexity
- From: Brennan <email@hidden>
- Date: Tue, 10 Dec 2002 00:14:26 +0100
Hi folks,
Here's a little distraction I thought I would share with you.
As most of us know, lists and arrays are not quite the same.
Arrays are of a fixed size and in most (all?) implementations can only contain data of the same type.
Arrays are created by specifying their dimensions, and accessed by specifying 'coordinates' within those dimensions.
Lists, on the other hand can contain data of any type, and their very structure is dynamic.
They expand, both in raw size, but also structurally, to accomodate their contents as far as RAM allows. You do not need to know their maximum size or their structure in advance.
It might appear, then, that lists are superior in almost every respect, but anyone who has built a complex structure of nested lists will appreciate that it can be extremely taxing and inconvenient to construct such a thing, and tricky to fetch data from an innermost list because of all the required gymnastics with 'item n of...'.
Sometimes what you really want is an array with known dimensions.
It's sometimes just more convenient to refer to an item in a 'square' (maybe the proper word is 'orthogonal'? please advise!) data structure by a set of coordinates - i.e. a list of integers - instead of by a load of 'item' references.
With this in mind, I've created these handlers allowing 'arrays' of arbitrary complexity to be created and manipulated, using lists and recursion. None of the gory details need be known, but the code is not so difficult (and short enough) to understand if you are not fazed by recursion.
They aren't completely watertight, and will break if you do silly things, like passing the wrong type of parameters to them, but they seem to work ok. I will leave the exception handling as an exercise to the reader.
One interesting thing here is that you can pull out - or even modify - 'sub-arrays' from larger ones - i.e. you can pull a 2d array out of a 5d array or whatever. You should know what you are doing if you do this, because the sub array will be returned by reference, not by value, so modifying its structure may screw things up. I'm not sure whether this kind of thing is possible with typical array implementations, such as in javascript, but lists allow this kind of thing so it's here.
Suggestions for optimization and other feedback is welcome.
Example calls:
set a to generateArray({2, 3, 6, 8}, 0) -- 4 dimensional array of zeroes
getVal(a, {2, 3, 2, 5})
setVal(a, {2, 3, 2, 5}, 99)
-- and now our feature presentation...
on generateArray(dimensionz, fillval)
set cdim to (count dimensionz)
if cdim > 0 then
set car to (item 1 of dimensionz)
set inner to {}
if cdim = 1 then
set cdr to {}
else
copy (items 2 thru end of dimensionz) to cdr
end if
repeat with n from 1 to car
-- go deeper --
set inner to inner & {generateArray(cdr, fillval)}
end repeat
return inner
else
-- come back up again --
return fillval
end if
end generateArray
--
on getVal(arr, coords)
set car to item 1 of coords
set ccrds to (count coords)
if ccrds > 1 then
if ccrds = 1 then
set cdr to {}
else
copy (items 2 thru end of coords) to cdr
end if
return getVal((item car of arr), cdr) -- go deeper --
else -- get value and come back up again --
return (item car of arr)
end if
end getVal
--
-- note, this handler requires the getVal handler
on setVal(arr, coords, newval)
set ccrds to (count coords)
set i to (item ccrds of coords)
copy (items 1 thru ccrds of coords) to truncated
set item i of getVal(arr, truncated) to newval
end setVal
Brennan
_______________________________________________
applescript-users mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/applescript-users
Do not post admin requests to the list. They will be ignored.