最快的方式找出是否2的ICollection< T>集合包含相同的对象 [英] Fastest way to find out whether two ICollection<T> collections contain the same objects

查看:179
本文介绍了最快的方式找出是否2的ICollection< T>集合包含相同的对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是找出最快的方式是否两个的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&LT; T&GT;集合包含相同的对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆