Scala 中的有限可增长数组
我希望能够将类似数组的结构增长到最大大小,之后每次添加新元素时,最旧的(第一个)元素将从结构中删除。我不知道最好的方法是什么,但一种方法是扩展 ArrayBuffer 类,并重写 += 运算符,这样如果达到最大大小,则每次新的元素都会删除第一个元素已添加 1 个。我还没有弄清楚如何正确扩展集合。到目前为止我所拥有的是:
class FiniteGrowableArray[A](maxLength:Int) extends scala.collection.mutable.ArrayBuffer {
override def +=(elem:A): <insert some return type here> = {
// append element
if(length > maxLength) remove(0)
<returned collection>
}
}
有人可以建议一条更好的路径和/或帮助我沿着这条路走下去吗?注意:我需要在 += 操作之间多次任意访问结构内的元素。
谢谢
I would like to be able to grow an Array-like structure up to a maximum size, after which the oldest (1st) element would be dropped off the structure every time a new element is added. I don't know what the best way to do this is, but one way would be to extend the ArrayBuffer class, and override the += operator so that if the maximum size has been reached, the first element is dropped every time a new one is added. I haven't figured out how to properly extend collections yet. What I have so far is:
class FiniteGrowableArray[A](maxLength:Int) extends scala.collection.mutable.ArrayBuffer {
override def +=(elem:A): <insert some return type here> = {
// append element
if(length > maxLength) remove(0)
<returned collection>
}
}
Can someone suggest a better path and/or help me along this one? NOTE: I will need to arbitrarily access elements within the structure multiple times in between the += operations.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如其他人所讨论的,您需要一个环形缓冲区。但是,您还必须决定是否确实需要所有集合方法,如果是,那么当您过滤最大大小为 N 的环形缓冲区时会发生什么 - 它是否保留其最大大小,或者什么?
如果您仅仅能够查看您的环形缓冲区作为集合层次结构的一部分(但不想有效地使用集合来生成新的环形缓冲区),那么您可以:
现在您可以很容易地直接使用它,并且可以在需要时跳出到 IndexedSeq:
如果你想将东西放回到环形缓冲区中,你可以
然后你可以做类似的事情
As others have discussed, you want a ring buffer. However, you also have to decide if you actually want all of the collections methods or not, and if so, what happens when you filter a ring buffer of maximum size N--does it keep its maximum size, or what?
If you're okay with merely being able to view your ring buffer as part of the collections hierarchy (but don't want to use collections efficiently to generate new ring buffers) then you can just:
Now you can use this easily directly, and you can jump out to IndexedSeq when needed:
and if you want to put things back into a ring buffer, you can
and then you can do things like