; German forum: http://www.purebasic.fr/german/viewtopic.php?t=1749&highlight= ; Author: Deeem2031 ; Date: 23. January 2005 ; OS: Windows ; Demo: Yes ; Shows the internal handling of LinkedLists in PureBasic ; Zeigt die interne Verwaltung von LinkedLists in PureBasic ;PB_LinkedLists ;by Deeém2031 (23.01.2005) ;for Purebasic 3.92 Structure PB_LinkedListData_Struc *FirstElement.PB_LinkedListElement_Struc ; FirstElement(LL()): = @LL()-8 *LastElement.PB_LinkedListElement_Struc ; LastElement(LL()): = @LL()-8 SizeofElement.l ; (Structure + *NextElement.l and *PrevElement.l (8 Byte)) AmountofElements.l ; = CountList() NumberofCurrentElement.l ; = ListIndex() + 1 (with the normal PB 3.92 LL-Library NumberofCurrentElement.l is equal to the result of ListIndex(), with the new one from purebasic.com/beta its bigger by 1) StructureMap.l ; "the structureMap is the address of the structure associated with the list, so if there is string in it they can be freed easily" by AlphaSND (explained at the end of the file) IsSetNumberofCurrentElement.l ; Is NumberofCurrentElement saving the correct value? (0=yes,other=no) EndStructure Structure PB_LinkedListElement_Struc *NextElement.l *PrevElement.l ;[...] Content EndStructure Structure PB_LinkedList_Struc *PB_LinkedListData.PB_LinkedListData_Struc *CurrentElement.PB_LinkedListElement_Struc EndStructure NewList LL.l() ;To get the Pointer to the PB_LinkedList-Struc you must use Assembler: Global *p.PB_LinkedList_Struc !MOV Eax, dword t_LL ; "LL" is the name of the LinkedList !MOV dword [p_p],Eax Debug "Pointer to the PB_LinkedList-Struc: "+Str(*p) ;At the beginning *FirstElement, *LastElement, *CurrentElement, AmountofElements.l and NumberofCurrentElement.l are 0 Debug "*p\PB_LinkedListData\FirstElement: "+Str(*p\PB_LinkedListData\FirstElement) Debug "*p\PB_LinkedListData\LastElement: "+Str(*p\PB_LinkedListData\LastElement) Debug "*p\CurrentElement: "+Str(*p\CurrentElement) Debug "*p\PB_LinkedListData\AmountofElement: "+Str(*p\PB_LinkedListData\AmountofElements) Debug "*p\PB_LinkedListData\NumberofCurrentElement: "+Str(*p\PB_LinkedListData\NumberofCurrentElement) Debug "" Debug "---AddElement()---" Debug "" AddElement(LL()) ;*FirstElement and *LastElement change if you create the first Element ;*CurrentElement change, too. ;AmountofElements and NumberofCurrentElement increase by 1 ... Debug "*p\PB_LinkedListData\FirstElement: "+Str(*p\PB_LinkedListData\FirstElement) Debug "*p\PB_LinkedListData\LastElement: "+Str(*p\PB_LinkedListData\LastElement) Debug "*p\CurrentElement: "+Str(*p\CurrentElement) Debug "*p\PB_LinkedListData\AmountofElement: "+Str(*p\PB_LinkedListData\AmountofElements) Debug "*p\PB_LinkedListData\NumberofCurrentElement: "+Str(*p\PB_LinkedListData\NumberofCurrentElement) ;@LL() always return the address to the PB_LinkedListData_Struc + 8 ;, because *NextElement.l And *PrevElement.l are saved in the Structure too. ;To change the address To the Next Element you can use PokeL(@LL()-8,address) ;For the previous PokeL(@LL()-4,address) Debug "Prev Element: "+Str(PeekL(@LL()-4))+" (0 because its the first Element)" Debug "Next Element: "+Str(PeekL(@LL()-8))+" (0 because its the last Element)" Debug "" Debug "---AddElement()---" Debug "" AddElement(LL()) Debug "Prev Element: "+Str(PeekL(@LL()-4)) Sec_ElementP = @LL() Debug "" Debug "---PreviousElement()---" Debug "" PreviousElement(LL()) Debug "Next Element: "+Str(PeekL(@LL()-8)) ;Some Procedures to show how the PB-Procs works Procedure LL_Reset(*LinkedList.PB_LinkedList_Struc) *LinkedList\CurrentElement = 0 EndProcedure Procedure.l LL_NextElement(*LinkedList.PB_LinkedList_Struc) If *LinkedList\CurrentElement\NextElement *LinkedList\CurrentElement = *LinkedList\CurrentElement\NextElement ProcedureReturn *LinkedList\CurrentElement\NextElement EndIf ProcedureReturn #False EndProcedure Procedure.l LL_PreviousElement(*LinkedList.PB_LinkedList_Struc) If *LinkedList\CurrentElement\PrevElement *LinkedList\CurrentElement = *LinkedList\CurrentElement\PrevElement ProcedureReturn *LinkedList\CurrentElement\PrevElement EndIf ProcedureReturn #False EndProcedure Procedure.l LL_FirstElement(*LinkedList.PB_LinkedList_Struc) *LinkedList\CurrentElement = *LinkedList\PB_LinkedListData\FirstElement ProcedureReturn *LinkedList\PB_LinkedListData\FirstElement EndProcedure Procedure.l LL_LastElement(*LinkedList.PB_LinkedList_Struc) *LinkedList\CurrentElement = *LinkedList\PB_LinkedListData\LastElement ProcedureReturn *LinkedList\PB_LinkedListData\LastElement EndProcedure Procedure LL_ChangeCurrentElement(*LinkedList.PB_LinkedList_Struc,*Element.l) *LinkedList\CurrentElement = *Element-8 EndProcedure Debug "ListIndex: "+Str(ListIndex(LL())) Debug "" Debug "---ChangeCurrentElement()--- to second element" ChangeCurrentElement(LL(),Sec_ElementP) If *p\PB_LinkedListData\IsSetNumberofCurrentElement Debug "This Value isn't correct:" Else Debug "This Value is correct:" EndIf Debug "ListIndex: "+Str(*p\PB_LinkedListData\NumberofCurrentElement-1) Debug "" Debug "ListIndex (PB): "+Str(ListIndex(LL())) ; If *p\PB_LinkedListData\IsSetNumberofCurrentElement Debug "This Value isn't correct:" Else Debug "This Value is correct:" EndIf Debug "ListIndex: "+Str(*p\PB_LinkedListData\NumberofCurrentElement-1) Debug "" ;So now we play something with Structures in a LinkedList Structure teststruc l.l s.s w.w b.b f.f s2.s s3.s EndStructure ;StructureMap of teststruc: ;s_teststruc: ;dd 4 ;String are at Positions 4,15 and 19 ;dd 15 ;dd 19 ;dd -1 ;-1 ist the endcharacter of a structurefield NewList LL2.teststruc() ;To get the Pointer to the PB_LinkedList-Struc you must use Assembler: Global *p.PB_LinkedList_Struc !MOV Eax, dword t_LL2 ; "LL2" is the name of the LinkedList !MOV dword [p_p],Eax Debug "Size of one LL2-element: "+Str(*p\PB_LinkedListData\SizeofElement)+"bytes" Debug "" If *p\PB_LinkedListData\StructureMap ;The structuremap is to speed up PB handling with strings Debug "The linkedliststructure contain a string at" While PeekL(*p\PB_LinkedListData\StructureMap+i) <> -1 Debug "Position: "+Str(PeekL(*p\PB_LinkedListData\StructureMap+i)) i+4 Wend EndIf ; IDE Options = PureBasic v4.02 (Windows - x86) ; Folding = --