StackExtensions.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. using System;
  2. using System.Collections.Generic;
  3. namespace Terminal.Gui {
  4. /// <summary>
  5. /// Extension of <see cref="Stack{T}"/> helper to work with specific <see cref="IEqualityComparer{T}"/>
  6. /// </summary>
  7. public static class StackExtensions {
  8. /// <summary>
  9. /// Replaces an stack object values that match with the value to replace.
  10. /// </summary>
  11. /// <typeparam name="T">The stack object type.</typeparam>
  12. /// <param name="stack">The stack object.</param>
  13. /// <param name="valueToReplace">Value to replace.</param>
  14. /// <param name="valueToReplaceWith">Value to replace with to what matches the value to replace.</param>
  15. /// <param name="comparer">The comparison object.</param>
  16. public static void Replace<T> (this Stack<T> stack, T valueToReplace,
  17. T valueToReplaceWith, IEqualityComparer<T> comparer = null)
  18. {
  19. comparer = comparer ?? EqualityComparer<T>.Default;
  20. var temp = new Stack<T> ();
  21. while (stack.Count > 0) {
  22. var value = stack.Pop ();
  23. if (comparer.Equals (value, valueToReplace)) {
  24. stack.Push (valueToReplaceWith);
  25. break;
  26. }
  27. temp.Push (value);
  28. }
  29. while (temp.Count > 0)
  30. stack.Push (temp.Pop ());
  31. }
  32. /// <summary>
  33. /// Swap two stack objects values that matches with the both values.
  34. /// </summary>
  35. /// <typeparam name="T">The stack object type.</typeparam>
  36. /// <param name="stack">The stack object.</param>
  37. /// <param name="valueToSwapFrom">Value to swap from.</param>
  38. /// <param name="valueToSwapTo">Value to swap to.</param>
  39. /// <param name="comparer">The comparison object.</param>
  40. public static void Swap<T> (this Stack<T> stack, T valueToSwapFrom,
  41. T valueToSwapTo, IEqualityComparer<T> comparer = null)
  42. {
  43. comparer = comparer ?? EqualityComparer<T>.Default;
  44. int index = stack.Count - 1;
  45. T [] stackArr = new T [stack.Count];
  46. while (stack.Count > 0) {
  47. var value = stack.Pop ();
  48. if (comparer.Equals (value, valueToSwapFrom)) {
  49. stackArr [index] = valueToSwapTo;
  50. } else if (comparer.Equals (value, valueToSwapTo)) {
  51. stackArr [index] = valueToSwapFrom;
  52. } else {
  53. stackArr [index] = value;
  54. }
  55. index--;
  56. }
  57. for (int i = 0; i < stackArr.Length; i++)
  58. stack.Push (stackArr [i]);
  59. }
  60. /// <summary>
  61. /// Move the first stack object value to the end.
  62. /// </summary>
  63. /// <typeparam name="T">The stack object type.</typeparam>
  64. /// <param name="stack">The stack object.</param>
  65. public static void MoveNext<T> (this Stack<T> stack)
  66. {
  67. var temp = new Stack<T> ();
  68. var last = stack.Pop ();
  69. while (stack.Count > 0) {
  70. var value = stack.Pop ();
  71. temp.Push (value);
  72. }
  73. temp.Push (last);
  74. while (temp.Count > 0)
  75. stack.Push (temp.Pop ());
  76. }
  77. /// <summary>
  78. /// Move the last stack object value to the top.
  79. /// </summary>
  80. /// <typeparam name="T">The stack object type.</typeparam>
  81. /// <param name="stack">The stack object.</param>
  82. public static void MovePrevious<T> (this Stack<T> stack)
  83. {
  84. var temp = new Stack<T> ();
  85. T first = default;
  86. while (stack.Count > 0) {
  87. var value = stack.Pop ();
  88. temp.Push (value);
  89. if (stack.Count == 1) {
  90. first = stack.Pop ();
  91. }
  92. }
  93. while (temp.Count > 0)
  94. stack.Push (temp.Pop ());
  95. stack.Push (first);
  96. }
  97. /// <summary>
  98. /// Find all duplicates stack objects values.
  99. /// </summary>
  100. /// <typeparam name="T">The stack object type.</typeparam>
  101. /// <param name="stack">The stack object.</param>
  102. /// <param name="comparer">The comparison object.</param>
  103. /// <returns>The duplicates stack object.</returns>
  104. public static Stack<T> FindDuplicates<T> (this Stack<T> stack, IEqualityComparer<T> comparer = null)
  105. {
  106. comparer = comparer ?? EqualityComparer<T>.Default;
  107. var dup = new Stack<T> ();
  108. T [] stackArr = stack.ToArray ();
  109. for (int i = 0; i < stackArr.Length; i++) {
  110. var value = stackArr [i];
  111. for (int j = i + 1; j < stackArr.Length; j++) {
  112. var valueToFind = stackArr [j];
  113. if (comparer.Equals (value, valueToFind) && !Contains (dup, valueToFind)) {
  114. dup.Push (value);
  115. }
  116. }
  117. }
  118. return dup;
  119. }
  120. /// <summary>
  121. /// Check if the stack object contains the value to find.
  122. /// </summary>
  123. /// <typeparam name="T">The stack object type.</typeparam>
  124. /// <param name="stack">The stack object.</param>
  125. /// <param name="valueToFind">Value to find.</param>
  126. /// <param name="comparer">The comparison object.</param>
  127. /// <returns><c>true</c> If the value was found.<c>false</c> otherwise.</returns>
  128. public static bool Contains<T> (this Stack<T> stack, T valueToFind, IEqualityComparer<T> comparer = null)
  129. {
  130. comparer = comparer ?? EqualityComparer<T>.Default;
  131. foreach (T obj in stack) {
  132. if (comparer.Equals (obj, valueToFind)) {
  133. return true;
  134. }
  135. }
  136. return false;
  137. }
  138. /// <summary>
  139. /// Move the stack object value to the index.
  140. /// </summary>
  141. /// <typeparam name="T">The stack object type.</typeparam>
  142. /// <param name="stack">The stack object.</param>
  143. /// <param name="valueToMove">Value to move.</param>
  144. /// <param name="index">The index where to move.</param>
  145. /// <param name="comparer">The comparison object.</param>
  146. public static void MoveTo<T> (this Stack<T> stack, T valueToMove, int index = 0,
  147. IEqualityComparer<T> comparer = null)
  148. {
  149. if (index < 0) {
  150. return;
  151. }
  152. comparer = comparer ?? EqualityComparer<T>.Default;
  153. var temp = new Stack<T> ();
  154. var toMove = default (T);
  155. var stackCount = stack.Count;
  156. var count = 0;
  157. while (stack.Count > 0) {
  158. var value = stack.Pop ();
  159. if (comparer.Equals (value, valueToMove)) {
  160. toMove = value;
  161. break;
  162. }
  163. temp.Push (value);
  164. count++;
  165. }
  166. int idx = 0;
  167. while (stack.Count < stackCount) {
  168. if (count - idx == index) {
  169. stack.Push (toMove);
  170. } else {
  171. stack.Push (temp.Pop ());
  172. }
  173. idx++;
  174. }
  175. }
  176. }
  177. }