aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/UndoStack.cs
diff options
context:
space:
mode:
authorJustin Clark-Casey (justincc)2011-07-18 02:01:12 +0100
committerJustin Clark-Casey (justincc)2011-07-18 02:01:12 +0100
commit3f8e571b7887758514645c46b2b26d7c3fc82e45 (patch)
tree446912d24ef58adc3b30e59823f2a342385fcf48 /OpenSim/Framework/UndoStack.cs
parentMerge branch 'master' of ssh://opensimulator.org/var/git/opensim (diff)
downloadopensim-SC-3f8e571b7887758514645c46b2b26d7c3fc82e45.zip
opensim-SC-3f8e571b7887758514645c46b2b26d7c3fc82e45.tar.gz
opensim-SC-3f8e571b7887758514645c46b2b26d7c3fc82e45.tar.bz2
opensim-SC-3f8e571b7887758514645c46b2b26d7c3fc82e45.tar.xz
Use a standard generic system stack for the undo/redo stacks instead of our own homebrew.
system stack also uses an array, so no performance penalty. Also exposes undo count and adds a test assertion for correct undo count after resize
Diffstat (limited to '')
-rw-r--r--OpenSim/Framework/UndoStack.cs40
1 files changed, 39 insertions, 1 deletions
diff --git a/OpenSim/Framework/UndoStack.cs b/OpenSim/Framework/UndoStack.cs
index fde63b1..8f8849d 100644
--- a/OpenSim/Framework/UndoStack.cs
+++ b/OpenSim/Framework/UndoStack.cs
@@ -45,59 +45,96 @@ namespace OpenSim.Framework
45 m_Undos = new T[capacity + 1]; 45 m_Undos = new T[capacity + 1];
46 } 46 }
47 47
48 /// <summary>
49 /// Is the stack full?
50 /// </summary>
48 public bool IsFull 51 public bool IsFull
49 { 52 {
50 get { return m_new == m_old; } 53 get
54 {
55 // If the old and new pointers are in the same place then all stack slots are occupied.
56 return m_new == m_old;
57 }
51 } 58 }
52 59
60 /// <summary>
61 /// Capacity of the stack.
62 /// </summary>
53 public int Capacity 63 public int Capacity
54 { 64 {
55 get { return m_Undos.Length - 1; } 65 get { return m_Undos.Length - 1; }
56 } 66 }
57 67
68 /// <summary>
69 /// Return the number of undos on the stack.
70 /// </summary>
58 public int Count 71 public int Count
59 { 72 {
60 get 73 get
61 { 74 {
62 int count = m_new - m_old - 1; 75 int count = m_new - m_old - 1;
76
63 if (count < 0) 77 if (count < 0)
64 count += m_Undos.Length; 78 count += m_Undos.Length;
79
65 return count; 80 return count;
66 } 81 }
67 } 82 }
68 83
84 /// <summary>
85 /// Push a new undo onto the stack.
86 /// </summary>
87 /// <param name="item"></param>
69 public void Push(T item) 88 public void Push(T item)
70 { 89 {
71 if (IsFull) 90 if (IsFull)
72 { 91 {
73 m_old++; 92 m_old++;
93
74 if (m_old >= m_Undos.Length) 94 if (m_old >= m_Undos.Length)
75 m_old -= m_Undos.Length; 95 m_old -= m_Undos.Length;
76 } 96 }
97
77 if (++m_new >= m_Undos.Length) 98 if (++m_new >= m_Undos.Length)
78 m_new -= m_Undos.Length; 99 m_new -= m_Undos.Length;
100
79 m_Undos[m_new] = item; 101 m_Undos[m_new] = item;
80 } 102 }
81 103
104 /// <summary>
105 /// Pop and item from the top of the undo stack.
106 /// </summary>
107 /// <returns></returns>
82 public T Pop() 108 public T Pop()
83 { 109 {
84 if (Count > 0) 110 if (Count > 0)
85 { 111 {
86 T deleted = m_Undos[m_new]; 112 T deleted = m_Undos[m_new];
87 m_Undos[m_new--] = default(T); 113 m_Undos[m_new--] = default(T);
114
88 if (m_new < 0) 115 if (m_new < 0)
89 m_new += m_Undos.Length; 116 m_new += m_Undos.Length;
117
90 return deleted; 118 return deleted;
91 } 119 }
92 else 120 else
121 {
93 throw new InvalidOperationException("Cannot pop from empty stack"); 122 throw new InvalidOperationException("Cannot pop from empty stack");
123 }
94 } 124 }
95 125
126 /// <summary>
127 /// Peek at the undo on the top of the stack.
128 /// </summary>
129 /// <returns></returns>
96 public T Peek() 130 public T Peek()
97 { 131 {
98 return m_Undos[m_new]; 132 return m_Undos[m_new];
99 } 133 }
100 134
135 /// <summary>
136 /// Clear the stack.
137 /// </summary>
101 public void Clear() 138 public void Clear()
102 { 139 {
103 if (Count > 0) 140 if (Count > 0)
@@ -106,6 +143,7 @@ namespace OpenSim.Framework
106 { 143 {
107 m_Undos[i] = default(T); 144 m_Undos[i] = default(T);
108 } 145 }
146
109 m_new = 1; 147 m_new = 1;
110 m_old = 0; 148 m_old = 0;
111 } 149 }