0

I have recently created an elaborate Undo/Redo mechanism for a programm of mine. It is an editor that works with specific XML files. However, since certain changes may or may not change any amount of nodes in the XML file, I am currently backing up the whole XML document as a clone.

So far, I've been using two System.Collections.Generic.Stack(Of XmlNode) objects to store them, and skipping back and forth works very well. But now I want to limit the number of steps one can undo, i. e. I need to throw out the oldest entries if the number of items in the undo stack exceeds a certain threshold.

How would I do that?

P.S.: It occured to me that I might use something like a Deque, so I already implemented my own DoubleEndedQueue(Of T). I could easily emulate a limited stack with that. It uses a System.Collections.Generic.List(Of T), though, and I don't know if List.Insert(item, 0) is high-performance O(1) or O(n).

LWChris
  • 3,320
  • 1
  • 22
  • 39
  • Maybe try something similar to [this SO answer on limiting the length of a Queue](http://stackoverflow.com/questions/5852863/fixed-size-queue-which-automatically-dequeues-old-values-upon-new-enques)? – Adrian Aug 20 '13 at 00:03
  • As their solution does also only wrap another unlimited Queue object, I guess there is no "offical" class to accomplish that. I'll use my class then, because I need a Stack, and the stack needs "dequeue" to remove the _oldest_ entries. Their queue is limited but it can't "pop" for my stack. I think there is no other way than using a Deque. :) – LWChris Aug 21 '13 at 23:05
  • Pretty much. There's nothing 'official' in .NET to accomplish this. – Adrian Aug 21 '13 at 23:45

0 Answers0