diff options
author | Som Snytt <som.snytt@gmail.com> | 2016-09-05 23:00:20 -0700 |
---|---|---|
committer | Som Snytt <som.snytt@gmail.com> | 2016-09-06 20:50:48 -0700 |
commit | 9a6ef0fe508a7ba9692871ee05452c8dbd29888b (patch) | |
tree | 8b9775998ccb3c78b67f198805fbbf905a4b6c41 | |
parent | dc6b91822f695250938ee06ad21818b1ca8a778d (diff) | |
download | scala-9a6ef0fe508a7ba9692871ee05452c8dbd29888b.tar.gz scala-9a6ef0fe508a7ba9692871ee05452c8dbd29888b.tar.bz2 scala-9a6ef0fe508a7ba9692871ee05452c8dbd29888b.zip |
SI-9913 Tighten bolts on span iterator
Extra privacy, and the tricky state transition is made
more tabular.
-rw-r--r-- | src/library/scala/collection/Iterator.scala | 23 |
1 files changed, 10 insertions, 13 deletions
diff --git a/src/library/scala/collection/Iterator.scala b/src/library/scala/collection/Iterator.scala index 720339f054..03b9fbff26 100644 --- a/src/library/scala/collection/Iterator.scala +++ b/src/library/scala/collection/Iterator.scala @@ -648,15 +648,15 @@ trait Iterator[+A] extends TraversableOnce[A] { * handling of structural calls. It's not what's intended here. */ class Leading extends AbstractIterator[A] { - var lookahead: mutable.Queue[A] = null - var hd: A = _ + private[this] var lookahead: mutable.Queue[A] = null + private[this] var hd: A = _ /* Status is kept with magic numbers * 1 means next element is in hd and we're still reading into this iterator * 0 means we're still reading but haven't found a next element * -1 means we are done reading into the iterator, so we must rely on lookahead * -2 means we are done but have saved hd for the other iterator to use as its first element */ - var status = 0 + private[this] var status = 0 private def store(a: A) { if (lookahead == null) lookahead = new mutable.Queue[A] lookahead += a @@ -680,14 +680,11 @@ trait Iterator[+A] extends TraversableOnce[A] { } else empty.next() } - def finish(): Boolean = { - if (status == -1) false - else if (status == -2) { - status = -1 - true - } - else { - if (status == 1) store(hd) + def finish(): Boolean = status match { + case -2 => status = -1 ; true + case -1 => false + case 1 => store(hd) ; status = 0 ; finish() + case 0 => status = -1 while (self.hasNext) { val a = self.next() @@ -698,8 +695,8 @@ trait Iterator[+A] extends TraversableOnce[A] { } } false - } } + def trailer: A = hd } val leading = new Leading @@ -732,7 +729,7 @@ trait Iterator[+A] extends TraversableOnce[A] { if (status > 0) self.next() else { status = 1 - val ans = myLeading.hd + val ans = myLeading.trailer myLeading = null ans } |