Beispiel #1
0
        /// <summary>
        /// Determines if there was a shift between 2 extreme points
        /// </summary>
        /// <param name="p1">First extreme point</param>
        /// <param name="p2">Second extreme point</param>
        /// <returns>If there was a shift or not</returns>
        private static bool IsShift(ExtremeType p1, ExtremeType p2)
        {
            // if points are not defined or are of the same type
            if (p1 == ExtremeType.None || p2 == ExtremeType.None || p1 == p2)
            {
                return(false);
            }

            // that is a shift
            return(true);
        }
Beispiel #2
0
        public PriorityQueue(IEnumerable <T> values, ExtremeType type, IComparer <T> comparer)
        {
            CodeContract.RequiresArgumentNotNull(comparer, "comparer");

            m_comparer = comparer;

            switch (type)
            {
            case ExtremeType.Minimum:
                m_inRightOrder = Less;
                break;

            case ExtremeType.Maximum:
                m_inRightOrder = Greater;
                break;

            default:
                throw new ArgumentOutOfRangeException("type", "ExtremeType can only be Minmun or Maximum");
            }

            m_indexDict = new Dictionary <T, int>();

            //initialize prioirty queue heap
            if (values != null)
            {
                m_binaryHeap = new List <T>(values);

                for (int i = 0; i < m_binaryHeap.Count; i++)
                {
                    m_indexDict.Add(m_binaryHeap[i], i);
                }
            }
            else
            {
                m_binaryHeap = new List <T>();
            }


            InitializeHeap();
        }
Beispiel #3
0
        /// <summary>
        /// Finds peaks and valleys in certain histogram
        /// </summary>
        /// <param name="histogram">Histogram involved</param>
        /// <returns>Histogram's peaks and valleys</returns>
        /// <remarks>Does not perform histogram normalization</remarks>
        public static KeyValuePair <List <KeyValuePair <int, int> >, List <KeyValuePair <int, int> > > HistFind(double[] histogram)
        {
            // using var because static types are so darn long
            var peaks   = new List <KeyValuePair <int, int> >();
            var valleys = new List <KeyValuePair <int, int> >();
            var result  = new KeyValuePair <List <KeyValuePair <int, int> >, List <KeyValuePair <int, int> > >(peaks, valleys);

            // setting interval size
            int dx = Dx;

            // this is because there's always point shared betwwen intervals
            int offset = dx - 1;

            // there can not exist peaks or valleys (there's at most one interval)
            if (dx < 2 || histogram.Length < 2 * offset + 1)
            {
                return(result);
            }

            // setting the smoothness of the curve
            int smoothness = Smoothness;

            // Ip: previous interval (Ip = [a, b])
            // Ic: current interval  (Ic = [b, c])
            int a = 0;
            int b = offset;
            int c = b + offset;

            // analyzing Ip monotony
            Monotony ip_monotony = FindMonotony(histogram[a], histogram[b], dx);

            // setting previous extreme point
            ExtremeType previous_extreme       = ExtremeType.None;
            int         previous_extreme_index = 1;

            if (ip_monotony == Monotony.Abate)
            {
                previous_extreme = ExtremeType.Max;
            }
            else if (ip_monotony == Monotony.Grow)
            {
                previous_extreme = ExtremeType.Min;
            }

            // setting current extreme point
            ExtremeType current_extreme       = ExtremeType.None;
            int         current_extreme_index = 0;

            // setting the first extreme point of the last registered shift
            ExtremeType previous_shift_extreme       = ExtremeType.None;
            int         previous_shift_extreme_index = 0;

            // while the current interval is in range
            while (c < histogram.Length)
            {
                // analyzing the monotony in Ic
                Monotony ic_monotony = FindMonotony(histogram[b], histogram[c], dx);

                // if there was a change in the monotony => there's a extreme
                if (ip_monotony != ic_monotony)
                {
                    // classifying the new extreme founded
                    current_extreme       = GetExtremeType(ip_monotony, ic_monotony);
                    current_extreme_index = b;

                    // if there was a shift indeed
                    if (current_extreme != previous_extreme /*&& previous_extreme != ExtremeType.None*/)
                    {
                        // if this is not the first shift and phenomenon is smooth enough
                        if (previous_shift_extreme != ExtremeType.None && (current_extreme_index - previous_shift_extreme_index) / dx >= smoothness)
                        {
                            // we are in the presence of a VALLEY
                            if (previous_shift_extreme == ExtremeType.Max && current_extreme == ExtremeType.Max)
                            {
                                valleys.Add(new KeyValuePair <int, int>(previous_shift_extreme_index, current_extreme_index));
                            }
                            // we are in the presence of a PEAK
                            else if (previous_shift_extreme == ExtremeType.Min && current_extreme == ExtremeType.Min)
                            {
                                peaks.Add(new KeyValuePair <int, int>(previous_shift_extreme_index, current_extreme_index));
                            }
                        }

                        // update previous shift extreme
                        previous_shift_extreme       = previous_extreme;
                        previous_shift_extreme_index = previous_extreme_index;
                    }

                    // updating previous extreme point
                    previous_extreme       = current_extreme;
                    previous_extreme_index = current_extreme_index;
                }

                // updating intervals
                a  = b;
                b  = c;
                c += offset;

                // updating last interval monotony
                ip_monotony = ic_monotony;
            }

            return(result);
        }
Beispiel #4
0
 public PriorityQueue(ExtremeType type) : this(null, type, Comparer <T> .Default)
 {
 }
Beispiel #5
0
 public PriorityQueue(IEnumerable <T> values, ExtremeType type) : this(values, type, Comparer <T> .Default)
 {
 }