Pointer to a linked list

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Pointer to a linked list

Post by User_Russian »

Need to add the ability to work with a linked list (array, map, etc.) by pointer.

Code: Select all

Procedure Test(List x.l())
  List y() = x()
    
EndProcedure

NewList MyList.l()

Test(MyList())
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Pointer to a linked list

Post by STARGÅTE »

:?:

LinkedLists, Maps and Array be pushed as a pointer!
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Pointer to a linked list

Post by Polo »

Already exists even if it's maybe not as elegant as what your code would suggest, see ChangeCurrentElement.
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Pointer to a linked list

Post by User_Russian »

Another example.

Code: Select all

Structure a4 
  String.s 
  List x.i() 
EndStructure 

Structure a3 
  List x.a4() 
EndStructure 

Structure a2 
  List x.a3() 
EndStructure 

Structure ax 
  List x.a2() 
EndStructure

Procedure Test(List x.a2()) 
  
  SelectElement(x(),0)
  SelectElement(x()\x(),0)
  SelectElement(x()\x()\x(),0)
  SelectElement(x()\x()\x()\x(),0)
  
  List y.i() = @x()\x()\x()\x()
  
  ForEach y()
    Debug y()
  Next
  
EndProcedure 

Test.ax 

For i=1 To 10 
  If AddElement(Test\x()) 
    For x=1 To 10 
      If AddElement(Test\x()\x()) 
        For y=1 To 10 
          If AddElement(Test\x()\x()\x()) 
            For z=1 To 10 
              If AddElement(Test\x()\x()\x()\x()) 
                Test\x()\x()\x()\x()=z 
                Test\x()\x()\x()\String = Str(i)+Str(x)+Str(y)+Str(z) 
              EndIf 
            Next z 
          EndIf 
        Next y 
      EndIf 
    Next x 
  EndIf 
Next i 
 

Test(Test\x()) 
Need to get a pointer to the list and work with this list.
The code will be more readable and optimized.

Requires the same result as with the structures, but using lists and arrays.

Code: Select all

*MyStruct.Point
Point.Point

Point\x=1
Point\y=2

*MyStruct = @Point

Debug *MyStruct\x
Debug *MyStruct\y
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Pointer to a linked list

Post by STARGÅTE »

For example:

Code: Select all

Structure a4 
	String.s 
	List x.i() 
EndStructure 
; [...]

Protected *a4.a4 = @x()\x()\x()

ForEach *a4\x()
	Debug *a4\x()
Next
you can not use:

Code: Select all

*Element() = Element()\Element()\Element()
because *Element() itself could be a list, so no way to use it as pointer, as you wish
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Pointer to a linked list

Post by User_Russian »

STARGÅTE wrote:you can not use:
I want to add this feature.
Polo
Addict
Addict
Posts: 2422
Joined: Tue May 06, 2003 5:07 pm
Location: UK

Re: Pointer to a linked list

Post by Polo »

User_Russian wrote:
STARGÅTE wrote:you can not use:
I want to add this feature.
This is not possible to add, for the reason Stargate explained ;)
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Pointer to a linked list

Post by User_Russian »

Polo wrote:
User_Russian wrote:
STARGÅTE wrote:you can not use:
I want to add this feature.
This is not possible to add, for the reason Stargate explained ;)
Why not?
After all, you need the following, with the only difference, then a pointer to the list can be created in any part of the code, not only in the argument of the procedure.

Code: Select all

Structure a4 
  String.s 
  List x.i() 
EndStructure 

Structure a3 
  List x.a4() 
EndStructure 

Structure a2 
  List x.a3() 
EndStructure 

Structure ax 
  List x.a2() 
EndStructure

Procedure Test(List y.i()) 
  
  ForEach y()
    Debug y()
  Next
  
EndProcedure 

Test.ax 

For i=1 To 10 
  If AddElement(Test\x()) 
    For x=1 To 10 
      If AddElement(Test\x()\x()) 
        For y=1 To 10 
          If AddElement(Test\x()\x()\x()) 
            For z=1 To 10 
              If AddElement(Test\x()\x()\x()\x()) 
                Test\x()\x()\x()\x()=z 
                Test\x()\x()\x()\String = Str(i)+Str(x)+Str(y)+Str(z) 
              EndIf 
            Next z 
          EndIf 
        Next y 
      EndIf 
    Next x 
  EndIf 
Next i 


SelectElement(Test\x(),0)
SelectElement(Test\x()\x(),0)
SelectElement(Test\x()\x()\x(),0)
SelectElement(Test\x()\x()\x()\x(),0)

Test(@Test\x()\x()\x()\x())
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: Pointer to a linked list

Post by User_Russian »

Here is another example.
There is a code in which repeated the same action with different linked lists.

Code: Select all

NewList x1.l()
NewList x2.l()
NewList x3.l()
NewList x4.l()

ForEach x1()
  ; Working with a list.
Next

ForEach x2()
  ; Working with a list.
Next

ForEach x3()
  ; Working with a list.
Next

ForEach x4()
  ; Working with a list.
Next
This is a simple example.
In the real code inside the loop enumeration list items is a lot of code. Duplicate it for each list is not desirable.

I now use a procedure to reduce code size.

Code: Select all

Procedure Test(List x.l())
  ForEach x()
    ; Working with a list.
  Next
EndProcedure

NewList x1.l()
NewList x2.l()
NewList x3.l()
NewList x4.l()

Test(x1())
Test(x2())
Test(x3())
Test(x4())
It would be great if we could get a pointer to list (not to be confused with the current element) and work with it, that is, to refuse procedure.

Code: Select all

NewList x1.l()
NewList x2.l()
NewList x3.l()
NewList x4.l()

For i=1 To 4
  Select i
    Case 1 : List x.l() = x1()
    Case 2 : List x.l() = x2()
    Case 3 : List x.l() = x3()
    Case 4 : List x.l() = x4()
  EndSelect
  
  ForEach x()
    ; Working with a list.
  Next
Next i
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Pointer to a linked list

Post by helpy »

User_Russian wrote:It would be great if we could get a pointer to list (not to be confused with the current element) and work with it, that is, to refuse procedure.
You could use an array of Lists:

Code: Select all

EnableExplicit

Structure tList
	List x.l()
EndStructure

Dim ListArray.tList(3)
Define i, j

For i=0 To 3
	For j = 1 To 9
		AddElement( ListArray(i)\x() )
		ListArray(i)\x() = (i+1)*1000 + j
	Next j
Next i


For i=0 To 3
	ForEach ListArray(i)\x()
		Debug ListArray(i)\x()
	Next
	Debug "-"
Next i
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
User avatar
helpy
Enthusiast
Enthusiast
Posts: 552
Joined: Sat Jun 28, 2003 12:01 am

Re: Pointer to a linked list

Post by helpy »

I am not sure, if somone could use this code:

Code: Select all

Procedure procGetPointer( *list )
	ProcedureReturn *list
EndProcedure
Prototype.i ptGetPointer( List x.l() )
GetPointer.ptGetPointer = @procGetPointer()

NewList x.l()

For i = 1 To 9
	Debug "List pointer: " + Hex(GetPointer( x() ), #PB_Integer)
	AddElement( x() )
	x() = i
	Debug "Element pointer: " + Hex(@x(),#PB_Integer)
	Debug "-"
Next 
Debug "List pointer: " + Hex(GetPointer( x() ), #PB_Integer)
cu, guido
Windows 10 / Windows 7
PB Last Final / Last Beta Testing
Post Reply