Petr Schreiber
10-07-2010, 11:50
Hi,
I attach simple example of "lists" for ThinBASIC. Think of them as arrays you can append to without need to care about UBOUND checks.
Little demo below illustrates the use on basic example. The important fact is that the list "unit" is written in generic way, so you can use the list for any kind of UDT. You just pass size of element once, during the list creation, and then you pass pointers.
The only restriction is each instance of list must use single type of UDT items (the same limitation as for arrays).
Sample usage:
' -- Demo list to hold UDT data
' -- Petr Schreiber, 2010
Uses "Console"
#INCLUDE "unit_List.tBasic"
' -- Custom data
Type vec2d
x As Single
y As Single
End Type
Dim v As vec2D
Dim i As Long
Dim list As DWord = List_Create(SizeOf(vec2d))
v.x = 1
v.y = 2
List_Add(list, VarPtr(v))
v.x = 3
v.y = 4
List_Add(list, VarPtr(v))
WriteContentsOfTheList(list)
PrintL "Overwriting element #2"
v.x = 5
v.y = 6
List_SetAt(list, 2, VarPtr(v))
WriteContentsOfTheList(list)
PrintL "Expanding to 4 cells"
List_SetCount(list, 4)
WriteContentsOfTheList(list)
PrintL "Inserting 3x item 7,8 to the first index"
v.x = 7
v.y = 8
List_InsertAt(list, 1, VarPtr(v))
List_InsertAt(list, 1, VarPtr(v))
List_InsertAt(list, 1, VarPtr(v))
WriteContentsOfTheList(list)
PrintL "Removing the first item twice"
List_RemoveAt(list, 1)
List_RemoveAt(list, 1)
WriteContentsOfTheList(list)
' -- You need to destroy the list
List_Destroy(list)
PrintL "Press any key to quit..."
WaitKey
Function WriteContentsOfTheList( list As DWord )
Dim i As Long
Dim v As Vec2D
PrintL "---"
PrintL "Count:", List_GetCount(list)
PrintL "---"
For i = 1 To List_GetCount(list)
v = List_GetAt(list, i)
PrintL v.x, v.y
Next
PrintL "---"
PrintL
PrintL "Press any key to continue..."
PrintL
PrintL
WaitKey
End Function
The complete code is attached in the ZIP.
Petr
I attach simple example of "lists" for ThinBASIC. Think of them as arrays you can append to without need to care about UBOUND checks.
Little demo below illustrates the use on basic example. The important fact is that the list "unit" is written in generic way, so you can use the list for any kind of UDT. You just pass size of element once, during the list creation, and then you pass pointers.
The only restriction is each instance of list must use single type of UDT items (the same limitation as for arrays).
Sample usage:
' -- Demo list to hold UDT data
' -- Petr Schreiber, 2010
Uses "Console"
#INCLUDE "unit_List.tBasic"
' -- Custom data
Type vec2d
x As Single
y As Single
End Type
Dim v As vec2D
Dim i As Long
Dim list As DWord = List_Create(SizeOf(vec2d))
v.x = 1
v.y = 2
List_Add(list, VarPtr(v))
v.x = 3
v.y = 4
List_Add(list, VarPtr(v))
WriteContentsOfTheList(list)
PrintL "Overwriting element #2"
v.x = 5
v.y = 6
List_SetAt(list, 2, VarPtr(v))
WriteContentsOfTheList(list)
PrintL "Expanding to 4 cells"
List_SetCount(list, 4)
WriteContentsOfTheList(list)
PrintL "Inserting 3x item 7,8 to the first index"
v.x = 7
v.y = 8
List_InsertAt(list, 1, VarPtr(v))
List_InsertAt(list, 1, VarPtr(v))
List_InsertAt(list, 1, VarPtr(v))
WriteContentsOfTheList(list)
PrintL "Removing the first item twice"
List_RemoveAt(list, 1)
List_RemoveAt(list, 1)
WriteContentsOfTheList(list)
' -- You need to destroy the list
List_Destroy(list)
PrintL "Press any key to quit..."
WaitKey
Function WriteContentsOfTheList( list As DWord )
Dim i As Long
Dim v As Vec2D
PrintL "---"
PrintL "Count:", List_GetCount(list)
PrintL "---"
For i = 1 To List_GetCount(list)
v = List_GetAt(list, i)
PrintL v.x, v.y
Next
PrintL "---"
PrintL
PrintL "Press any key to continue..."
PrintL
PrintL
WaitKey
End Function
The complete code is attached in the ZIP.
Petr