When to use PushListPosition and PopListPosition

Just starting out? Need help? Post your questions and find answers here.
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

When to use PushListPosition and PopListPosition

Post by jacdelad »

Hello,
eck49 asked about the differences between arrays, lists, and maps. This made me remember that I also wanted to ask something about LinkedLists:

I do understand what PushListPosition and PopListPosition do. I also understand how a stack works (LIFO...). But can someone tell me a scenario where to use that?
I'm working with lists, which is naturally almost inevitable. I also use @MyList() and ChangeCurrentElement() to store a pointer to the active element and restore it when a procedure needs to interfere between an operation cycling through a list (as an example). But I never needed to build a stack and I cannot think of a problem that requires one. Maybe a hint to a scenario can open my eyes. Thanks.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
User avatar
Tenaja
Addict
Addict
Posts: 1959
Joined: Tue Nov 09, 2010 10:15 pm

Re: When to use PushListPosition and PopListPosition

Post by Tenaja »

In a compiler, if you want to unroll a loop, you can push the start, and if the condition warrants an unroll, pop it to repeat the code. Obviously, you remove the jump bits, but that's one use.

Basically, any looping action where you want to jump back and forth. It allows a virtual "goto" when iterating the list, rather than individually Next-ing each element.
User avatar
TI-994A
Addict
Addict
Posts: 2704
Joined: Sat Feb 19, 2011 3:47 am
Location: Singapore
Contact:

Re: When to use PushListPosition and PopListPosition

Post by TI-994A »

jacdelad wrote:...I do understand what PushListPosition and PopListPosition do. I also understand how a stack works (LIFO...). But can someone tell me a scenario where to use that?
Simply put, these functions are meant to preserve the current positions in a list at any given time.

Scenario: a recursive call to a loop which iterates a list, with each iteration requiring different starting or ending positions of the list.
Texas Instruments TI-99/4A Home Computer: the first home computer with a 16bit processor, crammed into an 8bit architecture. Great hardware - Poor design - Wonderful BASIC engine. And it could talk too! Please visit my YouTube Channel :D
eck49
Enthusiast
Enthusiast
Posts: 153
Joined: Sat Nov 14, 2020 10:08 pm
Location: England

Re: When to use PushListPosition and PopListPosition

Post by eck49 »

This looks like when trying to solve a problem by exhaustion, where each assumption is a node in a tree. If set of assumptions proves to be incorrect you want to backtrack the last surviving one without forgetting the ones further up the tree.
Ubuntu 22.04 64-bit
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Re: When to use PushListPosition and PopListPosition

Post by freak »

jacdelad wrote:I also use @MyList() and ChangeCurrentElement() to store a pointer to the active element and restore it when a procedure needs to interfere between an operation cycling through a list (as an example).
PushListPosition() and PopListPosition() are just convenience methods to do basically the same thing. They remember the list position so you can iterate the list and restore the previous state without affecting other code that may rely on the current position. You can do the same by manually remembering the position and restoring it afterwards the way you mentioned.

They have some advantages though:
  • They can also remember/restore the state where a list has no current element (after ResetList()). The ChangeCurrentElement() function only works if you have a current element.
  • The stack behavior means you can use the functions without worrying that there is already a "remembered current element" from somewhere else. As long as you always balance the push/pop calls, you can use it recursively.
  • The debugger warns you if you do a DeleteElement() on an element that is remembered by PushListPosition(). If you get a pointer with @MyList() and then delete the element the debugger cannot help you
So it is just a convenient way to remember/restore the list state without having to think about edge cases (such as an empty list or a list with no current element). It is nothing that cannot be done in other ways as well though with a little more code.
quidquid Latine dictum sit altum videtur
User avatar
jacdelad
Addict
Addict
Posts: 1993
Joined: Wed Feb 03, 2021 12:46 pm
Location: Riesa

Re: When to use PushListPosition and PopListPosition

Post by jacdelad »

Aye, thanks to everyone. I guess I can use it somewhere.
Good morning, that's a nice tnetennba!

PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Post Reply