/// <summary>Determines if the stepper contains any of the predicated values.</summary> /// <typeparam name="T">The generic type of the stepper.</typeparam> /// <param name="stepper">The stepper to determine if any predicated values exist.</param> /// <param name="where">The predicate.</param> /// <returns>True if any of the predicated values exist or </returns> public static bool Any <T>(this StepperBreak <T> stepper, Predicate <T> where) { bool any = false; stepper(x => (any = where (x)) ? Break : Continue); return(any); }
/// <summary>Checks to see if a given object is in this data structure.</summary> /// <typeparam name="T">The generic type stored in the structure.</typeparam> /// <typeparam name="K">The type of the key to look up.</typeparam> /// <param name="stepper">The structure to check against.</param> /// <param name="key">The key to check for.</param> /// <param name="equate">Delegate for equating two instances of different types.</param> /// <returns>true if the item is in this structure; false if not.</returns> public static bool Contains <T, K>(this StepperBreak <T> stepper, K key, Equate <T, K> equate) { bool contains = false; stepper((T step) => { if (equate(step, key)) { contains = true; return(StepStatus.Break); } return(StepStatus.Continue); }); return(contains); }
/// <summary>Trys to look up an item this structure by a given key.</summary> /// <typeparam name="T">The generic type stored in the structure.</typeparam> /// <typeparam name="K">The type of the key to look up.</typeparam> /// <param name="stepper">The structure to check against.</param> /// <param name="key">The key to look up.</param> /// <param name="equate">Delegate for equating two instances of different types.</param> /// <param name="item">The item if it was found or null if not the default(Type) value.</param> /// <returns>true if the key was found; false if the key was not found.</returns> public static bool TryGet <T, K>(this StepperBreak <T> stepper, K key, Equate <T, K> equate, out T item) { bool contains = false; T temp = default(T); stepper((T step) => { if (equate(step, key)) { contains = true; temp = step; return(StepStatus.Break); } return(StepStatus.Continue); }); item = temp; return(contains); }
/// <summary>Determines if the data contains any duplicates.</summary> /// <typeparam name="T">The generic type of the data.</typeparam> /// <param name="stepper">The stepper function for the data.</param> /// <param name="equate">An equality function for the data</param> /// <param name="hash">A hashing function for the data.</param> /// <returns>True if the data contains duplicates. False if not.</returns> public static bool ContainsDuplicates <T>(this StepperBreak <T> stepper, Equate <T> equate, Hash <T> hash) { bool duplicateFound = false; SetHashLinked <T> set = new SetHashLinked <T>(equate, hash); stepper(x => { if (set.Contains(x)) { duplicateFound = true; return(Break); } else { set.Add(x); return(Continue); } }); return(duplicateFound); }
/// <summary> /// Determines if the data contains any duplicates. /// </summary> /// <typeparam name="T">The generic type of the data.</typeparam> /// <param name="stepper">The stepper function for the data.</param> /// <param name="equate">An equality function for the data</param> /// <param name="hash">A hashing function for the data.</param> /// <returns>True if the data contains duplicates. False if not.</returns> public static bool ContainsDuplicates <T>(this StepperBreak <T> stepper, Equate <T> equate, Hash <T> hash) { bool duplicateFound = false; Towel.DataStructures.SetHashArray <T> set = new Towel.DataStructures.SetHashArray <T>(equate, hash); stepper((T item) => { if (set.Contains(item)) { duplicateFound = true; return(StepStatus.Break); } else { set.Add(item); return(StepStatus.Continue); } }); return(duplicateFound); }
/// <summary>Looks up an item this structure by a given key.</summary> /// <typeparam name="T">The generic type stored in the structure.</typeparam> /// <typeparam name="K">The type of the key to look up.</typeparam> /// <param name="stepper">The structure to check against.</param> /// <param name="key">The key to look up.</param> /// <param name="equate">Delegate for equating two instances of different types.</param> /// <returns>The item with the corresponding to the given key.</returns> public static T Get <T, K>(this StepperBreak <T> stepper, K key, Equate <T, K> equate) { bool contains = false; T item = default(T); stepper((T step) => { if (equate(step, key)) { contains = true; item = step; return(StepStatus.Break); } return(StepStatus.Continue); }); if (contains == false) { throw new InvalidOperationException("item not found in structure"); } return(item); }
/// <summary>Determines if the data contains any duplicates.</summary> /// <typeparam name="T">The generic type of the data.</typeparam> /// <param name="stepper">The stepper function for the data.</param> /// <returns>True if the data contains duplicates. False if not.</returns> public static bool ContainsDuplicates <T>(this StepperBreak <T> stepper) => ContainsDuplicates(stepper, Equate.Default, Hash.Default);
static void Main() { Console.WriteLine("You are runnning the BasicsAndExtensions example."); Console.WriteLine("============================================"); Console.WriteLine(); #region TryParse { TryParse("123.4", out double a); TryParse("12.3", out float b); TryParse("1", out byte c); TryParse("1234", out int d); TryParse("1234", out Program e); TryParse("Red", out ConsoleColor f); TryParse("Ordinal", out StringComparison g); Console.WriteLine(" TryParse------------------------------------"); Console.WriteLine(); Console.WriteLine(" TryParse(\"123.4\", out double a) := " + a + "d"); Console.WriteLine(" TryParse(\"12.3\", out float b) := " + b + "f"); Console.WriteLine(" TryParse(\"1\", out byte c) := " + c); Console.WriteLine(" TryParse(\"1234\", out int d) := " + d); Console.WriteLine(" TryParse(\"1234\", out Program e) := " + (e?.ToString() ?? "null")); Console.WriteLine(" TryParse(\"Red\", out ConsoleColor f) := " + f); Console.WriteLine(" TryParse(\"Ordinal\", out StringComparison g) := " + g); Console.WriteLine(); } #endregion #region Convert { double a = Convert <int, double>(1234); float b = Convert <int, float>(123); int c = Convert <double, int>(123.4d); int d = Convert <float, int>(12.3f); Console.WriteLine(" Convert------------------------------------"); Console.WriteLine(); Console.WriteLine(" Convert<int, double>(1234) := " + a + "d"); Console.WriteLine(" Convert<int, float>(123) := " + b + "f"); Console.WriteLine(" Convert<double, int>(123.4d) := " + c); Console.WriteLine(" Convert<float, int>(12.3f) := " + d); Console.WriteLine(); } #endregion #region Stepper { Console.WriteLine(" Stepper------------------------------------"); Console.WriteLine(); Console.WriteLine(" A Towel.Stepper<T> in Towel is similar to a "); Console.WriteLine(" System.Collections.Generic.IEnumerable<T> but"); Console.WriteLine(" it uses delegates rather than an enumerator."); Console.WriteLine(" There are pros/cons to both methodologies."); Console.WriteLine(); System.Collections.Generic.IEnumerable <int> iEnumerable = Ɐ(1, 2, 3); Console.Write(" iEnumerable values:"); foreach (int value in iEnumerable) { Console.Write(" " + value); } Console.WriteLine(); Stepper <int> stepper = Ɐ(1, 2, 3); Console.Write(" stepper values:"); stepper(value => Console.Write(" " + value)); Console.WriteLine(); /// You can "break" a foreach loop, but you cannot break a stepper traversal. /// For this, there is another type of stepper that is breakable. "Towel.StepperBreak<T>" /// is a breakable version of the stepper. StepperBreak <int> stepperBreak = Ɐ(1, 2, 3, 4, 5, 6); Console.Write(" stepperBreak values:"); stepperBreak(value => { Console.Write(" " + value); return(value >= 3 ? Break : Continue); }); Console.WriteLine(); /// You cannot alter the values of an IEnumerable during iteration, however, /// you can do so with a "Towel.StepperRef<T>". StepperRef <int> stepperRef = Ɐ(0, 1, 2); Console.Write(" stepperRef values:"); stepperRef((ref int value) => { value++; Console.Write(" " + value); }); Console.WriteLine(); /// The "Towel.StepperRefBreak<T>" is a stepper type that allows for altering /// values and breaking iteration. StepperRefBreak <int> stepperRefBreak = Ɐ(0, 1, 2, 3, 4, 5); Console.Write(" stepperRefBreak values:"); stepperRefBreak((ref int value) => { value++; Console.Write(" " + value); return(value >= 3 ? Break : Continue); }); Console.WriteLine();