/// <summary>
        /// Linearly goes through all records looking for the highest and lowest record for given target property
        /// </summary>
        /// <typeparam name="T">Generic type for compiler to infer for the delegate</typeparam>
        /// <param name="region">Region's records to search through</param>
        /// <param name="targetProperty">for a property to find min max versions</param>
        public static void FindMinMax <T>(ref RecordCollection region, Func <SeismicRecord, T> targetProperty) where T : IComparable
        {
            //seed values to compare against
            T min = targetProperty(region.Records[0]);
            T max = targetProperty(region.Records[0]);
            //Indexes to retrieve the max and min records when the search is done
            int minIndex = 0; int maxIndex = 0;

            for (int i = 0; i < region.Records.currentCapacity; i++)
            {
                //when the current min is bigger than the next value in the array
                if (min.CompareTo(targetProperty(region.Records[i])) > 0)
                {
                    //set the current min to that lower value
                    min      = targetProperty(region.Records[i]);
                    maxIndex = i; //record the most up-to-date min record's position
                }
                //when the current max is lower than the next value in the array
                else if (max.CompareTo(targetProperty(region.Records[i])) < 0)
                {
                    //set the current max to higher value
                    max      = targetProperty(region.Records[i]);
                    minIndex = i;//record the most up-to-date max record's position
                }
            }

            //informs user for which parameter the min max request was and tells which column to look for.
            string paramName = targetProperty.Method.GetParameters()[0].Name;
            string maxOutput = RecordCollection.formatPrinter(paramName, region.Records[minIndex], targetProperty);
            string minOutput = RecordCollection.formatPrinter(paramName, region.Records[maxIndex], targetProperty);

            Console.WriteLine($"Minimum Value for {paramName.ToUpper()}:\n{minOutput}\n" +
                              $"Maximum Value for {paramName.ToUpper()}:\n{maxOutput}");
        }
Beispiel #2
0
        /// <summary>
        /// Helper method to print out all the found results in the BinarySearch
        /// </summary>
        /// <typeparam name="T">Generic type for the TResult in targetProperty</typeparam>
        /// <typeparam name="F">Generic type for the key that will be searched (Type T == Type F in this program)</typeparam>
        /// <param name="key">The reference to generic key type</param>
        /// <param name="region">Which array the operation to be performed on</param>
        /// <param name="targetProperty">Property that the search will be conducted on (month, day etc)</param>
        /// <param name="left">Lowest index boundry in the array</param>
        /// <param name="right">Highest index boundry in the array</param>
        public static void BinarySearchAll <T, F>(F key, int left, int right,
                                                  ref RecordCollection region, Func <SeismicRecord, T> targetProperty)
            where T : IComparable
            where F : IComparable
        {
            //array to capture the matched keys from the binary search
            RecordCollection foundVals = new RecordCollection();

            BinarySearch(key, left, right, ref region, ref foundVals, targetProperty);
            //if the array of found records is not empty
            if (foundVals.Records.currentCapacity != 0)
            {
                //if the record should be displayed with the corresponding fields
                if (region.CorrespondingFields == true)
                {
                    foundVals.Records.ListRecords();
                }
                else//if only the value for the record should be displaye
                {
                    //capturing the name of the lambda parameter in the delegate to determine which record field's format to return the key in
                    string paramName = targetProperty.Method.GetParameters()[0].Name;
                    //setting the string to the formatted key
                    string formatOutput = RecordCollection.formatPrinter(paramName, foundVals.Records[0], targetProperty);
                    //for each record present in foundVals
                    for (int i = 0; i < foundVals.Records.currentCapacity; i++)
                    {
                        Console.WriteLine(formatOutput);//output the record
                    }
                }
            }
            else //if the array is emptry, it means the BinarySearch yielded no results
            {
                Console.WriteLine($"'{key}' was not found among the records!");
            }

            //search yield information for the key
            Console.WriteLine($"Found records for '{key}' : {foundVals.Records.currentCapacity} records");
        }