UnderwareDESIGN

PlayBASIC => Resources => Source Codes => Topic started by: kevin on February 13, 2008, 07:17:19 PM

Title: Manual LInk List Control
Post by: kevin on February 13, 2008, 07:17:19 PM
 Manual List Controls
[plink]See 2008 Developer Blog (http://www.underwaredesign.com/forums/index.php?topic=3628.0)[/plink]

[pbcode]


   Type tItem
         X,Y,Z
   EndType

   Dim Variable as tItem list
   

   Dim Me as tItem

   RemStart
      /// Manual Controls over list pointer

      NewList Me()         ; Alloc LIst this type variable

      ResetList Me()         ; reset pointer to first object in list

      StepList Me()         ; Move the list ptr to the next item in the list
      
      result=EndOfList(Me())   ; check if we've hit the end of the list
   RemEnd




               
      NewList Me()
      For lp=1 to 10
         ; add a new item
         Me = New tiTem
         Me.x= 100 +lp
         Me.y= 200 +lp
         Me.z= 300 +lp
      Next






      Do

         ;      
            Cls 0


            ; For each list processing
            For each me()
                  x=me.x      
                  y=me.y      
                  z=me.z      

                  if X<105 then continue
                  Print str$(x)+", "+str$(y)+", "+str$(z)+", "      
                  print "Index:"+str$(GetListPos(me()))
            next




         ; Process list from While loop
            ResetList Me()
            While not EndOfList(me())       ; Loop while current link  isn't at the end of the list

               x=me.x      
               y=me.y      
               z=me.z      
               Print str$(x)+", "+str$(y)+", "+str$(z)+", "      

               print "Index:"+str$(GetListPos(me()))

               StepList Me()   ; step  to the next link in the list
            EndWhile




         ; Kill the first item
            ResetList Me()
            Me = null      ; Delete first cell
   
            ; Delete Cell 5 from the list
            FreeCell Me(),5


      Sync
      WaitKey
      Waitnokey
   loop

[/pbcode]


Note: Sample requires at least PlayBasic V1.63v

Title: Re: Manual LInk List Control
Post by: kevin on March 12, 2008, 07:59:07 AM
  New Linked List Helper Commands in PB1.63v

  There's a list of the new link list control functions found in PB1.63v.  These allow you to do stuff like set & get the current position (the current index).  Check the size of the list (numb of allocated objects in list) and get the Previous & Next objects from the current position.    

   

- SetListPos ListHandle(), CurrentPosIndex     ; Set the current
- CurrentPosIndex =GetListPos(ListHandle()) ; Get the Current posindex in the list
- FirstListIndex =GetListFirst(ListHandle()) ; Get the Current posindex in the list
- PreviousListIndex =GetListPrevious(ListHandle()) ; Get the index of the cell back from the Current position
- NextListIndex =GetListNext(ListHandle()) ; Get the index of the cell after the current position
- ListSize =GetListSize(ListHandle()) ; Get the sizer of the list (numb of allocated items)






Requires at least PlayBasic V1.63v

Example.

[pbcode]

   Type tPerson
      Name$
   EndType
    
   Dim Buddy as tPerson List

   Buddy = New tPerson
   Buddy.Name$="Kev"
    
   Buddy = New tPerson
   Buddy.Name$="dude"
    
   Buddy = New tPerson
   Buddy.Name$="olivia"

    Print " List Size:"+str$(GetListSize(Buddy()))
    
   For Each buddy()
       Print "          Persons Name:"+Buddy.name$
       Print " Current List Position:"+str$(GetListPos(Buddy()))
       Print "Previous List Position:"+str$(GetListPrevious(Buddy()))
       Print "    Next List Position:"+str$(GetListNext(Buddy()))
   Next

   Print "=========================="
   Print GetListFirst(Buddy())


   SetListPos Buddy(),2
   print "Name:"+Buddy.name$
   print EndOfList(Buddy())

   SetListPos Buddy(),1
   print "Name:"+Buddy.name$
   print EndOfList(Buddy())

   ; attempt to set list to a invalid position,
   SetListPos Buddy(),3
   print "Name:"+Buddy.name$
   print EndOfList(Buddy())

   ; attempt to set list to end link
   SetListPos Buddy(),-1
   print "Name:"+Buddy.name$
   print EndOfList(Buddy())

Sync
waitkey
    

[/pbcode]
Title: Re: Manual LInk List Control
Post by: kevin on March 21, 2008, 06:11:27 PM
 New Linked List Helper Commands in PB1.63v-beta3


 Pointer = GetListPtr(listhandle())



This command returns a (void) pointer to the current cell in the linked list.   Handly if you want to, access the types fields manually behind the scenes.




Usage Example

[pbcode]

   Type tpos
      x#,y#,z#
   endType
   Dim Me as tpos list
   
   Dim ptr as float pointer
   
   me = new tpos
   me.x   =111.456
   me.y   =222.456
   me.z   =333.456
      
   ptr=getlistPtr(me())

   if int(ptr)   
      print int(ptr)
   
      print me.x
      print me.y
      print me.z


      *ptr =66.66
      ptr=ptr+1
      *ptr =77.66
      ptr=ptr+1
      *ptr =88.66

      print me.x
      print me.y
      print me.z

   endif

   Sync
   waitkey
[/pbcode]