using System; using System.Collections.Generic; namespace Terminal.Gui { /// /// Extension of helper to work with specific /// public static class StackExtensions { /// /// Replaces an stack object values that match with the value to replace. /// /// The stack object type. /// The stack object. /// Value to replace. /// Value to replace with to what matches the value to replace. /// The comparison object. public static void Replace (this Stack stack, T valueToReplace, T valueToReplaceWith, IEqualityComparer comparer = null) { comparer = comparer ?? EqualityComparer.Default; var temp = new Stack (); while (stack.Count > 0) { var value = stack.Pop (); if (comparer.Equals (value, valueToReplace)) { stack.Push (valueToReplaceWith); break; } temp.Push (value); } while (temp.Count > 0) stack.Push (temp.Pop ()); } /// /// Swap two stack objects values that matches with the both values. /// /// The stack object type. /// The stack object. /// Value to swap from. /// Value to swap to. /// The comparison object. public static void Swap (this Stack stack, T valueToSwapFrom, T valueToSwapTo, IEqualityComparer comparer = null) { comparer = comparer ?? EqualityComparer.Default; int index = stack.Count - 1; T [] stackArr = new T [stack.Count]; while (stack.Count > 0) { var value = stack.Pop (); if (comparer.Equals (value, valueToSwapFrom)) { stackArr [index] = valueToSwapTo; } else if (comparer.Equals (value, valueToSwapTo)) { stackArr [index] = valueToSwapFrom; } else { stackArr [index] = value; } index--; } for (int i = 0; i < stackArr.Length; i++) stack.Push (stackArr [i]); } /// /// Move the first stack object value to the end. /// /// The stack object type. /// The stack object. public static void MoveNext (this Stack stack) { var temp = new Stack (); var last = stack.Pop (); while (stack.Count > 0) { var value = stack.Pop (); temp.Push (value); } temp.Push (last); while (temp.Count > 0) stack.Push (temp.Pop ()); } /// /// Move the last stack object value to the top. /// /// The stack object type. /// The stack object. public static void MovePrevious (this Stack stack) { var temp = new Stack (); T first = default; while (stack.Count > 0) { var value = stack.Pop (); temp.Push (value); if (stack.Count == 1) { first = stack.Pop (); } } while (temp.Count > 0) stack.Push (temp.Pop ()); stack.Push (first); } /// /// Find all duplicates stack objects values. /// /// The stack object type. /// The stack object. /// The comparison object. /// The duplicates stack object. public static Stack FindDuplicates (this Stack stack, IEqualityComparer comparer = null) { comparer = comparer ?? EqualityComparer.Default; var dup = new Stack (); T [] stackArr = stack.ToArray (); for (int i = 0; i < stackArr.Length; i++) { var value = stackArr [i]; for (int j = i + 1; j < stackArr.Length; j++) { var valueToFind = stackArr [j]; if (comparer.Equals (value, valueToFind) && !Contains (dup, valueToFind)) { dup.Push (value); } } } return dup; } /// /// Check if the stack object contains the value to find. /// /// The stack object type. /// The stack object. /// Value to find. /// The comparison object. /// true If the value was found.false otherwise. public static bool Contains (this Stack stack, T valueToFind, IEqualityComparer comparer = null) { comparer = comparer ?? EqualityComparer.Default; foreach (T obj in stack) { if (comparer.Equals (obj, valueToFind)) { return true; } } return false; } /// /// Move the stack object value to the index. /// /// The stack object type. /// The stack object. /// Value to move. /// The index where to move. /// The comparison object. public static void MoveTo (this Stack stack, T valueToMove, int index = 0, IEqualityComparer comparer = null) { if (index < 0) { return; } comparer = comparer ?? EqualityComparer.Default; var temp = new Stack (); var toMove = default (T); var stackCount = stack.Count; var count = 0; while (stack.Count > 0) { var value = stack.Pop (); if (comparer.Equals (value, valueToMove)) { toMove = value; break; } temp.Push (value); count++; } int idx = 0; while (stack.Count < stackCount) { if (count - idx == index) { stack.Push (toMove); } else { stack.Push (temp.Pop ()); } idx++; } } } }