forked from uzh/signal-collect
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reimplemented the iterator concatenator.
No longer accesses an iterator unless all previously added ones have been exhausted. No recurses on a tree (could trigger a stack overflow exception).
- Loading branch information
Showing
3 changed files
with
119 additions
and
57 deletions.
There are no files selected for viewing
59 changes: 59 additions & 0 deletions
59
src/main/scala/com/signalcollect/util/IteratorConcatenator.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* @author Philip Stutz | ||
* | ||
* Copyright 2014 University of Zurich | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
* | ||
*/ | ||
|
||
package com.signalcollect.util | ||
|
||
import scala.annotation.tailrec | ||
import scala.collection.mutable.Queue | ||
|
||
/** | ||
* To avoid https://issues.scala-lang.org/browse/SI-8428, which is not really fixed. | ||
* | ||
* Unfortunately I could not reproduce the problem outside of a large and complex TripleRush evaluation. | ||
*/ | ||
final class IteratorConcatenator[U] extends Iterator[U] { | ||
|
||
val iterators = new Queue[Iterator[U]]() | ||
|
||
def clear { | ||
iterators.clear | ||
} | ||
|
||
def appendIterator(i: Iterator[U]) { | ||
iterators.enqueue(i) | ||
} | ||
|
||
def next: U = { | ||
iterators.head.next | ||
} | ||
|
||
@tailrec def hasNext: Boolean = { | ||
if (iterators.isEmpty) { | ||
false | ||
} else { | ||
val headHasNext = iterators.head.hasNext | ||
if (!headHasNext) { | ||
iterators.dequeue | ||
hasNext | ||
} else { | ||
true | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/test/scala/com/signalcollect/util/IteratorConcatenatorSpec.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
/* | ||
* @author Philip Stutz | ||
* | ||
* Copyright 2014 University of Zurich | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package com.signalcollect.util | ||
|
||
import org.scalacheck.Gen | ||
import org.scalacheck.Gen._ | ||
import org.scalacheck.Arbitrary._ | ||
import org.scalatest.FlatSpec | ||
import org.scalatest.ShouldMatchers | ||
import org.scalatest.prop.Checkers | ||
import java.io.DataOutputStream | ||
import java.io.ByteArrayOutputStream | ||
import org.scalacheck.Arbitrary | ||
|
||
class IteratorConcatenatorSpec extends FlatSpec with ShouldMatchers with Checkers with TestAnnouncements { | ||
|
||
"IteratorConcatenator" should "correctly concatenate multiple iterators" in { | ||
val c = new IteratorConcatenator[Int] | ||
for (i <- 1 to 1000 by 10) { | ||
c.appendIterator((i until i + 10).iterator) | ||
} | ||
c.appendIterator(Seq(1001, 1002, 1003).iterator) | ||
c.appendIterator(Set(1004).iterator) | ||
assert(c.toList == (1 to 1004).toList) | ||
} | ||
|
||
it should "should work with random appends" in { | ||
check( | ||
(is: List[List[Int]]) => { | ||
val c = new IteratorConcatenator[Int] | ||
is.foreach { list => c.appendIterator(list.iterator) } | ||
assert(c.toList == is.flatMap(_.iterator).toList) | ||
true | ||
}, | ||
minSuccessful(100)) | ||
} | ||
|
||
} |