集合修改项 [英] Collection modify item

查看:104
本文介绍了集合修改项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已阅读了大量文章,内容涉及为特定的实现选择正确的集合, 而且我知道最终归结为对真实数据进行基准测试,但是在我忙着这样做的时候:

I have read tons of articles on choosing the correct collection for a specific implementation, and I understand that in the end it will come down to benchmarking real data, but while I'm busy doing that:

  • C#中的排序集合允许修改项目 包含?我似乎找不到任何东西?

  • What sorted collection in c# allows the modification of an item contained?I can't seem to find any?

这是因为修改可能会作为删除来实现 然后重新插入,从而实现显式的修改"功能 没有意义吗?

Is this because a modify would probably be implemented as a removal then re-insertion, thus making an explicit 'Modify' function pointless?

我需要一个集合(自定义库或标准库), 对其执行以下操作.

I am in need of a collection (custom or standard library), with the following operations performed on it.

  • 插入-经常
  • 经常删除-
  • 修改-非常频繁
  • 选择Top X元素-每次上述任何一次发生,以及更多同时发生.

当前我正在使用SortedSet,因为它提供了O(logn)插入,但是我不清楚 清除性能以及如何最好地修改项目.

Currently I am using a SortedSet, as it provides O(logn) inserts,but I am unclear on removal performance and how to best modify an item.

推荐答案

首先,我们需要弄清修改集合的含义.

First, we need to clarify the meaning of modifying a collection.

通常,操纵集合是指从列表中插入/删除项目. 要修改单个项目,基本上是访问该项目并修改其属性.访问项目的成本取决于集合的实现,但是对项目属性的修改不取决于集合. 另外请注意,如果集合项不可更改,则不能对其进行修改.

Generally, manipulating collection refers to insert/delete items from the list. To modify an individual item, it's basically accessing the item and modify its properties. Cost of accessing an item depends on the collection implementation, but modification of item propertis is not dependant on the collection. Also note, you cannot modify a collection item if it's not mutable.

如果您只是寻找最佳的内置集合,则基本上是在SortedList和SortedSet之间进行选择(SortedDictionary与SortedSet相同).

If you are just looking to find the best built-in collections, you are basically choosing between SortedList and SortedSet (SortedDictionary is the same as SortedSet).

SortedList在内部将数据存储为数组,因此可以按索引进行有效访问(用于获取前X个项目); SortedSet具有更快的插入和删除速度(按常数因子),但是索引访问将需要在树中搜索下一项,在最坏的情况下为O(log n),最好的原因为O(1).

SortedList stores data internally as array, so it has efficient access by index (for getting top X items); SortedSet has faster insertion and removal (by constant factor), indexed access however will require searching through the tree for next item which in worst case is O(log n) and best caes O(1).

除此之外,两者之间的差异很小,因为它们都实现了Red Black Tree,只是实现细节有所不同.

Aside from that, difference between the two is quite small, as they both implements Red Black Tree, just differs in implementation details.

实际性能必须根据您的用例进行衡量.但是你已经知道了.

Actual performance will have to be measured for your user cases. But you already know that.

这篇关于集合修改项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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