本文介绍了C#.NET的间隔数据类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找.NET 4.0的间隔数据类型.例如区间(a,b],所有点x都使a< x< = b.

I'm looking an interval data type for .NET 4.0. For example the interval (a,b], all point x such that a<x<=b.

我想做的是创建具有以下属性的间隔:

What i would like to be able to do are create intervals with the following properites:

  • 封闭端和开放端
  • 无限制的时间间隔,完全无限制,并且右/左无限制.

有了这些,我想做类似的事情:

With these I would like to do thing like:

  • 检查点是否在间隔中.
  • 检查两个间隔是否重叠.
  • 将两个重叠的间隔合并为一个间隔.
  • 检查时间间隔集合是否覆盖单个时间间隔.
  • 等:)

如果我可以同时使用数字数据类型和日期时间,那将很好.

Would be nice if I could work with both numerical datatype and datetimes.

我知道逻辑很简单,但是我也没有理由成为第一个需要这种东西的人.

I know that the logic is pretty straight forward, but I see no reason that I would be the first one to need such a thing either.

推荐答案

EDIT 2019:从C#8.0/.NET Core 3.x/.NET Standard 2.1开始,现在有一个 System.Range ,它为端点提供了最小的间隔功能.我将其余所有答案保持原样.

EDIT 2019: As of C# 8.0/.NET Core 3.x/.NET Standard 2.1 there is now a System.Range that provides minimal interval functionality with endpoints. I'm leaving the rest of this answer as-is.

正如其他人所述,没有积分间隔类型.根据项目的需要,一个简单的 Tuple< T1,T2> 或调用 Enumerable.Range 并添加几行代码即可. HashSet< T> 包含一组操作方法,例如UnionWith,IntersectWith等,但仍然存储所有项目,而不仅仅是端点.

As others have stated, there are no integrated interval type. Depending on the needs of your project, a simple Tuple<T1, T2> or call to Enumerable.Range with a few additional lines of code might suffice. The HashSet<T> contains set operation methods, such as UnionWith, IntersectWith and more, but still stores all the items, not just the endpoints.

可以在网上找到许多实现.基本的通用范围类 Microsoft Research动态数据显示项目的一部分,并且另一个来自 Kevin Gadd .AForge项目包含一个非通用的 IntInterval/DoubleInterval实现.其他( 1 2 )这样的问题也可能引起人们的兴趣.安迪·克莱默(Andy Clymer)在他的博客.可以在 CodeProject 中找到更完整的解决方案.乔恩·斯基特(Jon Skeet)的书发自俄罗斯的爱意.似乎有一些( 1 2 )商业解决方案也是如此.在此之前我找不到其他人.

Many implementations can be found online. There is the basic generic Range class part of the Microsoft Research Dynamic Data Display project and another from Kevin Gadd. The AForge project contains a non-generic IntInterval/DoubleInterval implementation. Other (1, 2) SO questions might also be of interest. Andy Clymer has an interesting dynamically compiled implementation on his blog. More complete solutions can be found on CodeProject, in Jon Skeet's book and From Russia with Love. There seems to be a few (1, 2) commercial solutions as well. I've seen others before that I can't find at the moment.

无论您做什么工作,在使用通用间隔类型时都请当心.实际上很难编写正确的整体通用间隔类,因为整数和浮点间隔具有不同的数学属性.例如,所有整数间隔都可以用封闭的端点表示,而对 [1,2] [3,6] 可以视为连续的,等效于 [1,6] .对于浮点间隔,这都不是正确的.有关详细信息,请参见维基百科.一组类可能更好,可以使用抽象的通用基类和类型化的派生类IntInterval或DoubleInterval来实现不同的行为.

Whatever you do, please watch out when using a generic interval type. It's actually hard to write a correct monolithic generic interval class because integer and floating point intervals have different mathematical properties. For example all integer intervals can be represented with closed endpoints and the pair [1,2] [3,6] can be considered as contiguous, equivalent to [1,6]. None of this is true with floating points intervals. See Wikipedia for details. A group of classes might be better, with an abstract generic base class and typed derived classes IntInterval or DoubleInterval to implement the different behaviors.

除了数学之外,通用间隔类型还有一些实现上的困难.使用C#中的泛型无法轻松进行算术运算,并且需要处理浮点NaN和舍入错误.请参阅关于 Interval<; T> 以获得更多信息.(很多都转换为C#和.NET.)幸运的是,只需 IComparable< T> .

Aside from the math, there are a few more implementation difficulties with generic interval types. It's not possible to easily do arithmetic with generics in C#, and there is floating point NaN and rounding errors to take care of. See the Boost library documentation for Interval<T> for more on this. (A lot of it translates to C# and .NET.) Luckily many operations can be done with just IComparable<T>.

正如我之前提到的,在功能和正确性方面选择合适的方法都取决于您项目的要求.

As I mentioned before, the choice of what is appropriate in terms of functionality and correctness all depends on the requirements of your projects.

这篇关于C#.NET的间隔数据类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-12 08:36