UndoStack.cs 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 OpenSimulator 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. using System;
  28. namespace OpenSim.Framework
  29. {
  30. /// <summary>
  31. /// Undo stack. Deletes entries beyond a certain capacity
  32. /// </summary>
  33. /// <typeparam name="T"></typeparam>
  34. [Serializable]
  35. public class UndoStack<T>
  36. {
  37. private int m_new = 1;
  38. private int m_old = 0;
  39. private T[] m_Undos;
  40. public UndoStack(int capacity)
  41. {
  42. m_Undos = new T[capacity + 1];
  43. }
  44. public bool IsFull
  45. {
  46. get { return m_new == m_old; }
  47. }
  48. public int Capacity
  49. {
  50. get { return m_Undos.Length - 1; }
  51. }
  52. public int Count
  53. {
  54. get
  55. {
  56. int count = m_new - m_old - 1;
  57. if (count < 0)
  58. count += m_Undos.Length;
  59. return count;
  60. }
  61. }
  62. public void Push(T item)
  63. {
  64. if (IsFull)
  65. {
  66. m_old++;
  67. if (m_old >= m_Undos.Length)
  68. m_old -= m_Undos.Length;
  69. }
  70. if (++m_new >= m_Undos.Length)
  71. m_new -= m_Undos.Length;
  72. m_Undos[m_new] = item;
  73. }
  74. public T Pop()
  75. {
  76. if (Count > 0)
  77. {
  78. T deleted = m_Undos[m_new];
  79. m_Undos[m_new--] = default(T);
  80. if (m_new < 0)
  81. m_new += m_Undos.Length;
  82. return deleted;
  83. }
  84. else
  85. throw new InvalidOperationException("Cannot pop from empty stack");
  86. }
  87. public T Peek()
  88. {
  89. return m_Undos[m_new];
  90. }
  91. public void Clear()
  92. {
  93. if (Count > 0)
  94. {
  95. for (int i = 0; i < m_Undos.Length; i++)
  96. {
  97. m_Undos[i] = default(T);
  98. }
  99. m_new = 1;
  100. m_old = 0;
  101. }
  102. }
  103. }
  104. }