From 7500808e11266eed085f647fcfc2871a85dbb5c5 Mon Sep 17 00:00:00 2001 From: James Strachan Date: Tue, 17 Apr 2012 14:05:32 +0100 Subject: [PATCH] #KT-1788 Fixed - added standard collection APIs to the various kinds of Array and removed the few old hand-crafted versions of these methods --- .../BooleanArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../BooleanArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../BooleanArraysFromJUtilCollections.kt | 30 +++ .../generated/ByteArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../ByteArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../ByteArraysFromJUtilCollections.kt | 30 +++ .../generated/CharArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../CharArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../CharArraysFromJUtilCollections.kt | 30 +++ .../DoubleArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../DoubleArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../DoubleArraysFromJUtilCollections.kt | 30 +++ .../FloatArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../FloatArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../FloatArraysFromJUtilCollections.kt | 30 +++ .../generated/IntArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../IntArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../IntArraysFromJUtilCollections.kt | 30 +++ .../generated/LongArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../LongArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../LongArraysFromJUtilCollections.kt | 30 +++ .../ShortArraysFromJLangIterables.kt | 223 ++++++++++++++++++ .../ShortArraysFromJLangIterablesLazy.kt | 103 ++++++++ .../ShortArraysFromJUtilCollections.kt | 30 +++ libraries/stdlib/src/kotlin/Arrays.kt | 10 - libraries/stdlib/src/kotlin/Strings.kt | 2 +- libraries/stdlib/test/CollectionTest.kt | 2 +- libraries/stdlib/test/GenerateStandardLib.kt | 36 ++- 28 files changed, 2885 insertions(+), 13 deletions(-) create mode 100644 libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt create mode 100644 libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt create mode 100644 libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt create mode 100644 libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt diff --git a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt new file mode 100644 index 00000000000..5a602b2773e --- /dev/null +++ b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? { + 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 > BooleanArray.filterTo(result: C, predicate: (Boolean) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > BooleanArray.filterNotTo(result: L, predicate: (Boolean) -> Boolean) : L { + 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 > BooleanArray?.filterNotNullTo(result: L) : L { + 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 BooleanArray.flatMapTo(result: Collection, transform: (Boolean) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Boolean, operation: (Boolean, Boolean) -> Boolean): Boolean { + 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 BooleanArray.foldRight(initial: Boolean, operation: (Boolean, Boolean) -> Boolean): Boolean = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Boolean) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > BooleanArray.takeWhileTo(result: L, predicate: (Boolean) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun BooleanArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > BooleanArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun BooleanArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..2b5046f565c --- /dev/null +++ b/libraries/stdlib/src/generated/BooleanArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun BooleanArray.take(n: Int): List { + fun countTo(n: Int): (Boolean) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/BooleanArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..c34dcc55fe4 --- /dev/null +++ b/libraries/stdlib/src/generated/BooleanArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 BooleanArray.map(transform : (Boolean) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/ByteArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt new file mode 100644 index 00000000000..3188a83b549 --- /dev/null +++ b/libraries/stdlib/src/generated/ByteArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 ByteArray.find(predicate: (Byte) -> Boolean) : Byte? { + 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 > ByteArray.filterTo(result: C, predicate: (Byte) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > ByteArray.filterNotTo(result: L, predicate: (Byte) -> Boolean) : L { + 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 > ByteArray?.filterNotNullTo(result: L) : L { + 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 ByteArray.flatMapTo(result: Collection, transform: (Byte) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Byte, operation: (Byte, Byte) -> Byte): Byte { + 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 ByteArray.foldRight(initial: Byte, operation: (Byte, Byte) -> Byte): Byte = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Byte) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > ByteArray.takeWhileTo(result: L, predicate: (Byte) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun ByteArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > ByteArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun ByteArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..d2bd87e3822 --- /dev/null +++ b/libraries/stdlib/src/generated/ByteArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun ByteArray.take(n: Int): List { + fun countTo(n: Int): (Byte) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/ByteArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..0165d0012e5 --- /dev/null +++ b/libraries/stdlib/src/generated/ByteArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 ByteArray.map(transform : (Byte) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/CharArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt new file mode 100644 index 00000000000..1206ff7c23b --- /dev/null +++ b/libraries/stdlib/src/generated/CharArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 CharArray.find(predicate: (Char) -> Boolean) : Char? { + 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 > CharArray.filterTo(result: C, predicate: (Char) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > CharArray.filterNotTo(result: L, predicate: (Char) -> Boolean) : L { + 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 > CharArray?.filterNotNullTo(result: L) : L { + 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 CharArray.flatMapTo(result: Collection, transform: (Char) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Char, operation: (Char, Char) -> Char): Char { + 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 CharArray.foldRight(initial: Char, operation: (Char, Char) -> Char): Char = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Char) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > CharArray.takeWhileTo(result: L, predicate: (Char) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun CharArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > CharArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun CharArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..896aa1df647 --- /dev/null +++ b/libraries/stdlib/src/generated/CharArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun CharArray.take(n: Int): List { + fun countTo(n: Int): (Char) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/CharArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..eb35cd0dfc3 --- /dev/null +++ b/libraries/stdlib/src/generated/CharArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 CharArray.map(transform : (Char) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/DoubleArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt new file mode 100644 index 00000000000..dd7ace59644 --- /dev/null +++ b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 DoubleArray.find(predicate: (Double) -> Boolean) : Double? { + 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 > DoubleArray.filterTo(result: C, predicate: (Double) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > DoubleArray.filterNotTo(result: L, predicate: (Double) -> Boolean) : L { + 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 > DoubleArray?.filterNotNullTo(result: L) : L { + 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 DoubleArray.flatMapTo(result: Collection, transform: (Double) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Double, operation: (Double, Double) -> Double): Double { + 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 DoubleArray.foldRight(initial: Double, operation: (Double, Double) -> Double): Double = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Double) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > DoubleArray.takeWhileTo(result: L, predicate: (Double) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun DoubleArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > DoubleArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun DoubleArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..e5f9bb5bae4 --- /dev/null +++ b/libraries/stdlib/src/generated/DoubleArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun DoubleArray.take(n: Int): List { + fun countTo(n: Int): (Double) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/DoubleArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..afa8ff2a3f4 --- /dev/null +++ b/libraries/stdlib/src/generated/DoubleArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 DoubleArray.map(transform : (Double) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/FloatArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt new file mode 100644 index 00000000000..faa3b25d640 --- /dev/null +++ b/libraries/stdlib/src/generated/FloatArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 FloatArray.find(predicate: (Float) -> Boolean) : Float? { + 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 > FloatArray.filterTo(result: C, predicate: (Float) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > FloatArray.filterNotTo(result: L, predicate: (Float) -> Boolean) : L { + 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 > FloatArray?.filterNotNullTo(result: L) : L { + 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 FloatArray.flatMapTo(result: Collection, transform: (Float) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Float, operation: (Float, Float) -> Float): Float { + 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 FloatArray.foldRight(initial: Float, operation: (Float, Float) -> Float): Float = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Float) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > FloatArray.takeWhileTo(result: L, predicate: (Float) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun FloatArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > FloatArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun FloatArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..cb97842e2c1 --- /dev/null +++ b/libraries/stdlib/src/generated/FloatArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun FloatArray.take(n: Int): List { + fun countTo(n: Int): (Float) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/FloatArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..fe579caa640 --- /dev/null +++ b/libraries/stdlib/src/generated/FloatArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 FloatArray.map(transform : (Float) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/IntArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt new file mode 100644 index 00000000000..c971a10989a --- /dev/null +++ b/libraries/stdlib/src/generated/IntArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 IntArray.find(predicate: (Int) -> Boolean) : Int? { + 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 > IntArray.filterTo(result: C, predicate: (Int) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > IntArray.filterNotTo(result: L, predicate: (Int) -> Boolean) : L { + 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 > IntArray?.filterNotNullTo(result: L) : L { + 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 IntArray.flatMapTo(result: Collection, transform: (Int) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Int, operation: (Int, Int) -> Int): Int { + 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 IntArray.foldRight(initial: Int, operation: (Int, Int) -> Int): Int = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Int) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > IntArray.takeWhileTo(result: L, predicate: (Int) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun IntArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > IntArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun IntArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..ab8e2b72057 --- /dev/null +++ b/libraries/stdlib/src/generated/IntArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun IntArray.take(n: Int): List { + fun countTo(n: Int): (Int) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/generated/IntArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..4ad9976eb1c --- /dev/null +++ b/libraries/stdlib/src/generated/IntArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 IntArray.map(transform : (Int) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/LongArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt new file mode 100644 index 00000000000..42f24d0fcf0 --- /dev/null +++ b/libraries/stdlib/src/generated/LongArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 LongArray.find(predicate: (Long) -> Boolean) : Long? { + 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 > LongArray.filterTo(result: C, predicate: (Long) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > LongArray.filterNotTo(result: L, predicate: (Long) -> Boolean) : L { + 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 > LongArray?.filterNotNullTo(result: L) : L { + 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 LongArray.flatMapTo(result: Collection, transform: (Long) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Long, operation: (Long, Long) -> Long): Long { + 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 LongArray.foldRight(initial: Long, operation: (Long, Long) -> Long): Long = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Long) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > LongArray.takeWhileTo(result: L, predicate: (Long) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun LongArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > LongArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun LongArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..e9b6ec4fae3 --- /dev/null +++ b/libraries/stdlib/src/generated/LongArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun LongArray.take(n: Int): List { + fun countTo(n: Int): (Long) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/LongArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..b652b767ad8 --- /dev/null +++ b/libraries/stdlib/src/generated/LongArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 LongArray.map(transform : (Long) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > 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/ShortArraysFromJLangIterables.kt b/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt new file mode 100644 index 00000000000..da763e49581 --- /dev/null +++ b/libraries/stdlib/src/generated/ShortArraysFromJLangIterables.kt @@ -0,0 +1,223 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterables.kt +package kotlin + +import kotlin.util.* + +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 + return true +} + +/** + * 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 + 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 ShortArray.find(predicate: (Short) -> Boolean) : Short? { + 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 > ShortArray.filterTo(result: C, predicate: (Short) -> 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* + * + * @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList + */ +public inline fun > ShortArray.filterNotTo(result: L, predicate: (Short) -> Boolean) : L { + 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 > ShortArray?.filterNotNullTo(result: L) : L { + 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 ShortArray.flatMapTo(result: Collection, transform: (Short) -> Collection) : Collection { + for (element in this) { + val list = transform(element) + if (list != null) { + for (r in list) result.add(r) + } + } + return result +} + +/** + * 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) + +/** + * 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: Short, operation: (Short, Short) -> Short): Short { + 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 ShortArray.foldRight(initial: Short, operation: (Short, Short) -> Short): Short = reverse().fold(initial, operation) + +/** + * 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) + +/** + * 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: Map>, toKey: (Short) -> 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 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().sure() +} + +/** Returns a list containing the first elements that satisfy the given *predicate* */ +public inline fun > ShortArray.takeWhileTo(result: L, predicate: (Short) -> Boolean) : L { + for (element in this) if (predicate(element)) result.add(element) else break + return result +} + +/** + * Reverses the order the elements into a list + * + * @includeFunctionBody ../../test/CollectionTest.kt reverse + */ +public inline fun ShortArray.reverse() : List { + val answer = LinkedList() + for (element in this) answer.addFirst(element) + return answer +} + +/** Copies all elements into the given collection */ +public inline fun > ShortArray.toCollection(result: C) : C { + for (element in this) result.add(element) + return result +} + +/** 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 [[SortedSet]] */ +public inline fun ShortArray.toSortedSet() : SortedSet = toCollection(TreeSet()) + +/** + 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) + return answer +} +*/ diff --git a/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt b/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt new file mode 100644 index 00000000000..28d3b41795a --- /dev/null +++ b/libraries/stdlib/src/generated/ShortArraysFromJLangIterablesLazy.kt @@ -0,0 +1,103 @@ +// NOTE this file is auto-generated from src/kotlin/JLangIterablesLazy.kt +package kotlin + +import kotlin.util.* + +import java.util.ArrayList +import java.util.Collection +import java.util.List + +// +// 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 the first *n* elements + * + * @includeFunctionBody ../../test/CollectionTest.kt take + */ +public inline fun ShortArray.take(n: Int): List { + fun countTo(n: Int): (Short) -> Boolean { + var count = 0 + return { ++count; count <= n } + } + 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/ShortArraysFromJUtilCollections.kt b/libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt new file mode 100644 index 00000000000..db0a787cb14 --- /dev/null +++ b/libraries/stdlib/src/generated/ShortArraysFromJUtilCollections.kt @@ -0,0 +1,30 @@ +// NOTE this file is auto-generated from src/kotlin/JUtilCollections.kt +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 ShortArray.map(transform : (Short) -> R) : java.util.List { + return mapTo(java.util.ArrayList(this.size), 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 > ShortArray.mapTo(result: C, transform : (Short) -> R) : C { + for (item in this) + result.add(transform(item)) + return result +} diff --git a/libraries/stdlib/src/kotlin/Arrays.kt b/libraries/stdlib/src/kotlin/Arrays.kt index b6b803217e7..74a8cd50b08 100644 --- a/libraries/stdlib/src/kotlin/Arrays.kt +++ b/libraries/stdlib/src/kotlin/Arrays.kt @@ -124,13 +124,3 @@ public inline fun Array.isEmpty() : Boolean = this.size == 0 /** Returns the array if its not null or else returns an empty array */ public inline fun Array?.orEmpty() : Array = if (this != null) this else array() - -public inline fun CharArray.toList(): List { - val list = ArrayList(this.size) - for (c in this) { - if (c != null) { - list.add(Character(c)) - } - } - return list -} \ No newline at end of file diff --git a/libraries/stdlib/src/kotlin/Strings.kt b/libraries/stdlib/src/kotlin/Strings.kt index 75079e69479..81945388444 100644 --- a/libraries/stdlib/src/kotlin/Strings.kt +++ b/libraries/stdlib/src/kotlin/Strings.kt @@ -54,7 +54,7 @@ public inline fun String.getBytes() : ByteArray = (this as java.lang.String).get public inline fun String.toCharArray() : CharArray = (this as java.lang.String).toCharArray().sure() -public inline fun String.toCharList(): List = toCharArray().toList() +public inline fun String.toCharList(): List = toCharArray().toList() public inline fun String.format(format : String, vararg args : Any?) : String = java.lang.String.format(format, args).sure() diff --git a/libraries/stdlib/test/CollectionTest.kt b/libraries/stdlib/test/CollectionTest.kt index 069973acbee..01eb8d11c62 100644 --- a/libraries/stdlib/test/CollectionTest.kt +++ b/libraries/stdlib/test/CollectionTest.kt @@ -162,7 +162,7 @@ class CollectionTest { test fun flatMap() { val data = arrayList("", "foo", "bar", "x", "") - val characters = data.flatMap{ it.toCharList() } + val characters = data.flatMap{ it.toCharList() } println("Got list of characters ${characters}") assertEquals(7, characters.size()) val text = characters.makeString("") diff --git a/libraries/stdlib/test/GenerateStandardLib.kt b/libraries/stdlib/test/GenerateStandardLib.kt index 5e9470c716d..edd79dba1ea 100644 --- a/libraries/stdlib/test/GenerateStandardLib.kt +++ b/libraries/stdlib/test/GenerateStandardLib.kt @@ -48,6 +48,8 @@ fun main(args: Array) { } val outDir = File(srcDir, "../generated") + val otherArrayNames = arrayList("Boolean", "Byte", "Char", "Short", "Int", "Long", "Float", "Double") + // JLangIterables - Generic iterable stuff generateFile(File(outDir, "ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) { it.replaceAll("java.lang.Iterable) { generateFile(File(outDir, "ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) { it.replaceAll("java.lang.Iterable java.lang.Iterable", "${arrayName}Array"). + replaceAll(" java.lang.Iterable", "${arrayName}Array"). + replaceAll("java.lang.Iterable", "${arrayName}Array"). + replaceAll("java.lang.Iterable", "${arrayName}Array")) + } + + generateFile(File(outDir, "${arrayName}ArraysFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) { + replace(it) + } + generateFile(File(outDir, "${arrayName}ArraysFromJLangIterablesLazy.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterablesLazy.kt")) { + replace(it) + } + } generateFile(File(outDir, "StandardFromJLangIterables.kt"), "package kotlin\n\nimport kotlin.util.*", File(srcDir, "JLangIterables.kt")) { it.replaceAll("java.lang.Iterable) { // JUtilCollections - methods returning a collection of the same input size (if its a collection) - generateFile(File(outDir, "ArraysFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) { it.replaceAll("java.util.Collection java.util.Collection", "${arrayName}Array"). + replaceAll("java.util.Collection", "${arrayName}Array")) + } + } generateFile(File(outDir, "JUtilIterablesFromJUtilCollections.kt"), "package kotlin", File(srcDir, "JUtilCollections.kt")) { it.replaceAll("java.util.Collection) { it.replaceAll("java.util.Collection", " ").replaceAll(" ", " "). + replaceAll("", "<${arrayName}>").replaceAll("", "<${arrayName}>"). + replaceAll("\\(T\\)", "(${arrayName})").replaceAll("T\\?", "${arrayName}?"). + replaceAll("T,", "${arrayName},"). + replaceAll("T\\)", "${arrayName})"). + replaceAll(" T ", " ${arrayName} ") +} +