Page 1 of 1
					
				PreviousElement()
				Posted: Fri Nov 24, 2006 5:10 pm
				by Psychophanta
				Code: Select all
Structure da
  a1.f
  a2.b
EndStructure
NewList li.da()
For t=1 To 5
  AddElement(li())
  li()\a2=t
Next
;Bugs (or General Discussion) ?:
;One: Conveniently, PreviousElement() should allow to iterate items from the end of the list backto the beginning of the list, but it doesn't: 
ResetList(li())
Debug PreviousElement(li()); <- Returns 0. Function failed  (O_O)
Debug ListIndex(li()); <- Returns -1. So then linked list continues reseted
Debug "----------------"
;Another: PreviousElement() doesn't returns the address of the element as manual says, and BTW, NextElement() neither !
SelectElement(li(),2)
Debug PreviousElement(li()); Returns an address (as the manual says)
Debug @li(); <- Returns different address  (O_O)
Please mods. be kind to move this to Bugs Report section in case it is bug report.
 
			
					
				
				Posted: Fri Nov 24, 2006 5:33 pm
				by ts-soft
				After ResetList() there is no PreviousElement(), only NextElement()
			 
			
					
				
				Posted: Fri Nov 24, 2006 5:36 pm
				by netmaestro
				It's working correctly imho. ts-soft is right about ResetList() and you must bear in mind that PreviousElement() doesn't return a pointer to the element it just moved to, but to the one before that. This is so you can know if there's another previous element to move to.
			 
			
					
				
				Posted: Fri Nov 24, 2006 6:56 pm
				by Psychophanta
				netmaestro wrote:It's working correctly imho. ts-soft is right about ResetList()
If NextElement() goes forward starting from a ResetList() sentence, then PreviousElement() should go to backward.
netmaestro wrote:PreviousElement() doesn't return a pointer to the element it just moved to, but to the one before that.
Nope! look (3 different addresses):
Code: Select all
Structure da
  a1.f
  a2.b
EndStructure
NewList li.da()
For t=1 To 5
  AddElement(li())
  li()\a2=t
Next
SelectElement(li(),2)
Debug @li()
Debug PreviousElement(li())
Debug @li()
 
			
					
				
				Posted: Fri Nov 24, 2006 7:15 pm
				by ts-soft
				>> If NextElement() goes forward starting from a ResetList() sentence, then PreviousElement() should go to backward. 
ResetList() places before first element, backward is'nt a element.
			 
			
					
				
				Posted: Fri Nov 24, 2006 7:39 pm
				by netmaestro
				psycho, psycho, psycho! Study carefully:
Code: Select all
Structure da 
  a1.f 
  a2.b 
EndStructure 
NewList li.da() 
For t=1 To 5 
  AddElement(li()) 
  li()\a2=t 
Next 
SelectElement(li(),2) 
Debug Str(@li() ) + " Address of element 2"
Debug Str(PreviousElement(li())) + " = Move to element 1 and show address of element 0"
Debug Str(@li()) + " Show Address of the element we're on, element 1"
3 different addresses is correct.
 
			
					
				
				Posted: Fri Nov 24, 2006 8:25 pm
				by Trond
				One: Conveniently, PreviousElement() should allow to iterate items from the end of the list backto the beginning of the list, but it doesn't:
A list is not a circle. When you get to the end you aren't suddenly at the start. This is a list:
Element 1 - Element 2 - Element 3 - Element 4 - Element 5
When you are before element 1 and go backwards no logic says you should end up on element 5.
;Another: PreviousElement() doesn't returns the address of the element as manual says, and BTW, NextElement() neither !
Yes it does. It's @List() that doesn't return the address of an element.
@List() returns the address of the contents stored at that element. The data is not placed at the start of the element, at the start of the element are pointers to the previous and next element.
Edit: netmaestro, netmaestro, netmaestro, Study carefully /\ ... 

  PreviousElement() does not return the address of the previously selected element like you assume.
 
			
					
				
				Posted: Fri Nov 24, 2006 8:43 pm
				by netmaestro
				ARRGH! I didn't say previously selected element! If you are currently on element 2, PreviousElement() will: a)Move to element 1, and  b) Return a pointer to element 0. SHEESH! Why is this so problematic? It works like this so that after doing a PreviousElement(), you can know where the next call to PreviousElement() is going to take you.
			 
			
					
				
				Posted: Fri Nov 24, 2006 10:13 pm
				by Trond
				netmaestro wrote:Why is this so problematic?
Lemme guess once: Because it's wrong?
Code: Select all
Structure da 
  a1.f 
  a2.b 
EndStructure 
NewList li.da() 
For t=1 To 5 
  AddElement(li()) 
  li()\a2=t 
Next 
Debug "Netmaestro says:"
SelectElement(li(),2) 
Debug Str(@li() ) + " Address of element 2" 
Debug Str(PreviousElement(li())) + " = Move to element 1 and show address of element 0" 
Debug Str(@li()) + " Show Address of the element we're on, element 1"
Debug "--------------"
Debug "But netmaestro is wrong! This is correct:"
SelectElement(li(), 2)
Debug Str(@li()-8) + " Address of element 2"
Debug Str(PreviousElement(li())) + " Move to element 1 and show address of element --1--" 
Debug Str(@li()) + " Show the address of the data of the element we're on, element 1"
Debug ""
Debug Str(FirstElement(li())) + " As a bonus, show that THIS is the address of element 0!"
Debug Str(@li()-8) + " Double-checking that THIS is element 0"
 
			
					
				
				Posted: Sat Nov 25, 2006 12:51 am
				by netmaestro
				You're right, I was thinking the structure was:
*previous.element
*next.element
; data
but the structure is actually:
*next.element
*previous.element
;data
so you have to peek 4 bytes further up to get the address of the previous element.
			 
			
					
				
				Posted: Sat Nov 25, 2006 2:22 am
				by Dare
				So we can say:
Code: Select all
Structure UDT
  myLong.l
  myFloat.f
EndStructure
NewList aboutMe.UDT()
For i = 1 To 5
  AddElement( aboutMe() )
  aboutMe()\myLong = i * 2
  aboutMe()\myFloat = i * 2.2
Next
i = 0
ForEach aboutMe()
  Debug "I am " + Str(i) + " and I start at " + Str(@aboutMe() - 8)
  Debug "-->> After me comes " + Str(i + 1) +" at address " + Str( PeekL ( @aboutMe() - 8) ) + " (which info is held at addy " + Str(@aboutMe() - 8) + ")"
  Debug "-->> Before me lies " + Str(i - 1) +" at address " + Str( PeekL ( @aboutMe() - 4) ) + " (which info is held at addy " + Str(@aboutMe() - 4) + ")"
  Debug "-->> My long value is at " + Str( @aboutMe() ) + " and is " + Str( PeekL ( @aboutMe() ) )
  Debug "-->> My float value is at " + Str(@aboutMe() + 4 ) + " and is " + StrF( PeekF ( @aboutMe() + 4 ) )
  Debug "..............."
  i + 1
Next
 
			
					
				
				Posted: Sat Nov 25, 2006 4:47 am
				by netmaestro
				All looks right to me Dare, good work!
			 
			
					
				
				Posted: Sat Nov 25, 2006 12:04 pm
				by Psychophanta
				Well, then how to run onto a linked list from last to the first element?
I mean, something like ForEach-Next, but from Last element downto first one.
Wouldn't be this the logical way?
Code: Select all
ResetList(li())
While PreviousElement(li())
  ;do things...
Wend
 
			
					
				
				Posted: Sat Nov 25, 2006 12:26 pm
				by Trond
				Psychophanta wrote:Wouldn't be this the logical way?
Code: Select all
ResetList(li())
While PreviousElement(li())
  ;do things...
Wend
 
If you want to run from the last element, it's logical to use LastElement():
Code: Select all
If LastElement(A())
  Repeat
    Debug A()
  Until PreviousElement(A()) = 0
EndIf