Re-implement the Undo stack as a List; the old implementation was buggy

avinationmerge
Tom Grimshaw 2010-07-03 06:10:02 -07:00
parent 2b49cb9227
commit 39ae1def85
1 changed files with 18 additions and 33 deletions

View File

@ -26,6 +26,7 @@
*/ */
using System; using System;
using System.Collections.Generic;
namespace OpenSim.Framework namespace OpenSim.Framework
{ {
@ -36,33 +37,30 @@ namespace OpenSim.Framework
[Serializable] [Serializable]
public class UndoStack<T> public class UndoStack<T>
{ {
private int m_new = 1; private List<T> m_undolist;
private int m_old = 0; private int m_max;
private T[] m_Undos;
public UndoStack(int capacity) public UndoStack(int capacity)
{ {
m_Undos = new T[capacity + 1]; m_undolist = new List<T>();
m_max = capacity;
} }
public bool IsFull public bool IsFull
{ {
get { return m_new == m_old; } get { return m_undolist.Count >= m_max; }
} }
public int Capacity public int Capacity
{ {
get { return m_Undos.Length - 1; } get { return m_max; }
} }
public int Count public int Count
{ {
get get
{ {
int count = m_new - m_old - 1; return m_undolist.Count;
if (count < 0)
count += m_Undos.Length;
return count;
} }
} }
@ -70,45 +68,32 @@ namespace OpenSim.Framework
{ {
if (IsFull) if (IsFull)
{ {
m_old++; m_undolist.RemoveAt(0);
if (m_old >= m_Undos.Length)
m_old -= m_Undos.Length;
} }
if (++m_new >= m_Undos.Length) m_undolist.Add(item);
m_new -= m_Undos.Length;
m_Undos[m_new] = item;
} }
public T Pop() public T Pop()
{ {
if (Count > 0) if (m_undolist.Count > 0)
{ {
T deleted = m_Undos[m_new]; int ind = m_undolist.Count - 1;
m_Undos[m_new--] = default(T); T item = m_undolist[ind];
if (m_new < 0) m_undolist.RemoveAt(ind);
m_new += m_Undos.Length; return item;
return deleted;
} }
else else
throw new InvalidOperationException("Cannot pop from emtpy stack"); throw new InvalidOperationException("Cannot pop from empty stack");
} }
public T Peek() public T Peek()
{ {
return m_Undos[m_new]; return m_undolist[m_undolist.Count - 1];
} }
public void Clear() public void Clear()
{ {
if (Count > 0) m_undolist.Clear();
{
for (int i = 0; i < m_Undos.Length; i++)
{
m_Undos[i] = default(T);
}
m_new = 1;
m_old = 0;
}
} }
} }
} }