English 中文(简体)
Haskell: Datastruture with O(1) append and O(1) indexing?
原标题:Haskell: Datastruture with O(1) append and O(1) indexing?

我正在寻找哈萨克尔的数据结构,支持快速指数化和快速指数化。 这是由于再次入侵造成的一场敏化问题。

从病媒在C++(即易变,但在此情况下是顺便宜的)中的工作来看,与(已摊销的)O(1)附录和O(1)指数化两种病媒相比,似乎可以变换(不见对该问题的评论)。 这本书是否可以在Haskell查阅,或者如果我拿到数据。 O(1) 附录和O(log(min(i,n-i)) 索引

在一份相关的说明中,作为Haskell新bie,我发现自己渴望为Haskell数据结构提供实用的简明指南。 理想的情况是,这将对最实用的数据结构以及业绩特征和在哈斯凯尔图书馆实施时的点人进行更全面的概述。 看来,那里有大量信息,但我发现这种信息很少分散。 我要求太多?

最佳回答

如果记忆发挥作用,则C++病媒作为带束和大小信息的阵列加以实施。 如果插入会增加超过规模的束缚,那么规模就会加倍。 这是一种被摊销的O(1)时间插入(不是O(1),正如你所声称的那样),并且可以在Haskell使用<代码”的罚款。 轨道类型,或许有适当的<代码>IO或<代码>>t

问题回答

简单易变的问题,你通常希望一劳永逸地建立桌子,然后不作改动。 在此情况下,你可以避免不得不担心 app事,而不是把建造电化表视为一种行动。

一种办法是利用zy评价,并在我们重新构造时参考表格。

import Data.Array
fibs = listArray (0, n-1) $ 0 : 1 : [fibs!(i-1) + fibs!(i-2) | i <- [2..n-1]]
  where n = 100

如果表各要素之间的依附关系使得难以在时间之前提出简单的评估顺序,这种方法就特别有用。 然而,它需要使用箱式阵列或病媒,这可能使这种做法由于间接费用的增加而不适合大型表格。

For unboxed vectors, you have operations like constructN which lets you build a table in a pure way while using mutation underneath to make it efficient. It does this by giving the function you pass an immutable view of the prefix of the vector constructed so far, which you can then use to compute the next element.

import Data.Vector.Unboxed as V
fibs = constructN 100 f
  where f xs | i < 2 = i
             | otherwise = xs!(i-1) + xs!(i-2)
             where i = V.length xs

查阅https://stackoverflow.com/a/9613203/771837>。 更知情地选择你应使用什么。

但是,如果你想要相当于C++,则使用Data.Vector>。





相关问题
The Fastest DataStructure to Filter with in C#

Currently we are filtering and sorting data with a datatable. /// <summary> /// Filters the data table and returns a new data table with only the filtered rows. /// </summary>...

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Java large datastructure for storing a matrix

I need to store a 2d matrix containing zip codes and the distance in km between each one of them. My client has an application that calculates the distances which are then stored in an Excel file. ...

Holding onto items after a postback

I have an ASP.NET web application and I want to be able to take items from a master list and store them temporarliy into one of four other lists. The other lists need to survive post backs so that ...

negative number in the stack

I am a new student in the compilers world ^_^ and I want to know is legal represent negative number in the stack. For example: infix: 1-5=-4 postfix: 15- The statements are: push(1) push(5) x=...

What type of struct/container would you use in this instance?

I am trying to figure out what type of structure or container I should use for a quick project. I need to have an unknown number of sets that will be entered from the GUI (each one will have a name, ...

热门标签