Haskell:使用O(1)追加和O(1)索引的Datastruture? [英] Haskell: Datastruture with O(1) append and O(1) indexing?

查看:128
本文介绍了Haskell:使用O(1)追加和O(1)索引的Datastruture?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一个支持快速索引和快速追加的Haskell中的数据结构。这是一个由递归引起的回忆问题。



从矢量工作在c ++(这是可变的,但在这种情况下无关紧要)的方式似乎应该是可能的(摊销)O(1)附加和O(1)索引的不变变量是可能的(好的,不是,看到这个问题的意见)。在Haskell中这个是否可以使用,或者我应该使用Data.Sequence,它有(AFAICT反正)O(1)附加和O(log(min(i,ni)))索引?


$ b $在相关的笔记中,作为一个Haskell新手,我发现自己渴望Haskell数据结构的实用,简明的指南。理想情况下,这将对最实用的数据结构以及Haskell库实现的性能特征和指针给出相当全面的概述。似乎有很多信息在那里,但我发现它有点分散。我问太多了吗?

解决方案

如果内存服务,C ++向量被实现为具有边界和大小信息的数组。当插入将增加超出大小的界限时,大小将增加一倍。这是O(1)时间插入(不是O(1),你声称),并且可以使用数组类型在Haskell中进行模拟, code> IO 或 ST prepended。


I am looking for a data structure in Haskell that supports both fast indexing and fast append. This is for a memoization problem which arises from recursion.

From the way vectors work in c++ (which are mutable, but that shouldn't matter in this case) it seems immutable vectors with both (amortized) O(1) append and O(1) indexing should be possible (ok, it's not, see comments to this question). Is this poossible in Haskell or should I go with Data.Sequence which has (AFAICT anyway) O(1) append and O(log(min(i,n-i))) indexing?

On a related note, as a Haskell newbie I find myself longing for a practical, concise guide to Haskell data structures. Ideally this would give a fairly comprehensive overview over the most practical data structures along with performance characteristics and pointers to Haskell libraries where they are implemented. It seems that there is a lot of information out there, but I have found it to be a little scattered. Am I asking too much?

解决方案

If memory serves, C++ vectors are implemented as an array with bounds and size information. When an insertion would increase the bounds beyond the size, the size is doubled. This is amortized O(1) time insertion (not O(1) as you claim), and can be emulated just fine in Haskell using the Array type, perhaps with suitable IO or ST prepended.

这篇关于Haskell:使用O(1)追加和O(1)索引的Datastruture?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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