Created
November 23, 2011 17:33
-
-
Save gclaramunt/1389311 to your computer and use it in GitHub Desktop.
circular buffer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Would be interesting to implement CB with off-heap memory and bulk operations for byte arrays