News:

PlayBASIC2DLL V0.99 Revision I Commercial Edition released! - Convert PlayBASIC programs to super fast Machine Code. 

Main Menu

Manual LInk List Control

Started by kevin, February 13, 2008, 07:17:19 PM

Previous topic - Next topic

kevin

 Manual List Controls
See 2008 Developer Blog (login required)

PlayBASIC Code: [Select]
   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





Note: Sample requires at least PlayBasic V1.63v


kevin

#1
  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.

PlayBASIC Code: [Select]
   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




kevin

#2
 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

PlayBASIC Code: [Select]
   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