summaryrefslogtreecommitdiff
path: root/src/library/scala/collection/Iterator.scala
diff options
context:
space:
mode:
authorRex Kerr <ichoran@gmail.com>2014-05-29 08:04:14 -0700
committerRex Kerr <ichoran@gmail.com>2014-08-24 13:41:45 -0700
commit3d64deeb687452cb7e8e53b5a172e7bc7c50bf1b (patch)
treefac47d36d7d63ca867b33834180d8d5d4556354f /src/library/scala/collection/Iterator.scala
parentd4b5c7b95de88d3890be654e06da812c6eb607f5 (diff)
downloadscala-3d64deeb687452cb7e8e53b5a172e7bc7c50bf1b.tar.gz
scala-3d64deeb687452cb7e8e53b5a172e7bc7c50bf1b.tar.bz2
scala-3d64deeb687452cb7e8e53b5a172e7bc7c50bf1b.zip
SI-8474 Inconsistent behavior of patch method
Changed Iterator to be consistent with other collections. Also fixed SeqViewLike to validate/constrain inputs. No specific tests; quasi-comprehensive collection tests will cover this later.
Diffstat (limited to 'src/library/scala/collection/Iterator.scala')
-rw-r--r--src/library/scala/collection/Iterator.scala40
1 files changed, 29 insertions, 11 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala
index f6f46e158f..660cc5a42a 100644
--- a/src/library/scala/collection/Iterator.scala
+++ b/src/library/scala/collection/Iterator.scala
@@ -1088,6 +1088,9 @@ trait Iterator[+A] extends TraversableOnce[A] {
}
/** Returns this iterator with patched values.
+ * Patching at negative indices is the same as patching starting at 0.
+ * Patching at indices at or larger than the length of the original iterator appends the patch to the end.
+ * If more values are replaced than actually exist, the excess is ignored.
*
* @param from The start index from which to patch
* @param patchElems The iterator of patch values
@@ -1096,18 +1099,33 @@ trait Iterator[+A] extends TraversableOnce[A] {
*/
def patch[B >: A](from: Int, patchElems: Iterator[B], replaced: Int): Iterator[B] = new AbstractIterator[B] {
private var origElems = self
- private var i = 0
- def hasNext: Boolean =
- if (i < from) origElems.hasNext
- else patchElems.hasNext || origElems.hasNext
+ private var i = (if (from > 0) from else 0) // Counts down, switch to patch on 0, -1 means use patch first
+ def hasNext: Boolean = {
+ if (i == 0) {
+ origElems = origElems drop replaced
+ i = -1
+ }
+ origElems.hasNext || patchElems.hasNext
+ }
def next(): B = {
- // We have to do this *first* just in case from = 0.
- if (i == from) origElems = origElems drop replaced
- val result: B =
- if (i < from || !patchElems.hasNext) origElems.next()
- else patchElems.next()
- i += 1
- result
+ if (i == 0) {
+ origElems = origElems drop replaced
+ i = -1
+ }
+ if (i < 0) {
+ if (patchElems.hasNext) patchElems.next()
+ else origElems.next()
+ }
+ else {
+ if (origElems.hasNext) {
+ i -= 1
+ origElems.next()
+ }
+ else {
+ i = -1
+ patchElems.next()
+ }
+ }
}
}