F#可变大小的窗口数据结构 [英] F# Immutable variable sized window data structure

查看:93
本文介绍了F#可变大小的窗口数据结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我下面有一个我需要的数据结构的描述,我想使用不可变的数据结构实现它。我试图确定...有一个现有的数据结构,将支持我在这里做什么,或者我需要创建一个 - 如果我需要创建它,什么是一个好的开始的地方(积木)?

I have below a description of a data structure I need and I want to implement it using immutable data structures. I'm trying to determine... is there an existing data structure out there that will support what I'm trying to do here or do I need to create one--and if I need to create it, what would be a good place to start (building blocks)?

我有一个稳定的一定类型的输入值流。我想将它们添加到持久/不变的数据结构中以保存它们的历史记录,并且在每次添加时,它将检查历史记录并确定是否删除一个或多个最旧的项目(例如,如果历史是> a某些长度或一个值具有一定的属性)。

I have a steady stream of incoming values of a certain type. I want to add them to a persistent/immutable data structure to hold a history of them, and on each add, it will review the history and determine if one or more oldest items will be removed (for example, if the history is > a certain length or a value has a certain property).

推荐答案

不知道更多关于您的要求,我只想说一个香草设置<'a> 做的不够合适。我更喜欢'List'在'列表',所以你总是有O(lg n)访问最大和最小的项目,允许您通过插入日期/时间订购您的集合,以有效访问最新和最旧的项目

Without knowing more about your requirements, I'd just say a vanilla Set<'a> does a more than adequate job. I'd prefer a 'Set' over a 'List' so you always have O(lg n) access to the largest and smallest items, allowing you to ordered your set by insert date/time for efficient access to the newest and oldest items.

看起来很容易包装一套,以便其添加/删除方法调用您的回调:

Seems very easy to wrap up a set so that its Add/Remove methods invoke your callbacks:

type AwesomeSet(internalSet : Set<'a>, insertCallback : 'a -> unit, removeCallback : 'a -> unit) =
    member this.Add(x) =
        insertCallback(x)
        AwesomeSet(internalSet.Add x, insertCallback, removeCallback)

    member this.Remove(x) =
        removeCallback(x)
        AwesomeSet(internalSet.Remove x, insertCallback, removeCallback)

    member this.Count = internalSet.Count
    member this.Min = internalSet.MinimumElement
    member this.Max = internalSet.MaximumElement

这篇关于F#可变大小的窗口数据结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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