Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created November 23, 2011 17:33
Show Gist options
  • Save gclaramunt/1389311 to your computer and use it in GitHub Desktop.
Save gclaramunt/1389311 to your computer and use it in GitHub Desktop.
circular buffer
package my.collections
class CirculrBufferIterator[T](buffer:Array[T], start:Int) extends Iterator[T]{
var idx=0
override def hasNext = idx<buffer.size
override def next()={
val i=idx
idx=idx+1
buffer(i)
}
}
class CircularBuffer[T](size:Int)(implicit m:Manifest[T]) extends Seq[T]{
val buffer=new Array[T](size);
var bIdx=0;
override def apply (idx: Int): T = buffer((bIdx+idx) % size)
override def length = size
override def iterator= new CirculrBufferIterator[T](buffer, bIdx)
def add(e:T)= {
buffer(bIdx)=e
bIdx=(bIdx +1) % size
}
}
@vitamon
Copy link

vitamon commented Jul 4, 2016

Would be interesting to implement CB with off-heap memory and bulk operations for byte arrays

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment