Code archives/Miscellaneous/Simple B3D Reference Array
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
Reference Array. Can be used to pass arrays of various sizes to and from functions | |||||
;******************************************************************************************* ;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 Array\Size = Size Array\PtrSize = Len(Str(Size)) For ElementCount = 1 To Size Element = New TElement Array\PtrList = Array\PtrList + RSet(Handle(Element),Array\PtrSize) 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(Array\PtrList,1 + (ElementCount*Array\PtrSize),Array\PtrSize))) Element = Object.TElement(ElementPtr) Delete Element ElementCount=ElementCount+1 If ElementCount = Array\Size 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(Array\PtrList,1 + (Index * Array\PtrSize),Array\PtrSize))) Return Object.TElement(ElementPtr) End Function Function ArraySize(ArrayPtr) Local Array.TArray = Object.TArray(ArrayPtr) Return Array\Size End Function Function ArrayGet$(ArrayPtr,Index) Local Element.TElement = TArray_GetElementObject(ArrayPtr,Index) Return Element\Value End Function Function ArraySet$(ArrayPtr, Index, Value$) Local Element.TElement = TArray_GetElementObject(ArrayPtr,Index) Element\Value = 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
| ||
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?) |
| ||
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 |
| ||
Nice - I can recall having a little bother with passing values for arrays in the past |
Code Archives Forum