C# .Net 中是否有“Set”数据结构?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a "Set" data structure in .Net?
提问by
Ideally, I'm looking for a templated logical Set class. It would have all of the standard set operations such as Union, Intersection, Etc., and collapse duplicated items.
理想情况下,我正在寻找模板化的逻辑 Set 类。它将具有所有标准集合操作,例如并集、交集等,并折叠重复项。
I ended up creating my own set class based on the C# Dictionary<>- just using the Keys.
我最终基于 C# Dictionary<> 创建了自己的集合类 - 仅使用键。
回答by Matt Hamilton
HashSet<T>
is about the closest you'll get, I think.
HashSet<T>
我想是你会得到的最接近的。
回答by Dale Ragan
No, there is not one natively in the framework. There is an open source implementation that most projects use, (i.e. nHibernate) called Iesi.Collections. Here's a CodeProject article about it:
不,框架中没有原生的。大多数项目都使用一个开源实现(即 nHibernate),称为 Iesi.Collections。这是一篇关于它的 CodeProject 文章:
回答by lomaxx
I don't think c# has anything built in, but I know there are a couple of implementations floating around on the net. There are also some good articles around on this sort of thing:
我不认为 c# 有任何内置的东西,但我知道网上有几个实现。还有一些关于这类事情的好文章:
This is part 6of a series on efficiently representing data structure. This part focuses on representing sets in C#.
这是高效表示数据结构系列的第 6 部分。这部分重点介绍在 C# 中表示集合。
An implementationof a set collection
An implementationof a set class
Yet another implementationof a set class
And finally...
最后...
I've actually used this librarymyself as the basis of a set implementation that I did a year or so ago.
我自己实际上已经使用这个库作为我一年左右前做的一套实现的基础。
回答by Brad Leach
The best set implementation I have seen is part of the wonderful Wintellect's Power Collections: http://www.codeplex.com/PowerCollections.
我见过的最好的集合实现是精彩的 Wintellect 的 Power Collections 的一部分:http: //www.codeplex.com/PowerCollections。
The set implementation can be found here:
http://www.codeplex.com/PowerCollections/SourceControl/FileView.aspx?itemId=101886&changeSetId=6259
It has all the expected set operations (union, intersect, etc).
集合实现可以在这里找到:
http: //www.codeplex.com/PowerCollections/SourceControl/FileView.aspx?itemId=101886&changeSetId= 6259
它具有所有预期的集合操作(联合、相交等)。
Hope this helps!
希望这可以帮助!
回答by dharmatech
Here's a simple implementation:
这是一个简单的实现:
public sealed class MathSet<T> : HashSet<T>, IEquatable<MathSet<T>>
{
public override int GetHashCode() => this.Select(elt => elt.GetHashCode()).Sum().GetHashCode();
public bool Equals(MathSet<T> obj) => SetEquals(obj);
public override bool Equals(object obj) => Equals(obj as MathSet<T>);
public static bool operator ==(MathSet<T> a, MathSet<T> b) =>
ReferenceEquals(a, null) ? ReferenceEquals(b, null) : a.Equals(b);
public static bool operator !=(MathSet<T> a, MathSet<T> b) => !(a == b);
}
Example usage:
用法示例:
var a = new MathSet<int> { 1, 2, 3 };
var b = new MathSet<int> { 3, 2, 1 };
var c = a.Equals(b); // true
var d = new MathSet<MathSet<int>> { a, b }; // contains one element
var e = a == b; // true
See this questionfor why this approach was considered over HashSet
.
请参阅此问题以了解为何考虑结束此方法HashSet
。