[bb] Simple B3D Reference Array by Spencer [ 1+ years ago ]

Started by BlitzBot, June 29, 2017, 00:28:39

Previous topic - Next topic

BlitzBot

Title : Simple B3D Reference Array
Author : Spencer
Posted : 1+ years ago

Description : Reference Array. Can be used to pass arrays of various sizes to and from functions

Code :
Code (blitzbasic) Select
;*******************************************************************************************
;Type TArray : A Reference Array good for passing arrays between functions
;
; -Functions-                                   -Example-
; CreateArray( array_size )                     Global A = CreateArray(42)
; ArraySet( array , index, value$ )             ArraySet(A, 30, "String value at index 30")
; ArrayGet$( array , index )                    Print ArrayGet(A, 30)
; ArraySize( array )                            Print ArraySize(A)
; DeleteArray( array )                          DeleteArray(A)
;
;*******************************************************************************************
Type TArray
    Field Size
    Field PtrSize
    Field PtrList$
End Type

Type TElement
    Field Value$
End Type

Function CreateArray(Size)
    Local Array.TArray = New TArray
    Local ElementCount = 0
    Local Element.TElement
    ArraySize = Size
    ArrayPtrSize = Len(Str(Size))
    For ElementCount = 1 To Size
        Element = New TElement
        ArrayPtrList = ArrayPtrList + RSet(Handle(Element),ArrayPtrSize)
    Next
    Return Handle(Array)
End Function

Function DeleteArray(ArrayPtr)
    Local Array.TArray = Object.TArray(ArrayPtr)
    Local ElementCount = 0
    Local ElementPtr
    Local Element.TElement
    Repeat
        ElementPtr =Int(Trim(Mid(ArrayPtrList,1 + (ElementCount*ArrayPtrSize),ArrayPtrSize)))
        Element = Object.TElement(ElementPtr)
        Delete Element    
        ElementCount=ElementCount+1
        If ElementCount = ArraySize Then
            Exit
        EndIf
    Forever
    Delete Array
End Function

Function TArray_GetElementObject.TElement(ArrayPtr,Index)
    Local Array.TArray = Object.TArray(ArrayPtr)
    Local ElementPtr = Int(Trim(Mid(ArrayPtrList,1 + (Index * ArrayPtrSize),ArrayPtrSize)))
    Return Object.TElement(ElementPtr)
End Function

Function ArraySize(ArrayPtr)
    Local Array.TArray = Object.TArray(ArrayPtr)
    Return ArraySize
End Function

Function ArrayGet$(ArrayPtr,Index)
    Local Element.TElement = TArray_GetElementObject(ArrayPtr,Index)
    Return ElementValue
End Function

Function ArraySet$(ArrayPtr, Index, Value$)
    Local Element.TElement = TArray_GetElementObject(ArrayPtr,Index)
    ElementValue = Value
End Function

;*******************************************************************************************
;END Type TArray
;*******************************************************************************************



Global A = CreateArray(42)

ArraySet(A,31,"Value at index 31")
Print ArrayGet(A,31)
Print ArraySize(A)    

ExampleFunction(A)

Print ArrayGet(A,23)
Input "Done..."

ArraySet(A,0,"Hello world")

Function ExampleFunction(array)
    Print "Array[0] = " + ArrayGet(array,0)
    Print "ArraySize = " + ArraySize(array)
    ArraySet(A,23, "Hello world")
End Function


Comments :


Yasha(Posted 1+ years ago)

 You're going to hate me for this, but:
Function SquareArray(a[10])
    Local e
    For e = 1 to 10
        a[e] = a[e] * a[e]
    Next
End Function

Function PrintArray(a[10])
    Local e
    For e = 1 to 10
        Write a[e] + " "
    Next
    Print ""
End Function

Local arr1[10], arr2[10], e

For e = 1 to 10
    arr1[e] = e
    arr2[e] = 11 - e
Next

SquareArray arr1
SquareArray arr2

PrintArray arr1
PrintArray arr2

Waitkey
End
Native B3D arrays can only be passed upward though, so your method is still useful.(Pssst. Why are you returning TArray Handles instead of TArrays?)


Spencer(Posted 1+ years ago)

 Thanks for the response Yasha. I guess the usefulness of this code comes into play when you are writing a Function that needs to create and return an array of variable size.    Global Point2D = CreatePoint(2)
    Global Point3D = CreatePoint(3)

    Print ArrayGet(Point2D,0)
    Print ArrayGet(Point2D,1)

    Print ArrayGet(Point3D,0)
    Print ArrayGet(Point3D,1)
    Print ArrayGet(Point3D,2)

   Input "Done"

    Function CreatePoint(Dimensions)

        Local ptrArray = CreateArray(Dimensions)

        If Dimensions = 2 Then
            ArraySet(ptrArray,0, 1)
            ArraySet(ptrArray,1, 1)
        ElseIf Dimensions = 3 Then
            ArraySet(ptrArray,0, 1.02 )
            ArraySet(ptrArray,1, 1.02 )
            ArraySet(ptrArray,2, 1.02 )
        Else
              Input "Dimensions not valid."
        EndIf

        Return ptrArray

    End Function




Blitzplotter(Posted 1+ years ago)

 Nice - I can recall having a little bother with passing values for arrays in the past [/i]