Improved implementations of ByteIterator.++

This commit is contained in:
Oliver Schulz 2012-05-17 22:56:59 +02:00
parent 018f1c92d5
commit b7e2246ce8

View file

@ -26,8 +26,10 @@ abstract class ByteIterator extends BufferedIterator[Byte] {
protected def clear(): Unit protected def clear(): Unit
def ++(that: TraversableOnce[Byte]): ByteIterator = def ++(that: TraversableOnce[Byte]): ByteIterator = {
ByteArrayIterator(that.toArray) if (that.isEmpty) this
else ByteArrayIterator(that.toArray)
}
// *must* be overridden by derived classes // *must* be overridden by derived classes
override def clone: ByteIterator = null override def clone: ByteIterator = null
@ -146,9 +148,12 @@ class ByteArrayIterator private (private var array: Array[Byte], private var fro
final override def length = { val l = len; clear(); l } final override def length = { val l = len; clear(); l }
final override def ++(that: TraversableOnce[Byte]) = that match { final override def ++(that: TraversableOnce[Byte]) = that match {
case that: ByteIterator {
if (that.isEmpty) this
else if (this.isEmpty) that
else that match {
case that: ByteArrayIterator { case that: ByteArrayIterator {
if (this.isEmpty) that if ((this.array eq that.array) && (this.until == that.from)) {
else if ((this.array eq that.array) && (this.until == that.from)) {
this.until = that.until this.until = that.until
that.clear() that.clear()
this this
@ -158,7 +163,9 @@ class ByteArrayIterator private (private var array: Array[Byte], private var fro
result result
} }
} }
case that: MultiByteArrayIterator if (this.isEmpty) that else (this +: that) case that: MultiByteArrayIterator this +: that
}
}
case _ super.++(that) case _ super.++(that)
} }
@ -267,16 +274,24 @@ class MultiByteArrayIterator private (private var iterators: List[ByteArrayItera
} }
final override def ++(that: TraversableOnce[Byte]) = that match { final override def ++(that: TraversableOnce[Byte]) = that match {
case that: ByteArrayIterator if (this.isEmpty) that else { case that: ByteIterator {
iterators = iterators :+ that if (that.isEmpty) this
else if (this.isEmpty) that
else {
that match {
case that: ByteArrayIterator {
iterators = this.iterators :+ that
that.clear() that.clear()
this this
} }
case that: MultiByteArrayIterator if (this.isEmpty) that else { case that: MultiByteArrayIterator {
iterators = this.iterators ++ that.iterators iterators = this.iterators ++ that.iterators
that.clear() that.clear()
this this
} }
}
}
}
case _ super.++(that) case _ super.++(that)
} }