/// <summary>
        /// Uses binary search algorithm to find a distinct group of specific items in a nested collection that contains
        /// sorted sub collections
        /// </summary>
        /// <typeparam name="T"> Type of base element </typeparam>
        /// <param name="nestedCollection"> Collection of collections to iterate through to find distinct group frequency </param>
        /// <param name="group">            group of elements to find the frequency of </param>
        /// <returns> New group object of the group to find and their frequency </returns>
        internal static int CalculateFrequencyOfGroup <T>(IEnumerable <IList <T> > nestedCollection,
                                                          IList <T> group) where T : IComparable <T>, IEquatable <T>
        {
            int count = 0;

            foreach (var collection in nestedCollection)
            {
                var length       = collection.Count;
                var groupCounter = 0;

                for (var i = 0; i < group.Count; i++)
                {
                    bool found = false;
                    int  index = BinarySearchHelper.BinarySearch(collection, 0, length - 1, group[i]);

                    if (index >= 0)
                    {
                        found = true;
                    }

                    // If the value is not found in the collection
                    if (!found)
                    {
                        continue;
                    }
                    else
                    {
                        groupCounter++;
                    }
                }
                if (groupCounter == group.Count)
                {
                    count++;
                }
            }
            return(count);
        }
Beispiel #2
0
        /// <summary>
        /// Uses binary search algorithm to find the frequency of a single value when passed a flat sorted collection.
        /// </summary>
        /// <typeparam name="T"> Primitive type of the elements in collection </typeparam>
        /// <param name="collection"> Flat sorted collection passed </param>
        /// <param name="value">      Value to find the frequency of </param>
        /// <returns>
        /// New Singles object with the value and number of occurrences. Will return Item as null if passed null
        /// </returns>
        internal static Singles <T> CalculateSinglesSorted <T>(IList <T> collection, T value) where T : IComparable <T>, IEquatable <T>
        {
            var length = collection.Count;
            int index  = BinarySearchHelper.BinarySearch(collection, 0, length - 1, value);

            // If the value is not found in the collection
            if (index == -1)
            {
                return(new Singles <T> {
                    Item = value, Frequency = 0
                });
            }

            // When an element is found count all occurrences to the left
            var count = 1;
            var left  = index - 1;

            while (left >= 0 && collection[left].Equals(value))
            {
                count++;
                left--;
            }

            // Then count all occurrences to the right
            var right = index + 1;

            while (right < length && collection[right].Equals(value))
            {
                count++;
                right++;
            }

            return(new Singles <T> {
                Item = value, Frequency = count
            });
        }