diff --git a/libraries/stdlib/src/generated/Arrays.kt b/libraries/stdlib/src/generated/Arrays.kt new file mode 100644 index 00000000000..474e70b0fdb --- /dev/null +++ b/libraries/stdlib/src/generated/Arrays.kt @@ -0,0 +1,405 @@ +package kotlin + +import java.util.* + +/** + * Returns *true* if all elements match the given *predicate* + */ +public inline fun Array.all(predicate: (T) -> Boolean) : Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns *true* if any elements match the given *predicate* + */ +public inline fun Array.any(predicate: (T) -> Boolean) : Boolean { + for (element in this) if (predicate(element)) return true + return false +} + +/** + * Returns the number of elements which match the given *predicate* + */ +public inline fun Array.count(predicate: (T) -> Boolean) : Int { + var count = 0 + for (element in this) if (predicate(element)) count++ + return count +} + +/** + * Returns the first element which matches the given *predicate* or *null* if none matched + */ +public inline fun Array.find(predicate: (T) -> Boolean) : T? { + for (element in this) if (predicate(element)) return element + return null +} + +/** + * Returns a list containing all elements which match the given *predicate* + */ +public inline fun Array.filter(predicate: (T) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > Array.filterTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) + return result +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun Array.filterNot(predicate: (T) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > Array.filterNotTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Returns a list containing all the non-*null* elements + */ +public inline fun Array.filterNotNull() : List { + return filterNotNullTo>(ArrayList()) +} + +/** + * Filters all non-*null* elements into the given list + */ +public inline fun > Array.filterNotNullTo(result: C) : C { + for (element in this) if (element != null) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections + */ +public inline fun Array.partition(predicate: (T) -> Boolean) : Pair, List> { + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun Array.map(transform : (T) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection + */ +public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single list + */ +public inline fun Array.flatMap(transform: (T)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > Array.flatMapTo(result: C, transform: (T) -> Iterable) : C { + for (element in this) { + val list = transform(element) + for (r in list) result.add(r) + } + return result +} + +/** + * Performs the given *operation* on each element + */ +public inline fun Array.forEach(operation: (T) -> Unit) : Unit { + for (element in this) operation(element) +} + +/** + * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements + */ +public inline fun Array.fold(initial: R, operation: (R, T) -> R) : R { + var answer = initial + for (element in this) answer = operation(answer, element) + return answer +} + +/** + * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements + */ +public inline fun Array.foldRight(initial: R, operation: (T, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} + +/** + * Applies binary operation to all elements of iterable, going from left to right. + * Similar to fold function, but uses the first element as initial value + */ +public inline fun Array.reduce(operation: (T, T) -> T) : T { + val iterator = this.iterator() + if (!iterator.hasNext()) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway + while (iterator.hasNext()) { + result = operation(result, iterator.next()) + } + + return result +} + +/** + * Applies binary operation to all elements of iterable, going from right to left. + * Similar to foldRight function, but uses the last element as initial value + */ +public inline fun Array.reduceRight(operation: (T, T) -> T) : T { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} + +/** + * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by + */ +public inline fun Array.groupBy(toKey: (T) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} + +public inline fun Array.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + list.add(element) + } + return result +} + +/** + * Returns a list containing everything but the first *n* elements + */ +public inline fun Array.drop(n: Int) : List { + return dropWhile(countTo(n)) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun Array.dropWhile(predicate: (T) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > Array.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result +} + +/** + * Returns a list containing the first *n* elements + */ +public inline fun Array.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun Array.takeWhile(predicate: (T) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > Array.takeWhileTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Copies all elements into the given collection + */ +public inline fun > Array.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** + * Reverses the order the elements into a list + */ +public inline fun Array.reverse() : List { + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list +} + +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun Array.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} + +/** + * Copies all elements into a [[List]] + */ +public inline fun Array.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun Array.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun Array.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + */ +public inline fun Array.requireNoNulls() : Array { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this") + } + } + return this as Array +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun Array.plus(element: T) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun Array.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun Array.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun Array.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > Array.sortBy(f: (T) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Array.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Array.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/ArraysFromCollections.kt b/libraries/stdlib/src/generated/ArraysFromCollections.kt deleted file mode 100644 index bcef4169b07..00000000000 --- a/libraries/stdlib/src/generated/ArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/ArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/ArraysFromCollectionsJVM.kt deleted file mode 100644 index a66038b92b7..00000000000 --- a/libraries/stdlib/src/generated/ArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun Array.map(transform : (T) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/ArraysFromIterables.kt b/libraries/stdlib/src/generated/ArraysFromIterables.kt deleted file mode 100644 index 2f951bd535b..00000000000 --- a/libraries/stdlib/src/generated/ArraysFromIterables.kt +++ /dev/null @@ -1,286 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - -import java.util.* - -/** - * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all - */ -public inline fun Array.all(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (!predicate(element)) return false - return true -} - -/** - * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any - */ -public inline fun Array.any(predicate: (T) -> Boolean) : Boolean { - for (element in this) if (predicate(element)) return true - return false -} - -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun Array.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - -/** - * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count - */ -public inline fun Array.count(predicate: (T) -> Boolean) : Int { - var count = 0 - for (element in this) if (predicate(element)) count++ - return count -} - -/** - * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find - */ -public inline fun Array.find(predicate: (T) -> Boolean) : T? { - for (element in this) if (predicate(element)) return element - return null -} - -/** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList - */ -public inline fun > Array.filterTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) - return result -} - -/** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition - */ -public inline fun Array.partition(predicate: (T) -> Boolean) : Pair, List> { - val first = ArrayList() - val second = ArrayList() - for (element in this) { - if (predicate(element)) { - first.add(element) - } else { - second.add(element) - } - } - return Pair(first, second) -} - -/** - * Returns a list containing all elements which do not match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList - */ -public inline fun > Array.filterNotTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result -} - -/** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList - */ -public inline fun > Array?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } - return result -} - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun Array.flatMapTo(result: MutableCollection, transform: (T) -> Collection) : Collection { - for (element in this) { - val list = transform(element) - for (r in list) result.add(r) - } - return result -} - -/** - * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach - */ -public inline fun Array.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element) - -/** - * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold - */ -public inline fun Array.fold(initial: R, operation: (R, T) -> R): R { - var answer = initial - for (element in this) answer = operation(answer, element) - return answer -} - -/** - * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight - */ -public inline fun Array.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - - -/** - * Applies binary operation to all elements of iterable, going from left to right. - * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce - */ -public inline fun Array.reduce(operation: (T, T) -> T): T { - val iterator = this.iterator() - if (!iterator.hasNext()) { - throw UnsupportedOperationException("Empty iterable can't be reduced") - } - - var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway - while (iterator.hasNext()) { - result = operation(result, iterator.next()) - } - - return result -} - -/** - * Applies binary operation to all elements of iterable, going from right to left. - * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight - */ -public inline fun Array.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) } - - -/** - * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ -public inline fun Array.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) - -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ -public inline fun Array.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { - for (element in this) { - val key = toKey(element) - val list = result.getOrPut(key) { ArrayList() } - list.add(element) - } - return result -} - -/** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt makeString - */ -public inline fun Array.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { - val buffer = StringBuilder() - appendString(buffer, separator, prefix, postfix, limit, truncated) - return buffer.toString() -} - -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > Array.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { - var start = true - for (element in this) { - if (start && predicate(element)) { - // ignore - } else { - start = false - result.add(element) - } - } - return result -} - -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > Array.takeWhileTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (predicate(element)) result.add(element) else break - return result -} - -/** Copies all elements into the given collection */ -public inline fun > Array.toCollection(result: C) : C { - for (element in this) result.add(element) - return result -} - -/** - * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse - */ -public inline fun Array.reverse() : List { - val list = toCollection(ArrayList()) - Collections.reverse(list) - return list -} - -/** Copies all elements into a [[LinkedList]] */ -public inline fun Array.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun Array.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun Array.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun Array.toSet() : Set = toCollection(HashSet()) - -/** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun Array.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer -} -*/ diff --git a/libraries/stdlib/src/generated/ArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/ArraysFromIterablesJVM.kt deleted file mode 100644 index 7a2658981f3..00000000000 --- a/libraries/stdlib/src/generated/ArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun Array.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/ArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/ArraysFromIterablesLazy.kt deleted file mode 100644 index 9a8382ef08a..00000000000 --- a/libraries/stdlib/src/generated/ArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun Array.filter(predicate: (T) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun Array.filterNot(predicate: (T)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun Array?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun Array.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun Array.plus(element: T): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun Array.plus(elements: Array): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun Array.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun Array.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun Array.dropWhile(predicate: (T) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun Array.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun Array.takeWhile(predicate: (T) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/BooleanArraysFromIterables.kt b/libraries/stdlib/src/generated/BooleanArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/BooleanArraysFromIterables.kt rename to libraries/stdlib/src/generated/BooleanArrays.kt index bda6c547ac3..fa5b800ca76 100644 --- a/libraries/stdlib/src/generated/BooleanArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/BooleanArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun BooleanArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C { +public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > BooleanArray.filterNotTo(result: C, predicate: (Boolean) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair } /** - * Returns a list containing all elements which do not match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + * Returns a new List containing the results of applying the given *transform* function to each element in this collection */ -public inline fun > BooleanArray.filterNotTo(result: C, predicate: (Boolean) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun BooleanArray.map(transform : (Boolean) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > BooleanArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun BooleanArray.flatMapTo(result: MutableCollection, transform: (Boolean) -> Collection) : Collection { +public inline fun BooleanArray.flatMap(transform: (Boolean)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > BooleanArray.flatMapTo(result: C, transform: (Boolean) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun BooleanArray.flatMapTo(result: MutableCollection, trans /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit) : Unit = for (element in this) operation(element) +public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun BooleanArray.fold(initial: R, operation: (R, Boolean) -> R): R { +public inline fun BooleanArray.fold(initial: R, operation: (R, Boolean) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun BooleanArray.fold(initial: R, operation: (R, Boolean) -> R /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun BooleanArray.foldRight(initial: R, operation: (Boolean, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean): Boolean { +public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean) : Boolean { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Boolean = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean = reverse().reduce { x, y -> operation(y, x) } - +public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean) : Boolean { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun BooleanArray.groupBy(toKey: (Boolean) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun BooleanArray.groupByTo(result: MutableMap>, toKey: (Boolean) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun BooleanArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > BooleanArray.dropWhileTo(result: L, predicate: (Boolean) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > BooleanArray.dropWhileTo(result: L, predicate: (Boolean) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > BooleanArray.dropWhileTo(result: L, return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > BooleanArray.takeWhileTo(result: C, predicate: (Boolean) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun BooleanArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > BooleanArray.takeWhileTo(result: C, predicate: (Boolean) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > BooleanArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > BooleanArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun BooleanArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun BooleanArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun BooleanArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun BooleanArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun BooleanArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun BooleanArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun BooleanArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun BooleanArray.toSortedList(transform: fun(Boolean) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun BooleanArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun BooleanArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun BooleanArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun BooleanArray.plus(element: Boolean) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun BooleanArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun BooleanArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun BooleanArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > BooleanArray.sortBy(f: (Boolean) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Boolean, y: Boolean) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun BooleanArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun BooleanArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/BooleanArraysFromCollections.kt b/libraries/stdlib/src/generated/BooleanArraysFromCollections.kt deleted file mode 100644 index 9f309ebb2da..00000000000 --- a/libraries/stdlib/src/generated/BooleanArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/BooleanArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/BooleanArraysFromCollectionsJVM.kt deleted file mode 100644 index e3b6f785ec6..00000000000 --- a/libraries/stdlib/src/generated/BooleanArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun BooleanArray.map(transform : (Boolean) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/BooleanArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/BooleanArraysFromIterablesJVM.kt deleted file mode 100644 index 1575286598c..00000000000 --- a/libraries/stdlib/src/generated/BooleanArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun BooleanArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/BooleanArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/BooleanArraysFromIterablesLazy.kt deleted file mode 100644 index d12e52f1adf..00000000000 --- a/libraries/stdlib/src/generated/BooleanArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun BooleanArray.filterNot(predicate: (Boolean)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun BooleanArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun BooleanArray.flatMap(transform: (Boolean)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun BooleanArray.plus(element: Boolean): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun BooleanArray.plus(elements: BooleanArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun BooleanArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun BooleanArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun BooleanArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/ByteArraysFromIterables.kt b/libraries/stdlib/src/generated/ByteArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/ByteArraysFromIterables.kt rename to libraries/stdlib/src/generated/ByteArrays.kt index 08e424dfb7e..bcc2ec7de8b 100644 --- a/libraries/stdlib/src/generated/ByteArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/ByteArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun ByteArray.all(predicate: (Byte) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun ByteArray.all(predicate: (Byte) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun ByteArray.any(predicate: (Byte) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun ByteArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun ByteArray.count(predicate: (Byte) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun ByteArray.count(predicate: (Byte) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C { +public inline fun ByteArray.filter(predicate: (Byte) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > ByteArray.filterNotTo(result: C, predicate: (Byte) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair> ByteArray.filterNotTo(result: C, predicate: (Byte) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun ByteArray.map(transform : (Byte) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > ByteArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > ByteArray.mapTo(result: C, transform : (Byte) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun ByteArray.flatMapTo(result: MutableCollection, transform: (Byte) -> Collection) : Collection { +public inline fun ByteArray.flatMap(transform: (Byte)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > ByteArray.flatMapTo(result: C, transform: (Byte) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun ByteArray.flatMapTo(result: MutableCollection, transfor /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun ByteArray.forEach(operation: (Byte) -> Unit) : Unit = for (element in this) operation(element) +public inline fun ByteArray.forEach(operation: (Byte) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun ByteArray.fold(initial: R, operation: (R, Byte) -> R): R { +public inline fun ByteArray.fold(initial: R, operation: (R, Byte) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun ByteArray.fold(initial: R, operation: (R, Byte) -> R): R { /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun ByteArray.foldRight(initial: R, operation: (Byte, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun ByteArray.foldRight(initial: R, operation: (Byte, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte): Byte { +public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte) : Byte { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Byte = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte = reverse().reduce { x, y -> operation(y, x) } - +public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte) : Byte { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun ByteArray.groupBy(toKey: (Byte) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun ByteArray.groupBy(toKey: (Byte) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun ByteArray.groupByTo(result: MutableMap>, toKey: (Byte) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun ByteArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > ByteArray.dropWhileTo(result: L, predicate: (Byte) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > ByteArray.dropWhileTo(result: L, predicate: (Byte) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > ByteArray.dropWhileTo(result: L, predic return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > ByteArray.takeWhileTo(result: C, predicate: (Byte) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun ByteArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > ByteArray.takeWhileTo(result: C, predicate: (Byte) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > ByteArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > ByteArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun ByteArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun ByteArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun ByteArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun ByteArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun ByteArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun ByteArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun ByteArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun ByteArray.toSortedList(transform: fun(Byte) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun ByteArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun ByteArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun ByteArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun ByteArray.plus(element: Byte) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun ByteArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun ByteArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun ByteArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > ByteArray.sortBy(f: (Byte) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Byte, y: Byte) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ByteArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ByteArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/ByteArraysFromCollections.kt b/libraries/stdlib/src/generated/ByteArraysFromCollections.kt deleted file mode 100644 index 72759b1d8c4..00000000000 --- a/libraries/stdlib/src/generated/ByteArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > ByteArray.mapTo(result: C, transform : (Byte) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/ByteArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/ByteArraysFromCollectionsJVM.kt deleted file mode 100644 index 9355d5b7a5a..00000000000 --- a/libraries/stdlib/src/generated/ByteArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun ByteArray.map(transform : (Byte) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/ByteArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/ByteArraysFromIterablesJVM.kt deleted file mode 100644 index 5a0f7780042..00000000000 --- a/libraries/stdlib/src/generated/ByteArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun ByteArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/ByteArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/ByteArraysFromIterablesLazy.kt deleted file mode 100644 index fda727c7c07..00000000000 --- a/libraries/stdlib/src/generated/ByteArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun ByteArray.filter(predicate: (Byte) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun ByteArray.filterNot(predicate: (Byte)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun ByteArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun ByteArray.flatMap(transform: (Byte)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun ByteArray.plus(element: Byte): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun ByteArray.plus(elements: ByteArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun ByteArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun ByteArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun ByteArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/CharArraysFromIterables.kt b/libraries/stdlib/src/generated/CharArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/CharArraysFromIterables.kt rename to libraries/stdlib/src/generated/CharArrays.kt index bb29bb0bec2..79696bffb71 100644 --- a/libraries/stdlib/src/generated/CharArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/CharArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun CharArray.all(predicate: (Char) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun CharArray.all(predicate: (Char) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun CharArray.any(predicate: (Char) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun CharArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun CharArray.count(predicate: (Char) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun CharArray.count(predicate: (Char) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C { +public inline fun CharArray.filter(predicate: (Char) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun CharArray.filterNot(predicate: (Char) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > CharArray.filterNotTo(result: C, predicate: (Char) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair> CharArray.filterNotTo(result: C, predicate: (Char) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun CharArray.map(transform : (Char) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > CharArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > CharArray.mapTo(result: C, transform : (Char) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun CharArray.flatMapTo(result: MutableCollection, transform: (Char) -> Collection) : Collection { +public inline fun CharArray.flatMap(transform: (Char)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > CharArray.flatMapTo(result: C, transform: (Char) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun CharArray.flatMapTo(result: MutableCollection, transfor /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun CharArray.forEach(operation: (Char) -> Unit) : Unit = for (element in this) operation(element) +public inline fun CharArray.forEach(operation: (Char) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun CharArray.fold(initial: R, operation: (R, Char) -> R): R { +public inline fun CharArray.fold(initial: R, operation: (R, Char) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun CharArray.fold(initial: R, operation: (R, Char) -> R): R { /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun CharArray.foldRight(initial: R, operation: (Char, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun CharArray.foldRight(initial: R, operation: (Char, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun CharArray.reduce(operation: (Char, Char) -> Char): Char { +public inline fun CharArray.reduce(operation: (Char, Char) -> Char) : Char { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Char = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char = reverse().reduce { x, y -> operation(y, x) } - +public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char) : Char { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun CharArray.groupBy(toKey: (Char) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun CharArray.groupBy(toKey: (Char) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun CharArray.groupByTo(result: MutableMap>, toKey: (Char) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun CharArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > CharArray.dropWhileTo(result: L, predicate: (Char) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > CharArray.dropWhileTo(result: L, predicate: (Char) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > CharArray.dropWhileTo(result: L, predic return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > CharArray.takeWhileTo(result: C, predicate: (Char) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun CharArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > CharArray.takeWhileTo(result: C, predicate: (Char) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > CharArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > CharArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun CharArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun CharArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun CharArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun CharArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun CharArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun CharArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun CharArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun CharArray.toSortedList(transform: fun(Char) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun CharArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun CharArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun CharArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun CharArray.plus(element: Char) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun CharArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun CharArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun CharArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > CharArray.sortBy(f: (Char) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Char, y: Char) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun CharArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun CharArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/CharArraysFromCollections.kt b/libraries/stdlib/src/generated/CharArraysFromCollections.kt deleted file mode 100644 index aa564b95d26..00000000000 --- a/libraries/stdlib/src/generated/CharArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > CharArray.mapTo(result: C, transform : (Char) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/CharArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/CharArraysFromCollectionsJVM.kt deleted file mode 100644 index d1c415d221c..00000000000 --- a/libraries/stdlib/src/generated/CharArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun CharArray.map(transform : (Char) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/CharArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/CharArraysFromIterablesJVM.kt deleted file mode 100644 index 1a27a24096c..00000000000 --- a/libraries/stdlib/src/generated/CharArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun CharArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/CharArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/CharArraysFromIterablesLazy.kt deleted file mode 100644 index d84077dc06a..00000000000 --- a/libraries/stdlib/src/generated/CharArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun CharArray.filter(predicate: (Char) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun CharArray.filterNot(predicate: (Char)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun CharArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun CharArray.flatMap(transform: (Char)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun CharArray.plus(element: Char): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun CharArray.plus(elements: CharArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun CharArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun CharArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun CharArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/Collections.kt b/libraries/stdlib/src/generated/Collections.kt new file mode 100644 index 00000000000..bad3407e410 --- /dev/null +++ b/libraries/stdlib/src/generated/Collections.kt @@ -0,0 +1,87 @@ +package kotlin + +import java.util.* + +/** + * Returns a list containing all elements which match the given *predicate* + */ +public inline fun Collection.filter(predicate: (T) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun Collection.filterNot(predicate: (T) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all the non-*null* elements + */ +public inline fun Collection.filterNotNull() : List { + return filterNotNullTo>(ArrayList()) +} + +/** + * Returns a new List containing the results of applying the given *transform* function to each element in this collection + */ +public inline fun Collection.map(transform : (T) -> R) : List { + return mapTo(ArrayList(), transform) +} + +/** + * Returns a list containing the first *n* elements + */ +public inline fun Collection.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun Collection.takeWhile(predicate: (T) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + */ +public inline fun Collection.requireNoNulls() : Collection { + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $this") + } + } + return this as Collection +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun Collection.plus(element: T) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun Collection.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun Collection.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + diff --git a/libraries/stdlib/src/generated/DoubleArraysFromIterables.kt b/libraries/stdlib/src/generated/DoubleArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/DoubleArraysFromIterables.kt rename to libraries/stdlib/src/generated/DoubleArrays.kt index e10ae8c4079..6c5b20bddf4 100644 --- a/libraries/stdlib/src/generated/DoubleArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/DoubleArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun DoubleArray.all(predicate: (Double) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun DoubleArray.all(predicate: (Double) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun DoubleArray.any(predicate: (Double) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun DoubleArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun DoubleArray.count(predicate: (Double) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun DoubleArray.count(predicate: (Double) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C { +public inline fun DoubleArray.filter(predicate: (Double) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > DoubleArray.filterNotTo(result: C, predicate: (Double) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair> DoubleArray.filterNotTo(result: C, predicate: (Double) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun DoubleArray.map(transform : (Double) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > DoubleArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > DoubleArray.mapTo(result: C, transform : (Double) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun DoubleArray.flatMapTo(result: MutableCollection, transform: (Double) -> Collection) : Collection { +public inline fun DoubleArray.flatMap(transform: (Double)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > DoubleArray.flatMapTo(result: C, transform: (Double) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun DoubleArray.flatMapTo(result: MutableCollection, transf /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun DoubleArray.forEach(operation: (Double) -> Unit) : Unit = for (element in this) operation(element) +public inline fun DoubleArray.forEach(operation: (Double) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun DoubleArray.fold(initial: R, operation: (R, Double) -> R): R { +public inline fun DoubleArray.fold(initial: R, operation: (R, Double) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun DoubleArray.fold(initial: R, operation: (R, Double) -> R): /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun DoubleArray.foldRight(initial: R, operation: (Double, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun DoubleArray.foldRight(initial: R, operation: (Double, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double): Double { +public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double) : Double { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Double = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double): Double = reverse().reduce { x, y -> operation(y, x) } - +public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double) : Double { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun DoubleArray.groupBy(toKey: (Double) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun DoubleArray.groupBy(toKey: (Double) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun DoubleArray.groupByTo(result: MutableMap>, toKey: (Double) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun DoubleArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > DoubleArray.dropWhileTo(result: L, predicate: (Double) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > DoubleArray.dropWhileTo(result: L, predicate: (Double) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > DoubleArray.dropWhileTo(result: L, pr return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > DoubleArray.takeWhileTo(result: C, predicate: (Double) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun DoubleArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > DoubleArray.takeWhileTo(result: C, predicate: (Double) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > DoubleArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > DoubleArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun DoubleArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun DoubleArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun DoubleArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun DoubleArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun DoubleArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun DoubleArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun DoubleArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun DoubleArray.toSortedList(transform: fun(Double) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun DoubleArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun DoubleArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun DoubleArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun DoubleArray.plus(element: Double) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun DoubleArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun DoubleArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun DoubleArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > DoubleArray.sortBy(f: (Double) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Double, y: Double) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun DoubleArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun DoubleArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/DoubleArraysFromCollections.kt b/libraries/stdlib/src/generated/DoubleArraysFromCollections.kt deleted file mode 100644 index 0ed5a11806a..00000000000 --- a/libraries/stdlib/src/generated/DoubleArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > DoubleArray.mapTo(result: C, transform : (Double) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/DoubleArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/DoubleArraysFromCollectionsJVM.kt deleted file mode 100644 index dbba91b15f3..00000000000 --- a/libraries/stdlib/src/generated/DoubleArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun DoubleArray.map(transform : (Double) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/DoubleArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/DoubleArraysFromIterablesJVM.kt deleted file mode 100644 index b8015d5bf5b..00000000000 --- a/libraries/stdlib/src/generated/DoubleArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun DoubleArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/DoubleArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/DoubleArraysFromIterablesLazy.kt deleted file mode 100644 index 06ce8491160..00000000000 --- a/libraries/stdlib/src/generated/DoubleArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun DoubleArray.filter(predicate: (Double) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun DoubleArray.filterNot(predicate: (Double)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun DoubleArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun DoubleArray.flatMap(transform: (Double)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun DoubleArray.plus(element: Double): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun DoubleArray.plus(elements: DoubleArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun DoubleArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun DoubleArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun DoubleArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/FloatArraysFromIterables.kt b/libraries/stdlib/src/generated/FloatArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/FloatArraysFromIterables.kt rename to libraries/stdlib/src/generated/FloatArrays.kt index 187f9dedd34..f7ffe1ff959 100644 --- a/libraries/stdlib/src/generated/FloatArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/FloatArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun FloatArray.all(predicate: (Float) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun FloatArray.all(predicate: (Float) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun FloatArray.any(predicate: (Float) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun FloatArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun FloatArray.count(predicate: (Float) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun FloatArray.count(predicate: (Float) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C { +public inline fun FloatArray.filter(predicate: (Float) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > FloatArray.filterNotTo(result: C, predicate: (Float) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair> FloatArray.filterNotTo(result: C, predicate: (Float) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun FloatArray.map(transform : (Float) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > FloatArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > FloatArray.mapTo(result: C, transform : (Float) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun FloatArray.flatMapTo(result: MutableCollection, transform: (Float) -> Collection) : Collection { +public inline fun FloatArray.flatMap(transform: (Float)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > FloatArray.flatMapTo(result: C, transform: (Float) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun FloatArray.flatMapTo(result: MutableCollection, transfo /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun FloatArray.forEach(operation: (Float) -> Unit) : Unit = for (element in this) operation(element) +public inline fun FloatArray.forEach(operation: (Float) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun FloatArray.fold(initial: R, operation: (R, Float) -> R): R { +public inline fun FloatArray.fold(initial: R, operation: (R, Float) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun FloatArray.fold(initial: R, operation: (R, Float) -> R): R /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun FloatArray.foldRight(initial: R, operation: (Float, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun FloatArray.foldRight(initial: R, operation: (Float, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun FloatArray.reduce(operation: (Float, Float) -> Float): Float { +public inline fun FloatArray.reduce(operation: (Float, Float) -> Float) : Float { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Float = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Float = reverse().reduce { x, y -> operation(y, x) } - +public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float) : Float { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun FloatArray.groupBy(toKey: (Float) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun FloatArray.groupBy(toKey: (Float) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun FloatArray.groupByTo(result: MutableMap>, toKey: (Float) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun FloatArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > FloatArray.dropWhileTo(result: L, predicate: (Float) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > FloatArray.dropWhileTo(result: L, predicate: (Float) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > FloatArray.dropWhileTo(result: L, pred return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > FloatArray.takeWhileTo(result: C, predicate: (Float) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun FloatArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > FloatArray.takeWhileTo(result: C, predicate: (Float) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > FloatArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > FloatArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun FloatArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun FloatArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun FloatArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun FloatArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun FloatArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun FloatArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun FloatArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun FloatArray.toSortedList(transform: fun(Float) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun FloatArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun FloatArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun FloatArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun FloatArray.plus(element: Float) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun FloatArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun FloatArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun FloatArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > FloatArray.sortBy(f: (Float) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Float, y: Float) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun FloatArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun FloatArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/FloatArraysFromCollections.kt b/libraries/stdlib/src/generated/FloatArraysFromCollections.kt deleted file mode 100644 index 0c83627030c..00000000000 --- a/libraries/stdlib/src/generated/FloatArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > FloatArray.mapTo(result: C, transform : (Float) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/FloatArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/FloatArraysFromCollectionsJVM.kt deleted file mode 100644 index 978d100824f..00000000000 --- a/libraries/stdlib/src/generated/FloatArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun FloatArray.map(transform : (Float) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/FloatArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/FloatArraysFromIterablesJVM.kt deleted file mode 100644 index 6ba6ef279c3..00000000000 --- a/libraries/stdlib/src/generated/FloatArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun FloatArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/FloatArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/FloatArraysFromIterablesLazy.kt deleted file mode 100644 index 045808a46a2..00000000000 --- a/libraries/stdlib/src/generated/FloatArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun FloatArray.filter(predicate: (Float) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun FloatArray.filterNot(predicate: (Float)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun FloatArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun FloatArray.flatMap(transform: (Float)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun FloatArray.plus(element: Float): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun FloatArray.plus(elements: FloatArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun FloatArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun FloatArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun FloatArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/IntArraysFromIterables.kt b/libraries/stdlib/src/generated/IntArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/IntArraysFromIterables.kt rename to libraries/stdlib/src/generated/IntArrays.kt index ea262e166ac..eeb853d4020 100644 --- a/libraries/stdlib/src/generated/IntArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/IntArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun IntArray.all(predicate: (Int) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun IntArray.all(predicate: (Int) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun IntArray.any(predicate: (Int) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun IntArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun IntArray.count(predicate: (Int) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun IntArray.count(predicate: (Int) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C { +public inline fun IntArray.filter(predicate: (Int) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun IntArray.filterNot(predicate: (Int) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > IntArray.filterNotTo(result: C, predicate: (Int) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair> IntArray.filterNotTo(result: C, predicate: (Int) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun IntArray.map(transform : (Int) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > IntArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > IntArray.mapTo(result: C, transform : (Int) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun IntArray.flatMapTo(result: MutableCollection, transform: (Int) -> Collection) : Collection { +public inline fun IntArray.flatMap(transform: (Int)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > IntArray.flatMapTo(result: C, transform: (Int) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun IntArray.flatMapTo(result: MutableCollection, transform /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun IntArray.forEach(operation: (Int) -> Unit) : Unit = for (element in this) operation(element) +public inline fun IntArray.forEach(operation: (Int) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun IntArray.fold(initial: R, operation: (R, Int) -> R): R { +public inline fun IntArray.fold(initial: R, operation: (R, Int) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun IntArray.fold(initial: R, operation: (R, Int) -> R): R { /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun IntArray.foldRight(initial: R, operation: (Int, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun IntArray.foldRight(initial: R, operation: (Int, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun IntArray.reduce(operation: (Int, Int) -> Int): Int { +public inline fun IntArray.reduce(operation: (Int, Int) -> Int) : Int { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Int = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int = reverse().reduce { x, y -> operation(y, x) } - +public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int) : Int { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun IntArray.groupBy(toKey: (Int) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun IntArray.groupBy(toKey: (Int) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun IntArray.groupByTo(result: MutableMap>, toKey: (Int) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun IntArray.groupByTo(result: MutableMap> } /** - * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt makeString + * Returns a list containing everything but the first *n* elements */ -public inline fun IntArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { - val buffer = StringBuilder() - appendString(buffer, separator, prefix, postfix, limit, truncated) - return buffer.toString() +public inline fun IntArray.drop(n: Int) : List { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > IntArray.dropWhileTo(result: L, predicate: (Int) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > IntArray.dropWhileTo(result: L, predicate: (Int) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > IntArray.dropWhileTo(result: L, predicat return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > IntArray.takeWhileTo(result: C, predicate: (Int) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun IntArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > IntArray.takeWhileTo(result: C, predicate: (Int) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > IntArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > IntArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun IntArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun IntArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun IntArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun IntArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun IntArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun IntArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun IntArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun IntArray.toSortedList(transform: fun(Int) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun IntArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun IntArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun IntArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun IntArray.plus(element: Int) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun IntArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun IntArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun IntArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > IntArray.sortBy(f: (Int) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Int, y: Int) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun IntArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun IntArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/IntArraysFromCollections.kt b/libraries/stdlib/src/generated/IntArraysFromCollections.kt deleted file mode 100644 index 7e6c41d5795..00000000000 --- a/libraries/stdlib/src/generated/IntArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > IntArray.mapTo(result: C, transform : (Int) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/IntArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/IntArraysFromCollectionsJVM.kt deleted file mode 100644 index a7a89c69d50..00000000000 --- a/libraries/stdlib/src/generated/IntArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun IntArray.map(transform : (Int) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/IntArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/IntArraysFromIterablesJVM.kt deleted file mode 100644 index b51bec3c1bf..00000000000 --- a/libraries/stdlib/src/generated/IntArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun IntArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/IntArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/IntArraysFromIterablesLazy.kt deleted file mode 100644 index 38500e7a116..00000000000 --- a/libraries/stdlib/src/generated/IntArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun IntArray.filter(predicate: (Int) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun IntArray.filterNot(predicate: (Int)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun IntArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun IntArray.flatMap(transform: (Int)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun IntArray.plus(element: Int): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun IntArray.plus(elements: IntArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun IntArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun IntArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun IntArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/kotlin/Iterables.kt b/libraries/stdlib/src/generated/Iterables.kt similarity index 62% rename from libraries/stdlib/src/kotlin/Iterables.kt rename to libraries/stdlib/src/generated/Iterables.kt index eefcd68c7a8..36724978829 100644 --- a/libraries/stdlib/src/kotlin/Iterables.kt +++ b/libraries/stdlib/src/generated/Iterables.kt @@ -4,8 +4,6 @@ import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun Iterable.all(predicate: (T) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -14,40 +12,14 @@ public inline fun Iterable.all(predicate: (T) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun Iterable.any(predicate: (T) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun Iterable.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun Iterable.count(predicate: (T) -> Boolean) : Int { var count = 0 @@ -57,18 +29,14 @@ public inline fun Iterable.count(predicate: (T) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ -public inline fun Iterable.find(predicate: (T) -> Boolean) : T? { +public inline fun Iterable.find(predicate: (T) -> Boolean) : T? { for (element in this) if (predicate(element)) return element return null } /** * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList */ public inline fun > Iterable.filterTo(result: C, predicate: (T) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) @@ -76,9 +44,23 @@ public inline fun > Iterable.filterTo(result: C } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > Iterable.filterNotTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Filters all non-*null* elements into the given list + */ +public inline fun > Iterable.filterNotNullTo(result: C) : C { + for (element in this) if (element != null) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun Iterable.partition(predicate: (T) -> Boolean) : Pair, List> { val first = ArrayList() @@ -94,33 +76,26 @@ public inline fun Iterable.partition(predicate: (T) -> Boolean) : Pair
  • > Iterable.filterNotTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result -} - -/** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList - */ -public inline fun > Iterable?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > Iterable.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun Iterable.flatMapTo(result: MutableCollection, transform: (T) -> Collection) : Collection { +public inline fun Iterable.flatMap(transform: (T)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > Iterable.flatMapTo(result: C, transform: (T) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -130,71 +105,45 @@ public inline fun Iterable.flatMapTo(result: MutableCollection, tra /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun Iterable.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element) +public inline fun Iterable.forEach(operation: (T) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun Iterable.fold(initial: R, operation: (R, T) -> R): R { +public inline fun Iterable.fold(initial: R, operation: (R, T) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer } -/** - * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight - */ -public inline fun Iterable.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - - /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun Iterable.reduce(operation: (T, T) -> T): T { +public inline fun Iterable.reduce(operation: (T, T) -> T) : T { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } -/** - * Applies binary operation to all elements of iterable, going from right to left. - * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight - */ -public inline fun Iterable.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) } - - /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun Iterable.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun Iterable.groupBy(toKey: (T) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun Iterable.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -205,20 +154,22 @@ public inline fun Iterable.groupByTo(result: MutableMap Iterable.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { - val buffer = StringBuilder() - appendString(buffer, separator, prefix, postfix, limit, truncated) - return buffer.toString() +public inline fun Iterable.drop(n: Int) : List { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun Iterable.dropWhile(predicate: (T) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ public inline fun > Iterable.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { var start = true for (element in this) { @@ -232,13 +183,17 @@ public inline fun > Iterable.dropWhileTo(result: L, p return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ public inline fun > Iterable.takeWhileTo(result: C, predicate: (T) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ +/** + * Copies all elements into the given collection + */ public inline fun > Iterable.toCollection(result: C) : C { for (element in this) result.add(element) return result @@ -246,8 +201,6 @@ public inline fun > Iterable.toCollection(resul /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun Iterable.reverse() : List { val list = toCollection(ArrayList()) @@ -255,23 +208,83 @@ public inline fun Iterable.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun Iterable.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun Iterable.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun Iterable.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun Iterable.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun Iterable.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun Iterable.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer + * Copies all elements into a [[List]] + */ +public inline fun Iterable.toList() : List { + return toCollection(ArrayList()) } -*/ + +/** + * Copies all elements into a [[Set]] + */ +public inline fun Iterable.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun Iterable.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun Iterable.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > Iterable.sortBy(f: (T) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterable.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterable.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/Iterators.kt b/libraries/stdlib/src/generated/Iterators.kt new file mode 100644 index 00000000000..9448ab6d1b6 --- /dev/null +++ b/libraries/stdlib/src/generated/Iterators.kt @@ -0,0 +1,84 @@ +package kotlin + +import java.util.* + +/** + * Returns an iterator over elements which match the given *predicate* + */ +public inline fun Iterator.filter(predicate: (T) -> Boolean) : Iterator { + return FilterIterator(this, predicate) +} + +/** + * Returns an iterator over elements which don't match the given *predicate* + */ +public inline fun Iterator.filterNot(predicate: (T) -> Boolean) : Iterator { + return filter {!predicate(it)} +} + +/** + * Returns an iterator over non-*null* elements + */ +public inline fun Iterator.filterNotNull() : Iterator { + return FilterNotNullIterator(this) +} + +/** + * Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R* + */ +public inline fun Iterator.map(transform : (T) -> R) : Iterator { + return MapIterator(this, transform) +} + +/** + * Returns an iterator over the concatenated results of transforming each element to one or more values + */ +public inline fun Iterator.flatMap(transform: (T) -> Iterator) : Iterator { + return FlatMapIterator(this, transform) +} + +/** + * Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements + */ +public inline fun Iterator.requireNoNulls() : Iterator { + return map{ + if (it == null) throw IllegalArgumentException("null element in iterator $this") else it + } +} + +/** + * Returns an iterator restricted to the first *n* elements + */ +public inline fun Iterator.take(n: Int) : Iterator { + var count = n + return takeWhile{ --count >= 0 } +} + +/** + * Returns an iterator restricted to the first elements that match the given *predicate* + */ +public inline fun Iterator.takeWhile(predicate: (T) -> Boolean) : Iterator { + return TakeWhileIterator(this, predicate) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun Iterator.plus(element: T) : Iterator { + return CompositeIterator(this, SingleIterator(element)) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun Iterator.plus(iterator: Iterator) : Iterator { + return CompositeIterator(this, iterator) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun Iterator.plus(collection: Iterable) : Iterator { + return plus(collection.iterator()) +} + diff --git a/libraries/stdlib/src/generated/IteratorsFromIterables.kt b/libraries/stdlib/src/generated/IteratorsCommon.kt similarity index 60% rename from libraries/stdlib/src/generated/IteratorsFromIterables.kt rename to libraries/stdlib/src/generated/IteratorsCommon.kt index 4283ce4f8e8..781480d8886 100644 --- a/libraries/stdlib/src/generated/IteratorsFromIterables.kt +++ b/libraries/stdlib/src/generated/IteratorsCommon.kt @@ -1,19 +1,9 @@ package kotlin -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun Iterator.all(predicate: (T) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -22,40 +12,14 @@ public inline fun Iterator.all(predicate: (T) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun Iterator.any(predicate: (T) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun Iterator.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun Iterator.count(predicate: (T) -> Boolean) : Int { var count = 0 @@ -65,18 +29,14 @@ public inline fun Iterator.count(predicate: (T) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ -public inline fun Iterator.find(predicate: (T) -> Boolean) : T? { +public inline fun Iterator.find(predicate: (T) -> Boolean) : T? { for (element in this) if (predicate(element)) return element return null } /** * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList */ public inline fun > Iterator.filterTo(result: C, predicate: (T) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) @@ -84,9 +44,23 @@ public inline fun > Iterator.filterTo(result: C } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > Iterator.filterNotTo(result: C, predicate: (T) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Filters all non-*null* elements into the given list + */ +public inline fun > Iterator.filterNotNullTo(result: C) : C { + for (element in this) if (element != null) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun Iterator.partition(predicate: (T) -> Boolean) : Pair, List> { val first = ArrayList() @@ -102,33 +76,26 @@ public inline fun Iterator.partition(predicate: (T) -> Boolean) : Pair
  • > Iterator.filterNotTo(result: C, predicate: (T) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result -} - -/** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList - */ -public inline fun > Iterator?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > Iterator.mapTo(result: C, transform : (T) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun Iterator.flatMapTo(result: MutableCollection, transform: (T) -> Collection) : Collection { +public inline fun Iterator.flatMap(transform: (T)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > Iterator.flatMapTo(result: C, transform: (T) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -138,71 +105,45 @@ public inline fun Iterator.flatMapTo(result: MutableCollection, tra /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun Iterator.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element) +public inline fun Iterator.forEach(operation: (T) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun Iterator.fold(initial: R, operation: (R, T) -> R): R { +public inline fun Iterator.fold(initial: R, operation: (R, T) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer } -/** - * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight - */ -public inline fun Iterator.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - - /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun Iterator.reduce(operation: (T, T) -> T): T { +public inline fun Iterator.reduce(operation: (T, T) -> T) : T { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } -/** - * Applies binary operation to all elements of iterable, going from right to left. - * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight - */ -public inline fun Iterator.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) } - - /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun Iterator.groupBy(toKey: (T) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun Iterator.groupBy(toKey: (T) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun Iterator.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -213,20 +154,22 @@ public inline fun Iterator.groupByTo(result: MutableMap Iterator.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String { - val buffer = StringBuilder() - appendString(buffer, separator, prefix, postfix, limit, truncated) - return buffer.toString() +public inline fun Iterator.drop(n: Int) : List { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun Iterator.dropWhile(predicate: (T) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ public inline fun > Iterator.dropWhileTo(result: L, predicate: (T) -> Boolean) : L { var start = true for (element in this) { @@ -240,13 +183,17 @@ public inline fun > Iterator.dropWhileTo(result: L, p return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ public inline fun > Iterator.takeWhileTo(result: C, predicate: (T) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ +/** + * Copies all elements into the given collection + */ public inline fun > Iterator.toCollection(result: C) : C { for (element in this) result.add(element) return result @@ -254,8 +201,6 @@ public inline fun > Iterator.toCollection(resul /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun Iterator.reverse() : List { val list = toCollection(ArrayList()) @@ -263,23 +208,83 @@ public inline fun Iterator.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun Iterator.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun Iterator.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun Iterator.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun Iterator.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun Iterator.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun Iterator.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) - return answer + * Copies all elements into a [[List]] + */ +public inline fun Iterator.toList() : List { + return toCollection(ArrayList()) } -*/ + +/** + * Copies all elements into a [[Set]] + */ +public inline fun Iterator.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun Iterator.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun Iterator.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > Iterator.sortBy(f: (T) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterator.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun Iterator.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/IteratorsFromIterablesJVM.kt b/libraries/stdlib/src/generated/IteratorsFromIterablesJVM.kt deleted file mode 100644 index 5872d1c6b66..00000000000 --- a/libraries/stdlib/src/generated/IteratorsFromIterablesJVM.kt +++ /dev/null @@ -1,15 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun Iterator.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/LongArraysFromIterables.kt b/libraries/stdlib/src/generated/LongArrays.kt similarity index 50% rename from libraries/stdlib/src/generated/LongArraysFromIterables.kt rename to libraries/stdlib/src/generated/LongArrays.kt index fac42e60106..e61d722ec3a 100644 --- a/libraries/stdlib/src/generated/LongArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/LongArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun LongArray.all(predicate: (Long) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun LongArray.all(predicate: (Long) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun LongArray.any(predicate: (Long) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun LongArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun LongArray.count(predicate: (Long) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun LongArray.count(predicate: (Long) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C { +public inline fun LongArray.filter(predicate: (Long) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun LongArray.filterNot(predicate: (Long) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > LongArray.filterNotTo(result: C, predicate: (Long) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair> LongArray.filterNotTo(result: C, predicate: (Long) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun LongArray.map(transform : (Long) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > LongArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > LongArray.mapTo(result: C, transform : (Long) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun LongArray.flatMapTo(result: MutableCollection, transform: (Long) -> Collection) : Collection { +public inline fun LongArray.flatMap(transform: (Long)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > LongArray.flatMapTo(result: C, transform: (Long) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun LongArray.flatMapTo(result: MutableCollection, transfor /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun LongArray.forEach(operation: (Long) -> Unit) : Unit = for (element in this) operation(element) +public inline fun LongArray.forEach(operation: (Long) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun LongArray.fold(initial: R, operation: (R, Long) -> R): R { +public inline fun LongArray.fold(initial: R, operation: (R, Long) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun LongArray.fold(initial: R, operation: (R, Long) -> R): R { /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun LongArray.foldRight(initial: R, operation: (Long, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun LongArray.foldRight(initial: R, operation: (Long, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun LongArray.reduce(operation: (Long, Long) -> Long): Long { +public inline fun LongArray.reduce(operation: (Long, Long) -> Long) : Long { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Long = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long = reverse().reduce { x, y -> operation(y, x) } - +public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long) : Long { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun LongArray.groupBy(toKey: (Long) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun LongArray.groupBy(toKey: (Long) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun LongArray.groupByTo(result: MutableMap>, toKey: (Long) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun LongArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > LongArray.dropWhileTo(result: L, predicate: (Long) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > LongArray.dropWhileTo(result: L, predicate: (Long) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > LongArray.dropWhileTo(result: L, predic return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > LongArray.takeWhileTo(result: C, predicate: (Long) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun LongArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > LongArray.takeWhileTo(result: C, predicate: (Long) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > LongArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > LongArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun LongArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun LongArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun LongArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun LongArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun LongArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun LongArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun LongArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun LongArray.toSortedList(transform: fun(Long) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun LongArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun LongArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun LongArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun LongArray.plus(element: Long) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun LongArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun LongArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun LongArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > LongArray.sortBy(f: (Long) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Long, y: Long) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun LongArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun LongArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/LongArraysFromCollections.kt b/libraries/stdlib/src/generated/LongArraysFromCollections.kt deleted file mode 100644 index fbbd5245956..00000000000 --- a/libraries/stdlib/src/generated/LongArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > LongArray.mapTo(result: C, transform : (Long) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/LongArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/LongArraysFromCollectionsJVM.kt deleted file mode 100644 index cf28a55ec08..00000000000 --- a/libraries/stdlib/src/generated/LongArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun LongArray.map(transform : (Long) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/LongArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/LongArraysFromIterablesJVM.kt deleted file mode 100644 index 153103b5966..00000000000 --- a/libraries/stdlib/src/generated/LongArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun LongArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/LongArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/LongArraysFromIterablesLazy.kt deleted file mode 100644 index b6672234880..00000000000 --- a/libraries/stdlib/src/generated/LongArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun LongArray.filter(predicate: (Long) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun LongArray.filterNot(predicate: (Long)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun LongArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun LongArray.flatMap(transform: (Long)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun LongArray.plus(element: Long): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun LongArray.plus(elements: LongArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun LongArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun LongArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun LongArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/ShortArraysFromIterables.kt b/libraries/stdlib/src/generated/ShortArrays.kt similarity index 51% rename from libraries/stdlib/src/generated/ShortArraysFromIterables.kt rename to libraries/stdlib/src/generated/ShortArrays.kt index 110095d800d..3923a2d424d 100644 --- a/libraries/stdlib/src/generated/ShortArraysFromIterables.kt +++ b/libraries/stdlib/src/generated/ShortArrays.kt @@ -1,20 +1,9 @@ package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Iterables.kt -// - - import java.util.* /** * Returns *true* if all elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt all */ public inline fun ShortArray.all(predicate: (Short) -> Boolean) : Boolean { for (element in this) if (!predicate(element)) return false @@ -23,40 +12,14 @@ public inline fun ShortArray.all(predicate: (Short) -> Boolean) : Boolean { /** * Returns *true* if any elements match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt any */ public inline fun ShortArray.any(predicate: (Short) -> Boolean) : Boolean { for (element in this) if (predicate(element)) return true return false } -/** - * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied - * - * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will - * a special *truncated* separator (which defaults to "..." - * - * @includeFunctionBody ../../test/CollectionTest.kt appendString - */ -public inline fun ShortArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit { - buffer.append(prefix) - var count = 0 - for (element in this) { - if (++count > 1) buffer.append(separator) - if (limit < 0 || count <= limit) { - val text = if (element == null) "null" else element.toString() - buffer.append(text) - } else break - } - if (limit >= 0 && count > limit) buffer.append(truncated) - buffer.append(postfix) -} - /** * Returns the number of elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt count */ public inline fun ShortArray.count(predicate: (Short) -> Boolean) : Int { var count = 0 @@ -66,8 +29,6 @@ public inline fun ShortArray.count(predicate: (Short) -> Boolean) : Int { /** * Returns the first element which matches the given *predicate* or *null* if none matched - * - * @includeFunctionBody ../../test/CollectionTest.kt find */ public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? { for (element in this) if (predicate(element)) return element @@ -75,19 +36,37 @@ public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? { } /** - * Filters all elements which match the given predicate into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList + * Returns a list containing all elements which match the given *predicate* */ -public inline fun > ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C { +public inline fun ShortArray.filter(predicate: (Short) -> Boolean) : List { + return filterTo(ArrayList(), predicate) +} + +/** + * Filters all elements which match the given predicate into the given list + */ +public inline fun > ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) return result } /** - * Partitions this collection into a pair of collection - * - * @includeFunctionBody ../../test/CollectionTest.kt partition + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean) : List { + return filterNotTo(ArrayList(), predicate) +} + +/** + * Returns a list containing all elements which do not match the given *predicate* + */ +public inline fun > ShortArray.filterNotTo(result: C, predicate: (Short) -> Boolean) : C { + for (element in this) if (!predicate(element)) result.add(element) + return result +} + +/** + * Partitions this collection into a pair of collections */ public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair, List> { val first = ArrayList() @@ -103,33 +82,33 @@ public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair> ShortArray.filterNotTo(result: C, predicate: (Short) -> Boolean) : C { - for (element in this) if (!predicate(element)) result.add(element) - return result +public inline fun ShortArray.map(transform : (Short) -> R) : List { + return mapTo(ArrayList(), transform) } /** - * Filters all non-*null* elements into the given list - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList + * Transforms each element of this collection with the given *transform* function and + * adds each return value to the given *results* collection */ -public inline fun > ShortArray?.filterNotNullTo(result: C) : C { - if (this != null) { - for (element in this) if (element != null) result.add(element) - } +public inline fun > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { + for (item in this) + result.add(transform(item)) return result } /** * Returns the result of transforming each element to one or more values which are concatenated together into a single list - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap */ -public inline fun ShortArray.flatMapTo(result: MutableCollection, transform: (Short) -> Collection) : Collection { +public inline fun ShortArray.flatMap(transform: (Short)-> Iterable) : List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns the result of transforming each element to one or more values which are concatenated together into a single collection + */ +public inline fun > ShortArray.flatMapTo(result: C, transform: (Short) -> Iterable) : C { for (element in this) { val list = transform(element) for (r in list) result.add(r) @@ -139,17 +118,15 @@ public inline fun ShortArray.flatMapTo(result: MutableCollection, transfo /** * Performs the given *operation* on each element - * - * @includeFunctionBody ../../test/CollectionTest.kt forEach */ -public inline fun ShortArray.forEach(operation: (Short) -> Unit) : Unit = for (element in this) operation(element) +public inline fun ShortArray.forEach(operation: (Short) -> Unit) : Unit { + for (element in this) operation(element) +} /** * Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt fold */ -public inline fun ShortArray.fold(initial: R, operation: (R, Short) -> R): R { +public inline fun ShortArray.fold(initial: R, operation: (R, Short) -> R) : R { var answer = initial for (element in this) answer = operation(answer, element) return answer @@ -157,53 +134,61 @@ public inline fun ShortArray.fold(initial: R, operation: (R, Short) -> R): R /** * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements - * - * @includeFunctionBody ../../test/CollectionTest.kt foldRight */ -public inline fun ShortArray.foldRight(initial: R, operation: (Short, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)}) - +public inline fun ShortArray.foldRight(initial: R, operation: (Short, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Applies binary operation to all elements of iterable, going from left to right. * Similar to fold function, but uses the first element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduce */ -public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short { +public inline fun ShortArray.reduce(operation: (Short, Short) -> Short) : Short { val iterator = this.iterator() if (!iterator.hasNext()) { throw UnsupportedOperationException("Empty iterable can't be reduced") } - + var result: Short = iterator.next() //compiler doesn't understand that result will initialized anyway while (iterator.hasNext()) { result = operation(result, iterator.next()) } - + return result } /** * Applies binary operation to all elements of iterable, going from right to left. * Similar to foldRight function, but uses the last element as initial value - * - * @includeFunctionBody ../../test/CollectionTest.kt reduceRight */ -public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Short = reverse().reduce { x, y -> operation(y, x) } - +public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short) : Short { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} /** * Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy */ -public inline fun ShortArray.groupBy(toKey: (Short) -> K) : Map> = groupByTo(HashMap>(), toKey) +public inline fun ShortArray.groupBy(toKey: (Short) -> K) : Map> { + return groupByTo(HashMap>(), toKey) +} -/** - * Groups the elements in the collection into the given [[Map]] using the supplied *toKey* function to calculate the key to group the elements by - * - * @includeFunctionBody ../../test/CollectionTest.kt groupBy - */ public inline fun ShortArray.groupByTo(result: MutableMap>, toKey: (Short) -> K) : Map> { for (element in this) { val key = toKey(element) @@ -214,21 +199,23 @@ public inline fun ShortArray.groupByTo(result: MutableMap { + return dropWhile(countTo(n)) } -/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */ -public inline fun > ShortArray.dropWhileTo(result: L, predicate: (Short) -> Boolean) : L { +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean) : List { + return dropWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the everything but the first elements that satisfy the given *predicate* + */ +public inline fun > ShortArray.dropWhileTo(result: L, predicate: (Short) -> Boolean) : L { var start = true for (element in this) { if (start && predicate(element)) { @@ -241,22 +228,38 @@ public inline fun > ShortArray.dropWhileTo(result: L, pred return result } -/** Returns a list containing the first elements that satisfy the given *predicate* */ -public inline fun > ShortArray.takeWhileTo(result: C, predicate: (Short) -> Boolean) : C { +/** + * Returns a list containing the first *n* elements + */ +public inline fun ShortArray.take(n: Int) : List { + return takeWhile(countTo(n)) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean) : List { + return takeWhileTo(ArrayList(), predicate) +} + +/** + * Returns a list containing the first elements that satisfy the given *predicate* + */ +public inline fun > ShortArray.takeWhileTo(result: C, predicate: (Short) -> Boolean) : C { for (element in this) if (predicate(element)) result.add(element) else break return result } -/** Copies all elements into the given collection */ -public inline fun > ShortArray.toCollection(result: C) : C { +/** + * Copies all elements into the given collection + */ +public inline fun > ShortArray.toCollection(result: C) : C { for (element in this) result.add(element) return result } /** * Reverses the order the elements into a list - * - * @includeFunctionBody ../../test/CollectionTest.kt reverse */ public inline fun ShortArray.reverse() : List { val list = toCollection(ArrayList()) @@ -264,23 +267,112 @@ public inline fun ShortArray.reverse() : List { return list } -/** Copies all elements into a [[LinkedList]] */ -public inline fun ShortArray.toLinkedList() : LinkedList = toCollection(LinkedList()) - -/** Copies all elements into a [[List]] */ -public inline fun ShortArray.toList() : List = toCollection(ArrayList()) - -/** Copies all elements into a [[List] */ -public inline fun ShortArray.toCollection() : Collection = toCollection(ArrayList()) - -/** Copies all elements into a [[Set]] */ -public inline fun ShortArray.toSet() : Set = toCollection(HashSet()) +/** + * Copies all elements into a [[LinkedList]] + */ +public inline fun ShortArray.toLinkedList() : LinkedList { + return toCollection(LinkedList()) +} /** - TODO figure out necessary variance/generics ninja stuff... :) -public inline fun ShortArray.toSortedList(transform: fun(Short) : java.lang.Comparable<*>) : List { - val answer = this.toList() - answer.sort(transform) + * Copies all elements into a [[List]] + */ +public inline fun ShortArray.toList() : List { + return toCollection(ArrayList()) +} + +/** + * Copies all elements into a [[Set]] + */ +public inline fun ShortArray.toSet() : Set { + return toCollection(LinkedHashSet()) +} + +/** + * Copies all elements into a [[SortedSet]] + */ +public inline fun ShortArray.toSortedSet() : SortedSet { + return toCollection(TreeSet()) +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the given element at the end + */ +public inline fun ShortArray.plus(element: Short) : List { + val answer = ArrayList() + toCollection(answer) + answer.add(element) return answer } -*/ + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following iterator + */ +public inline fun ShortArray.plus(iterator: Iterator) : List { + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer +} + +/** + * Creates an [[Iterator]] which iterates over this iterator then the following collection + */ +public inline fun ShortArray.plus(collection: Iterable) : List { + return plus(collection.iterator()) +} + +/** + * Returns an iterator of Pairs(index, data) + */ +public inline fun ShortArray.withIndices() : Iterator> { + return IndexIterator(iterator()) +} + +/** + * Copies all elements into a [[List]] and sorts it by value of compare_function(element) + * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + */ +public inline fun > ShortArray.sortBy(f: (Short) -> R) : List { + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: Short, y: Short) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList +} + +/** + * Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ShortArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit { + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) +} + +/** + * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + * a special *truncated* separator (which defaults to "..." + */ +public inline fun ShortArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String { + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() +} + diff --git a/libraries/stdlib/src/generated/ShortArraysFromCollections.kt b/libraries/stdlib/src/generated/ShortArraysFromCollections.kt deleted file mode 100644 index 92f709c4344..00000000000 --- a/libraries/stdlib/src/generated/ShortArraysFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/ShortArraysFromCollectionsJVM.kt b/libraries/stdlib/src/generated/ShortArraysFromCollectionsJVM.kt deleted file mode 100644 index c25d31f8216..00000000000 --- a/libraries/stdlib/src/generated/ShortArraysFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun ShortArray.map(transform : (Short) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/generated/ShortArraysFromIterablesJVM.kt b/libraries/stdlib/src/generated/ShortArraysFromIterablesJVM.kt deleted file mode 100644 index 3a30a6f1bb8..00000000000 --- a/libraries/stdlib/src/generated/ShortArraysFromIterablesJVM.kt +++ /dev/null @@ -1,16 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesJVM.kt -// - - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun ShortArray.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/generated/ShortArraysFromIterablesLazy.kt b/libraries/stdlib/src/generated/ShortArraysFromIterablesLazy.kt deleted file mode 100644 index b390069b522..00000000000 --- a/libraries/stdlib/src/generated/ShortArraysFromIterablesLazy.kt +++ /dev/null @@ -1,119 +0,0 @@ -package kotlin - - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/IterablesLazy.kt -// - - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun ShortArray.filter(predicate: (Short) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun ShortArray.filterNot(predicate: (Short)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun ShortArray?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun ShortArray.flatMap(transform: (Short)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun ShortArray.plus(element: Short): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun ShortArray.plus(elements: ShortArray): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun ShortArray.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun ShortArray.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun ShortArray.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/generated/StandardFromCollections.kt b/libraries/stdlib/src/generated/StandardFromCollections.kt deleted file mode 100644 index 8d309ec3b9a..00000000000 --- a/libraries/stdlib/src/generated/StandardFromCollections.kt +++ /dev/null @@ -1,28 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/Collections.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Iterable.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/generated/StandardFromCollectionsJVM.kt b/libraries/stdlib/src/generated/StandardFromCollectionsJVM.kt deleted file mode 100644 index b2559c88ee1..00000000000 --- a/libraries/stdlib/src/generated/StandardFromCollectionsJVM.kt +++ /dev/null @@ -1,27 +0,0 @@ -package kotlin - -// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -// -// Generated from input file: src/kotlin/CollectionsJVM.kt -// - - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun Iterable.map(transform : (T) -> R) : List { - return mapTo(java.util.ArrayList(), transform) -} diff --git a/libraries/stdlib/src/kotlin/Collections.kt b/libraries/stdlib/src/kotlin/Collections.kt deleted file mode 100644 index 655a6158090..00000000000 --- a/libraries/stdlib/src/kotlin/Collections.kt +++ /dev/null @@ -1,20 +0,0 @@ -package kotlin - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Transforms each element of this collection with the given *transform* function and - * adds each return value to the given *results* collection - */ -public inline fun > Collection.mapTo(result: C, transform : (T) -> R) : C { - for (item in this) - result.add(transform(item)) - return result -} diff --git a/libraries/stdlib/src/kotlin/CollectionsJVM.kt b/libraries/stdlib/src/kotlin/CollectionsJVM.kt deleted file mode 100644 index 870243bfd81..00000000000 --- a/libraries/stdlib/src/kotlin/CollectionsJVM.kt +++ /dev/null @@ -1,19 +0,0 @@ -package kotlin - -import java.util.* - -// -// This file contains methods which are optimised for working on Collection / Array collections where the size -// could be used to implement a more optimal solution -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a new List containing the results of applying the given *transform* function to each element in this collection - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun Collection.map(transform : (T) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} diff --git a/libraries/stdlib/src/kotlin/Exceptions.kt b/libraries/stdlib/src/kotlin/Exceptions.kt new file mode 100644 index 00000000000..504f7a61f50 --- /dev/null +++ b/libraries/stdlib/src/kotlin/Exceptions.kt @@ -0,0 +1 @@ +public class EmptyIterableException(val it : Iterable<*>) : RuntimeException("$it is empty") diff --git a/libraries/stdlib/src/kotlin/IterablesJVM.kt b/libraries/stdlib/src/kotlin/IterablesJVM.kt deleted file mode 100644 index 825915b9001..00000000000 --- a/libraries/stdlib/src/kotlin/IterablesJVM.kt +++ /dev/null @@ -1,7 +0,0 @@ -package kotlin - -import java.util.* - -/** Copies all elements into a [[SortedSet]] */ -public inline fun Iterable.toSortedSet() : SortedSet = toCollection(TreeSet()) - diff --git a/libraries/stdlib/src/kotlin/IterablesLazy.kt b/libraries/stdlib/src/kotlin/IterablesLazy.kt deleted file mode 100644 index 2650d0c7221..00000000000 --- a/libraries/stdlib/src/kotlin/IterablesLazy.kt +++ /dev/null @@ -1,110 +0,0 @@ -package kotlin - -import java.util.ArrayList - -// -// This file contains methods which could have a lazy implementation for things like -// Iterator or java.util.Iterator -// -// See [[GenerateStandardLib.kt]] for more details -// - -/** - * Returns a list containing all elements which match the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt filter - */ -public inline fun Iterable.filter(predicate: (T) -> Boolean) : List = filterTo(ArrayList(), predicate) - -/** - * Returns a list containing all elements which do not match the given predicate - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNot - */ -public inline fun Iterable.filterNot(predicate: (T)-> Boolean) : List = filterNotTo(ArrayList(), predicate) - -/** - * Returns a list containing all the non-*null* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt filterNotNull - */ -public inline fun Iterable?.filterNotNull() : List = filterNotNullTo>(java.util.ArrayList()) - -/** - * Returns the result of transforming each element to one or more values which are concatenated together into a single collection - * - * @includeFunctionBody ../../test/CollectionTest.kt flatMap - */ -public inline fun Iterable.flatMap(transform: (T)-> Collection) : Collection = flatMapTo(ArrayList(), transform) - -/** - * Creates a copy of this collection as a [[List]] with the element added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plus - */ -public inline fun Iterable.plus(element: T): List { - val list = toCollection(ArrayList()) - list.add(element) - return list -} - - -/** - * Creates a copy of this collection as a [[List]] with all the elements added at the end - * - * @includeFunctionBody ../../test/CollectionTest.kt plusCollection - */ -public inline fun Iterable.plus(elements: Iterable): List { - val list = toCollection(ArrayList()) - list.addAll(elements.toCollection()) - return list -} - -/** - * Returns a list containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements - * - * @includeFunctionBody ../../test/CollectionTest.kt requireNoNulls - */ -public inline fun Iterable.requireNoNulls() : List { - val list = ArrayList() - for (element in this) { - if (element == null) { - throw IllegalArgumentException("null element found in $this") - } else { - list.add(element) - } - } - return list -} - -/** - * Returns a list containing everything but the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt drop - */ -public inline fun Iterable.drop(n: Int): List { - return dropWhile(countTo(n)) -} - -/** - * Returns a list containing the everything but the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt dropWhile - */ -public inline fun Iterable.dropWhile(predicate: (T) -> Boolean): List = dropWhileTo(ArrayList(), predicate) - -/** - * Returns a list containing the first *n* elements - * - * @includeFunctionBody ../../test/CollectionTest.kt take - */ -public inline fun Iterable.take(n: Int): List { - return takeWhile(countTo(n)) -} - -/** - * Returns a list containing the first elements that satisfy the given *predicate* - * - * @includeFunctionBody ../../test/CollectionTest.kt takeWhile - */ -public inline fun Iterable.takeWhile(predicate: (T) -> Boolean): List = takeWhileTo(ArrayList(), predicate) diff --git a/libraries/stdlib/src/kotlin/IterablesSpecial.kt b/libraries/stdlib/src/kotlin/IterablesSpecial.kt index ad0dedc5889..894719c3b7b 100644 --- a/libraries/stdlib/src/kotlin/IterablesSpecial.kt +++ b/libraries/stdlib/src/kotlin/IterablesSpecial.kt @@ -34,42 +34,14 @@ private fun countTo(n: Int): (T) -> Boolean { * * Will throw an exception if there are no elements */ -// TODO: Specify type of the exception public inline fun Iterable.first() : T { - if (this is AbstractList) { - return this.get(0) + if (this is List) { + return this.first() } return this.iterator().next() } -/** - * Get the last element in the collection. - * - * If base collection implements [[List]] interface, the combination of size() and get() - * methods will be used for getting last element. Otherwise, this method determines the - * last item by iterating through the all items. - * - * Will throw an exception if there are no elements. - * - * @includeFunctionBody ../../test/CollectionTest.kt last - */ -// TODO: Specify type of the exception -public fun Iterable.last() : T { - if (this is List) { - return this.get(this.size() - 1) - } - - val iterator = this.iterator() - var last : T = iterator.next() - - while (iterator.hasNext()) { - last = iterator.next() - } - - return last; -} - /** * Checks if collection contains given item. * @@ -91,20 +63,6 @@ public fun Iterable.containsItem(item : T) : Boolean { return false } -/** - * Convert collection of arbitrary elements to a List of tuples of the index and the element - * - * @includeFunctionBody ../../test/ListTest.kt withIndices - */ -public fun Iterable.withIndices(): List> { - val answer = ArrayList>() - var nextIndex = 0 - for (e in this) { - answer.add(Pair(nextIndex, e)) - nextIndex++ - } - return answer -} public inline fun > MutableIterable.sort() : List { val list = toCollection(ArrayList()) diff --git a/libraries/stdlib/src/kotlin/IterablesSpecialJVM.kt b/libraries/stdlib/src/kotlin/IterablesSpecialJVM.kt deleted file mode 100644 index dfaec4be239..00000000000 --- a/libraries/stdlib/src/kotlin/IterablesSpecialJVM.kt +++ /dev/null @@ -1,25 +0,0 @@ -package kotlin - -import java.util.AbstractList -import java.util.Comparator -import java.util.ArrayList - -// TODO this function is here as it breaks the JS compiler; lets move back to JLangIterablesSpecial when it works again :) - -/** - * Copies all elements into a [[List]] and sorts it by value of compare_function(element) - * - * E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple - * - * @includeFunctionBody ../../test/CollectionTest.kt sortBy - */ -public inline fun > Iterable.sortBy(f: (T) -> R): List { - val sortedList = toCollection(ArrayList()) - val sortBy: Comparator = comparator {(x: T, y: T) -> - val xr = f(x) - val yr = f(y) - xr.compareTo(yr) - } - java.util.Collections.sort(sortedList, sortBy) - return sortedList -} diff --git a/libraries/stdlib/src/kotlin/Iterators.kt b/libraries/stdlib/src/kotlin/Iterators.kt index c70657584b2..e501eeefb0d 100644 --- a/libraries/stdlib/src/kotlin/Iterators.kt +++ b/libraries/stdlib/src/kotlin/Iterators.kt @@ -5,19 +5,12 @@ import java.util.Collections /** * Returns an iterator which invokes the function to calculate the next value on each iteration until the function returns *null* - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt fibonacci */ -public inline fun iterate(nextFunction: () -> T?) : Iterator = FunctionIterator(nextFunction) +public inline fun iterate(nextFunction: () -> T?) : Iterator { + return FunctionIterator(nextFunction) +} -/** - * Returns an iterator over elements which match the given *predicate* - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange - */ -public inline fun Iterator.filter(predicate: (T) -> Boolean) : Iterator = FilterIterator(this, predicate) - -private class FilterIterator(val iterator : Iterator, val predicate: (T)-> Boolean) : AbstractIterator() { +class FilterIterator(val iterator : Iterator, val predicate: (T)-> Boolean) : AbstractIterator() { override protected fun computeNext(): Unit { while (iterator.hasNext()) { val next = iterator.next() @@ -30,13 +23,7 @@ private class FilterIterator(val iterator : Iterator, val predicate: (T)-> } } -/** Returns an iterator over elements which do not match the given *predicate* */ -public inline fun Iterator.filterNot(predicate: (T) -> Boolean) : Iterator = filter { !predicate(it) } - -/** Returns an iterator over non-*null* elements */ -public inline fun Iterator?.filterNotNull() : Iterator = FilterNotNullIterator(this) - -private class FilterNotNullIterator(val iterator : Iterator?) : AbstractIterator() { +class FilterNotNullIterator(val iterator : Iterator?) : AbstractIterator() { override protected fun computeNext(): Unit { if (iterator != null) { while (iterator.hasNext()) { @@ -51,14 +38,7 @@ private class FilterNotNullIterator(val iterator : Iterator?) : AbstractI } } -/** - * Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R* - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt mapAndTakeWhileExtractTheTransformedElements - */ -public inline fun Iterator.map(transform: (T) -> R): Iterator = MapIterator(this, transform) - -private class MapIterator(val iterator : Iterator, val transform: (T) -> R) : AbstractIterator() { +class MapIterator(val iterator : Iterator, val transform: (T) -> R) : AbstractIterator() { override protected fun computeNext() : Unit { if (iterator.hasNext()) { setNext((transform)(iterator.next())) @@ -68,14 +48,7 @@ private class MapIterator(val iterator : Iterator, val transform: (T) - } } -/** - * Returns an iterator over the concatenated results of transforming each element to one or more values - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt flatMapAndTakeExtractTheTransformedElements - */ -public inline fun Iterator.flatMap(transform: (T) -> Iterator): Iterator = FlatMapIterator(this, transform) - -private class FlatMapIterator(val iterator : Iterator, val transform: (T) -> Iterator) : AbstractIterator() { +class FlatMapIterator(val iterator : Iterator, val transform: (T) -> Iterator) : AbstractIterator() { var transformed: Iterator = iterate { null } override protected fun computeNext() : Unit { @@ -94,63 +67,7 @@ private class FlatMapIterator(val iterator : Iterator, val transform: ( } } -/** - * Creates an [[Iterator]] which iterates over this iterator then the given element at the end - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt plus - */ -public inline fun Iterator.plus(element: T): Iterator { - return CompositeIterator(this, SingleIterator(element)) -} - - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following iterator - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt plusCollection - */ -public inline fun Iterator.plus(iterator: Iterator): Iterator { - return CompositeIterator(this, iterator) -} - -/** - * Creates an [[Iterator]] which iterates over this iterator then the following collection - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt plusCollection - */ -public inline fun Iterator.plus(collection: Iterable): Iterator = plus(collection.iterator()) - -/** - * Returns an iterator containing all the non-*null* elements, lazily throwing an [[IllegalArgumentException]] - if there are any null elements - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt requireNoNulls - */ -public inline fun Iterator.requireNoNulls(): Iterator { - return map{ - if (it == null) throw IllegalArgumentException("null element in iterator $this") else it - } -} - - -/** - * Returns an iterator restricted to the first *n* elements - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements - */ -public inline fun Iterator.take(n: Int): Iterator { - var count = n - return takeWhile{ --count >= 0 } -} - -/** - * Returns an iterator restricted to the first elements that match the given *predicate* - * - * @includeFunctionBody ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange - */ -public inline fun Iterator.takeWhile(predicate: (T) -> Boolean): Iterator = TakeWhileIterator(this, predicate) - -private class TakeWhileIterator(val iterator: Iterator, val predicate: (T) -> Boolean) : AbstractIterator() { +class TakeWhileIterator(val iterator: Iterator, val predicate: (T) -> Boolean) : AbstractIterator() { override protected fun computeNext() : Unit { if (iterator.hasNext()) { val item = iterator.next() @@ -162,3 +79,71 @@ private class TakeWhileIterator(val iterator: Iterator, val predicate: (T) done() } } + +/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */ +class FunctionIterator(val nextFunction: () -> T?): AbstractIterator() { + + override protected fun computeNext(): Unit { + val next = (nextFunction)() + if (next == null) { + done() + } else { + setNext(next) + } + } +} + +/** An [[Iterator]] which iterates over a number of iterators in sequence */ +class CompositeIterator(vararg iterators: Iterator): AbstractIterator() { + + val iteratorsIter = iterators.iterator() + var currentIter: Iterator? = null + + override protected fun computeNext(): Unit { + while (true) { + if (currentIter == null) { + if (iteratorsIter.hasNext()) { + currentIter = iteratorsIter.next() + } else { + done() + return + } + } + val iter = currentIter + if (iter != null) { + if (iter.hasNext()) { + setNext(iter.next()) + return + } else { + currentIter = null + } + } + } + } +} + +/** A singleton [[Iterator]] which invokes once over a value */ +class SingleIterator(val value: T): AbstractIterator() { + var first = true + + override protected fun computeNext(): Unit { + if (first) { + first = false + setNext(value) + } else { + done() + } + } +} + +class IndexIterator(val iterator : Iterator): Iterator> { + private var index : Int = 0 + + override fun next(): Pair { + return Pair(index++, iterator.next()) + } + + override fun hasNext(): Boolean { + return iterator.hasNext() + } +} diff --git a/libraries/stdlib/src/kotlin/IteratorsJVM.kt b/libraries/stdlib/src/kotlin/IteratorsJVM.kt index 69abfb30a44..75033c0029d 100644 --- a/libraries/stdlib/src/kotlin/IteratorsJVM.kt +++ b/libraries/stdlib/src/kotlin/IteratorsJVM.kt @@ -1,7 +1,6 @@ package kotlin import kotlin.support.* -import java.util.Collections /** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */ public inline fun Iterator.filterIsInstance(klass: Class): Iterator = FilterIsIterator(this, klass) diff --git a/libraries/stdlib/src/kotlin/Lists.kt b/libraries/stdlib/src/kotlin/Lists.kt new file mode 100644 index 00000000000..e9db3773177 --- /dev/null +++ b/libraries/stdlib/src/kotlin/Lists.kt @@ -0,0 +1,69 @@ +package kotlin + +/** + * Get the first element in the list or throws [[EmptyIterableException]] if list is empty. + */ +public inline fun List.first() : T { + return if (size() > 0) get(0) else throw EmptyIterableException(this) +} + +/** + * Get the first element in the list or *null* if list is empty. + */ +public inline fun List.firstOrNull() : T? { + return if (size() > 0) get(0) else null +} + +/** + * Get the last element in the list or throws [[EmptyIterableException]] if list is empty. + */ +public inline fun List.last() : T { + val s = size() + return if (s > 0) get(s - 1) else throw EmptyIterableException(this) +} + +/** + * Get the last element in the list or *null* if list is empty. + */ +public inline fun List.lastOrNull() : T? { + val s = size() + return if (s > 0) get(s - 1) else null +} + +public inline fun List.forEachWithIndex(operation : (Int, T) -> Unit) { + for (index in indices) { + operation(index, get(index)) + } +} + +/** + * Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements + */ +public inline fun List.foldRight(initial: R, operation: (T, R) -> R) : R { + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} + +/** + * Applies binary operation to all elements of iterable, going from right to left. + * Similar to foldRight function, but uses the last element as initial value + */ +public inline fun List.reduceRight(operation: (T, T) -> T) : T { + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r +} diff --git a/libraries/stdlib/src/kotlin/Maps.kt b/libraries/stdlib/src/kotlin/Maps.kt index 4f00239c174..bda2cca48df 100644 --- a/libraries/stdlib/src/kotlin/Maps.kt +++ b/libraries/stdlib/src/kotlin/Maps.kt @@ -115,3 +115,21 @@ public inline fun Map.toMap(map: MutableMap): Map { map.putAll(this) return map } + +/** + * Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] + * + * @includeFunctionBody ../../test/CollectionTest.kt map + */ +public inline fun Map.map(transform: (Map.Entry) -> R) : List { + return mapTo(java.util.ArrayList(this.size), transform) +} + +/** + * Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] + * + * @includeFunctionBody ../../test/MapTest.kt mapValues + */ +public inline fun Map.mapValues(transform : (Map.Entry) -> R): Map { + return mapValuesTo(java.util.HashMap(this.size), transform) +} diff --git a/libraries/stdlib/src/kotlin/MapsJVM.kt b/libraries/stdlib/src/kotlin/MapsJVM.kt index 9bd11df7220..6c71c9f3277 100644 --- a/libraries/stdlib/src/kotlin/MapsJVM.kt +++ b/libraries/stdlib/src/kotlin/MapsJVM.kt @@ -42,22 +42,3 @@ public inline fun Map.toProperties(): Properties { return answer } -/** - * Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] - * - * @includeFunctionBody ../../test/CollectionTest.kt map - */ -public inline fun Map.map(transform: (Map.Entry) -> R) : List { - return mapTo(java.util.ArrayList(this.size), transform) -} - - -/** - * Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]] - * - * @includeFunctionBody ../../test/MapTest.kt mapValues - */ -public inline fun Map.mapValues(transform : (Map.Entry) -> R): Map { - return mapValuesTo(java.util.HashMap(this.size), transform) -} - diff --git a/libraries/stdlib/src/kotlin/OrderingJVM.kt b/libraries/stdlib/src/kotlin/OrderingJVM.kt index 71a09faf61a..9ef69e752c9 100644 --- a/libraries/stdlib/src/kotlin/OrderingJVM.kt +++ b/libraries/stdlib/src/kotlin/OrderingJVM.kt @@ -6,7 +6,7 @@ import java.util.Comparator * Helper method for implementing [[Comparable]] methods using a list of functions * to calculate the values to compare */ -inline fun compareBy(a: T?, b: T?, vararg functions: T.() -> Any?): Int { +inline fun compareBy(a: T?, b: T?, vararg functions: T.() -> Comparable<*>?): Int { require(functions.size > 0) if (a === b) return 0 if (a == null) return - 1 @@ -25,34 +25,23 @@ inline fun compareBy(a: T?, b: T?, vararg functions: T.() -> Any?): Int { * they are compared via [[#equals()]] and if they are not the same then * the [[#hashCode()]] method is used as the difference */ -public inline fun compareValues(a: T?, b: T?): Int { +public inline fun > compareValues(a: T?, b: T?): Int { if (a === b) return 0 if (a == null) return - 1 if (b == null) return 1 - if (a is Comparable<*>) { - return (a as Comparable).compareTo(b) - } - if (a == b) { - return 0 - } - if (a is Object && b is Object) { - val diff = a.hashCode() - b.hashCode() - return if (diff == 0) 1 else diff - } else { - // TODO??? - return 1 - } + + return (a as Comparable).compareTo(b) } /** * Creates a comparator using the sequence of functions used to calculate a value to compare on */ -public inline fun comparator(vararg val functions: T.() -> Any?): Comparator { +public inline fun comparator(vararg val functions: T.() -> Comparable<*>?): Comparator { return FunctionComparator(*functions) } -private class FunctionComparator(vararg val functions: T.() -> Any?): Comparator { +private class FunctionComparator(vararg val functions: T.() -> Comparable<*>?): Comparator { public override fun toString(): String { return "FunctionComparator${functions.toList()}" diff --git a/libraries/stdlib/src/kotlin/Standard.kt b/libraries/stdlib/src/kotlin/Standard.kt index 082398ccf19..b6c964466af 100644 --- a/libraries/stdlib/src/kotlin/Standard.kt +++ b/libraries/stdlib/src/kotlin/Standard.kt @@ -51,14 +51,3 @@ public inline fun A.to(that: B): Pair = Pair(this, that) Run function f */ public inline fun run(f: () -> T) : T = f() - -/** - * A helper method for creating a [[Runnable]] from a function - */ -public inline fun runnable(action: ()-> Unit): Runnable { - return object: Runnable { - public override fun run() { - action() - } - } -} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/StandardJVM.kt b/libraries/stdlib/src/kotlin/StandardJVM.kt index 97691e789dd..7e995f870b5 100644 --- a/libraries/stdlib/src/kotlin/StandardJVM.kt +++ b/libraries/stdlib/src/kotlin/StandardJVM.kt @@ -47,3 +47,14 @@ public inline fun callable(action: ()-> T): Callable { public override fun call() = action() } } + +/** + * A helper method for creating a [[Runnable]] from a function + */ +public inline fun runnable(action: ()-> Unit): Runnable { + return object: Runnable { + public override fun run() { + action() + } + } +} diff --git a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt b/libraries/stdlib/src/kotlin/support/AbstractIterator.kt index 83d8510954d..6835dc9790a 100644 --- a/libraries/stdlib/src/kotlin/support/AbstractIterator.kt +++ b/libraries/stdlib/src/kotlin/support/AbstractIterator.kt @@ -33,13 +33,13 @@ public abstract class AbstractIterator: Iterator { override fun next(): T { if (!hasNext()) throw NoSuchElementException() state = State.NotReady - return next!! + return next as T } /** Returns the next element in the iteration without advancing the iteration */ fun peek(): T { if (!hasNext()) throw NoSuchElementException() - return next!!; + return next as T; } private fun tryToComputeNext(): Boolean { @@ -76,59 +76,4 @@ public abstract class AbstractIterator: Iterator { } } -/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */ -class FunctionIterator(val nextFunction: () -> T?): AbstractIterator() { - - override protected fun computeNext(): Unit { - val next = (nextFunction)() - if (next == null) { - done() - } else { - setNext(next) - } - } -} - -/** An [[Iterator]] which iterates over a number of iterators in sequence */ -class CompositeIterator(vararg iterators: Iterator): AbstractIterator() { - - val iteratorsIter = iterators.iterator() - var currentIter: Iterator? = null - - override protected fun computeNext(): Unit { - while (true) { - if (currentIter == null) { - if (iteratorsIter.hasNext()) { - currentIter = iteratorsIter.next() - } else { - done() - return - } - } - val iter = currentIter - if (iter != null) { - if (iter.hasNext()) { - setNext(iter.next()) - return - } else { - currentIter = null - } - } - } - } -} - -/** A singleton [[Iterator]] which invokes once over a value */ -class SingleIterator(val value: T): AbstractIterator() { - var first = true - - override protected fun computeNext(): Unit { - if (first) { - first = false - setNext(value) - } else { - done() - } - } -} diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index d53ff076aff..89bb02a1e15 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -351,7 +351,6 @@ class CollectionTest { test fun lastException() { fails { arrayList().last() } - fails { hashSet().last() } } test fun subscript() { diff --git a/libraries/stdlib/test/ListTest.kt b/libraries/stdlib/test/ListTest.kt index 778b2da744b..383fa421771 100644 --- a/libraries/stdlib/test/ListTest.kt +++ b/libraries/stdlib/test/ListTest.kt @@ -33,15 +33,29 @@ class ListTest { assertEquals("bar", data.last) } - test fun withIndices() { + test fun forEachWithIndex() { val data = arrayList("foo", "bar") - val wis = data.withIndices() var index = 0 - for (withIndex in wis) { - assertEquals(withIndex.first, index) - assertEquals(withIndex.second, data[index]) + + data.forEachWithIndex { (i, d) -> + assertEquals(i, index) + assertEquals(d, data[index]) index++ } + + assertEquals(data.size(), index) + } + + test fun withIndices() { + val data = arrayList("foo", "bar") + var index = 0 + + for ((i, d) in data.withIndices()) { + assertEquals(i, index) + assertEquals(d, data[index]) + index++ + } + assertEquals(data.size(), index) } diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt b/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt deleted file mode 100644 index 069febdcda0..00000000000 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateStandardLib.kt +++ /dev/null @@ -1,154 +0,0 @@ -package org.jetbrains.kotlin.tools - -import java.io.* - -private val COMMON_AUTOGENERATED_WARNING: String = """// -// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt -// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib -//""" - -fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> String) { - generateFile(outFile, header, arrayList(inputFile), f) -} - -fun generateFile(outFile: File, header: String, inputFile: File, jvmFile: File, f: (String)-> String) { - generateFile(outFile, header, arrayList(inputFile, jvmFile), f) -} - -fun generateFile(outFile: File, header: String, inputFiles: List, f: (String)-> String) { - outFile.getParentFile()?.mkdirs() - val writer = PrintWriter(FileWriter(outFile)) - try { - writer.println(header) - - for (file in inputFiles) { - writer.println(""" -$COMMON_AUTOGENERATED_WARNING -// Generated from input file: $file -// -""") - - println("Parsing $file and writing $outFile") - val reader = FileReader(file).buffered() - try { - // TODO ideally we'd use a filterNot() here :) - val iter = reader.lineIterator() - while (iter.hasNext()) { - val line = iter.next() - - if (line.startsWith("package")) continue - - val xform = f(line) - writer.println(xform) - } - } finally { - reader.close() - reader.close() - } - } - } finally { - writer.close() - } -} - - -/** - * Generates methods in the standard library which are mostly identical - * but just using a different input kind. - * - * Kinda like mimicking source macros here, but this avoids the inefficiency of type conversions - * at runtime. - */ -fun main(args: Array) { - var srcDir = File("src/kotlin") - if (!srcDir.exists()) { - srcDir = File("stdlib/src/kotlin") - require(srcDir.exists(), "Could not find the src/kotlin directory!") - } - val outDir = File(srcDir, "../generated") - - val jsCoreDir = File(srcDir, "../../../../js/js.libraries/src/core") - require(jsCoreDir.exists()) - generateDomAPI(File(jsCoreDir, "dom.kt")) - generateDomEventsAPI(File(jsCoreDir, "domEvents.kt")) - - val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") - - // Iterables - Generic iterable stuff - generateFile(File(outDir, "ArraysFromIterables.kt"), "package kotlin\n", File(srcDir, "Iterables.kt")) { - it.replaceAll("Iterable Iterable", "${arrayName}Array"). - replaceAll(" Iterable", "${arrayName}Array"). - replaceAll("Iterable", "${arrayName}Array"). - replaceAll("Iterable", "${arrayName}Array")) - } - - generateFile(File(outDir, "${arrayName}ArraysFromIterables.kt"), "package kotlin\n", File(srcDir, "Iterables.kt")) { - replace(it) - } - generateFile(File(outDir, "${arrayName}ArraysFromIterablesJVM.kt"), "package kotlin\n", File(srcDir, "IterablesJVM.kt")) { - replace(it) - } - generateFile(File(outDir, "${arrayName}ArraysFromIterablesLazy.kt"), "package kotlin\n", File(srcDir, "IterablesLazy.kt")) { - replace(it) - } - } - - generateFile(File(outDir, "IteratorsFromIterables.kt"), "package kotlin", File(srcDir, "Iterables.kt")) { - it.replaceAll("Iterable Collection", "${arrayName}Array"). - replaceAll("Collection", "${arrayName}Array")) - } - generateFile(File(outDir, "${arrayName}ArraysFromCollectionsJVM.kt"), "package kotlin", File(srcDir, "CollectionsJVM.kt")) { - replaceGenerics(arrayName, it.replaceAll(" Collection", "${arrayName}Array"). - replaceAll("Collection", "${arrayName}Array")) - } - } - - generateFile(File(outDir, "StandardFromCollections.kt"), "package kotlin", File(srcDir, "Collections.kt")) { - it.replaceAll("Collection", " ").replaceAll(" ", " "). - replaceAll("", "<${arrayName}>").replaceAll("", "<${arrayName}>"). - replaceAll("\\(T\\)", "(${arrayName})").replaceAll("T\\?", "${arrayName}?"). - replaceAll("T,", "${arrayName},"). - replaceAll("T\\)", "${arrayName})"). - replaceAll(" T ", " ${arrayName} ") -} - diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateDownTos.kt similarity index 98% rename from libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt rename to libraries/tools/kotlin-stdlib-gen/src/generators/GenerateDownTos.kt index 8d8a9a99a12..e18b396ff5b 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateDownTos.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateDownTos.kt @@ -1,4 +1,4 @@ -package org.jetbrains.kotlin.tools +package generators import java.io.File import java.io.FileWriter @@ -57,4 +57,4 @@ $COMMON_AUTOGENERATED_WARNING } finally { writer.close() } -} \ No newline at end of file +} diff --git a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateJavaScriptStubs.kt similarity index 99% rename from libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt rename to libraries/tools/kotlin-stdlib-gen/src/generators/GenerateJavaScriptStubs.kt index 1ca0d7e5b37..e5e7da9056e 100644 --- a/libraries/stdlib/test/org/jetbrains/kotlin/tools/GenerateJavaScriptStubs.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateJavaScriptStubs.kt @@ -1,4 +1,4 @@ -package org.jetbrains.kotlin.tools +package generators import java.io.File import java.io.FileWriter diff --git a/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt new file mode 100644 index 00000000000..abc234b1b81 --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/generators/GenerateStandardLib.kt @@ -0,0 +1,137 @@ +package generators + +import java.io.* +import templates.* +import templates.Family.* + +private val COMMON_AUTOGENERATED_WARNING: String = """// +// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt +// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib +//""" + +fun generateFile(outFile: File, header: String, inputFile: File, f: (String)-> String) { + generateFile(outFile, header, arrayListOf(inputFile), f) +} + +fun generateFile(outFile: File, header: String, inputFile: File, jvmFile: File, f: (String)-> String) { + generateFile(outFile, header, arrayListOf(inputFile, jvmFile), f) +} + +fun generateFile(outFile: File, header: String, inputFiles: List, f: (String)-> String) { + outFile.getParentFile()?.mkdirs() + val writer = PrintWriter(FileWriter(outFile)) + try { + writer.println(header) + + for (file in inputFiles) { + writer.println(""" +$COMMON_AUTOGENERATED_WARNING +// Generated from input file: $file +// +""") + + println("Parsing $file and writing $outFile") + val reader = FileReader(file).buffered() + try { + // TODO ideally we'd use a filterNot() here :) + val iter = reader.lineIterator() + while (iter.hasNext()) { + val line = iter.next() + + if (line.startsWith("package")) continue + + val xform = f(line) + writer.println(xform) + } + } finally { + reader.close() + reader.close() + } + } + } finally { + writer.close() + } +} + + +/** + * Generates methods in the standard library which are mostly identical + * but just using a different input kind. + * + * Kinda like mimicking source macros here, but this avoids the inefficiency of type conversions + * at runtime. + */ +fun main(args: Array) { + require(args.size == 1, "Expecting Kotlin project home path as an argument") + + val outDir = File(File(args[0]), "libraries/stdlib/src/generated") + require(outDir.exists(), "${outDir.getPath()} doesn't exist!") + + val jsCoreDir = File(args[0], "js/js.libraries/src/core") + require(jsCoreDir.exists(), "${jsCoreDir.getPath()} doesn't exist!") + + generateDomAPI(File(jsCoreDir, "dom.kt")) + generateDomEventsAPI(File(jsCoreDir, "domEvents.kt")) + + val otherArrayNames = arrayListOf("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") + + iterators() + templates.writeTo(File(outDir, "Iterators.kt")) { + buildFor(Iterators, "") + } + + val iteratorSignatures = templates.map { it.signature.flat() }.toSet() + templates.clear() + + collections() + templates.writeTo(File(outDir, "Arrays.kt")) { + buildFor(Arrays, "") + } + + for (a in otherArrayNames) { + templates.writeTo(File(outDir, "${a}Arrays.kt")) { + buildFor(PrimitiveArrays, a) + } + } + + templates.writeTo(File(outDir, "Iterables.kt")) { + if (iteratorSignatures contains signature.flat()) "" else buildFor(Iterables, "") + } + + templates.writeTo(File(outDir, "IteratorsCommon.kt")) { + if (iteratorSignatures contains signature.flat()) "" else buildFor(Iterators, "") + } + + templates.writeTo(File(outDir, "Collections.kt")) { + if (iteratorSignatures contains signature.flat()) buildFor(Collections, "") else "" + } + + generateDownTos(File(outDir, "DownTo.kt"), "package kotlin") +} + +fun String.flat() = this.replaceAll(" ", "") + +fun List.writeTo(file : File, builder : GenericFunction.() -> String) { + println("Generating file: ${file.getPath()}") + val its = FileWriter(file) + + its.use { + its.append("package kotlin\n\n") + its.append("import java.util.*\n\n") + for (t in this) { + its.append(t.builder()) + } + } +} + +// Pretty hacky way to code generate; ideally we'd be using the AST and just changing the function prototypes +fun replaceGenerics(arrayName: String, it: String): String { + return it.replaceAll(" ", " ").replaceAll(" ", " "). + replaceAll("", "<${arrayName}>").replaceAll("", "<${arrayName}>"). + replaceAll("\\(T\\)", "(${arrayName})").replaceAll("T\\?", "${arrayName}?"). + replaceAll("T,", "${arrayName},"). + replaceAll("T\\)", "${arrayName})"). + replaceAll(" T ", " ${arrayName} ") +} + diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt new file mode 100644 index 00000000000..9a77b3a926e --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Collections.kt @@ -0,0 +1,584 @@ +package templates + +import templates.Family.* + +fun collections() { + f("all(predicate: (T) -> Boolean)") { + doc = "Returns *true* if all elements match the given *predicate*" + returns("Boolean") + + body { + """ + for (element in this) if (!predicate(element)) return false + return true + """ + } + } + + f("any(predicate: (T) -> Boolean)") { + doc = "Returns *true* if any elements match the given *predicate*" + returns("Boolean") + + body { + """ + for (element in this) if (predicate(element)) return true + return false + """ + } + } + + f("count(predicate: (T) -> Boolean)") { + doc = "Returns the number of elements which match the given *predicate*" + returns("Int") + body { + """ + var count = 0 + for (element in this) if (predicate(element)) count++ + return count + """ + } + } + + + f("find(predicate: (T) -> Boolean)") { + doc = "Returns the first element which matches the given *predicate* or *null* if none matched" + typeParam("T:Any") + returns("T?") + + body { + """ + for (element in this) if (predicate(element)) return element + return null + """ + } + } + + f("filter(predicate: (T) -> Boolean)") { + doc = "Returns a list containing all elements which match the given *predicate*" + returns("List") + + body { + "return filterTo(ArrayList(), predicate)" + } + + Iterators.returns("Iterator(this, predicate)" + } + } + + f("filterTo(result: C, predicate: (T) -> Boolean)") { + doc = "Filters all elements which match the given predicate into the given list" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (predicate(element)) result.add(element) + return result + """ + } + } + + f("filterNot(predicate: (T) -> Boolean)") { + doc = "Returns a list containing all elements which do not match the given *predicate*" + returns("List") + + body { + "return filterNotTo(ArrayList(), predicate)" + } + } + + f("filterNotTo(result: C, predicate: (T) -> Boolean)") { + doc = "Returns a list containing all elements which do not match the given *predicate*" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (!predicate(element)) result.add(element) + return result + """ + } + } + + f("filterNotNull()") { + absentFor(PrimitiveArrays) // Those are inherently non-nulls + doc = "Returns a list containing all the non-*null* elements" + typeParam("T:Any") + toNullableT = true + returns("List") + + body { + "return filterNotNullTo>(ArrayList())" + } + } + + f("filterNotNullTo(result: C)") { + absentFor(PrimitiveArrays) // Those are inherently non-nulls + doc = "Filters all non-*null* elements into the given list" + typeParam("T:Any") + toNullableT = true + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (element != null) result.add(element) + return result + """ + } + } + + f("partition(predicate: (T) -> Boolean)") { + doc = "Partitions this collection into a pair of collections" + returns("Pair, List>") + + body { + """ + val first = ArrayList() + val second = ArrayList() + for (element in this) { + if (predicate(element)) { + first.add(element) + } else { + second.add(element) + } + } + return Pair(first, second) + """ + } + } + + f("map(transform : (T) -> R)") { + doc = "Returns a new List containing the results of applying the given *transform* function to each element in this collection" + typeParam("R") + returns("List") + + body { + "return mapTo(ArrayList(), transform)" + } + } + + f("mapTo(result: C, transform : (T) -> R)") { + doc = """ + Transforms each element of this collection with the given *transform* function and + adds each return value to the given *results* collection + """ + + typeParam("R") + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (item in this) + result.add(transform(item)) + return result + """ + } + } + + f("flatMap(transform: (T)-> Iterable)") { + doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single list" + typeParam("R") + returns("List") + + body { + "return flatMapTo(ArrayList(), transform)" + } + } + + + f("flatMapTo(result: C, transform: (T) -> Iterable)") { + doc = "Returns the result of transforming each element to one or more values which are concatenated together into a single collection" + typeParam("R") + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) { + val list = transform(element) + for (r in list) result.add(r) + } + return result + """ + } + } + + f("forEach(operation: (T) -> Unit)") { + doc = "Performs the given *operation* on each element" + returns("Unit") + body { + """ + for (element in this) operation(element) + """ + } + } + + f("fold(initial: R, operation: (R, T) -> R)") { + doc = "Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements" + typeParam("R") + returns("R") + + body { + """ + var answer = initial + for (element in this) answer = operation(answer, element) + return answer + """ + } + } + + f("foldRight(initial: R, operation: (T, R) -> R)") { + doc = "Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements" + typeParam("R") + returns("R") + + absentFor(Iterators, Iterables, Collections) + + body { + """ + var r = initial + var index = size - 1 + + while (index >= 0) { + r = operation(get(index--), r) + } + + return r + """ + } + } + + f("reduce(operation: (T, T) -> T)") { + doc = """ + Applies binary operation to all elements of iterable, going from left to right. + Similar to fold function, but uses the first element as initial value + """ + returns("T") + + body { + """ + val iterator = this.iterator() + if (!iterator.hasNext()) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway + while (iterator.hasNext()) { + result = operation(result, iterator.next()) + } + + return result + """ + } + } + + f("reduceRight(operation: (T, T) -> T)") { + doc = """ + Applies binary operation to all elements of iterable, going from right to left. + Similar to foldRight function, but uses the last element as initial value + """ + returns("T") + absentFor(Iterators, Iterables, Collections) + + body { + """ + var index = size - 1 + if (index < 0) { + throw UnsupportedOperationException("Empty iterable can't be reduced") + } + + var r = get(index--) + while (index >= 0) { + r = operation(get(index--), r) + } + + return r + """ + } + } + + f("groupBy(toKey: (T) -> K)") { + doc = "Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by" + typeParam("K") + returns("Map>") + + body { "return groupByTo(HashMap>(), toKey)" } + } + + f("groupByTo(result: MutableMap>, toKey: (T) -> K)") { + typeParam("K") + returns("Map>") + body { + """ + for (element in this) { + val key = toKey(element) + val list = result.getOrPut(key) { ArrayList() } + list.add(element) + } + return result + """ + } + } + + f("drop(n: Int)") { + doc = "Returns a list containing everything but the first *n* elements" + returns("List") + body { + "return dropWhile(countTo(n))" + } + } + + f("dropWhile(predicate: (T) -> Boolean)") { + doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*" + returns("List") + body { + "return dropWhileTo(ArrayList(), predicate)" + } + } + + + f("dropWhileTo(result: L, predicate: (T) -> Boolean)") { + doc = "Returns a list containing the everything but the first elements that satisfy the given *predicate*" + typeParam("L: MutableList") + returns("L") + + body { + """ + var start = true + for (element in this) { + if (start && predicate(element)) { + // ignore + } else { + start = false + result.add(element) + } + } + return result + """ + } + } + + f("take(n: Int)") { + doc = "Returns a list containing the first *n* elements" + returns("List") + body { + "return takeWhile(countTo(n))" + } + } + + f("takeWhile(predicate: (T) -> Boolean)") { + doc = "Returns a list containing the first elements that satisfy the given *predicate*" + returns("List") + + body { + "return takeWhileTo(ArrayList(), predicate)" + } + } + + f("takeWhileTo(result: C, predicate: (T) -> Boolean)") { + doc = "Returns a list containing the first elements that satisfy the given *predicate*" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) if (predicate(element)) result.add(element) else break + return result + """ + } + } + + f("toCollection(result: C)") { + doc = "Copies all elements into the given collection" + typeParam("C: MutableCollection") + returns("C") + + body { + """ + for (element in this) result.add(element) + return result + """ + } + } + + f("reverse()") { + doc = "Reverses the order the elements into a list" + returns("List") + body { + """ + val list = toCollection(ArrayList()) + Collections.reverse(list) + return list + """ + } + } + + f("toLinkedList()") { + doc = "Copies all elements into a [[LinkedList]]" + returns("LinkedList") + + body { "return toCollection(LinkedList())" } + } + + f("toList()") { + doc = "Copies all elements into a [[List]]" + returns("List") + + body { "return toCollection(ArrayList())" } + } + + f("toSet()") { + doc = "Copies all elements into a [[Set]]" + returns("Set") + + body { "return toCollection(LinkedHashSet())" } + } + + f("toSortedSet()") { + doc = "Copies all elements into a [[SortedSet]]" + returns("SortedSet") + + body { "return toCollection(TreeSet())" } + } + + f("requireNoNulls()") { + absentFor(PrimitiveArrays) // Those are inherently non-nulls + doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" + typeParam("T:Any") + toNullableT = true + returns("SELF") + + body { + val THIS = "\$this" + """ + for (element in this) { + if (element == null) { + throw IllegalArgumentException("null element found in $THIS") + } + } + return this as SELF + """ + } + + } + + f("plus(element: T)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end" + returns("List") + + body { + """ + val answer = ArrayList() + toCollection(answer) + answer.add(element) + return answer + """ + } + + } + + f("plus(iterator: Iterator)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator" + returns("List") + + body { + """ + val answer = ArrayList() + toCollection(answer) + for (element in iterator) { + answer.add(element) + } + return answer + """ + } + } + + f("plus(collection: Iterable)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection" + returns("List") + + body { + "return plus(collection.iterator())" + } + } + + f("withIndices()") { + doc = "Returns an iterator of Pairs(index, data)" + returns("Iterator>") + + body { + "return IndexIterator(iterator())" + } + } + + f("sortBy(f: (T) -> R)") { + doc = """ + Copies all elements into a [[List]] and sorts it by value of compare_function(element) + E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple + """ + returns("List") + typeParam("R: Comparable") + + body { + """ + val sortedList = toCollection(ArrayList()) + val sortBy: Comparator = comparator {(x: T, y: T) -> + val xr = f(x) + val yr = f(y) + xr.compareTo(yr) + } + java.util.Collections.sort(sortedList, sortBy) + return sortedList + """ + } + } + + f("appendString(buffer: Appendable, separator: String = \", \", prefix: String =\"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { + doc = + """ + Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied + + If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + a special *truncated* separator (which defaults to "..." + """ + returns("Unit") + + body { + """ + buffer.append(prefix) + var count = 0 + for (element in this) { + if (++count > 1) buffer.append(separator) + if (limit < 0 || count <= limit) { + val text = if (element == null) "null" else element.toString() + buffer.append(text) + } else break + } + if (limit >= 0 && count > limit) buffer.append(truncated) + buffer.append(postfix) + """ + } + } + + f("makeString(separator: String = \", \", prefix: String = \"\", postfix: String = \"\", limit: Int = -1, truncated: String = \"...\")") { + doc = """ + Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. + + If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will + a special *truncated* separator (which defaults to "..." + """ + + returns("String") + body { + """ + val buffer = StringBuilder() + appendString(buffer, separator, prefix, postfix, limit, truncated) + return buffer.toString() + """ + } + } +} diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt new file mode 100644 index 00000000000..72126f76ff6 --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Engine.kt @@ -0,0 +1,165 @@ +package templates + +import java.util.ArrayList +import templates.Family.* +import java.util.HashSet +import java.util.HashMap +import java.io.StringReader +import java.util.StringTokenizer + +enum class Family { + Iterators + Iterables + Collections + Arrays + PrimitiveArrays +} + +class GenericFunction(val signature : String) { + var doc : String = "" + var toNullableT : Boolean = false + val isInline : Boolean = true; + val blockedFor = HashSet() + val bodies = HashMap() + val returnTypes = HashMap() + val typeParams = ArrayList() + + fun body(b : () -> String) { + for (f in Family.values()) { + if (bodies[f] == null) f.body(b) + } + } + + fun Family.body(b : () -> String) { + bodies[this] = b() + } + + fun returns(r : String) { + for (f in Family.values()) { + if (returnTypes[f] == null) f.returns(r) + } + } + + fun Family.returns(r:String) { + returnTypes[this] = r + } + + fun typeParam(t:String) { + typeParams.add(t) + } + + fun absentFor(vararg f : Family) { + blockedFor.addAll(f.toCollection()) + } + + private fun effectiveTypeParams(f : Family) : List { + val types = ArrayList(typeParams) + if (typeParams.find { it.startsWith("T") } == null) { + types.add(0, "T") + } + + if (f == PrimitiveArrays) { + types.remove(types.find { it.startsWith("T") }) + } + + return types + } + + + + fun buildFor(f: Family, arrName : String = "") : String { + if (blockedFor.contains(f)) return "" + + if (returnTypes[f] == null) throw RuntimeException("No return type specified for $signature") + val retType = returnTypes[f]!! + + val selftype = when (f) { + Iterables -> "Iterable" + Collections -> "Collection" + Iterators -> "Iterator" + Arrays -> "Array" + PrimitiveArrays -> "${arrName}Array" + } + + fun String.renderType() : String { + val t = StringTokenizer(this, " \t\n,:()<>?.", true) + val answer = StringBuilder() + + while (t.hasMoreTokens()) { + val token = t.nextToken() + answer.append(when (token) { + "SELF" -> selftype + "T" -> if (f == Family.PrimitiveArrays) arrName else token + else -> token + }) + } + + return answer.toString() + } + + val builder = StringBuilder() + if (doc != "") { + builder.append("/**\n") + StringReader(doc).forEachLine { + val line = it.trim() + if (!line.isEmpty()) { + builder.append(" * ").append(line).append("\n") + } + } + builder.append(" */\n") + } + + builder.append("public ") + if (isInline) builder.append("inline ") + + builder.append("fun ") + + val types = effectiveTypeParams(f) + + if (!types.isEmpty()) { + builder.append(types.makeString(separator = ", ", prefix = "<", postfix = "> ").renderType()) + } + + builder.append(( + if (toNullableT) { + selftype.replace("T>", "T?>") + } + else { + selftype + }).renderType()) + + builder.append(".${signature.renderType()} : ${retType.renderType()} {") + + val body = bodies[f]!!.trim("\n") + val prefix : Int = body.takeWhile { it == ' ' }.length + + StringReader(body).forEachLine { + builder.append('\n') + var count = prefix + builder.append(" ").append(it.dropWhile {count-- > 0 && it == ' '} .renderType()) + } + + return builder.toString().trimTrailingSpaces() + "\n}\n\n" + } +} + +fun String.trimTrailingSpaces() : String { + var answer = this; + while (answer.endsWith(' ') || answer.endsWith('\n')) answer = answer.substring(0, answer.length() - 1) + return answer +} + +val templates = ArrayList() + +fun f(signature : String, init : GenericFunction.() -> Unit) { + val gf = GenericFunction(signature) + gf.init() + templates.add(gf) +} + +fun main(args : Array) { + collections() + for (t in templates) { + print(t.buildFor(PrimitiveArrays, "Byte")) + } +} diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt new file mode 100644 index 00000000000..533f99f5888 --- /dev/null +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterators.kt @@ -0,0 +1,119 @@ +package templates + +fun iterators() { + f("filter(predicate: (T) -> Boolean)") { + doc = "Returns an iterator over elements which match the given *predicate*" + + returns("Iterator") + body { + "return FilterIterator(this, predicate)" + } + } + + f("filterNot(predicate: (T) -> Boolean)") { + doc = "Returns an iterator over elements which don't match the given *predicate*" + returns("Iterator") + + body { + "return filter {!predicate(it)}" + } + } + + f("filterNotNull()") { + doc = "Returns an iterator over non-*null* elements" + typeParam("T:Any") + toNullableT = true + returns("Iterator") + + body { + "return FilterNotNullIterator(this)" + } + } + + f("map(transform : (T) -> R)") { + doc = "Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*" + typeParam("R") + returns("Iterator") + + body { + "return MapIterator(this, transform)" + } + } + + f("flatMap(transform: (T) -> Iterator)") { + doc = "Returns an iterator over the concatenated results of transforming each element to one or more values" + typeParam("R") + returns("Iterator") + + body { + "return FlatMapIterator(this, transform)" + } + } + + f("requireNoNulls()") { + doc = "Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements" + typeParam("T:Any") + toNullableT = true + returns("Iterator") + + body { + val THIS = "\$this" + """ + return map{ + if (it == null) throw IllegalArgumentException("null element in iterator $THIS") else it + } + """ + } + } + + + f("take(n: Int)") { + doc = "Returns an iterator restricted to the first *n* elements" + returns("Iterator") + body { + """ + var count = n + return takeWhile{ --count >= 0 } + """ + } + } + + f("takeWhile(predicate: (T) -> Boolean)") { + doc = "Returns an iterator restricted to the first elements that match the given *predicate*" + returns("Iterator") + + body { + "return TakeWhileIterator(this, predicate)" + } + } + + // TODO: drop(n), dropWhile + + f("plus(element: T)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the given element at the end" + returns("Iterator") + + body { + "return CompositeIterator(this, SingleIterator(element))" + } + + } + + f("plus(iterator: Iterator)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the following iterator" + returns("Iterator") + + body { + "return CompositeIterator(this, iterator)" + } + } + + f("plus(collection: Iterable)") { + doc = "Creates an [[Iterator]] which iterates over this iterator then the following collection" + returns("Iterator") + + body { + "return plus(collection.iterator())" + } + } +}