Forums before death by AOL, social media and spammers... "We can't have nice things"
|    comp.programming    |    Programming issues that transcend langua    |    57,431 messages    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
|    Message 55,656 of 57,431    |
|    =?UTF-8?B?w5bDtiBUaWli?= to Malcolm McLean    |
|    Re: Undo / Redo design pattern.    |
|    03 Apr 22 06:26:19    |
      From: ootiib@hot.ee              On Sunday, 3 April 2022 at 14:16:14 UTC+3, Malcolm McLean wrote:       > To implement a simple undo redo, you need two stacks, and a copyable       representation of the program state. You then push to the undo stack, with       straight forwards extensions for redo.        >        > However this is memory greedy. So it is better to use the following pattern,       on stack push, take the difference between stack top and the object being       pushed. Then delete stack top, and replace with the difference between the       pushed object and stack        top, then push. To pop the stack, apply the difference to stack top, and set       it as new stack top.        >        > This could be made generic, but it involves a user-supplied delta codec       which could be hard to write. Another way of doing it is to feed everything       through a tostring / fromstring representation.        >        > The problem is now reduced to writing the delta codec for two strings. The       proper way to do this is to use a string alignment algorithm, but that is       likely to be overkill. The majority of changes will be either one character or       one indel. If we've got        more than few changes, we can simply resort to storing the entire string.              Storing whole program state is pointlessly memory consuming and diffing whole       program state is pointlessly expensive.        More usual is to make all actions to be required to record their reverse       actions.               So when action is done then reverse action is stored to undo stack and redo       stack is cleaned.       Undo then means that reverse action is done and removed from undo stack and       action is stored to redo stack.       Redo then means that action is done and removed from redo stack and reverse       action is stored to undo stack.              That takes designing (and testing) your commands/actions/operations more       carefully.       But that is anyway not bad at all in the long run.               --- SoupGate-Win32 v1.05        * Origin: you cannot sedate... all the things you hate (1:229/2)    |
[   << oldest   |   < older   |   list   |   newer >   |   newest >>   ]
(c) 1994, bbs@darkrealms.ca