.Net 是否有 multiset 的实现?
我正在寻找多重集的 .Net 实现。谁能推荐一个好的吗?
(多重集或包是可以具有重复值的集合,您可以在其上执行集合操作:交集、差值等。例如,购物车可以被视为多重集,因为您可以多次出现相同的产品。)
I'm looking for a .Net implementation of a multiset. Can anyone recommend a good one?
(A multiset, or bag, is a set that can have duplicate values, and on which you can do set operations: intersection, difference, etc. A shopping cart for instance could be thought of as a multiset because you can have multiple occurrences of the same product.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我不知道,但是您可以使用
Dictionary
来实现,其中值是商品的数量。当第二次添加该项目时,您可以增加它在字典中的值。另一种可能性是简单地使用项目的
List
,您可以在其中放置重复项。对于购物车来说,这可能是更好的方法。I do not know about one, however you could use a
Dictionary
for that, in which the value is the quantity of the item. And when the item is added for the second time, you vould increase the value for it in the dictionary.An other possibility would be to simply use a
List
of items, in which you could put duplicates. This might be a better approach for a shopping cart.任何自称为多重集的 C# 实现的东西都不应该在内部基于字典。字典是哈希表,无序集合。 C++ 的集合、多重集合、映射和多重映射是有序的。在内部,每个都被表示为某种形式的自平衡二叉搜索树。
在 C# 中,我们应该使用 SortedDictionary 作为实现的基础,根据 Microsoft 自己的文档,SortedDictionary "是一个二叉搜索树,检索时间为 O(log n)"。基本多重集可以按如下方式实现:
Anything calling itself a C# implementation of a multiset should not be based on a Dictionary internally. Dictionaries are hash tables, unordered collections. C++'s sets, multisets, maps, and multimaps are ordered. Internally each is represented as some flavor of a self-balancing binary search tree.
In C# we should then use a SortedDictionary as the basis of our implementation as according to Microsoft's own documentation a SortedDictionary "is a binary search tree with O(log n) retrieval". A basic multiset can be implemented as follows:
另一种选择是仅包装
SortedSet
,但不是在其中存储类型T
,而是存储值元组(T value, int counter)
其中,每插入一个新的value
实例,counter
就会增加 1。本质上你是在强迫价值观是不同的。您可以有效地使用GetViewBetween()
查找特定值的counter
最大值,然后递增它以获取新添加值的计数器。与计数字典解决方案不同,您可以使用GetViewBetween()
复制功能equal_range
、lower_bound
和upper_bound
> 以 C++ 形式给出。下面是一些代码,显示了我的意思:现在您可以编写如下内容:
在过去,存在一些问题,其中
GetViewBetween()
在时间 O(输出大小) 中运行,而不是在时间 O( log n),但我认为这些已经解决了。当时它会对节点进行计数以缓存计数,现在它使用分层计数来有效地执行计数操作。请参阅此 StackOverflow 帖子和此库代码。Another option is to just wrap
SortedSet
, but instead of storing your typeT
in it, you store the value tuple(T value, int counter)
wherecounter
goes up by 1 with each new instance ofvalue
that is inserted. Essentially you're forcing the values to be distinct. You can efficiently useGetViewBetween()
to find the largest value ofcounter
for a particular value, then increment it to get the counter for a newly-added value. And unlike the count dictionary solution, you can useGetViewBetween()
to replicate the functionalityequal_range
,lower_bound
, andupper_bound
gives in C++. Here is some code showing what I mean:Now you can write something like this:
In the past, there were some issues where
GetViewBetween()
ran in time O(output size), rather than time O(log n), but I think those have been resolved. At the time it would count up nodes to cache the count, it now uses hierarchical counts to perform Count operations efficiently. See this StackOverflow post and this library code.您可以使用排序多重集的此实现: SortedMultiSet.cs
You can use this implementation of a sorted multiset: SortedMultiSet.cs