当前位置: 首页>>代码示例>>C#>>正文


C# Range.CompareTo方法代码示例

本文整理汇总了C#中Range.CompareTo方法的典型用法代码示例。如果您正苦于以下问题:C# Range.CompareTo方法的具体用法?C# Range.CompareTo怎么用?C# Range.CompareTo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在Range的用法示例。


在下文中一共展示了Range.CompareTo方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: RangeCompareToWithin

        public void RangeCompareToWithin()
        {
            var range = new Range<int>(3, 10);
            int value = 5;

            int comp = range.CompareTo(value);

            Assert.AreEqual(0, comp);
        }
开发者ID:dbaileychess,项目名称:CSMSL,代码行数:9,代码来源:RangeTestFixture.cs

示例2: RangeCompareToAbove

        public void RangeCompareToAbove()
        {
            var range = new Range<int>(3, 10);
            int value = 12;

            int comp = range.CompareTo(value);

            Assert.AreEqual(1, comp);
        }
开发者ID:dbaileychess,项目名称:CSMSL,代码行数:9,代码来源:RangeTestFixture.cs

示例3: RangeCompareToBelow

        public void RangeCompareToBelow()
        {
            var range = new Range<int>(3, 10);
            int value = 1;

            int comp = range.CompareTo(value);

            Assert.AreEqual(-1, comp);
        }
开发者ID:dbaileychess,项目名称:CSMSL,代码行数:9,代码来源:RangeTestFixture.cs

示例4: Intersect

        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));
        }
开发者ID:KhalipskiSiarhei,项目名称:Trainings,代码行数:70,代码来源:RangeComparer.cs


注:本文中的Range.CompareTo方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。