aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/OpenSim/Framework/UndoStack.cs
diff options
context:
space:
mode:
Diffstat (limited to 'OpenSim/Framework/UndoStack.cs')
-rw-r--r--OpenSim/Framework/UndoStack.cs128
1 files changed, 128 insertions, 0 deletions
diff --git a/OpenSim/Framework/UndoStack.cs b/OpenSim/Framework/UndoStack.cs
new file mode 100644
index 0000000..0b97644
--- /dev/null
+++ b/OpenSim/Framework/UndoStack.cs
@@ -0,0 +1,128 @@
1/*
2 * Copyright (c) Contributors, http://opensimulator.org/
3 * See CONTRIBUTORS.TXT for a full list of copyright holders.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 * * Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * * Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * * Neither the name of the OpenSim Project nor the
13 * names of its contributors may be used to endorse or promote products
14 * derived from this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE DEVELOPERS ``AS IS'' AND ANY
17 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE CONTRIBUTORS BE LIABLE FOR ANY
20 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28using System;
29using System.Collections.Generic;
30using System.Text;
31
32namespace OpenSim.Framework
33{
34 /// <summary>
35 /// Undo stack. Deletes entries beyond a certain capacity
36 /// </summary>
37 /// <typeparam name="T"></typeparam>
38 [Serializable]
39 public class UndoStack<T>
40 {
41
42 private int m_new = 1;
43 private int m_old = 0;
44 private T[] m_Undos;
45
46 public bool IsFull
47 {
48 get
49 {
50 return m_new == m_old;
51 }
52 }
53
54
55 public int Capacity
56 {
57 get
58 {
59 return m_Undos.Length - 1;
60 }
61 }
62
63
64 public UndoStack(int capacity)
65 {
66 m_Undos = new T[capacity + 1];
67 }
68
69 public void Push(T item)
70 {
71 if (IsFull)
72 {
73 m_old++;
74 if (m_old >= m_Undos.Length)
75 m_old -= m_Undos.Length;
76 }
77 if (++m_new >= m_Undos.Length)
78 m_new -= m_Undos.Length;
79 m_Undos[m_new] = item;
80 }
81
82 public T Pop()
83 {
84 if (Count > 0)
85 {
86 T deleted = m_Undos[m_new];
87 m_Undos[m_new--] = default(T);
88 if (m_new < 0)
89 m_new += m_Undos.Length;
90 return deleted;
91 }
92 else
93 throw new InvalidOperationException("Cannot pop from emtpy stack");
94 }
95
96 public int Count
97 {
98 get
99 {
100 int count = m_new - m_old - 1;
101 if (count < 0)
102 count += m_Undos.Length;
103 return count;
104 }
105 }
106
107
108 public T Peek()
109 {
110 return m_Undos[m_new];
111 }
112
113
114 public void Clear()
115 {
116 if (Count > 0)
117 {
118 for (int i = 0; i < m_Undos.Length; i++)
119 {
120 m_Undos[i] = default(T);
121 }
122 m_new = 1;
123 m_old = 0;
124 }
125 }
126
127 }
128} \ No newline at end of file