Created
March 17, 2009 17:08
-
-
Save okomok/80658 to your computer and use it in GitHub Desktop.
wrong mulitpass
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
// Copyright Shunsuke Sogame 2008-2009. | |
// Distributed under the terms of an MIT-style license. | |
package mada.iter | |
/** | |
* Contains utility methods operating on multi-pass iterables. | |
*/ | |
object MultiPass { | |
/** | |
* Creates from <code>Iterable</code>. | |
*/ | |
def elements[A](it: Iterable[A]): MultiPassIterator[A] = new MultiPassIterator[A] { | |
private var c: Iterable[A] = it.projection | |
private var i = c.elements | |
override def hasNext = i.hasNext | |
override def next = { c = Iterables.drop(c, 1); i.next } // drop is O(n)!! | |
override def clone = elements(c) | |
} | |
} | |
/** | |
* Placed only because of "academic" reason. | |
* This is equivalent to STL ForwardIterator, but practically unusable | |
* because JVM triggers heap-allocation every cloning of iterator. | |
* In JVM, ForwardRange algorithms is implemented by <code>Vector</code>. | |
* (This strategy has already been taken in <code>java.util.regex</code>.) | |
* As a result, you have to copy <code>Iterable</code> into <code>Vector</code>. | |
*/ | |
trait MultiPassIterator[+A] extends Iterator[A] { | |
override def clone: MultiPassIterator[A] = throw new CloneNotSupportedException | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment