methods for iterators and filters

This commit is contained in:
Alex Tkachman
2011-12-20 15:56:10 +02:00
parent 117f03cbbf
commit d37c6976b1
3 changed files with 102 additions and 0 deletions
+62
View File
@@ -0,0 +1,62 @@
namespace std
import java.util.*
import java.lang.Iterable
/*
Filters given iterator
*/
inline fun <T> java.util.Iterator<T>.filter(f: fun(T): Boolean) : java.util.Iterator<T> = FilterIterator<T>(this, f)
/*
Adds filtered elements in to given container
*/
inline fun <T,U : Collection<in T>> java.lang.Iterable<T>.filterTo(var container: U, filter: fun(T): Boolean) : U {
for(element in this) {
if(filter(element))
container.add(element)
}
return container
}
/*
Create iterator filtering given java.lang.Iterable
*/
inline fun <T> java.lang.Iterable<T>.filter(f: fun(T): Boolean) : java.util.Iterator<T> = (iterator() as java.util.Iterator<T>).filter(f)
private class FilterIterator<T>(val original: java.util.Iterator<T>, val filter: fun(T): Boolean) : java.util.Iterator<T> {
var state = 0
var nextElement: T? = null
override fun hasNext(): Boolean =
when(state) {
1 => true // checked and next present
2 => false // checked and next not present
else => {
while(original.hasNext()) {
val candidate = original.next()
if((filter)(candidate)) {
nextElement = candidate
state = 1
true
}
}
state = 2
false
}
}
override fun next(): T =
if(state != 1)
throw java.util.NoSuchElementException()
else {
val res = nextElement as T
nextElement = null
state = 0
res
}
override fun remove() {
throw java.lang.UnsupportedOperationException()
}
}
+38
View File
@@ -0,0 +1,38 @@
namespace std.util
import java.util.*
import java.util.Iterator
/*
Add iterated elements to given container
*/
fun <T,U: Collection<in T>> java.util.Iterator<T>.to(container: U) : U {
while(hasNext())
container.add(next())
return container
}
/*
Add iterated elements to java.util.ArrayList
*/
inline fun <T> java.util.Iterator<T>.toArrayList() = to(ArrayList<T>())
/*
Add iterated elements to java.util.LinkedList
*/
inline fun <T> java.util.Iterator<T>.toLinkedList() = to(LinkedList<T>())
/*
Add iterated elements to java.util.HashSet
*/
inline fun <T> java.util.Iterator<T>.toHashSet() = to(HashSet<T>())
/*
Add iterated elements to java.util.LinkedHashSet
*/
inline fun <T> java.util.Iterator<T>.toLinkedHashSet() = to(LinkedHashSet<T>())
/*
Add iterated elements to java.util.TreeSet
*/
inline fun <T> java.util.Iterator<T>.toTreeSet() = to(TreeSet<T>())
+2
View File
@@ -77,6 +77,7 @@ inline fun <T> java.lang.Iterable<T>.find(predicate: fun(T): Boolean) : T? {
/** Returns a new collection containing all elements in this collection which match the given predicate */
// TODO using: Collection<T> for the return type - I wonder if this exact type could be
// deduced somewhat from the This.Type; e.g. returning Set on a Set, Array on Array etc
/*
inline fun <T> java.lang.Iterable<T>.filter(predicate: fun(T): Boolean) : Collection<T> {
val result = this.create<T,T>()
for (elem in this) {
@@ -85,6 +86,7 @@ inline fun <T> java.lang.Iterable<T>.filter(predicate: fun(T): Boolean) : Collec
}
return result
}
*/
/**
* Returns the result of transforming each item in the collection to a one or more values which