From d37c6976b1cc4c2820b87cf7e94259dddddbe083 Mon Sep 17 00:00:00 2001 From: Alex Tkachman Date: Tue, 20 Dec 2011 15:56:10 +0200 Subject: [PATCH] methods for iterators and filters --- stdlib/ktSrc/Filter.kt | 62 +++++++++++++++++++++++++++++++++++++++ stdlib/ktSrc/Iterators.kt | 38 ++++++++++++++++++++++++ stdlib/ktSrc/JavaUtil.kt | 2 ++ 3 files changed, 102 insertions(+) create mode 100644 stdlib/ktSrc/Filter.kt create mode 100644 stdlib/ktSrc/Iterators.kt diff --git a/stdlib/ktSrc/Filter.kt b/stdlib/ktSrc/Filter.kt new file mode 100644 index 00000000000..4d5870476d9 --- /dev/null +++ b/stdlib/ktSrc/Filter.kt @@ -0,0 +1,62 @@ +namespace std + +import java.util.* +import java.lang.Iterable + +/* +Filters given iterator +*/ +inline fun java.util.Iterator.filter(f: fun(T): Boolean) : java.util.Iterator = FilterIterator(this, f) + +/* +Adds filtered elements in to given container +*/ +inline fun > java.lang.Iterable.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 java.lang.Iterable.filter(f: fun(T): Boolean) : java.util.Iterator = (iterator() as java.util.Iterator).filter(f) + +private class FilterIterator(val original: java.util.Iterator, val filter: fun(T): Boolean) : java.util.Iterator { + 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() + } +} diff --git a/stdlib/ktSrc/Iterators.kt b/stdlib/ktSrc/Iterators.kt new file mode 100644 index 00000000000..8a9b592ab26 --- /dev/null +++ b/stdlib/ktSrc/Iterators.kt @@ -0,0 +1,38 @@ +namespace std.util + +import java.util.* +import java.util.Iterator + +/* +Add iterated elements to given container +*/ +fun > java.util.Iterator.to(container: U) : U { + while(hasNext()) + container.add(next()) + return container +} + +/* +Add iterated elements to java.util.ArrayList +*/ +inline fun java.util.Iterator.toArrayList() = to(ArrayList()) + +/* +Add iterated elements to java.util.LinkedList +*/ +inline fun java.util.Iterator.toLinkedList() = to(LinkedList()) + +/* +Add iterated elements to java.util.HashSet +*/ +inline fun java.util.Iterator.toHashSet() = to(HashSet()) + +/* +Add iterated elements to java.util.LinkedHashSet +*/ +inline fun java.util.Iterator.toLinkedHashSet() = to(LinkedHashSet()) + +/* +Add iterated elements to java.util.TreeSet +*/ +inline fun java.util.Iterator.toTreeSet() = to(TreeSet()) diff --git a/stdlib/ktSrc/JavaUtil.kt b/stdlib/ktSrc/JavaUtil.kt index f0faab3ab2f..4f50c7afb62 100644 --- a/stdlib/ktSrc/JavaUtil.kt +++ b/stdlib/ktSrc/JavaUtil.kt @@ -77,6 +77,7 @@ inline fun java.lang.Iterable.find(predicate: fun(T): Boolean) : T? { /** Returns a new collection containing all elements in this collection which match the given predicate */ // TODO using: Collection 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 java.lang.Iterable.filter(predicate: fun(T): Boolean) : Collection { val result = this.create() for (elem in this) { @@ -85,6 +86,7 @@ inline fun java.lang.Iterable.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