StackExtensions.cs 6.8 KB

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