Uses "console", "oxygen"
Long pGetIndex
Long pFinish
' this function calculates the one-dimensional array-index of any
' "multidimensional" stored element
' pass one string containing the elements position within multidimensional
' storage, for example myStorage(1,1,1) setup string like MKDWD$(1,1,1)
' second string has to contain the dimensions Ubounds,
' retrieve Index in one-dimensional array or 0 if invalid parameters
' to keep the function small there's a minimum of 2 dimensions
' and a minimum-Ubound of 2 elements per dimension
O2_Basic RawText
Function GetIndex( bString Element, bString Bounds) As DWord, link #pGetIndex
If Len(Element) <> Len(Bounds)
' both strings have to match in length
Return 0
Else
' both strings have to have at least 2 dimensions
If Len(Element) < 8 Then Return 0
' If Len(Bounds) < 8 Then Return 0 - not necessary if both match in size...
EndIf
DWord i, position, numElements
DWord numDims = Len(Element)/SizeOf(DWord)
DWord vElement At StrPtr(Element)
DWord vBounds At StrPtr(Bounds)
For i = 1 To numDims
' element must not be out of bounds
If vElement[i] < 1 Or vElement[i] > vBounds[i] Then Return 0
' a dimension needs at least 2 elements
If vBounds[i] < 2 Then Return 0
Next
numElements = vBounds[1]
For i = 2 To numDims
numElements = numElements * vBounds[i]
Next
For i = 1 To numDims - 1
numElements = numElements/vBounds[i]
position = position + (vElement[i]-1) * numElements
Next
Function = position + vElement[numDims]
End Function
'-------------------------
Sub Finish() link #pFinish
terminate
End Sub
End RawText
If O2_Error <> "" Then
PrintL "Can not run - Error within o2-script:" + $CRLF
PrintL O2_Error
WaitKey
Stop
Else
' can run
O2_Exec
End If
'---------------------------------------------------------------
' TB_section
Declare Function GetIndex( ByVal String, ByVal String) As DWord At pGetIndex
Declare Sub Finish() At pFinish
' test:
PrintL "search Element 2,3 in bounds of 2,3"
PrintL Str$( GetIndex MKDWD$(2,3), MKDWD$(2,3) )
PrintL
PrintL "search Element 3,4,5 in bounds of 4,5,6"
PrintL Str$( GetIndex MKDWD$(3,4,5), MKDWD$(4,5,6) )
PrintL
PrintL "search Element 2,3,4,5 in bounds of 4,6,8,10"
PrintL Str$( GetIndex MKDWD$(2,3,4,5), MKDWD$(4,6,8,10) )
PrintL "search Element 1,1,1,1,1,1 in bounds of 2,4,6,8,10,12"
PrintL Str$( GetIndex MKDWD$(1,1,1,1,1,1), MKDWD$(2,4,6,8,10,12) )
WaitKey
Finish()
Limits: