PDA

View Full Version : Simple unit for UDT list in ThinBASIC



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

Charles Pegge
10-07-2010, 20:51
Thanks Petr, your program is a perfect candidate for OOP. Without it you have to work quite hard behind the scenes to make it all run smoothly.

Below is a work in progress, which I hope goes most of the way in encapsulating your concept. Each list is a single object and takes string elements, and it should be possible to extend the class generically for any type of element. (good for testing O2 and trapping bugs anyway!)



'=========
class list
'=========
'
protected
'
ls as string 'list content
sz as sys 'unit size of element
ct as sys 'count of elements stored
'
public
'
method Create(sys n) { ls="" : sz=n }
method Clear() { ls=nuls ct*sz : ct=0 }
method Destroy() { ls="" : ct=0 : sz=0 }
method SetAmount(sys n) { ls=nuls sz*n }
method Add(el as string) { a=ct*sz+1 : mid this.ls,a,el : ct+=1 }
method Get(sys n) as string { a=(n-1)*sz : return mid ls,a+1,sz }
method Insert(string el,sys n) { a=sz*n : ls=left(ls,a)+el+mid(ls,a+1) : ct+=1 }
method Delete(string el,sys n) { a=sz*n : ls=left(ls,a)+mid(ls,a+1) : ct-=1 }
method Count() as sys { return ct }
method Elsize() as sys { return sz }
method Length() as sys { return len(ls) }

end class

Petr Schreiber
10-07-2010, 21:03
Hi Charles,

thanks for your Oxy edition, for a moment I was puzzled by the "compressed" look of the code, but now I can see what it does.
You are right it is a bit easier with OOP, but at least I practiced a bit with my favourite Heap ThinBASIC functions :)

In your code, maybe the SetAmount is not exactly doing what mine List_SetCount does. It seems to me SetAmount does erase all the contents, while List_SetCount behaves more like REDIM PRESERVE.


Thanks!,
Petr

Charles Pegge
10-07-2010, 21:21
Ah yes. Something like this for Redim



method SetAmount(sys n) { ls+=nuls sz*n-len(ls) }


Using Generic elements or strings with overloading:


method Add(el as string) { a=ct*sz+1 : mid this.ls,a,el : ct+=1 }
method Add(byref p as any) { a=(ct-1)*sz+ *ls : copy a, &p, sz : ct+=1 }

method Get(sys n) as string { a=(n-1)*sz : return mid ls,a+1,sz }
method Get(sys n) as any { a=(ct-1)*sz+ *ls : return a }


Single liners make code building easy :)

Charles