Scala 集合循环缓冲区 [英] scala collections circular buffer
问题描述
只是在这里乱搞,使用循环缓冲区.这是一个明智的实现,还是有更快/更可靠的方法来给这只猫剥皮?
Just messing about here, with circular buffers. Is this a sensible implementation or is there a faster/more reliable way to skin this cat?
class CircularBuffer[T](size: Int)(implicit mf: Manifest[T]) {
private val arr = new scala.collection.mutable.ArrayBuffer[T]()
private var cursor = 0
val monitor = new ReentrantReadWriteLock()
def push(value: T) {
monitor.writeLock().lock()
try {
arr(cursor) = value
cursor += 1
cursor %= size
} finally {
monitor.writeLock().unlock()
}
}
def getAll: Array[T] = {
monitor.readLock().lock()
try {
val copy = new Array[T](size)
arr.copyToArray(copy)
copy
} finally {
monitor.readLock().unlock()
}
}
}
推荐答案
Creation
对现有 Scala 集合类和 appender 函数的类型声明比滚动你自己的"更容易实现.正如评论中所指出的,此实现可能不如真正的"循环缓冲区那样高效,但它只需很少的编码即可完成工作:
A type declaration to an existing scala collection class and an appender function is easier to implement than "rolling your own". As noted in the comments this implementation will likely not be as performant as a "true" circular buffer, but it gets the job done with very little coding:
import scala.collection.immutable
type CircularBuffer[T] = immutable.Vector[T]
def emptyCircularBuffer[T] : CircularBuffer[T] = immutable.Vector.empty[T]
def addToCircularBuffer[T](maxSize : Int)(buffer : CircularBuffer[T], item : T) : CircularBuffer[T] =
(buffer :+ item) takeRight maxSize
这意味着您的CircularBuffer"实际上是一个 Vector,您现在可以免费获得所有相应的 Vector 方法(filter、map、flatMap 等...):
This means that your "CircularBuffer" is actually a Vector and you now get all of the corresponding Vector methods (filter, map, flatMap, etc...) for free:
var intCircularBuffer = emptyCircularBuffer[Int]
//Vector(41)
intCircularBuffer = addToCircularBuffer(2)(intCircularBuffer, 41)
//Vector(41, 42)
intCircularBuffer = addToCircularBuffer(2)(intCircularBuffer, 42)
//Vector(42, 43)
intCircularBuffer = addToCircularBuffer(2)(intCircularBuffer, 43)
//Vector(42)
val evens : CircularBuffer[Int] = intCircularBuffer filter ( _ % 2 == 0)
索引
您可以类似地添加一个循环索引函数:
You can similarly add a function for circular indexing:
def circularIndex[T](buffer : CircularBuffer[T])(index : Int) : T =
buffer(index % buffer.size)
这篇关于Scala 集合循环缓冲区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!