Data.Vector会替换Data.Sequence吗? [英] Does Data.Vector replace Data.Sequence?

查看:106
本文介绍了Data.Vector会替换Data.Sequence吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直非常喜欢 Data.Sequence 。但正如我已经了解 Data.Vector ,似乎它可以完成Data.Sequence的所有功能,但更好,再加上它可以做更多的事情。我们是否应该贬低Data.Sequence和传讲Data.Vector? Data.Vector使用Data.Sequence是否有很好的理由?

解决方案

这些数据结构都不能取代其他的数据结构。 Data.Sequence Data.Vector 实际上处于可用于表示序列的数据结构的直径相对端。 p>


  • Data.Vector 是一个连续的元素数组。这意味着小的内存占用和O(1)查找,但可怕的变异,连接和复制(每个O(n))。 (如果删除持久性,则突变可以是O(1)。)另一方面,
  • Data.Sequence 是一个纯函数树。这意味着更高的内存使用率和更少的局部性,但它支持快速访问和变异O(log n)和可靠的连接O(log(min(n1,n2)))和复制。


数据结构的选择实际上取决于这里的任务。


  • 处理大线性或随机查找元素流最好使用 Data.Vector

  • 完成如果您需要拆分,连接和改变你的元素很多,使用 Data.Sequence


I've been quite a fan of Data.Sequence. But as I've been learning about Data.Vector, it seems that it can do everything Data.Sequence can, but better, plus it can do more stuff. Should we be deprecating Data.Sequence and preaching Data.Vector? Are there any good reasons for using Data.Sequence over Data.Vector?

解决方案

None of these data structures can replace the other; Data.Sequence and Data.Vector are actually at diametrically opposite ends of the data structures available for representing sequences.

  • Data.Vector is a contiguous array of elements. This means small memory footprint and O(1) lookup but terrible mutation, concatenation and copying (O(n) each). (Mutation can be O(1) if you drop persistence.)
  • Data.Sequence on the other hand is a purely functional tree. This means higher memory usage and less locality, but it supports fast access and mutation O(log n) and awesome concatenation O(log(min(n1,n2))) and copying.

The choice of data structure really depends on the task at hand here.

  • Processing large streams of elements in linear fashion or with random lookup is best done with Data.Vector.
  • If you need to split, concatenate and change your elements a lot, use Data.Sequence.

这篇关于Data.Vector会替换Data.Sequence吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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