Page 1 of 1

Pointer to a linked list

Posted: Wed Sep 05, 2012 9:50 am
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())

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 1:52 pm
by STARGÅTE
:?:

LinkedLists, Maps and Array be pushed as a pointer!

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 2:20 pm
by Polo
Already exists even if it's maybe not as elegant as what your code would suggest, see ChangeCurrentElement.

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 2:38 pm
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

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 2:51 pm
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

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 3:51 pm
by User_Russian
STARGÅTE wrote:you can not use:
I want to add this feature.

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 4:04 pm
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 ;)

Re: Pointer to a linked list

Posted: Wed Sep 05, 2012 9:46 pm
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())

Re: Pointer to a linked list

Posted: Fri Nov 09, 2012 10:02 pm
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

Re: Pointer to a linked list

Posted: Fri Nov 09, 2012 11:27 pm
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

Re: Pointer to a linked list

Posted: Sat Nov 10, 2012 12:14 am
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