PreviousElement()

Everything else that doesn't fall into one of the other PB categories.
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

PreviousElement()

Post 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.
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post by ts-soft »

After ResetList() there is no PreviousElement(), only NextElement()
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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()
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
User avatar
ts-soft
Always Here
Always Here
Posts: 5756
Joined: Thu Jun 24, 2004 2:44 pm
Location: Berlin - Germany

Post 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.
PureBasic 5.73 | SpiderBasic 2.30 | Windows 10 Pro (x64) | Linux Mint 20.1 (x64)
Old bugs good, new bugs bad! Updates are evil: might fix old bugs and introduce no new ones.
Image
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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 /\ ... :wink: PreviousElement() does not return the address of the previously selected element like you assume.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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"
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
BERESHEIT
Dare
Addict
Addict
Posts: 1965
Joined: Mon May 29, 2006 1:01 am
Location: Outback

Post 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
Dare2 cut down to size
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8452
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post by netmaestro »

All looks right to me Dare, good work!
BERESHEIT
User avatar
Psychophanta
Always Here
Always Here
Posts: 5153
Joined: Wed Jun 11, 2003 9:33 pm
Location: Anare
Contact:

Post 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
http://www.zeitgeistmovie.com

while (world==business) world+=mafia;
Trond
Always Here
Always Here
Posts: 7446
Joined: Mon Sep 22, 2003 6:45 pm
Location: Norway

Post 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
Post Reply