最快的方式找出是否2的ICollection< T>集合包含相同的对象 [英] Fastest way to find out whether two ICollection<T> collections contain the same objects
问题描述
什么是找出最快的方式是否两个的ICollection< T>
集合包含完全相同的条目?蛮力是明确的,我不知道是否有一个更优雅的方式。
我们正在使用C#2.0,所以没有扩展方法如果可能的话,请!
编辑:答案是既为有序和无序的集合有趣,就有希望为每个不同的
使用C5
的 http://www.itu.dk/research/c5/
ContainsAll
检查在
的所有项目提供集合在这款包包
(计重数)。
中的
项目寻找。
真,如果所有项目都发现
。
块引用>
[测试]
公共虚拟BOOL ContainsAll< U>(SCG.IEnumerable< U>项目),其中,U:T已
{
HashBag< T> RES =新HashBag< T>(itemequalityComparer);
的foreach(在项目牛逼的项目)
如果(res.ContainsCount(项目)LT; ContainsCount(项目))
res.Add(项目);
,否则
返回FALSE;
返回真;
}
What is the fastest way to find out whether two
ICollection<T>
collections contain precisely the same entries? Brute force is clear, I was wondering if there is a more elegant method.We are using C# 2.0, so no extension methods if possible, please!
Edit: the answer would be interesting both for ordered and unordered collections, and would hopefully be different for each.
解决方案use C5
http://www.itu.dk/research/c5/
ContainsAll
" Check if all items in a supplied collection is in this bag
(counting multiplicities).
The items to look for.
True if all items are found."
[Tested] public virtual bool ContainsAll<U>(SCG.IEnumerable<U> items) where U : T { HashBag<T> res = new HashBag<T>(itemequalityComparer); foreach (T item in items) if (res.ContainsCount(item) < ContainsCount(item)) res.Add(item); else return false; return true; }
这篇关于最快的方式找出是否2的ICollection< T>集合包含相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!