/AvalonEdit/ICSharpCode.AvalonEdit/Utils/ImmutableStack.cs

http://github.com/icsharpcode/ILSpy · C# · 132 lines · 71 code · 13 blank · 48 comment · 4 complexity · 52876920a9ce8283727e3d01f7014827 MD5 · raw file

  1. // Copyright (c) 2014 AlphaSierraPapa for the SharpDevelop Team
  2. //
  3. // Permission is hereby granted, free of charge, to any person obtaining a copy of this
  4. // software and associated documentation files (the "Software"), to deal in the Software
  5. // without restriction, including without limitation the rights to use, copy, modify, merge,
  6. // publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons
  7. // to whom the Software is furnished to do so, subject to the following conditions:
  8. //
  9. // The above copyright notice and this permission notice shall be included in all copies or
  10. // substantial portions of the Software.
  11. //
  12. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
  13. // INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  14. // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE
  15. // FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  16. // OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  17. // DEALINGS IN THE SOFTWARE.
  18. using System;
  19. using System.Collections.Generic;
  20. using System.Diagnostics;
  21. using System.Text;
  22. namespace ICSharpCode.AvalonEdit.Utils
  23. {
  24. /// <summary>
  25. /// An immutable stack.
  26. ///
  27. /// Using 'foreach' on the stack will return the items from top to bottom (in the order they would be popped).
  28. /// </summary>
  29. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix")]
  30. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Naming", "CA1711:IdentifiersShouldNotHaveIncorrectSuffix")]
  31. [Serializable]
  32. public sealed class ImmutableStack<T> : IEnumerable<T>
  33. {
  34. /// <summary>
  35. /// Gets the empty stack instance.
  36. /// </summary>
  37. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2104:DoNotDeclareReadOnlyMutableReferenceTypes", Justification = "ImmutableStack is immutable")]
  38. [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1000:DoNotDeclareStaticMembersOnGenericTypes")]
  39. public static readonly ImmutableStack<T> Empty = new ImmutableStack<T>();
  40. readonly T value;
  41. readonly ImmutableStack<T> next;
  42. private ImmutableStack()
  43. {
  44. }
  45. private ImmutableStack(T value, ImmutableStack<T> next)
  46. {
  47. this.value = value;
  48. this.next = next;
  49. }
  50. /// <summary>
  51. /// Pushes an item on the stack. This does not modify the stack itself, but returns a new
  52. /// one with the value pushed.
  53. /// </summary>
  54. public ImmutableStack<T> Push(T item)
  55. {
  56. return new ImmutableStack<T>(item, this);
  57. }
  58. /// <summary>
  59. /// Gets the item on the top of the stack.
  60. /// </summary>
  61. /// <exception cref="InvalidOperationException">The stack is empty.</exception>
  62. public T Peek()
  63. {
  64. if (IsEmpty)
  65. throw new InvalidOperationException("Operation not valid on empty stack.");
  66. return value;
  67. }
  68. /// <summary>
  69. /// Gets the item on the top of the stack.
  70. /// Returns <c>default(T)</c> if the stack is empty.
  71. /// </summary>
  72. public T PeekOrDefault()
  73. {
  74. return value;
  75. }
  76. /// <summary>
  77. /// Gets the stack with the top item removed.
  78. /// </summary>
  79. /// <exception cref="InvalidOperationException">The stack is empty.</exception>
  80. public ImmutableStack<T> Pop()
  81. {
  82. if (IsEmpty)
  83. throw new InvalidOperationException("Operation not valid on empty stack.");
  84. return next;
  85. }
  86. /// <summary>
  87. /// Gets if this stack is empty.
  88. /// </summary>
  89. public bool IsEmpty {
  90. get { return next == null; }
  91. }
  92. /// <summary>
  93. /// Gets an enumerator that iterates through the stack top-to-bottom.
  94. /// </summary>
  95. public IEnumerator<T> GetEnumerator()
  96. {
  97. ImmutableStack<T> t = this;
  98. while (!t.IsEmpty) {
  99. yield return t.value;
  100. t = t.next;
  101. }
  102. }
  103. System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
  104. {
  105. return this.GetEnumerator();
  106. }
  107. /// <inheritdoc/>
  108. public override string ToString()
  109. {
  110. StringBuilder b = new StringBuilder("[Stack");
  111. foreach (T val in this) {
  112. b.Append(' ');
  113. b.Append(val);
  114. }
  115. b.Append(']');
  116. return b.ToString();
  117. }
  118. }
  119. }