CompareTo() public method

public CompareTo ( other ) : int
return int
Example #1
0
        public void When_Range_is_compared_to_another_Range_Then_ranges_are_sorted_by_their_minimum()
        {
            Range <float> range = new Range <float>(-10, 10);

            Assert.That(range.CompareTo(new Range <float>(1, 2)), Is.EqualTo(-1));
            Assert.That(range.CompareTo(range), Is.Zero);
            Assert.That(range.CompareTo(new Range <float>(-11, -10)), Is.EqualTo(1));
        }
Example #2
0
        public void When_ranges_are_compared_Then_range_minimum_value_is_being_used()
        {
            Range <float> range = new Range <float>(-10, 10);

            Assert.That(range.CompareTo((object)new Range <float>(11, 100)), Is.EqualTo(-1));
            Assert.That(range.CompareTo((object)new Range <float>(9, 10)), Is.EqualTo(-1));
            Assert.That(range.CompareTo((object)new Range <float>(-9, -9)), Is.EqualTo(-1));
            Assert.That(range.CompareTo((object)new Range <float>(-10, 0)), Is.Zero);
            Assert.That(range.CompareTo((object)new Range <float>(-9, -9)), Is.EqualTo(-1));

            Assert.That(range.CompareTo((object)-10f), Is.Zero);
            Assert.That(range.CompareTo((object)1f), Is.EqualTo(-1));
            Assert.That(range.CompareTo((object)-100f), Is.EqualTo(1));

            Assert.That(range.CompareTo((object)null), Is.EqualTo(1));
            Assert.That(range.CompareTo((object)range), Is.Zero);
        }
Example #3
0
        public void RangeCompareToAbove()
        {
            var range = new Range <int>(3, 10);
            int value = 12;

            int comp = range.CompareTo(value);

            Assert.AreEqual(1, comp);
        }
Example #4
0
        public void RangeCompareToWithin()
        {
            var range = new Range <int>(3, 10);
            int value = 5;

            int comp = range.CompareTo(value);

            Assert.AreEqual(0, comp);
        }
Example #5
0
        public void RangeCompareToBelow()
        {
            var range = new Range <int>(3, 10);
            int value = 1;

            int comp = range.CompareTo(value);

            Assert.AreEqual(-1, comp);
        }
        int IComparable.CompareTo(object obj)
        {
            int lnCompare = Range.CompareTo((obj as DataGridDataItem).Range);

            if (lnCompare == 0)
            {
                return(Parent_mountain.CompareTo((obj as DataGridDataItem).Parent_mountain));
            }
            else
            {
                return(lnCompare);
            }
        }
Example #7
0
        /// <summary>
        /// Keep an index array which will have index to the current pointer for each matrix rows.
        /// If the matrix has k rows, Maintain a minheap of size k to get the minimum. Also keep the maxValue
        /// in a memory location and keep updating it as we add new elements in the minheap.
        ///
        /// Keep Extracting the min value from minheap and increase the pointer in the row where we get the minValue to point to the next element
        /// Add this element into the minheap and get the new min.
        /// During this operation, keep checking whether current range is less than the minRange. If yes, update minRange.
        /// Keep doing this till one of the rows in indexArr hits the end of the row. And return the minRange.
        ///
        /// The running time of this operation is O(nlog(k))
        /// where n is the total number of elements in the matrix and k is the number of rows of the matrix
        /// Use of minheap reduces the running time to O(nlog(k))
        ///
        /// The space requirement is O(k) to maintain the arrIndex array.
        ///
        /// </summary>
        /// <param name="mat"></param>
        /// <returns></returns>
        public Range GetMinRange(int[,] mat)
        {
            int[] indexArr = new int[mat.GetLength(0)];
            int   maxVal   = int.MinValue;
            Range minRange = null;

            PriorityQueue <int> pq = new PriorityQueue <int>(mat.GetLength(0));

            // Add the least element (at index 0) from each row into the priority queue
            for (int i = 0; i < mat.GetLength(0); i++)
            {
                if (mat[i, 0] > maxVal)
                {
                    maxVal = mat[i, 0];
                }
                pq.Insert(i, mat[i, 0]);
            }

            while (pq.Count == mat.GetLength(0))
            {
                // Extract the min and check if a min Range is reached.
                int   rowIndex     = pq.ExtractMin();
                Range currentRange = new Range(mat[rowIndex, indexArr[rowIndex]], maxVal);
                if (minRange == null || minRange.CompareTo(currentRange) > 0)
                {
                    minRange = currentRange;
                }

                indexArr[rowIndex] += 1;
                if (indexArr[rowIndex] >= mat.GetLength(1))
                {
                    // this condition means that we have reached at the end of one of the rows
                    // No need to check more, as we need elements from all the rows
                    break;
                }

                // Add the next min element in the priority queue and update the maxVal
                pq.Insert(rowIndex, mat[rowIndex, indexArr[rowIndex]]);
                if (mat[rowIndex, indexArr[rowIndex]] > maxVal)
                {
                    maxVal = mat[rowIndex, indexArr[rowIndex]];
                }
            }

            return(minRange);
        }
Example #8
0
 public int CompareTo(AsmBlock rhs)
 {
     return(Range.CompareTo(rhs.Range));
 }
Example #9
0
 /// <summary>
 /// Compares the current object with another object of the same type.
 /// </summary>
 /// <returns>
 /// A value that indicates the relative order of the objects being compared. The return value has the following meanings: Value Meaning Less than zero This object is less than the <paramref name="other" /> parameter.Zero This object is equal to <paramref name="other" />. Greater than zero This object is greater than <paramref name="other" />.
 /// </returns>
 /// <param name="other">An object to compare with this object.</param>
 public Int32 CompareTo(Ip2GeoLocation other)
 {
     return(addressRange.CompareTo(other.addressRange));
 }
Example #10
0
        public void RangeCompareToWithin()
        {
            var range = new Range<int>(3, 10);
            int value = 5;

            int comp = range.CompareTo(value);

            Assert.AreEqual(0, comp);
        }
Example #11
0
 /// <summary>
 /// Compares where the given <see cref="RangeValueType"/> is to the range.
 /// </summary>
 /// <param name="other">The point to compare to the range.</param>
 /// <returns>Return can be {-1; 0; 1}. -1 equals (given <see cref="Byte"/> under the range); 0 equals (given <see cref="Byte"/> in the range) or 1 equals (given <see cref="Byte"/> over the range).</returns>
 public int CompareTo(Byte other)
 {
     return(TheRange.CompareTo(other));
 }
Example #12
0
        /// <inheritdoc />
        public int CompareTo(TimeRate other)
        {
            int n = Range.CompareTo(other.Range);

            return(n != 0 ? n : Rate.CompareTo(other.Rate));
        }
Example #13
0
 /// <summary>
 /// Compares where the given <see cref="RangeValueType"/> is to the range.
 /// </summary>
 /// <param name="other">The point to compare to the range.</param>
 /// <returns>Return can be {-1; 0; 1}. -1 equals (given <see cref="UInt64"/> under the range); 0 equals (given <see cref="UInt64"/> in the range) or 1 equals (given <see cref="UInt64"/> over the range).</returns>
 public int CompareTo(UInt64 other)
 {
     return(TheRange.CompareTo(other));
 }
Example #14
0
 public int CompareTo(FieldRecord other)
 {
     return(Range.CompareTo(other.Range));
 }
Example #15
0
 public int CompareTo(AsmFunction rhs)
 {
     return Range.CompareTo(rhs.Range);
 }
Example #16
0
 /// <summary>
 /// Compares where the given <see cref="RangeValueType"/> is to the range.
 /// </summary>
 /// <param name="other">The point to compare to the range.</param>
 /// <returns>Return can be {-1; 0; 1}. -1 equals (given <see cref="Int16"/> under the range); 0 equals (given <see cref="Int16"/> in the range) or 1 equals (given <see cref="Int16"/> over the range).</returns>
 public int CompareTo(Int16 other)
 {
     return(TheRange.CompareTo(other));
 }
Example #17
0
        public void RangeCompareToAbove()
        {
            var range = new Range<int>(3, 10);
            int value = 12;

            int comp = range.CompareTo(value);

            Assert.AreEqual(1, comp);
        }
Example #18
0
        public void RangeCompareToBelow()
        {
            var range = new Range<int>(3, 10);
            int value = 1;

            int comp = range.CompareTo(value);

            Assert.AreEqual(-1, comp);
        }
Example #19
0
        public void CompareToTest()
        {
            var leftRange = new Range(5, 8);

            Assert.IsTrue(leftRange.CompareTo(new Range(5, 8)) == 0);
            Assert.IsTrue(leftRange.CompareTo(new Range(4, 6)) == 1);
            Assert.IsTrue(leftRange.CompareTo(new Range(3, 7)) == 1);
            Assert.IsTrue(leftRange.CompareTo(new Range(3, 9)) == -1);
            Assert.IsTrue(leftRange.CompareTo(new Range(3, 8)) == 0);
            Assert.IsTrue(leftRange.CompareTo(new Range(5, 9)) == -1);
            Assert.IsTrue(leftRange.CompareTo(new Range(9, 10)) == -1);
            Assert.IsTrue(leftRange.CompareTo(new Range(3, 4)) == 1);
            Assert.IsTrue(leftRange.CompareTo(new Range(4, 4)) == 1);
            Assert.IsTrue(leftRange.CompareTo(new Range(5, 5)) == 0);
            Assert.IsTrue(leftRange.CompareTo(new Range(6, 6)) == 0);
            Assert.IsTrue(leftRange.CompareTo(new Range(7, 7)) == 0);
            Assert.IsTrue(leftRange.CompareTo(new Range(8, 8)) == 0);
            Assert.IsTrue(leftRange.CompareTo(new Range(9, 9)) == -1);

            Assert.IsTrue(leftRange.CompareTo(5) == 0);
            Assert.IsTrue(leftRange.CompareTo(6) == 0);
            Assert.IsTrue(leftRange.CompareTo(7) == 0);
            Assert.IsTrue(leftRange.CompareTo(8) == 0);
            Assert.IsTrue(leftRange.CompareTo(9) == -1);
            Assert.IsTrue(leftRange.CompareTo(10) == -1);
            Assert.IsTrue(leftRange.CompareTo(4) == 1);
            Assert.IsTrue(leftRange.CompareTo(0) == 1);
        }
        public static IntersectionResult Intersect(Range range1, Range range2)
        {
            var compareResult = range1.CompareTo(range2);

            if (compareResult != 0)
            {
                return null;
            }
            if (range1.From > range2.From)
            {
                return Intersect(range2, range1);
            }

            // r1.From-r1.To-r2.From-r2-To
            if (range1.To < range2.From)
            {
                return null;
            }
            // r1.From-r2.From-r1.To-r2.To
            if (range1.From <= range2.From && range2.From <= range1.To && range1.To <= range2.To)
            {
                Range leftSetDifference = null;
                var intersection = new Range() { From = Min(range1.To, range2.From), To = Max(range1.To, range2.From) };
                Range rightSetDifference = null;

                if (range1.From < intersection.From)
                {
                    leftSetDifference = new Range() {From = range1.From, To = intersection.From - 1};
                }
                if (range2.To > intersection.To)
                {
                    rightSetDifference = new Range() { From = intersection.To + 1, To = range2.To };
                }

                return new IntersectionResult()
                {
                    Intersection = intersection,
                    LeftSetDifference = leftSetDifference,
                    RightSetDifference = rightSetDifference
                };
            }
            // r1.From-r2.From-r2.To-r2.1rom
            if (range1.From <= range2.From && range2.To <= range1.To)
            {
                Range leftSetDifference = null;
                var intersection = new Range() { From = Max(range1.From, range2.From), To = Min(range1.To, range2.To) };
                Range rightSetDifference = null;

                if (range1.From < intersection.From)
                {
                    leftSetDifference = new Range() { From = range1.From, To = intersection.From - 1 };
                }
                if (range1.To > intersection.To)
                {
                    rightSetDifference = new Range() { From = intersection.To + 1, To = range1.To };
                }

                return new IntersectionResult()
                {
                    Intersection = intersection,
                    LeftSetDifference = leftSetDifference,
                    RightSetDifference = rightSetDifference
                };
            }

            throw new NotSupportedException(
                string.Format(
                    "The ranges (From: {0}, To: {1}) and (From: {2}, To: {3}) are not supported to intersect",
                    range1.From, range1.To, range2.From, range2.To));
        }