地图的时间复杂性包含键并包含DART中的值

发布于 2025-01-26 03:59:53 字数 1100 浏览 4 评论 0原文

map.containskeymap.containsvalue的时间复杂性是什么?我想知道以下实现:

  • linkedhashmap
  • hashmap
  • splaytReemap

我假设Hash Map实现containsKey containskey 是摊销的常数时间,containsValue可能是线性时间。对于splaytreemapcontainskey可能是对数时间,而containsValue可能仍然是线性的。但是,文档在这个问题上。我能找到的最好的是linkedhashmap

带有预期恒定时间查找的插入订单[MAP]。

这没有指定您是在查找密钥还是值,但大概是指键。

docs for set> set set set < /code>(另一方面,如果导航到实现),则不是静音。他们给予时间复杂。

我认为这是文档中的监督,但也许它们是沉默的,因为没有保证的时间复杂性。 (但是,这很奇怪,因为它违背了开发人员的期望。)

What is the time complexity of Map.containsKey and Map.containsValue in Dart? I'd like to know for the following implementations:

  • LinkedHashMap
  • HashMap
  • SplayTreeMap

I assume for the hash map implementations containsKey is amortized constant time and containsValue is probably linear time. For SplayTreeMap, containsKey is probably logarithmic time while containsValue is probably still linear time. However, the documentation seems to be silent on the issue. The best I could find was for LinkedHashMap, which says:

An insertion-ordered [Map] with expected constant-time lookup.

This doesn't specify if you are looking up the key or the value, but presumably this is referring to the key.

The docs for Set (if you navigate to the implementations), on the other hand, are not silent. They give the time complexity.

I assume this is an oversight in the documentation, but perhaps they are silent because there is no guaranteed time complexity. (That's would be strange, though, because it goes against developer expectations.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

痴意少年 2025-02-02 03:59:53

对于包含,这与进行查找同时。

  • hashmaplinkedhashmap:预期的持续时间,deNenerate hashcode的最差线性时间 s。
  • splaytreemap,弹药对数时间。

对于包含value,它在元素数量(至少)中是线性的。它只是执行map.values.contains(...)的等效。没有有效的方法可以在地图中找到一个值,因此没有比以某种顺序浏览所有值更好的方法了。
某些潜在的hashmap实现可能会非常昂贵,因为它们穿越了整个背景商店,如果地图先生长了大,然后删除了很多元素,那么它可能会有一个备用商店明显大于其元素数量。其他实现自动撕裂,或将元素保存在连续的区域,并且不会遇到这个问题。
非常依赖实施。飞镖使用哪种实施的承诺。

For containsKey, it's the same time as doing a lookup.

  • HashMap and LinkedHashMap: Expected constant time, worst-case linear time for degenerate hashCodes.
  • SplayTreeMap, ammortized logarithmic time.

For containsValue, it's linear in the number of elements (at least). It simply does the equivalent of map.values.contains(...). There is no efficient way to find a single value in a map, so there is no better way than looking through all of them in some order.
Some potential HashMap implementations can be extra expensive because they traverse the entire backing store, and if the map had been grown big first, then had a lot of elements removed, then it might have a backing store which is significantly larger than its number of elements. Other implementations auto-shrink, or keep elements in a contiguous area, and won't have that problem.
Very implementation dependent. No promises which implementation Dart uses.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文