Skip to content

Commit aa1e1d0

Browse files
committed
SI-8428 Refactor ConcatIterator
Make the head iterator a constructor parameter, for easier construction and implementation of ++.
1 parent 1fa46a5 commit aa1e1d0

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

bincompat-forward.whitelist.conf

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,10 @@ filter {
139139
{
140140
matchName="scala.reflect.api.Internals#ReificationSupportApi.SyntacticPartialFunction"
141141
problemName=MissingMethodProblem
142-
}
142+
},
143+
{
144+
matchName="scala.collection.Iterator#ConcatIterator.this"
145+
problemName=MissingMethodProblem
146+
}
143147
]
144148
}

src/library/scala/collection/Iterator.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -165,11 +165,11 @@ object Iterator {
165165
/** Avoid stack overflows when applying ++ to lots of iterators by
166166
* flattening the unevaluated iterators out into a vector of closures.
167167
*/
168-
private[scala] final class ConcatIterator[+A](initial: Vector[() => Iterator[A]]) extends Iterator[A] {
169-
// current set to null when all iterators are exhausted
170-
private[this] var current: Iterator[A] = Iterator.empty
168+
private[scala] final class ConcatIterator[+A](private[this] var current: Iterator[A], initial: Vector[() => Iterator[A]]) extends Iterator[A] {
169+
@deprecated def this(initial: Vector[() => Iterator[A]]) = this(Iterator.empty, initial) // for binary compatibility
171170
private[this] var queue: Vector[() => Iterator[A]] = initial
172171
// Advance current to the next non-empty iterator
172+
// current is set to null when all iterators are exhausted
173173
private[this] def advance(): Boolean = {
174174
if (queue.isEmpty) {
175175
current = null
@@ -185,7 +185,7 @@ object Iterator {
185185
def next() = if (hasNext) current.next else Iterator.empty.next
186186

187187
override def ++[B >: A](that: => GenTraversableOnce[B]): Iterator[B] =
188-
new ConcatIterator((() => current) +: (queue :+ (() => that.toIterator)))
188+
new ConcatIterator(current, queue :+ (() => that.toIterator))
189189
}
190190

191191
private[scala] final class JoinIterator[+A](lhs: Iterator[A], that: => GenTraversableOnce[A]) extends Iterator[A] {
@@ -194,7 +194,7 @@ object Iterator {
194194
def next = if (lhs.hasNext) lhs.next else rhs.next
195195

196196
override def ++[B >: A](that: => GenTraversableOnce[B]) =
197-
new ConcatIterator(Vector(() => this, () => that.toIterator))
197+
new ConcatIterator(this, Vector(() => that.toIterator))
198198
}
199199
}
200200

0 commit comments

Comments
 (0)