Array of arrays

General discussion for topics related to the FreeBASIC project or its community.
Post Reply
Qlink
Posts: 79
Joined: Jun 06, 2007 15:21

Array of arrays

Post by Qlink »

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?
1000101
Posts: 2556
Joined: Jun 13, 2005 23:14
Location: SK, Canada

Post by 1000101 »

How do you mean exactly?

There are multi-dimensional arrays.

There are arrays of records (of arrays (of records (...))).

There are many ways to skin a cat.

There is not records of dynamic arrays, however. This is a future feature which has yet to be implemented.
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

You could check to see if Redim helps. If you implement a reusable class to provide this functionality, then you can test it outside of the program where you need it. Possibly you could have an array where each element points to another array.
Qlink
Posts: 79
Joined: Jun 06, 2007 15:21

Post by Qlink »

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.
stylin
Posts: 1253
Joined: Nov 06, 2005 5:19

Post by stylin »

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:

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
Qlink
Posts: 79
Joined: Jun 06, 2007 15:21

Post by Qlink »

>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?
KristopherWindsor
Posts: 2428
Joined: Jul 19, 2006 19:17
Location: Sunnyvale, CA
Contact:

Post by KristopherWindsor »

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
jevans4949
Posts: 1188
Joined: May 08, 2006 21:58
Location: Crewe, England

Post by jevans4949 »

@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.
angros47
Posts: 2409
Joined: Jun 21, 2005 19:04

Post by angros47 »

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
Post Reply