From 39ae1def85fcaf56761c88ad0a000e659941162c Mon Sep 17 00:00:00 2001 From: Tom Grimshaw Date: Sat, 3 Jul 2010 06:10:02 -0700 Subject: Re-implement the Undo stack as a List; the old implementation was buggy --- OpenSim/Framework/UndoStack.cs | 51 +++++++++++++++--------------------------- 1 file changed, 18 insertions(+), 33 deletions(-) (limited to 'OpenSim') diff --git a/OpenSim/Framework/UndoStack.cs b/OpenSim/Framework/UndoStack.cs index 4d800ae..3799180 100644 --- a/OpenSim/Framework/UndoStack.cs +++ b/OpenSim/Framework/UndoStack.cs @@ -26,6 +26,7 @@ */ using System; +using System.Collections.Generic; namespace OpenSim.Framework { @@ -36,33 +37,30 @@ namespace OpenSim.Framework [Serializable] public class UndoStack { - private int m_new = 1; - private int m_old = 0; - private T[] m_Undos; + private List m_undolist; + private int m_max; public UndoStack(int capacity) { - m_Undos = new T[capacity + 1]; + m_undolist = new List(); + m_max = capacity; } public bool IsFull { - get { return m_new == m_old; } + get { return m_undolist.Count >= m_max; } } public int Capacity { - get { return m_Undos.Length - 1; } + get { return m_max; } } public int Count { get { - int count = m_new - m_old - 1; - if (count < 0) - count += m_Undos.Length; - return count; + return m_undolist.Count; } } @@ -70,45 +68,32 @@ namespace OpenSim.Framework { if (IsFull) { - m_old++; - if (m_old >= m_Undos.Length) - m_old -= m_Undos.Length; + m_undolist.RemoveAt(0); } - if (++m_new >= m_Undos.Length) - m_new -= m_Undos.Length; - m_Undos[m_new] = item; + m_undolist.Add(item); } public T Pop() { - if (Count > 0) + if (m_undolist.Count > 0) { - T deleted = m_Undos[m_new]; - m_Undos[m_new--] = default(T); - if (m_new < 0) - m_new += m_Undos.Length; - return deleted; + int ind = m_undolist.Count - 1; + T item = m_undolist[ind]; + m_undolist.RemoveAt(ind); + return item; } else - throw new InvalidOperationException("Cannot pop from emtpy stack"); + throw new InvalidOperationException("Cannot pop from empty stack"); } public T Peek() { - return m_Undos[m_new]; + return m_undolist[m_undolist.Count - 1]; } public void Clear() { - if (Count > 0) - { - for (int i = 0; i < m_Undos.Length; i++) - { - m_Undos[i] = default(T); - } - m_new = 1; - m_old = 0; - } + m_undolist.Clear(); } } } -- cgit v1.1