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
def ++(that: TraversableOnce[Byte]): ByteIterator =
ByteArrayIterator(that.toArray)
def ++(that: TraversableOnce[Byte]): ByteIterator = {
if (that.isEmpty) this
else ByteArrayIterator(that.toArray)
}
// *must* be overridden by derived classes
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 ++(that: TraversableOnce[Byte]) = that match {
case that: ByteIterator {
if (that.isEmpty) this
else if (this.isEmpty) that
else that match {
case that: ByteArrayIterator {
if (this.isEmpty) that
else if ((this.array eq that.array) && (this.until == that.from)) {
if ((this.array eq that.array) && (this.until == that.from)) {
this.until = that.until
that.clear()
this
@ -158,7 +163,9 @@ class ByteArrayIterator private (private var array: Array[Byte], private var fro
result
}
}
case that: MultiByteArrayIterator if (this.isEmpty) that else (this +: that)
case that: MultiByteArrayIterator this +: that
}
}
case _ super.++(that)
}
@ -267,16 +274,24 @@ class MultiByteArrayIterator private (private var iterators: List[ByteArrayItera
}
final override def ++(that: TraversableOnce[Byte]) = that match {
case that: ByteArrayIterator if (this.isEmpty) that else {
iterators = iterators :+ that
case that: ByteIterator {
if (that.isEmpty) this
else if (this.isEmpty) that
else {
that match {
case that: ByteArrayIterator {
iterators = this.iterators :+ that
that.clear()
this
}
case that: MultiByteArrayIterator if (this.isEmpty) that else {
case that: MultiByteArrayIterator {
iterators = this.iterators ++ that.iterators
that.clear()
this
}
}
}
}
case _ super.++(that)
}