/// <summary> /// Calculates frequency of a group of distinct pairs in nested collection. For custom types ensure you override /// GetHashCode when implementing IEquatable for correct grouping. /// </summary> /// <typeparam name="T">Type of base element</typeparam> /// <param name="nestedCollection">Collection that contains the pair interested in</param> /// <param name="pair">Pair of elements to find the frequency of in the nested collection</param> /// <param name="IsSorted"> If true: uses Binary search algorithm. Every sub collection must be sorted </param> /// <returns>Empty if either pair element is null. Else returns a single Pair<T> object containing the pair /// searched and its frequency</returns> public static IEnumerable <Pairs <T> > CalculatePairs <T>(this IEnumerable <IList <T> > nestedCollection, IEnumerable <IList <T> > pairGroup, bool IsSorted = false) where T : IComparable <T>, IEquatable <T> { var index = 0; foreach (var pair in pairGroup) { if (pair is null) { index++; continue; } var distinct = pair.Distinct().Where(x => x is object); if (distinct.Count() != 2) { throw new ArgumentException($"Group {index} of {nameof(pair)} in {nameof(pairGroup)} does not contain exactly two distinct values"); } if (IsSorted) { var frequency = SpecificCollectionSorted.CalculateFrequencyOfGroup(nestedCollection, pair); yield return(new Pairs <T> { Item = pair[0], Item2 = pair[1], Frequency = frequency }); } else { yield return(SpecificPairsUnSorted.CalculateSpecificPairsUnSorted(nestedCollection, pair)); } index++; } }
/// <summary> /// Calculates frequency of a group of distinct tripets in nested collection using deferred execution. For /// custom types ensure you override GetHashCode when implementing IEquatable for correct grouping. /// </summary> /// <typeparam name="T"> Type of base element in collections </typeparam> /// <param name="nestedCollection"> Nested collection to calculate triplet frequency from </param> /// <param name="tripletGroup"> /// Collection of sub collections of distinct triplets to find the frequency of in the nested collection. Null /// in place of sub collection are ignored. /// </param> /// <returns> IEnumerable of triplets objects in descending order of frequency </returns> /// <exception cref="ArgumentException"> /// Thrown when a sub collection does not contain exactly three distinct values. Includes nulls in sub collection. /// </exception> public static IEnumerable <Triplets <T> > CalculateTriplets <T>(this IEnumerable <IList <T> > nestedCollection, IEnumerable <IList <T> > tripletGroup, bool IsSorted = false) where T : IComparable <T>, IEquatable <T> { var index = 0; foreach (var triplet in tripletGroup) { if (triplet is null) { index++; continue; } var distinct = triplet.Distinct().Where(x => x is object); if (distinct.Count() != 3) { throw new ArgumentException($"Group {index} of {nameof(triplet)} in {nameof(tripletGroup)} does not contain exactly three distinct values"); } if (IsSorted) { var frequency = SpecificCollectionSorted.CalculateFrequencyOfGroup(nestedCollection, triplet); yield return(new Triplets <T> { Item = triplet[0], Item2 = triplet[1], Item3 = triplet[2], Frequency = frequency }); } else { yield return(SpecificTripletsUnSorted.CalculateSpecifictripletsUnSorted(nestedCollection, triplet)); } index++; } }
/// <summary> Calculates frequency of distinct tripets in nested collection. /// For custom types ensure you override GetHashCode when implementing IEquatable for correct grouping. /// </summary> /// <typeparam name="T"> Type of base element in collections </typeparam> /// <param name="nestedCollection"> Nested collection to calculate triplet frequency from </param> /// <param name="triplet"> /// Collection of distinct triplets to find the frequency of in the nested collection /// </param> /// <returns> Triplet object containing the triplet and its frequency </returns> public static Triplets <T> CalculateTriplets <T>(this IEnumerable <IList <T> > nestedCollection, IList <T> triplet, bool IsSorted = false) where T : IComparable <T>, IEquatable <T> { var distinctTriplet = triplet.Distinct().Where(x => x != null); if (distinctTriplet.Count() != 3) { throw new ArgumentException($"{nameof(triplet)} does not contain exactly three distinct values"); } if (IsSorted) { var frequency = SpecificCollectionSorted.CalculateFrequencyOfGroup(nestedCollection, triplet); return(new Triplets <T> { Item = triplet[0], Item2 = triplet[1], Item3 = triplet[2], Frequency = frequency }); } else { return(SpecificTripletsUnSorted.CalculateSpecifictripletsUnSorted(nestedCollection, triplet)); } }
/// <summary> /// Calculates frequency of distinct pairs in nested collection. For custom types ensure you override /// GetHashCode when implementing IEquatable for correct grouping. /// </summary> /// <typeparam name="T">Type of base element</typeparam> /// <param name="nestedCollection">Collection that contains the pair interested in</param> /// <param name="pair">Pair of elements to find the frequency of in the nested collection</param> /// <param name="IsSorted"> If true: uses Binary search algorithm. Every sub collection must be sorted </param> /// <returns>Empty if either pair element is null. Else returns a single Pair<T> object containing the pair /// searched and its frequency</returns> public static Pairs <T> CalculatePairs <T>(this IEnumerable <IList <T> > nestedCollection, IList <T> pair, bool IsSorted = false) where T : IComparable <T>, IEquatable <T> { var distinctPair = pair.Distinct().Where(x => x != null); if (distinctPair.Count() != 2) { throw new ArgumentException($"{nameof(pair)} does not contain exactly two distinct elements"); } if (IsSorted) { var frequency = SpecificCollectionSorted.CalculateFrequencyOfGroup(nestedCollection, pair); return(new Pairs <T> { Item = pair[0], Item2 = pair[1], Frequency = frequency }); } else { return(SpecificPairsUnSorted.CalculateSpecificPairsUnSorted(nestedCollection, pair)); } }