/// <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>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> /// <remarks>Use the StepperBreak overload if possible. It is more effiecient.</remarks> public static bool ContainsDuplicates <T>(this Stepper <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; } else { set.Add(x); } }); 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> /// <remarks>Use the StepperBreak overload if possible. It is more effiecient.</remarks> public static bool ContainsDuplicates <T>(this Stepper <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; } else { set.Add(item); } }); return(duplicateFound); }
/// <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; SetHashArray <T> set = new SetHashArray <T>(equate, hash); stepper(x => { if (set.Contains(x)) { duplicateFound = true; return(StepStatus.Break); } else { set.Add(x); 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); }