Page 1 of 1

When to use PushListPosition and PopListPosition

Posted: Thu Feb 11, 2021 12:51 am
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.

Re: When to use PushListPosition and PopListPosition

Posted: Thu Feb 11, 2021 2:20 am
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.

Re: When to use PushListPosition and PopListPosition

Posted: Thu Feb 11, 2021 7:08 am
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.

Re: When to use PushListPosition and PopListPosition

Posted: Thu Feb 11, 2021 5:42 pm
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.

Re: When to use PushListPosition and PopListPosition

Posted: Fri Feb 12, 2021 9:40 pm
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.

Re: When to use PushListPosition and PopListPosition

Posted: Mon Feb 15, 2021 3:10 am
by jacdelad
Aye, thanks to everyone. I guess I can use it somewhere.