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.
When to use PushListPosition and PopListPosition
When to use PushListPosition and PopListPosition
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD
Re: When to use PushListPosition and PopListPosition
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.
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.
Re: When to use PushListPosition and PopListPosition
Simply put, these functions are meant to preserve the current positions in a list at any given time.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?
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 

Re: When to use PushListPosition and PopListPosition
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)
Purebasic 6.00 (as of 5 Sep 2022)
(native tongue: English)
Re: When to use PushListPosition and PopListPosition
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.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).
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
quidquid Latine dictum sit altum videtur
Re: When to use PushListPosition and PopListPosition
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
PureBasic 6.21/Windows 11 x64/Ryzen 7900X/32GB RAM/3TB SSD
Synology DS1821+/DX517, 130.9TB+50.8TB+2TB SSD