Results 1 to 4 of 4

Thread: Simple unit for UDT list in ThinBASIC

  1. #1
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Simple unit for UDT list in ThinBASIC

    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:
    [code=thinbasic]
    ' -- 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
    [/code]

    The complete code is attached in the ZIP.


    Petr
    Attached Files Attached Files
    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  2. #2

    Re: Simple unit for UDT list in ThinBASIC


    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!)

    [code=thinbasic]
    '=========
    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
    [/code]

  3. #3
    Super Moderator Petr Schreiber's Avatar
    Join Date
    Aug 2005
    Location
    Brno - Czech Republic
    Posts
    7,129
    Rep Power
    732

    Re: Simple unit for UDT list in ThinBASIC

    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

    Learn 3D graphics with ThinBASIC, learn TBGL!
    Windows 10 64bit - Intel Core i5-3350P @ 3.1GHz - 16 GB RAM - NVIDIA GeForce GTX 1050 Ti 4GB

  4. #4

    Re: Simple unit for UDT list in ThinBASIC


    Ah yes. Something like this for Redim

    [code=thinbasic]
    method SetAmount(sys n) { ls+=nuls sz*n-len(ls) }
    [/code]

    Using Generic elements or strings with overloading:

    [code=thinbasic] 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 }
    [/code]

    Single liners make code building easy

    Charles


Similar Threads

  1. List Parameters
    By danbaron in forum Science
    Replies: 0
    Last Post: 28-05-2011, 06:51

Members who have read this thread: 1

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •