Array of arrays
Array of arrays
Is there any way to have a dynamic array of dynamic arrays WITHOUT using pointers? I know how to use pointers, but I find it's messy and I'm more likely to screw something up. Since we already have arrays of variable length strings, why not arrays of arrays or other dynamic types of objects?
-
- Posts: 2428
- Joined: Jul 19, 2006 19:17
- Location: Sunnyvale, CA
- Contact:
The way I've currently been tackling things like this is to keep an array of pointers to arrays of the value (or, in the case of strings, to keep an array of zstring ptr ptrs). I have thought about writing a class to implement such a data type (I'll probably make it work like a python list or c# ArrayList, so it can take multiple types as well). That's probably what I'll do, but I just wondered if there was an easier way or plans to implement one in the future.
Qlink, if you want to learn about the technical difficulties of resizing multi-dimensional arrays (and other subtle issues involved with that) and why that hasn't been implemented, there have been discussions on the forums you can search for.
If you want to avoid the hassle of dealing with memory management yourself and use an existing solution, here's one using the latest SVN version of ext/Containers:
If you want to avoid the hassle of dealing with memory management yourself and use an existing solution, here's one using the latest SVN version of ext/Containers:
Code: Select all
# include once "ext/containers/array.bi"
type StringArray as ext.fbext_Array( ((string)) )
fbext_Instanciate( fbext_Array, ((StringArray)) )
type ArrayOfStringArrays as ext.fbext_Array( ((StringArray)) )
' create an array containing 5 empty string arrays.
var array = ArrayOfStringArrays( 5 )
' insert some elements into the string arrays.
for i as integer = 0 to array.Size() - 1
array.Index(i)->Insert( array.Index(i)->Begin(), i, str(i) )
next i
' print each element of each array; array.Index(0) is an empty array.
for i as integer = 0 to array.Size() - 1
for j as integer = 0 to array.Index(i)->Size() - 1
print *array.Index(i)->Index(j),
next j
print
next i
' outputs:
'
' 1
' 2 2
' 3 3 3
' 4 4 4 4
>Stylin, I did search but couldn't find what I was looking for. Thanks for your code, it was helpful :).
The main thing I can't figure out how to tackle is allowing a list to handle multiple different types, including numeric types, strings, and other lists. I suppose what I could do is to store it as an array with fixed-sized elements for each object with a byte as a sort of "type signature" and a pointer to the object that holds the actual value. Then I could use the value of the type byte to determine the type of the object being pointed to and what to do with it. Can anyone recommend a better way to do this?
The main thing I can't figure out how to tackle is allowing a list to handle multiple different types, including numeric types, strings, and other lists. I suppose what I could do is to store it as an array with fixed-sized elements for each object with a byte as a sort of "type signature" and a pointer to the object that holds the actual value. Then I could use the value of the type byte to determine the type of the object being pointed to and what to do with it. Can anyone recommend a better way to do this?
-
- Posts: 2428
- Joined: Jul 19, 2006 19:17
- Location: Sunnyvale, CA
- Contact:
You can fake generics with macros, sortof. It looks like stylin's code is already doing that. All of my code is on the other computer ( =p ), but essentially you put all of your code in a macro, then something like creatething(integer) could expand to a definition for Type thing_integer. Hopefully stylin can explain it more. ;P
-
- Posts: 1188
- Joined: May 08, 2006 21:58
- Location: Crewe, England
@QLink: Take a look at my LL.Zip in the archive.
This implements a doubly-linked list with no arrays at all, just an anchor element.
You can have a list of items which themselves include anchors for further lists.
My list item header includes forward and backward pointers and a byte for item type, typically set to null for item and "z" for the anchor. Youi can set it as required for different types.
You obtain the required storage for a new item yourself by ALLOCATE; you can then insert it into the list where you want. The zip file includes a demo with float types only.
I am working on another package, Area Manager, which allows you to sub-allocate space in a larger pool of memory.
This implements a doubly-linked list with no arrays at all, just an anchor element.
You can have a list of items which themselves include anchors for further lists.
My list item header includes forward and backward pointers and a byte for item type, typically set to null for item and "z" for the anchor. Youi can set it as required for different types.
You obtain the required storage for a new item yourself by ALLOCATE; you can then insert it into the list where you want. The zip file includes a demo with float types only.
I am working on another package, Area Manager, which allows you to sub-allocate space in a larger pool of memory.
As far as I recall, in SmallBasic there were both multi-dimensionals array up to 3 dimensions (line A(10,10,10)=1), and arrays of arrays (A(1)(5)=1), in a way similar to C. They work in a way similar to variable lenght strings, because every "array" can be of a different size.
I thinks that it has to be done using pointers, but a simpler way to use them would be appreciated
I thinks that it has to be done using pointers, but a simpler way to use them would be appreciated