Collections standard library is now generated from templates.

This commit is contained in:
Maxim Shafirov
2013-02-12 18:49:29 +04:00
parent 324a39d10b
commit 620aae28d0
78 changed files with 3782 additions and 3890 deletions
+405
View File
@@ -0,0 +1,405 @@
package kotlin
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*/
public inline fun <T> Array<out T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/**
* Returns *true* if any elements match the given *predicate*
*/
public inline fun <T> Array<out T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Returns the number of elements which match the given *predicate*
*/
public inline fun <T> Array<out T>.count(predicate: (T) -> Boolean) : Int {
var count = 0
for (element in this) if (predicate(element)) count++
return count
}
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*/
public inline fun <T:Any> Array<out T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <T> Array<out T>.filter(predicate: (T) -> Boolean) : List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <T, C: MutableCollection<in T>> Array<out T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T> Array<out T>.filterNot(predicate: (T) -> Boolean) : List<T> {
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Array<out T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Returns a list containing all the non-*null* elements
*/
public inline fun <T:Any> Array<out T?>.filterNotNull() : List<T> {
return filterNotNullTo<T, ArrayList<T>>(ArrayList<T>())
}
/**
* Filters all non-*null* elements into the given list
*/
public inline fun <T:Any, C: MutableCollection<in T>> Array<out T?>.filterNotNullTo(result: C) : C {
for (element in this) if (element != null) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun <T> Array<out T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
for (element in this) {
if (predicate(element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <T, R> Array<out T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), 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 <T, R, C: MutableCollection<in R>> Array<out T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*/
public inline fun <T, R> Array<out T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Array<out T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
}
return result
}
/**
* Performs the given *operation* on each element
*/
public inline fun <T> Array<out T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun <T, R> Array<out T>.fold(initial: R, operation: (R, T) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
}
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun <T, R> Array<out T>.foldRight(initial: R, operation: (T, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*/
public inline fun <T> Array<out T>.reduce(operation: (T, T) -> T) : T {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*/
public inline fun <T> Array<out T>.reduceRight(operation: (T, T) -> T) : T {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*/
public inline fun <T, K> Array<out T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), toKey)
}
public inline fun <T, K> Array<out T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return result
}
/**
* Returns a list containing everything but the first *n* elements
*/
public inline fun <T> Array<out T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Array<out T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Array<out T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
// ignore
} else {
start = false
result.add(element)
}
}
return result
}
/**
* Returns a list containing the first *n* elements
*/
public inline fun <T> Array<out T>.take(n: Int) : List<T> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T> Array<out T>.takeWhile(predicate: (T) -> Boolean) : List<T> {
return takeWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Array<out T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/**
* Copies all elements into the given collection
*/
public inline fun <T, C: MutableCollection<in T>> Array<out T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*/
public inline fun <T> Array<out T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
}
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun <T> Array<out T>.toLinkedList() : LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
* Copies all elements into a [[List]]
*/
public inline fun <T> Array<out T>.toList() : List<T> {
return toCollection(ArrayList<T>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun <T> Array<out T>.toSet() : Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun <T> Array<out T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public inline fun <T:Any> Array<out T?>.requireNoNulls() : Array<out T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
}
}
return this as Array<out T>
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Array<out T>.plus(element: T) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Array<out T>.plus(iterator: Iterator<T>) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun <T> Array<out T>.plus(collection: Iterable<T>) : List<T> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun <T> Array<out T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <T, R: Comparable<R>> Array<out T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun <T> Array<out T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun <T> Array<out T>.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()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Array<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <T, R> Array<T>.map(transform : (T) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,286 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> Array<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun <T> Array<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun <T> Array<T>.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 <T> Array<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0
for (element in this) if (predicate(element)) count++
return count
}
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun <T> Array<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Array<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
*/
public inline fun <T> Array<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
val second = ArrayList<T>()
for (element in this) {
if (predicate(element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Array<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Array<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <T, R> Array<T>.flatMapTo(result: MutableCollection<R>, transform: (T) -> Collection<R>) : Collection<R> {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
}
return result
}
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun <T> Array<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <T,R> Array<T>.fold(initial: R, operation: (R, T) -> R): R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
}
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <T,R> Array<T>.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun <T> Array<T>.reduce(operation: (T, T) -> T): T {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun <T> Array<T>.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) }
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <T, K> Array<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, MutableList<T>>(), 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 <T, K> Array<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList<T>() }
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 <T> Array<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <T, L: MutableList<in T>> Array<T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
// ignore
} else {
start = false
result.add(element)
}
}
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <T, C: MutableCollection<in T>> Array<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <T, C: MutableCollection<in T>> Array<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun <T> Array<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
Collections.reverse(list)
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <T> Array<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <T> Array<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <T> Array<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <T> Array<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun <in T> Array<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList()
answer.sort(transform)
return answer
}
*/
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun <T> Array<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<T> or java.util.Iterator<T>
//
// 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 <T> Array<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/**
* Returns a list containing all elements which do not match the given predicate
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
*/
public inline fun <T> Array<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun <T> Array<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
/**
* 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 <T, R> Array<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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 <T> Array<T>.plus(element: T): List<T> {
val list = toCollection(ArrayList<T>())
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 <T> Array<T>.plus(elements: Array<T>): List<T> {
val list = toCollection(ArrayList<T>())
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 <T> Array<T?>.requireNoNulls() : List<T> {
val list = ArrayList<T>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun <T> Array<T>.drop(n: Int): List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun <T> Array<T>.dropWhile(predicate: (T) -> Boolean): List<T> = dropWhileTo(ArrayList<T>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun <T> Array<T>.take(n: Int): List<T> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
*/
public inline fun <T> Array<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun BooleanArray.any(predicate: (Boolean) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun BooleanArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun BooleanArray.count(predicate: (Boolean) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun BooleanArray.find(predicate: (Boolean) -> Boolean) : Boolean?
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Boolean>> BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C {
public inline fun BooleanArray.filter(predicate: (Boolean) -> Boolean) : List<Boolean> {
return filterTo(ArrayList<Boolean>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Boolean>> BooleanArray.filterTo(result: C, predicate: (Boolean) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun BooleanArray.filterNot(predicate: (Boolean) -> Boolean) : List<Boolean> {
return filterNotTo(ArrayList<Boolean>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Boolean>> BooleanArray.filterNotTo(result: C, predicate: (Boolean) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair<List<Boolean>, List<Boolean>> {
val first = ArrayList<Boolean>()
@@ -103,33 +82,33 @@ public inline fun BooleanArray.partition(predicate: (Boolean) -> Boolean) : Pair
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Boolean>> BooleanArray.filterNotTo(result: C, predicate: (Boolean) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> BooleanArray.map(transform : (Boolean) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Boolean>> BooleanArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> BooleanArray.flatMapTo(result: MutableCollection<R>, transform: (Boolean) -> Collection<R>) : Collection<R> {
public inline fun <R> BooleanArray.flatMap(transform: (Boolean)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> BooleanArray.flatMapTo(result: C, transform: (Boolean) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> BooleanArray.flatMapTo(result: MutableCollection<R>, trans
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit) : Unit = for (element in this) operation(element)
public inline fun BooleanArray.forEach(operation: (Boolean) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> BooleanArray.fold(initial: R, operation: (R, Boolean) -> R): R {
public inline fun <R> BooleanArray.fold(initial: R, operation: (R, Boolean) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> BooleanArray.fold(initial: R, operation: (R, Boolean) -> R
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> BooleanArray.foldRight(initial: R, operation: (Boolean, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> BooleanArray.foldRight(initial: R, operation: (Boolean, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean): Boolean {
public inline fun BooleanArray.reduce(operation: (Boolean, Boolean) -> Boolean) : Boolean {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Boolean = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean): Boolean = reverse().reduce { x, y -> operation(y, x) }
public inline fun BooleanArray.reduceRight(operation: (Boolean, Boolean) -> Boolean) : Boolean {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> BooleanArray.groupBy(toKey: (Boolean) -> K) : Map<K, List<Boolean>> = groupByTo<K>(HashMap<K, MutableList<Boolean>>(), toKey)
public inline fun <K> BooleanArray.groupBy(toKey: (Boolean) -> K) : Map<K, List<Boolean>> {
return groupByTo(HashMap<K, MutableList<Boolean>>(), 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 <K> BooleanArray.groupByTo(result: MutableMap<K, MutableList<Boolean>>, toKey: (Boolean) -> K) : Map<K, MutableList<Boolean>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> BooleanArray.groupByTo(result: MutableMap<K, MutableList<B
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun BooleanArray.drop(n: Int) : List<Boolean> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Boolean>> BooleanArray.dropWhileTo(result: L, predicate: (Boolean) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean) : List<Boolean> {
return dropWhileTo(ArrayList<Boolean>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Boolean>> BooleanArray.dropWhileTo(result: L, predicate: (Boolean) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Boolean>> BooleanArray.dropWhileTo(result: L,
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Boolean>> BooleanArray.takeWhileTo(result: C, predicate: (Boolean) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun BooleanArray.take(n: Int) : List<Boolean> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun BooleanArray.takeWhile(predicate: (Boolean) -> Boolean) : List<Boolean> {
return takeWhileTo(ArrayList<Boolean>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Boolean>> BooleanArray.takeWhileTo(result: C, predicate: (Boolean) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Boolean>> BooleanArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Boolean>> BooleanArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun BooleanArray.reverse() : List<Boolean> {
val list = toCollection(ArrayList<Boolean>())
@@ -264,23 +267,112 @@ public inline fun BooleanArray.reverse() : List<Boolean> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun BooleanArray.toLinkedList() : LinkedList<Boolean> = toCollection(LinkedList<Boolean>())
/** Copies all elements into a [[List]] */
public inline fun BooleanArray.toList() : List<Boolean> = toCollection(ArrayList<Boolean>())
/** Copies all elements into a [[List] */
public inline fun BooleanArray.toCollection() : Collection<Boolean> = toCollection(ArrayList<Boolean>())
/** Copies all elements into a [[Set]] */
public inline fun BooleanArray.toSet() : Set<Boolean> = toCollection(HashSet<Boolean>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun BooleanArray.toLinkedList() : LinkedList<Boolean> {
return toCollection(LinkedList<Boolean>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun BooleanArray.toSortedList(transform: fun(Boolean) : java.lang.Comparable<*>) : List<Boolean> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun BooleanArray.toList() : List<Boolean> {
return toCollection(ArrayList<Boolean>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun BooleanArray.toSet() : Set<Boolean> {
return toCollection(LinkedHashSet<Boolean>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun BooleanArray.toSortedSet() : SortedSet<Boolean> {
return toCollection(TreeSet<Boolean>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun BooleanArray.plus(element: Boolean) : List<Boolean> {
val answer = ArrayList<Boolean>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun BooleanArray.plus(iterator: Iterator<Boolean>) : List<Boolean> {
val answer = ArrayList<Boolean>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun BooleanArray.plus(collection: Iterable<Boolean>) : List<Boolean> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun BooleanArray.withIndices() : Iterator<Pair<Int, Boolean>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> BooleanArray.sortBy(f: (Boolean) -> R) : List<Boolean> {
val sortedList = toCollection(ArrayList<Boolean>())
val sortBy: Comparator<Boolean> = comparator<Boolean> {(x: Boolean, y: Boolean) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun BooleanArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun BooleanArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> BooleanArray.mapTo(result: C, transform : (Boolean) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> BooleanArray.map(transform : (Boolean) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun BooleanArray.toSortedSet() : SortedSet<Boolean> = toCollection(TreeSet<Boolean>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Boolean> or java.util.Iterator<Boolean>
//
// 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<Boolean> = filterTo(ArrayList<Boolean>(), 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<Boolean> = filterNotTo(ArrayList<Boolean>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun BooleanArray?.filterNotNull() : List<Boolean> = filterNotNullTo<ArrayList<Boolean>>(java.util.ArrayList<Boolean>())
/**
* 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 <R> BooleanArray.flatMap(transform: (Boolean)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Boolean> {
val list = toCollection(ArrayList<Boolean>())
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<Boolean> {
val list = toCollection(ArrayList<Boolean>())
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<Boolean> {
val list = ArrayList<Boolean>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun BooleanArray.drop(n: Int): List<Boolean> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun BooleanArray.dropWhile(predicate: (Boolean) -> Boolean): List<Boolean> = dropWhileTo(ArrayList<Boolean>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun BooleanArray.take(n: Int): List<Boolean> {
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<Boolean> = takeWhileTo(ArrayList<Boolean>(), predicate)
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun ByteArray.all(predicate: (Byte) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun ByteArray.all(predicate: (Byte) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun ByteArray.any(predicate: (Byte) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun ByteArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun ByteArray.count(predicate: (Byte) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun ByteArray.count(predicate: (Byte) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun ByteArray.find(predicate: (Byte) -> Boolean) : Byte? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Byte>> ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C {
public inline fun ByteArray.filter(predicate: (Byte) -> Boolean) : List<Byte> {
return filterTo(ArrayList<Byte>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Byte>> ByteArray.filterTo(result: C, predicate: (Byte) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun ByteArray.filterNot(predicate: (Byte) -> Boolean) : List<Byte> {
return filterNotTo(ArrayList<Byte>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Byte>> ByteArray.filterNotTo(result: C, predicate: (Byte) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair<List<Byte>, List<Byte>> {
val first = ArrayList<Byte>()
@@ -103,33 +82,33 @@ public inline fun ByteArray.partition(predicate: (Byte) -> Boolean) : Pair<List<
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Byte>> ByteArray.filterNotTo(result: C, predicate: (Byte) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> ByteArray.map(transform : (Byte) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Byte>> ByteArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> ByteArray.mapTo(result: C, transform : (Byte) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> ByteArray.flatMapTo(result: MutableCollection<R>, transform: (Byte) -> Collection<R>) : Collection<R> {
public inline fun <R> ByteArray.flatMap(transform: (Byte)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> ByteArray.flatMapTo(result: C, transform: (Byte) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> ByteArray.flatMapTo(result: MutableCollection<R>, transfor
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun ByteArray.forEach(operation: (Byte) -> Unit) : Unit = for (element in this) operation(element)
public inline fun ByteArray.forEach(operation: (Byte) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> ByteArray.fold(initial: R, operation: (R, Byte) -> R): R {
public inline fun <R> ByteArray.fold(initial: R, operation: (R, Byte) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> ByteArray.fold(initial: R, operation: (R, Byte) -> R): R {
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> ByteArray.foldRight(initial: R, operation: (Byte, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> ByteArray.foldRight(initial: R, operation: (Byte, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte): Byte {
public inline fun ByteArray.reduce(operation: (Byte, Byte) -> Byte) : Byte {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Byte = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte): Byte = reverse().reduce { x, y -> operation(y, x) }
public inline fun ByteArray.reduceRight(operation: (Byte, Byte) -> Byte) : Byte {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> ByteArray.groupBy(toKey: (Byte) -> K) : Map<K, List<Byte>> = groupByTo<K>(HashMap<K, MutableList<Byte>>(), toKey)
public inline fun <K> ByteArray.groupBy(toKey: (Byte) -> K) : Map<K, List<Byte>> {
return groupByTo(HashMap<K, MutableList<Byte>>(), 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 <K> ByteArray.groupByTo(result: MutableMap<K, MutableList<Byte>>, toKey: (Byte) -> K) : Map<K, MutableList<Byte>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> ByteArray.groupByTo(result: MutableMap<K, MutableList<Byte
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun ByteArray.drop(n: Int) : List<Byte> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Byte>> ByteArray.dropWhileTo(result: L, predicate: (Byte) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean) : List<Byte> {
return dropWhileTo(ArrayList<Byte>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Byte>> ByteArray.dropWhileTo(result: L, predicate: (Byte) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Byte>> ByteArray.dropWhileTo(result: L, predic
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Byte>> ByteArray.takeWhileTo(result: C, predicate: (Byte) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun ByteArray.take(n: Int) : List<Byte> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun ByteArray.takeWhile(predicate: (Byte) -> Boolean) : List<Byte> {
return takeWhileTo(ArrayList<Byte>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Byte>> ByteArray.takeWhileTo(result: C, predicate: (Byte) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Byte>> ByteArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Byte>> ByteArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun ByteArray.reverse() : List<Byte> {
val list = toCollection(ArrayList<Byte>())
@@ -264,23 +267,112 @@ public inline fun ByteArray.reverse() : List<Byte> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun ByteArray.toLinkedList() : LinkedList<Byte> = toCollection(LinkedList<Byte>())
/** Copies all elements into a [[List]] */
public inline fun ByteArray.toList() : List<Byte> = toCollection(ArrayList<Byte>())
/** Copies all elements into a [[List] */
public inline fun ByteArray.toCollection() : Collection<Byte> = toCollection(ArrayList<Byte>())
/** Copies all elements into a [[Set]] */
public inline fun ByteArray.toSet() : Set<Byte> = toCollection(HashSet<Byte>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun ByteArray.toLinkedList() : LinkedList<Byte> {
return toCollection(LinkedList<Byte>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun ByteArray.toSortedList(transform: fun(Byte) : java.lang.Comparable<*>) : List<Byte> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun ByteArray.toList() : List<Byte> {
return toCollection(ArrayList<Byte>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun ByteArray.toSet() : Set<Byte> {
return toCollection(LinkedHashSet<Byte>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun ByteArray.toSortedSet() : SortedSet<Byte> {
return toCollection(TreeSet<Byte>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun ByteArray.plus(element: Byte) : List<Byte> {
val answer = ArrayList<Byte>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun ByteArray.plus(iterator: Iterator<Byte>) : List<Byte> {
val answer = ArrayList<Byte>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun ByteArray.plus(collection: Iterable<Byte>) : List<Byte> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun ByteArray.withIndices() : Iterator<Pair<Int, Byte>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> ByteArray.sortBy(f: (Byte) -> R) : List<Byte> {
val sortedList = toCollection(ArrayList<Byte>())
val sortBy: Comparator<Byte> = comparator<Byte> {(x: Byte, y: Byte) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun ByteArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun ByteArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> ByteArray.mapTo(result: C, transform : (Byte) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> ByteArray.map(transform : (Byte) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun ByteArray.toSortedSet() : SortedSet<Byte> = toCollection(TreeSet<Byte>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Byte> or java.util.Iterator<Byte>
//
// 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<Byte> = filterTo(ArrayList<Byte>(), 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<Byte> = filterNotTo(ArrayList<Byte>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun ByteArray?.filterNotNull() : List<Byte> = filterNotNullTo<ArrayList<Byte>>(java.util.ArrayList<Byte>())
/**
* 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 <R> ByteArray.flatMap(transform: (Byte)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Byte> {
val list = toCollection(ArrayList<Byte>())
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<Byte> {
val list = toCollection(ArrayList<Byte>())
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<Byte> {
val list = ArrayList<Byte>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun ByteArray.drop(n: Int): List<Byte> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun ByteArray.dropWhile(predicate: (Byte) -> Boolean): List<Byte> = dropWhileTo(ArrayList<Byte>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun ByteArray.take(n: Int): List<Byte> {
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<Byte> = takeWhileTo(ArrayList<Byte>(), predicate)
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun CharArray.all(predicate: (Char) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun CharArray.all(predicate: (Char) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun CharArray.any(predicate: (Char) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun CharArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun CharArray.count(predicate: (Char) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun CharArray.count(predicate: (Char) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun CharArray.find(predicate: (Char) -> Boolean) : Char? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Char>> CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C {
public inline fun CharArray.filter(predicate: (Char) -> Boolean) : List<Char> {
return filterTo(ArrayList<Char>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Char>> CharArray.filterTo(result: C, predicate: (Char) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun CharArray.filterNot(predicate: (Char) -> Boolean) : List<Char> {
return filterNotTo(ArrayList<Char>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Char>> CharArray.filterNotTo(result: C, predicate: (Char) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair<List<Char>, List<Char>> {
val first = ArrayList<Char>()
@@ -103,33 +82,33 @@ public inline fun CharArray.partition(predicate: (Char) -> Boolean) : Pair<List<
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Char>> CharArray.filterNotTo(result: C, predicate: (Char) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> CharArray.map(transform : (Char) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Char>> CharArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> CharArray.mapTo(result: C, transform : (Char) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> CharArray.flatMapTo(result: MutableCollection<R>, transform: (Char) -> Collection<R>) : Collection<R> {
public inline fun <R> CharArray.flatMap(transform: (Char)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> CharArray.flatMapTo(result: C, transform: (Char) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> CharArray.flatMapTo(result: MutableCollection<R>, transfor
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun CharArray.forEach(operation: (Char) -> Unit) : Unit = for (element in this) operation(element)
public inline fun CharArray.forEach(operation: (Char) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> CharArray.fold(initial: R, operation: (R, Char) -> R): R {
public inline fun <R> CharArray.fold(initial: R, operation: (R, Char) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> CharArray.fold(initial: R, operation: (R, Char) -> R): R {
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> CharArray.foldRight(initial: R, operation: (Char, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> CharArray.foldRight(initial: R, operation: (Char, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun CharArray.reduce(operation: (Char, Char) -> Char): Char {
public inline fun CharArray.reduce(operation: (Char, Char) -> Char) : Char {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Char = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char): Char = reverse().reduce { x, y -> operation(y, x) }
public inline fun CharArray.reduceRight(operation: (Char, Char) -> Char) : Char {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> CharArray.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> = groupByTo<K>(HashMap<K, MutableList<Char>>(), toKey)
public inline fun <K> CharArray.groupBy(toKey: (Char) -> K) : Map<K, List<Char>> {
return groupByTo(HashMap<K, MutableList<Char>>(), 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 <K> CharArray.groupByTo(result: MutableMap<K, MutableList<Char>>, toKey: (Char) -> K) : Map<K, MutableList<Char>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> CharArray.groupByTo(result: MutableMap<K, MutableList<Char
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun CharArray.drop(n: Int) : List<Char> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Char>> CharArray.dropWhileTo(result: L, predicate: (Char) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean) : List<Char> {
return dropWhileTo(ArrayList<Char>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Char>> CharArray.dropWhileTo(result: L, predicate: (Char) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Char>> CharArray.dropWhileTo(result: L, predic
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Char>> CharArray.takeWhileTo(result: C, predicate: (Char) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun CharArray.take(n: Int) : List<Char> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun CharArray.takeWhile(predicate: (Char) -> Boolean) : List<Char> {
return takeWhileTo(ArrayList<Char>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Char>> CharArray.takeWhileTo(result: C, predicate: (Char) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Char>> CharArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Char>> CharArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun CharArray.reverse() : List<Char> {
val list = toCollection(ArrayList<Char>())
@@ -264,23 +267,112 @@ public inline fun CharArray.reverse() : List<Char> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun CharArray.toLinkedList() : LinkedList<Char> = toCollection(LinkedList<Char>())
/** Copies all elements into a [[List]] */
public inline fun CharArray.toList() : List<Char> = toCollection(ArrayList<Char>())
/** Copies all elements into a [[List] */
public inline fun CharArray.toCollection() : Collection<Char> = toCollection(ArrayList<Char>())
/** Copies all elements into a [[Set]] */
public inline fun CharArray.toSet() : Set<Char> = toCollection(HashSet<Char>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun CharArray.toLinkedList() : LinkedList<Char> {
return toCollection(LinkedList<Char>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun CharArray.toSortedList(transform: fun(Char) : java.lang.Comparable<*>) : List<Char> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun CharArray.toList() : List<Char> {
return toCollection(ArrayList<Char>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun CharArray.toSet() : Set<Char> {
return toCollection(LinkedHashSet<Char>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun CharArray.toSortedSet() : SortedSet<Char> {
return toCollection(TreeSet<Char>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun CharArray.plus(element: Char) : List<Char> {
val answer = ArrayList<Char>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun CharArray.plus(iterator: Iterator<Char>) : List<Char> {
val answer = ArrayList<Char>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun CharArray.plus(collection: Iterable<Char>) : List<Char> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun CharArray.withIndices() : Iterator<Pair<Int, Char>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> CharArray.sortBy(f: (Char) -> R) : List<Char> {
val sortedList = toCollection(ArrayList<Char>())
val sortBy: Comparator<Char> = comparator<Char> {(x: Char, y: Char) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun CharArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun CharArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> CharArray.mapTo(result: C, transform : (Char) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> CharArray.map(transform : (Char) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun CharArray.toSortedSet() : SortedSet<Char> = toCollection(TreeSet<Char>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Char> or java.util.Iterator<Char>
//
// 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<Char> = filterTo(ArrayList<Char>(), 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<Char> = filterNotTo(ArrayList<Char>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun CharArray?.filterNotNull() : List<Char> = filterNotNullTo<ArrayList<Char>>(java.util.ArrayList<Char>())
/**
* 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 <R> CharArray.flatMap(transform: (Char)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Char> {
val list = toCollection(ArrayList<Char>())
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<Char> {
val list = toCollection(ArrayList<Char>())
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<Char> {
val list = ArrayList<Char>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun CharArray.drop(n: Int): List<Char> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun CharArray.dropWhile(predicate: (Char) -> Boolean): List<Char> = dropWhileTo(ArrayList<Char>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun CharArray.take(n: Int): List<Char> {
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<Char> = takeWhileTo(ArrayList<Char>(), predicate)
@@ -0,0 +1,87 @@
package kotlin
import java.util.*
/**
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <T> Collection<T>.filter(predicate: (T) -> Boolean) : List<T> {
return filterTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T> Collection<T>.filterNot(predicate: (T) -> Boolean) : List<T> {
return filterNotTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing all the non-*null* elements
*/
public inline fun <T:Any> Collection<T?>.filterNotNull() : List<T> {
return filterNotNullTo<T, ArrayList<T>>(ArrayList<T>())
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <T, R> Collection<T>.map(transform : (T) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Returns a list containing the first *n* elements
*/
public inline fun <T> Collection<T>.take(n: Int) : List<T> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T> Collection<T>.takeWhile(predicate: (T) -> Boolean) : List<T> {
return takeWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public inline fun <T:Any> Collection<T?>.requireNoNulls() : Collection<T> {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
}
}
return this as Collection<T>
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Collection<T>.plus(element: T) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Collection<T>.plus(iterator: Iterator<T>) : List<T> {
val answer = ArrayList<T>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun <T> Collection<T>.plus(collection: Iterable<T>) : List<T> {
return plus(collection.iterator())
}
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun DoubleArray.all(predicate: (Double) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun DoubleArray.all(predicate: (Double) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun DoubleArray.any(predicate: (Double) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun DoubleArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun DoubleArray.count(predicate: (Double) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun DoubleArray.count(predicate: (Double) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun DoubleArray.find(predicate: (Double) -> Boolean) : Double? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Double>> DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C {
public inline fun DoubleArray.filter(predicate: (Double) -> Boolean) : List<Double> {
return filterTo(ArrayList<Double>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Double>> DoubleArray.filterTo(result: C, predicate: (Double) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun DoubleArray.filterNot(predicate: (Double) -> Boolean) : List<Double> {
return filterNotTo(ArrayList<Double>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Double>> DoubleArray.filterNotTo(result: C, predicate: (Double) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair<List<Double>, List<Double>> {
val first = ArrayList<Double>()
@@ -103,33 +82,33 @@ public inline fun DoubleArray.partition(predicate: (Double) -> Boolean) : Pair<L
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Double>> DoubleArray.filterNotTo(result: C, predicate: (Double) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> DoubleArray.map(transform : (Double) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Double>> DoubleArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> DoubleArray.mapTo(result: C, transform : (Double) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> DoubleArray.flatMapTo(result: MutableCollection<R>, transform: (Double) -> Collection<R>) : Collection<R> {
public inline fun <R> DoubleArray.flatMap(transform: (Double)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> DoubleArray.flatMapTo(result: C, transform: (Double) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> DoubleArray.flatMapTo(result: MutableCollection<R>, transf
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun DoubleArray.forEach(operation: (Double) -> Unit) : Unit = for (element in this) operation(element)
public inline fun DoubleArray.forEach(operation: (Double) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> DoubleArray.fold(initial: R, operation: (R, Double) -> R): R {
public inline fun <R> DoubleArray.fold(initial: R, operation: (R, Double) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> DoubleArray.fold(initial: R, operation: (R, Double) -> R):
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> DoubleArray.foldRight(initial: R, operation: (Double, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> DoubleArray.foldRight(initial: R, operation: (Double, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double): Double {
public inline fun DoubleArray.reduce(operation: (Double, Double) -> Double) : Double {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Double = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double): Double = reverse().reduce { x, y -> operation(y, x) }
public inline fun DoubleArray.reduceRight(operation: (Double, Double) -> Double) : Double {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> DoubleArray.groupBy(toKey: (Double) -> K) : Map<K, List<Double>> = groupByTo<K>(HashMap<K, MutableList<Double>>(), toKey)
public inline fun <K> DoubleArray.groupBy(toKey: (Double) -> K) : Map<K, List<Double>> {
return groupByTo(HashMap<K, MutableList<Double>>(), 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 <K> DoubleArray.groupByTo(result: MutableMap<K, MutableList<Double>>, toKey: (Double) -> K) : Map<K, MutableList<Double>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> DoubleArray.groupByTo(result: MutableMap<K, MutableList<Do
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun DoubleArray.drop(n: Int) : List<Double> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Double>> DoubleArray.dropWhileTo(result: L, predicate: (Double) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean) : List<Double> {
return dropWhileTo(ArrayList<Double>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Double>> DoubleArray.dropWhileTo(result: L, predicate: (Double) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Double>> DoubleArray.dropWhileTo(result: L, pr
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Double>> DoubleArray.takeWhileTo(result: C, predicate: (Double) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun DoubleArray.take(n: Int) : List<Double> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun DoubleArray.takeWhile(predicate: (Double) -> Boolean) : List<Double> {
return takeWhileTo(ArrayList<Double>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Double>> DoubleArray.takeWhileTo(result: C, predicate: (Double) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Double>> DoubleArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Double>> DoubleArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun DoubleArray.reverse() : List<Double> {
val list = toCollection(ArrayList<Double>())
@@ -264,23 +267,112 @@ public inline fun DoubleArray.reverse() : List<Double> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun DoubleArray.toLinkedList() : LinkedList<Double> = toCollection(LinkedList<Double>())
/** Copies all elements into a [[List]] */
public inline fun DoubleArray.toList() : List<Double> = toCollection(ArrayList<Double>())
/** Copies all elements into a [[List] */
public inline fun DoubleArray.toCollection() : Collection<Double> = toCollection(ArrayList<Double>())
/** Copies all elements into a [[Set]] */
public inline fun DoubleArray.toSet() : Set<Double> = toCollection(HashSet<Double>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun DoubleArray.toLinkedList() : LinkedList<Double> {
return toCollection(LinkedList<Double>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun DoubleArray.toSortedList(transform: fun(Double) : java.lang.Comparable<*>) : List<Double> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun DoubleArray.toList() : List<Double> {
return toCollection(ArrayList<Double>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun DoubleArray.toSet() : Set<Double> {
return toCollection(LinkedHashSet<Double>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun DoubleArray.toSortedSet() : SortedSet<Double> {
return toCollection(TreeSet<Double>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun DoubleArray.plus(element: Double) : List<Double> {
val answer = ArrayList<Double>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun DoubleArray.plus(iterator: Iterator<Double>) : List<Double> {
val answer = ArrayList<Double>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun DoubleArray.plus(collection: Iterable<Double>) : List<Double> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun DoubleArray.withIndices() : Iterator<Pair<Int, Double>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> DoubleArray.sortBy(f: (Double) -> R) : List<Double> {
val sortedList = toCollection(ArrayList<Double>())
val sortBy: Comparator<Double> = comparator<Double> {(x: Double, y: Double) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun DoubleArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun DoubleArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> DoubleArray.mapTo(result: C, transform : (Double) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> DoubleArray.map(transform : (Double) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun DoubleArray.toSortedSet() : SortedSet<Double> = toCollection(TreeSet<Double>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Double> or java.util.Iterator<Double>
//
// 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<Double> = filterTo(ArrayList<Double>(), 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<Double> = filterNotTo(ArrayList<Double>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun DoubleArray?.filterNotNull() : List<Double> = filterNotNullTo<ArrayList<Double>>(java.util.ArrayList<Double>())
/**
* 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 <R> DoubleArray.flatMap(transform: (Double)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Double> {
val list = toCollection(ArrayList<Double>())
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<Double> {
val list = toCollection(ArrayList<Double>())
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<Double> {
val list = ArrayList<Double>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun DoubleArray.drop(n: Int): List<Double> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun DoubleArray.dropWhile(predicate: (Double) -> Boolean): List<Double> = dropWhileTo(ArrayList<Double>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun DoubleArray.take(n: Int): List<Double> {
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<Double> = takeWhileTo(ArrayList<Double>(), predicate)
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun FloatArray.all(predicate: (Float) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun FloatArray.all(predicate: (Float) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun FloatArray.any(predicate: (Float) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun FloatArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun FloatArray.count(predicate: (Float) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun FloatArray.count(predicate: (Float) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun FloatArray.find(predicate: (Float) -> Boolean) : Float? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Float>> FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C {
public inline fun FloatArray.filter(predicate: (Float) -> Boolean) : List<Float> {
return filterTo(ArrayList<Float>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Float>> FloatArray.filterTo(result: C, predicate: (Float) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun FloatArray.filterNot(predicate: (Float) -> Boolean) : List<Float> {
return filterNotTo(ArrayList<Float>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Float>> FloatArray.filterNotTo(result: C, predicate: (Float) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair<List<Float>, List<Float>> {
val first = ArrayList<Float>()
@@ -103,33 +82,33 @@ public inline fun FloatArray.partition(predicate: (Float) -> Boolean) : Pair<Lis
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Float>> FloatArray.filterNotTo(result: C, predicate: (Float) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> FloatArray.map(transform : (Float) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Float>> FloatArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> FloatArray.mapTo(result: C, transform : (Float) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> FloatArray.flatMapTo(result: MutableCollection<R>, transform: (Float) -> Collection<R>) : Collection<R> {
public inline fun <R> FloatArray.flatMap(transform: (Float)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> FloatArray.flatMapTo(result: C, transform: (Float) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> FloatArray.flatMapTo(result: MutableCollection<R>, transfo
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun FloatArray.forEach(operation: (Float) -> Unit) : Unit = for (element in this) operation(element)
public inline fun FloatArray.forEach(operation: (Float) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> FloatArray.fold(initial: R, operation: (R, Float) -> R): R {
public inline fun <R> FloatArray.fold(initial: R, operation: (R, Float) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> FloatArray.fold(initial: R, operation: (R, Float) -> R): R
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> FloatArray.foldRight(initial: R, operation: (Float, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> FloatArray.foldRight(initial: R, operation: (Float, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun FloatArray.reduce(operation: (Float, Float) -> Float): Float {
public inline fun FloatArray.reduce(operation: (Float, Float) -> Float) : Float {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Float = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float): Float = reverse().reduce { x, y -> operation(y, x) }
public inline fun FloatArray.reduceRight(operation: (Float, Float) -> Float) : Float {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> FloatArray.groupBy(toKey: (Float) -> K) : Map<K, List<Float>> = groupByTo<K>(HashMap<K, MutableList<Float>>(), toKey)
public inline fun <K> FloatArray.groupBy(toKey: (Float) -> K) : Map<K, List<Float>> {
return groupByTo(HashMap<K, MutableList<Float>>(), 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 <K> FloatArray.groupByTo(result: MutableMap<K, MutableList<Float>>, toKey: (Float) -> K) : Map<K, MutableList<Float>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> FloatArray.groupByTo(result: MutableMap<K, MutableList<Flo
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun FloatArray.drop(n: Int) : List<Float> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Float>> FloatArray.dropWhileTo(result: L, predicate: (Float) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean) : List<Float> {
return dropWhileTo(ArrayList<Float>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Float>> FloatArray.dropWhileTo(result: L, predicate: (Float) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Float>> FloatArray.dropWhileTo(result: L, pred
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Float>> FloatArray.takeWhileTo(result: C, predicate: (Float) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun FloatArray.take(n: Int) : List<Float> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun FloatArray.takeWhile(predicate: (Float) -> Boolean) : List<Float> {
return takeWhileTo(ArrayList<Float>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Float>> FloatArray.takeWhileTo(result: C, predicate: (Float) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Float>> FloatArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Float>> FloatArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun FloatArray.reverse() : List<Float> {
val list = toCollection(ArrayList<Float>())
@@ -264,23 +267,112 @@ public inline fun FloatArray.reverse() : List<Float> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun FloatArray.toLinkedList() : LinkedList<Float> = toCollection(LinkedList<Float>())
/** Copies all elements into a [[List]] */
public inline fun FloatArray.toList() : List<Float> = toCollection(ArrayList<Float>())
/** Copies all elements into a [[List] */
public inline fun FloatArray.toCollection() : Collection<Float> = toCollection(ArrayList<Float>())
/** Copies all elements into a [[Set]] */
public inline fun FloatArray.toSet() : Set<Float> = toCollection(HashSet<Float>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun FloatArray.toLinkedList() : LinkedList<Float> {
return toCollection(LinkedList<Float>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun FloatArray.toSortedList(transform: fun(Float) : java.lang.Comparable<*>) : List<Float> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun FloatArray.toList() : List<Float> {
return toCollection(ArrayList<Float>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun FloatArray.toSet() : Set<Float> {
return toCollection(LinkedHashSet<Float>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun FloatArray.toSortedSet() : SortedSet<Float> {
return toCollection(TreeSet<Float>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun FloatArray.plus(element: Float) : List<Float> {
val answer = ArrayList<Float>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun FloatArray.plus(iterator: Iterator<Float>) : List<Float> {
val answer = ArrayList<Float>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun FloatArray.plus(collection: Iterable<Float>) : List<Float> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun FloatArray.withIndices() : Iterator<Pair<Int, Float>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> FloatArray.sortBy(f: (Float) -> R) : List<Float> {
val sortedList = toCollection(ArrayList<Float>())
val sortBy: Comparator<Float> = comparator<Float> {(x: Float, y: Float) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun FloatArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun FloatArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> FloatArray.mapTo(result: C, transform : (Float) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> FloatArray.map(transform : (Float) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun FloatArray.toSortedSet() : SortedSet<Float> = toCollection(TreeSet<Float>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Float> or java.util.Iterator<Float>
//
// 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<Float> = filterTo(ArrayList<Float>(), 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<Float> = filterNotTo(ArrayList<Float>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun FloatArray?.filterNotNull() : List<Float> = filterNotNullTo<ArrayList<Float>>(java.util.ArrayList<Float>())
/**
* 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 <R> FloatArray.flatMap(transform: (Float)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Float> {
val list = toCollection(ArrayList<Float>())
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<Float> {
val list = toCollection(ArrayList<Float>())
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<Float> {
val list = ArrayList<Float>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun FloatArray.drop(n: Int): List<Float> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun FloatArray.dropWhile(predicate: (Float) -> Boolean): List<Float> = dropWhileTo(ArrayList<Float>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun FloatArray.take(n: Int): List<Float> {
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<Float> = takeWhileTo(ArrayList<Float>(), predicate)
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun IntArray.all(predicate: (Int) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun IntArray.all(predicate: (Int) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun IntArray.any(predicate: (Int) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun IntArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun IntArray.count(predicate: (Int) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun IntArray.count(predicate: (Int) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun IntArray.find(predicate: (Int) -> Boolean) : Int? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Int>> IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C {
public inline fun IntArray.filter(predicate: (Int) -> Boolean) : List<Int> {
return filterTo(ArrayList<Int>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Int>> IntArray.filterTo(result: C, predicate: (Int) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun IntArray.filterNot(predicate: (Int) -> Boolean) : List<Int> {
return filterNotTo(ArrayList<Int>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Int>> IntArray.filterNotTo(result: C, predicate: (Int) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair<List<Int>, List<Int>> {
val first = ArrayList<Int>()
@@ -103,33 +82,33 @@ public inline fun IntArray.partition(predicate: (Int) -> Boolean) : Pair<List<In
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Int>> IntArray.filterNotTo(result: C, predicate: (Int) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> IntArray.map(transform : (Int) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Int>> IntArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> IntArray.mapTo(result: C, transform : (Int) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> IntArray.flatMapTo(result: MutableCollection<R>, transform: (Int) -> Collection<R>) : Collection<R> {
public inline fun <R> IntArray.flatMap(transform: (Int)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> IntArray.flatMapTo(result: C, transform: (Int) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> IntArray.flatMapTo(result: MutableCollection<R>, transform
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun IntArray.forEach(operation: (Int) -> Unit) : Unit = for (element in this) operation(element)
public inline fun IntArray.forEach(operation: (Int) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> IntArray.fold(initial: R, operation: (R, Int) -> R): R {
public inline fun <R> IntArray.fold(initial: R, operation: (R, Int) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> IntArray.fold(initial: R, operation: (R, Int) -> R): R {
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> IntArray.foldRight(initial: R, operation: (Int, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> IntArray.foldRight(initial: R, operation: (Int, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun IntArray.reduce(operation: (Int, Int) -> Int): Int {
public inline fun IntArray.reduce(operation: (Int, Int) -> Int) : Int {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Int = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int): Int = reverse().reduce { x, y -> operation(y, x) }
public inline fun IntArray.reduceRight(operation: (Int, Int) -> Int) : Int {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> IntArray.groupBy(toKey: (Int) -> K) : Map<K, List<Int>> = groupByTo<K>(HashMap<K, MutableList<Int>>(), toKey)
public inline fun <K> IntArray.groupBy(toKey: (Int) -> K) : Map<K, List<Int>> {
return groupByTo(HashMap<K, MutableList<Int>>(), 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 <K> IntArray.groupByTo(result: MutableMap<K, MutableList<Int>>, toKey: (Int) -> K) : Map<K, MutableList<Int>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> IntArray.groupByTo(result: MutableMap<K, MutableList<Int>>
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun IntArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
public inline fun IntArray.drop(n: Int) : List<Int> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Int>> IntArray.dropWhileTo(result: L, predicate: (Int) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean) : List<Int> {
return dropWhileTo(ArrayList<Int>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Int>> IntArray.dropWhileTo(result: L, predicate: (Int) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Int>> IntArray.dropWhileTo(result: L, predicat
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Int>> IntArray.takeWhileTo(result: C, predicate: (Int) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun IntArray.take(n: Int) : List<Int> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun IntArray.takeWhile(predicate: (Int) -> Boolean) : List<Int> {
return takeWhileTo(ArrayList<Int>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Int>> IntArray.takeWhileTo(result: C, predicate: (Int) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Int>> IntArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Int>> IntArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun IntArray.reverse() : List<Int> {
val list = toCollection(ArrayList<Int>())
@@ -264,23 +267,112 @@ public inline fun IntArray.reverse() : List<Int> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun IntArray.toLinkedList() : LinkedList<Int> = toCollection(LinkedList<Int>())
/** Copies all elements into a [[List]] */
public inline fun IntArray.toList() : List<Int> = toCollection(ArrayList<Int>())
/** Copies all elements into a [[List] */
public inline fun IntArray.toCollection() : Collection<Int> = toCollection(ArrayList<Int>())
/** Copies all elements into a [[Set]] */
public inline fun IntArray.toSet() : Set<Int> = toCollection(HashSet<Int>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun IntArray.toLinkedList() : LinkedList<Int> {
return toCollection(LinkedList<Int>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun IntArray.toSortedList(transform: fun(Int) : java.lang.Comparable<*>) : List<Int> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun IntArray.toList() : List<Int> {
return toCollection(ArrayList<Int>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun IntArray.toSet() : Set<Int> {
return toCollection(LinkedHashSet<Int>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun IntArray.toSortedSet() : SortedSet<Int> {
return toCollection(TreeSet<Int>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun IntArray.plus(element: Int) : List<Int> {
val answer = ArrayList<Int>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun IntArray.plus(iterator: Iterator<Int>) : List<Int> {
val answer = ArrayList<Int>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun IntArray.plus(collection: Iterable<Int>) : List<Int> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun IntArray.withIndices() : Iterator<Pair<Int, Int>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> IntArray.sortBy(f: (Int) -> R) : List<Int> {
val sortedList = toCollection(ArrayList<Int>())
val sortBy: Comparator<Int> = comparator<Int> {(x: Int, y: Int) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun IntArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun IntArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> IntArray.mapTo(result: C, transform : (Int) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> IntArray.map(transform : (Int) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun IntArray.toSortedSet() : SortedSet<Int> = toCollection(TreeSet<Int>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Int> or java.util.Iterator<Int>
//
// 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<Int> = filterTo(ArrayList<Int>(), 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<Int> = filterNotTo(ArrayList<Int>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun IntArray?.filterNotNull() : List<Int> = filterNotNullTo<ArrayList<Int>>(java.util.ArrayList<Int>())
/**
* 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 <R> IntArray.flatMap(transform: (Int)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Int> {
val list = toCollection(ArrayList<Int>())
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<Int> {
val list = toCollection(ArrayList<Int>())
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<Int> {
val list = ArrayList<Int>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun IntArray.drop(n: Int): List<Int> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun IntArray.dropWhile(predicate: (Int) -> Boolean): List<Int> = dropWhileTo(ArrayList<Int>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun IntArray.take(n: Int): List<Int> {
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<Int> = takeWhileTo(ArrayList<Int>(), predicate)
@@ -4,8 +4,6 @@ import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -14,40 +12,14 @@ public inline fun <T> Iterable<T>.all(predicate: (T) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun <T> Iterable<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun <T> Iterable<T>.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 <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0
@@ -57,18 +29,14 @@ public inline fun <T> Iterable<T>.count(predicate: (T) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun <T> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
public inline fun <T:Any> Iterable<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Iterable<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
@@ -76,9 +44,23 @@ public inline fun <T, C: MutableCollection<in T>> Iterable<T>.filterTo(result: C
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Iterable<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
*/
public inline fun <T:Any, C: MutableCollection<in T>> Iterable<T?>.filterNotNullTo(result: C) : C {
for (element in this) if (element != null) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
@@ -94,33 +76,26 @@ public inline fun <T> Iterable<T>.partition(predicate: (T) -> Boolean) : Pair<Li
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, C: MutableCollection<in T>> Iterable<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Iterable<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <T, R> Iterable<T>.flatMapTo(result: MutableCollection<R>, transform: (T) -> Collection<R>) : Collection<R> {
public inline fun <T, R> Iterable<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -130,71 +105,45 @@ public inline fun <T, R> Iterable<T>.flatMapTo(result: MutableCollection<R>, tra
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
public inline fun <T> Iterable<T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <T,R> Iterable<T>.fold(initial: R, operation: (R, T) -> R): R {
public inline fun <T, R> Iterable<T>.fold(initial: R, operation: (R, T) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
}
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <T,R> Iterable<T>.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun <T> Iterable<T>.reduce(operation: (T, T) -> T): T {
public inline fun <T> Iterable<T>.reduce(operation: (T, T) -> T) : T {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun <T> Iterable<T>.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) }
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <T, K> Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, MutableList<T>>(), toKey)
public inline fun <T, K> Iterable<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), 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 <T, K> Iterable<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
@@ -205,20 +154,22 @@ public inline fun <T, K> Iterable<T>.groupByTo(result: MutableMap<K, MutableList
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun <T> Iterable<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
public inline fun <T> Iterable<T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Iterable<T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Iterable<T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
var start = true
for (element in this) {
@@ -232,13 +183,17 @@ public inline fun <T, L: MutableList<in T>> Iterable<T>.dropWhileTo(result: L, p
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Iterable<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
/**
* Copies all elements into the given collection
*/
public inline fun <T, C: MutableCollection<in T>> Iterable<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
@@ -246,8 +201,6 @@ public inline fun <T, C: MutableCollection<in T>> Iterable<T>.toCollection(resul
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun <T> Iterable<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
@@ -255,23 +208,83 @@ public inline fun <T> Iterable<T>.reverse() : List<T> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <T> Iterable<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <T> Iterable<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <T> Iterable<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun <T> Iterable<T>.toLinkedList() : LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun <in T> Iterable<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList()
answer.sort(transform)
return answer
* Copies all elements into a [[List]]
*/
public inline fun <T> Iterable<T>.toList() : List<T> {
return toCollection(ArrayList<T>())
}
*/
/**
* Copies all elements into a [[Set]]
*/
public inline fun <T> Iterable<T>.toSet() : Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun <T> Iterable<T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <T, R: Comparable<R>> Iterable<T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun <T> Iterable<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun <T> Iterable<T>.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()
}
@@ -0,0 +1,84 @@
package kotlin
import java.util.*
/**
* Returns an iterator over elements which match the given *predicate*
*/
public inline fun <T> Iterator<T>.filter(predicate: (T) -> Boolean) : Iterator<T> {
return FilterIterator<T>(this, predicate)
}
/**
* Returns an iterator over elements which don't match the given *predicate*
*/
public inline fun <T> Iterator<T>.filterNot(predicate: (T) -> Boolean) : Iterator<T> {
return filter {!predicate(it)}
}
/**
* Returns an iterator over non-*null* elements
*/
public inline fun <T:Any> Iterator<T?>.filterNotNull() : Iterator<T> {
return FilterNotNullIterator(this)
}
/**
* Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*
*/
public inline fun <T, R> Iterator<T>.map(transform : (T) -> R) : Iterator<R> {
return MapIterator<T, R>(this, transform)
}
/**
* Returns an iterator over the concatenated results of transforming each element to one or more values
*/
public inline fun <T, R> Iterator<T>.flatMap(transform: (T) -> Iterator<R>) : Iterator<R> {
return FlatMapIterator<T, R>(this, transform)
}
/**
* Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public inline fun <T:Any> Iterator<T?>.requireNoNulls() : Iterator<T> {
return map<T?, T>{
if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
}
}
/**
* Returns an iterator restricted to the first *n* elements
*/
public inline fun <T> Iterator<T>.take(n: Int) : Iterator<T> {
var count = n
return takeWhile{ --count >= 0 }
}
/**
* Returns an iterator restricted to the first elements that match the given *predicate*
*/
public inline fun <T> Iterator<T>.takeWhile(predicate: (T) -> Boolean) : Iterator<T> {
return TakeWhileIterator<T>(this, predicate)
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun <T> Iterator<T>.plus(element: T) : Iterator<T> {
return CompositeIterator<T>(this, SingleIterator(element))
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun <T> Iterator<T>.plus(iterator: Iterator<T>) : Iterator<T> {
return CompositeIterator<T>(this, iterator)
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun <T> Iterator<T>.plus(collection: Iterable<T>) : Iterator<T> {
return plus(collection.iterator())
}
@@ -1,19 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun <T> Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -22,40 +12,14 @@ public inline fun <T> Iterator<T>.all(predicate: (T) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun <T> Iterator<T>.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun <T> Iterator<T>.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 <T> Iterator<T>.count(predicate: (T) -> Boolean) : Int {
var count = 0
@@ -65,18 +29,14 @@ public inline fun <T> Iterator<T>.count(predicate: (T) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun <T> Iterator<T>.find(predicate: (T) -> Boolean) : T? {
public inline fun <T:Any> Iterator<T>.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
@@ -84,9 +44,23 @@ public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterTo(result: C
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
*/
public inline fun <T:Any, C: MutableCollection<in T>> Iterator<T?>.filterNotNullTo(result: C) : C {
for (element in this) if (element != null) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun <T> Iterator<T>.partition(predicate: (T) -> Boolean) : Pair<List<T>, List<T>> {
val first = ArrayList<T>()
@@ -102,33 +76,26 @@ public inline fun <T> Iterator<T>.partition(predicate: (T) -> Boolean) : Pair<Li
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T?>?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <T, R> Iterator<T>.flatMapTo(result: MutableCollection<R>, transform: (T) -> Collection<R>) : Collection<R> {
public inline fun <T, R> Iterator<T>.flatMap(transform: (T)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Iterator<T>.flatMapTo(result: C, transform: (T) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -138,71 +105,45 @@ public inline fun <T, R> Iterator<T>.flatMapTo(result: MutableCollection<R>, tra
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit = for (element in this) operation(element)
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <T,R> Iterator<T>.fold(initial: R, operation: (R, T) -> R): R {
public inline fun <T, R> Iterator<T>.fold(initial: R, operation: (R, T) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
}
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <T,R> Iterator<T>.foldRight(initial: R, operation: (T, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun <T> Iterator<T>.reduce(operation: (T, T) -> T): T {
public inline fun <T> Iterator<T>.reduce(operation: (T, T) -> T) : T {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun <T> Iterator<T>.reduceRight(operation: (T, T) -> T): T = reverse().reduce { x, y -> operation(y, x) }
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <T, K> Iterator<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> = groupByTo<T,K>(HashMap<K, MutableList<T>>(), toKey)
public inline fun <T, K> Iterator<T>.groupBy(toKey: (T) -> K) : Map<K, List<T>> {
return groupByTo(HashMap<K, MutableList<T>>(), 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 <T, K> Iterator<T>.groupByTo(result: MutableMap<K, MutableList<T>>, toKey: (T) -> K) : Map<K, MutableList<T>> {
for (element in this) {
val key = toKey(element)
@@ -213,20 +154,22 @@ public inline fun <T, K> Iterator<T>.groupByTo(result: MutableMap<K, MutableList
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun <T> Iterator<T>.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
public inline fun <T> Iterator<T>.drop(n: Int) : List<T> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T> Iterator<T>.dropWhile(predicate: (T) -> Boolean) : List<T> {
return dropWhileTo(ArrayList<T>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <T, L: MutableList<in T>> Iterator<T>.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
var start = true
for (element in this) {
@@ -240,13 +183,17 @@ public inline fun <T, L: MutableList<in T>> Iterator<T>.dropWhileTo(result: L, p
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
/**
* Copies all elements into the given collection
*/
public inline fun <T, C: MutableCollection<in T>> Iterator<T>.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
@@ -254,8 +201,6 @@ public inline fun <T, C: MutableCollection<in T>> Iterator<T>.toCollection(resul
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun <T> Iterator<T>.reverse() : List<T> {
val list = toCollection(ArrayList<T>())
@@ -263,23 +208,83 @@ public inline fun <T> Iterator<T>.reverse() : List<T> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> = toCollection(LinkedList<T>())
/** Copies all elements into a [[List]] */
public inline fun <T> Iterator<T>.toList() : List<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[List] */
public inline fun <T> Iterator<T>.toCollection() : Collection<T> = toCollection(ArrayList<T>())
/** Copies all elements into a [[Set]] */
public inline fun <T> Iterator<T>.toSet() : Set<T> = toCollection(HashSet<T>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun <T> Iterator<T>.toLinkedList() : LinkedList<T> {
return toCollection(LinkedList<T>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun <in T> Iterator<T>.toSortedList(transform: fun(T) : java.lang.Comparable<*>) : List<T> {
val answer = this.toList()
answer.sort(transform)
return answer
* Copies all elements into a [[List]]
*/
public inline fun <T> Iterator<T>.toList() : List<T> {
return toCollection(ArrayList<T>())
}
*/
/**
* Copies all elements into a [[Set]]
*/
public inline fun <T> Iterator<T>.toSet() : Set<T> {
return toCollection(LinkedHashSet<T>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> {
return toCollection(TreeSet<T>())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun <T> Iterator<T>.withIndices() : Iterator<Pair<Int, T>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <T, R: Comparable<R>> Iterator<T>.sortBy(f: (T) -> R) : List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun <T> Iterator<T>.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun <T> Iterator<T>.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()
}
@@ -1,15 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun <T> Iterator<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun LongArray.all(predicate: (Long) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun LongArray.all(predicate: (Long) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun LongArray.any(predicate: (Long) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun LongArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun LongArray.count(predicate: (Long) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun LongArray.count(predicate: (Long) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun LongArray.find(predicate: (Long) -> Boolean) : Long? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Long>> LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C {
public inline fun LongArray.filter(predicate: (Long) -> Boolean) : List<Long> {
return filterTo(ArrayList<Long>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Long>> LongArray.filterTo(result: C, predicate: (Long) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun LongArray.filterNot(predicate: (Long) -> Boolean) : List<Long> {
return filterNotTo(ArrayList<Long>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Long>> LongArray.filterNotTo(result: C, predicate: (Long) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair<List<Long>, List<Long>> {
val first = ArrayList<Long>()
@@ -103,33 +82,33 @@ public inline fun LongArray.partition(predicate: (Long) -> Boolean) : Pair<List<
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Long>> LongArray.filterNotTo(result: C, predicate: (Long) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> LongArray.map(transform : (Long) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Long>> LongArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> LongArray.mapTo(result: C, transform : (Long) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> LongArray.flatMapTo(result: MutableCollection<R>, transform: (Long) -> Collection<R>) : Collection<R> {
public inline fun <R> LongArray.flatMap(transform: (Long)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> LongArray.flatMapTo(result: C, transform: (Long) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> LongArray.flatMapTo(result: MutableCollection<R>, transfor
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun LongArray.forEach(operation: (Long) -> Unit) : Unit = for (element in this) operation(element)
public inline fun LongArray.forEach(operation: (Long) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> LongArray.fold(initial: R, operation: (R, Long) -> R): R {
public inline fun <R> LongArray.fold(initial: R, operation: (R, Long) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> LongArray.fold(initial: R, operation: (R, Long) -> R): R {
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> LongArray.foldRight(initial: R, operation: (Long, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> LongArray.foldRight(initial: R, operation: (Long, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun LongArray.reduce(operation: (Long, Long) -> Long): Long {
public inline fun LongArray.reduce(operation: (Long, Long) -> Long) : Long {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Long = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long): Long = reverse().reduce { x, y -> operation(y, x) }
public inline fun LongArray.reduceRight(operation: (Long, Long) -> Long) : Long {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> LongArray.groupBy(toKey: (Long) -> K) : Map<K, List<Long>> = groupByTo<K>(HashMap<K, MutableList<Long>>(), toKey)
public inline fun <K> LongArray.groupBy(toKey: (Long) -> K) : Map<K, List<Long>> {
return groupByTo(HashMap<K, MutableList<Long>>(), 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 <K> LongArray.groupByTo(result: MutableMap<K, MutableList<Long>>, toKey: (Long) -> K) : Map<K, MutableList<Long>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> LongArray.groupByTo(result: MutableMap<K, MutableList<Long
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun LongArray.drop(n: Int) : List<Long> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Long>> LongArray.dropWhileTo(result: L, predicate: (Long) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean) : List<Long> {
return dropWhileTo(ArrayList<Long>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Long>> LongArray.dropWhileTo(result: L, predicate: (Long) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Long>> LongArray.dropWhileTo(result: L, predic
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Long>> LongArray.takeWhileTo(result: C, predicate: (Long) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun LongArray.take(n: Int) : List<Long> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun LongArray.takeWhile(predicate: (Long) -> Boolean) : List<Long> {
return takeWhileTo(ArrayList<Long>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Long>> LongArray.takeWhileTo(result: C, predicate: (Long) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Long>> LongArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Long>> LongArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun LongArray.reverse() : List<Long> {
val list = toCollection(ArrayList<Long>())
@@ -264,23 +267,112 @@ public inline fun LongArray.reverse() : List<Long> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun LongArray.toLinkedList() : LinkedList<Long> = toCollection(LinkedList<Long>())
/** Copies all elements into a [[List]] */
public inline fun LongArray.toList() : List<Long> = toCollection(ArrayList<Long>())
/** Copies all elements into a [[List] */
public inline fun LongArray.toCollection() : Collection<Long> = toCollection(ArrayList<Long>())
/** Copies all elements into a [[Set]] */
public inline fun LongArray.toSet() : Set<Long> = toCollection(HashSet<Long>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun LongArray.toLinkedList() : LinkedList<Long> {
return toCollection(LinkedList<Long>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun LongArray.toSortedList(transform: fun(Long) : java.lang.Comparable<*>) : List<Long> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun LongArray.toList() : List<Long> {
return toCollection(ArrayList<Long>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun LongArray.toSet() : Set<Long> {
return toCollection(LinkedHashSet<Long>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun LongArray.toSortedSet() : SortedSet<Long> {
return toCollection(TreeSet<Long>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun LongArray.plus(element: Long) : List<Long> {
val answer = ArrayList<Long>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun LongArray.plus(iterator: Iterator<Long>) : List<Long> {
val answer = ArrayList<Long>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun LongArray.plus(collection: Iterable<Long>) : List<Long> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun LongArray.withIndices() : Iterator<Pair<Int, Long>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> LongArray.sortBy(f: (Long) -> R) : List<Long> {
val sortedList = toCollection(ArrayList<Long>())
val sortBy: Comparator<Long> = comparator<Long> {(x: Long, y: Long) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun LongArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun LongArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> LongArray.mapTo(result: C, transform : (Long) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> LongArray.map(transform : (Long) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun LongArray.toSortedSet() : SortedSet<Long> = toCollection(TreeSet<Long>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Long> or java.util.Iterator<Long>
//
// 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<Long> = filterTo(ArrayList<Long>(), 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<Long> = filterNotTo(ArrayList<Long>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun LongArray?.filterNotNull() : List<Long> = filterNotNullTo<ArrayList<Long>>(java.util.ArrayList<Long>())
/**
* 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 <R> LongArray.flatMap(transform: (Long)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Long> {
val list = toCollection(ArrayList<Long>())
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<Long> {
val list = toCollection(ArrayList<Long>())
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<Long> {
val list = ArrayList<Long>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun LongArray.drop(n: Int): List<Long> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun LongArray.dropWhile(predicate: (Long) -> Boolean): List<Long> = dropWhileTo(ArrayList<Long>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun LongArray.take(n: Int): List<Long> {
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<Long> = takeWhileTo(ArrayList<Long>(), predicate)
@@ -1,20 +1,9 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Iterables.kt
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt all
*/
public inline fun ShortArray.all(predicate: (Short) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
@@ -23,40 +12,14 @@ public inline fun ShortArray.all(predicate: (Short) -> Boolean) : Boolean {
/**
* Returns *true* if any elements match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt any
*/
public inline fun ShortArray.any(predicate: (Short) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt appendString
*/
public inline fun ShortArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "..."): Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt count
*/
public inline fun ShortArray.count(predicate: (Short) -> Boolean) : Int {
var count = 0
@@ -66,8 +29,6 @@ public inline fun ShortArray.count(predicate: (Short) -> Boolean) : Int {
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*
* @includeFunctionBody ../../test/CollectionTest.kt find
*/
public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? {
for (element in this) if (predicate(element)) return element
@@ -75,19 +36,37 @@ public inline fun ShortArray.find(predicate: (Short) -> Boolean) : Short? {
}
/**
* Filters all elements which match the given predicate into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterIntoLinkedList
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun <C: MutableCollection<Short>> ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C {
public inline fun ShortArray.filter(predicate: (Short) -> Boolean) : List<Short> {
return filterTo(ArrayList<Short>(), predicate)
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun <C: MutableCollection<in Short>> ShortArray.filterTo(result: C, predicate: (Short) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collection
*
* @includeFunctionBody ../../test/CollectionTest.kt partition
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun ShortArray.filterNot(predicate: (Short) -> Boolean) : List<Short> {
return filterNotTo(ArrayList<Short>(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun <C: MutableCollection<in Short>> ShortArray.filterNotTo(result: C, predicate: (Short) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair<List<Short>, List<Short>> {
val first = ArrayList<Short>()
@@ -103,33 +82,33 @@ public inline fun ShortArray.partition(predicate: (Short) -> Boolean) : Pair<Lis
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotIntoLinkedList
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun <C: MutableCollection<Short>> ShortArray.filterNotTo(result: C, predicate: (Short) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
public inline fun <R> ShortArray.map(transform : (Short) -> R) : List<R> {
return mapTo(ArrayList<R>(), transform)
}
/**
* Filters all non-*null* elements into the given list
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNullIntoLinkedList
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <C: MutableCollection<Short>> ShortArray?.filterNotNullTo(result: C) : C {
if (this != null) {
for (element in this) if (element != null) result.add(element)
}
public inline fun <R, C: MutableCollection<in R>> ShortArray.mapTo(result: C, transform : (Short) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*
* @includeFunctionBody ../../test/CollectionTest.kt flatMap
*/
public inline fun <R> ShortArray.flatMapTo(result: MutableCollection<R>, transform: (Short) -> Collection<R>) : Collection<R> {
public inline fun <R> ShortArray.flatMap(transform: (Short)-> Iterable<R>) : List<R> {
return flatMapTo(ArrayList<R>(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun <R, C: MutableCollection<in R>> ShortArray.flatMapTo(result: C, transform: (Short) -> Iterable<R>) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
@@ -139,17 +118,15 @@ public inline fun <R> ShortArray.flatMapTo(result: MutableCollection<R>, transfo
/**
* Performs the given *operation* on each element
*
* @includeFunctionBody ../../test/CollectionTest.kt forEach
*/
public inline fun ShortArray.forEach(operation: (Short) -> Unit) : Unit = for (element in this) operation(element)
public inline fun ShortArray.forEach(operation: (Short) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt fold
*/
public inline fun <R> ShortArray.fold(initial: R, operation: (R, Short) -> R): R {
public inline fun <R> ShortArray.fold(initial: R, operation: (R, Short) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
@@ -157,53 +134,61 @@ public inline fun <R> ShortArray.fold(initial: R, operation: (R, Short) -> R): R
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*
* @includeFunctionBody ../../test/CollectionTest.kt foldRight
*/
public inline fun <R> ShortArray.foldRight(initial: R, operation: (Short, R) -> R): R = reverse().fold(initial, {x, y -> operation(y, x)})
public inline fun <R> ShortArray.foldRight(initial: R, operation: (Short, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduce
*/
public inline fun ShortArray.reduce(operation: (Short, Short) -> Short): Short {
public inline fun ShortArray.reduce(operation: (Short, Short) -> Short) : Short {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: Short = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*
* @includeFunctionBody ../../test/CollectionTest.kt reduceRight
*/
public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short): Short = reverse().reduce { x, y -> operation(y, x) }
public inline fun ShortArray.reduceRight(operation: (Short, Short) -> Short) : Short {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*
* @includeFunctionBody ../../test/CollectionTest.kt groupBy
*/
public inline fun <K> ShortArray.groupBy(toKey: (Short) -> K) : Map<K, List<Short>> = groupByTo<K>(HashMap<K, MutableList<Short>>(), toKey)
public inline fun <K> ShortArray.groupBy(toKey: (Short) -> K) : Map<K, List<Short>> {
return groupByTo(HashMap<K, MutableList<Short>>(), 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 <K> ShortArray.groupByTo(result: MutableMap<K, MutableList<Short>>, toKey: (Short) -> K) : Map<K, MutableList<Short>> {
for (element in this) {
val key = toKey(element)
@@ -214,21 +199,23 @@ public inline fun <K> ShortArray.groupByTo(result: MutableMap<K, MutableList<Sho
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
*
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*
* @includeFunctionBody ../../test/CollectionTest.kt makeString
* Returns a list containing everything but the first *n* elements
*/
public inline fun 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()
public inline fun ShortArray.drop(n: Int) : List<Short> {
return dropWhile(countTo(n))
}
/** Returns a list containing the everything but the first elements that satisfy the given *predicate* */
public inline fun <L: MutableList<Short>> ShortArray.dropWhileTo(result: L, predicate: (Short) -> Boolean) : L {
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean) : List<Short> {
return dropWhileTo(ArrayList<Short>(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun <L: MutableList<in Short>> ShortArray.dropWhileTo(result: L, predicate: (Short) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
@@ -241,22 +228,38 @@ public inline fun <L: MutableList<Short>> ShortArray.dropWhileTo(result: L, pred
return result
}
/** Returns a list containing the first elements that satisfy the given *predicate* */
public inline fun <C: MutableCollection<Short>> ShortArray.takeWhileTo(result: C, predicate: (Short) -> Boolean) : C {
/**
* Returns a list containing the first *n* elements
*/
public inline fun ShortArray.take(n: Int) : List<Short> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun ShortArray.takeWhile(predicate: (Short) -> Boolean) : List<Short> {
return takeWhileTo(ArrayList<Short>(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun <C: MutableCollection<in Short>> ShortArray.takeWhileTo(result: C, predicate: (Short) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/** Copies all elements into the given collection */
public inline fun <C: MutableCollection<Short>> ShortArray.toCollection(result: C) : C {
/**
* Copies all elements into the given collection
*/
public inline fun <C: MutableCollection<in Short>> ShortArray.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Reverses the order the elements into a list
*
* @includeFunctionBody ../../test/CollectionTest.kt reverse
*/
public inline fun ShortArray.reverse() : List<Short> {
val list = toCollection(ArrayList<Short>())
@@ -264,23 +267,112 @@ public inline fun ShortArray.reverse() : List<Short> {
return list
}
/** Copies all elements into a [[LinkedList]] */
public inline fun ShortArray.toLinkedList() : LinkedList<Short> = toCollection(LinkedList<Short>())
/** Copies all elements into a [[List]] */
public inline fun ShortArray.toList() : List<Short> = toCollection(ArrayList<Short>())
/** Copies all elements into a [[List] */
public inline fun ShortArray.toCollection() : Collection<Short> = toCollection(ArrayList<Short>())
/** Copies all elements into a [[Set]] */
public inline fun ShortArray.toSet() : Set<Short> = toCollection(HashSet<Short>())
/**
* Copies all elements into a [[LinkedList]]
*/
public inline fun ShortArray.toLinkedList() : LinkedList<Short> {
return toCollection(LinkedList<Short>())
}
/**
TODO figure out necessary variance/generics ninja stuff... :)
public inline fun ShortArray.toSortedList(transform: fun(Short) : java.lang.Comparable<*>) : List<Short> {
val answer = this.toList()
answer.sort(transform)
* Copies all elements into a [[List]]
*/
public inline fun ShortArray.toList() : List<Short> {
return toCollection(ArrayList<Short>())
}
/**
* Copies all elements into a [[Set]]
*/
public inline fun ShortArray.toSet() : Set<Short> {
return toCollection(LinkedHashSet<Short>())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public inline fun ShortArray.toSortedSet() : SortedSet<Short> {
return toCollection(TreeSet<Short>())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public inline fun ShortArray.plus(element: Short) : List<Short> {
val answer = ArrayList<Short>()
toCollection(answer)
answer.add(element)
return answer
}
*/
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public inline fun ShortArray.plus(iterator: Iterator<Short>) : List<Short> {
val answer = ArrayList<Short>()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public inline fun ShortArray.plus(collection: Iterable<Short>) : List<Short> {
return plus(collection.iterator())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public inline fun ShortArray.withIndices() : Iterator<Pair<Int, Short>> {
return IndexIterator(iterator())
}
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*/
public inline fun <R: Comparable<R>> ShortArray.sortBy(f: (Short) -> R) : List<Short> {
val sortedList = toCollection(ArrayList<Short>())
val sortBy: Comparator<Short> = comparator<Short> {(x: Short, y: Short) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Appends the string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun ShortArray.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied.
* If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will
* a special *truncated* separator (which defaults to "..."
*/
public inline fun ShortArray.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <R, C: MutableCollection<in R>> ShortArray.mapTo(result: C, transform : (Short) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <R> ShortArray.map(transform : (Short) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -1,16 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesJVM.kt
//
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun ShortArray.toSortedSet() : SortedSet<Short> = toCollection(TreeSet<Short>())
@@ -1,119 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/IterablesLazy.kt
//
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<Short> or java.util.Iterator<Short>
//
// 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<Short> = filterTo(ArrayList<Short>(), 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<Short> = filterNotTo(ArrayList<Short>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun ShortArray?.filterNotNull() : List<Short> = filterNotNullTo<ArrayList<Short>>(java.util.ArrayList<Short>())
/**
* 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 <R> ShortArray.flatMap(transform: (Short)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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<Short> {
val list = toCollection(ArrayList<Short>())
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<Short> {
val list = toCollection(ArrayList<Short>())
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<Short> {
val list = ArrayList<Short>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun ShortArray.drop(n: Int): List<Short> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun ShortArray.dropWhile(predicate: (Short) -> Boolean): List<Short> = dropWhileTo(ArrayList<Short>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun ShortArray.take(n: Int): List<Short> {
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<Short> = takeWhileTo(ArrayList<Short>(), predicate)
@@ -1,28 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/Collections.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Iterable<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,27 +0,0 @@
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
// Generated from input file: src/kotlin/CollectionsJVM.kt
//
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <T, R> Iterable<T>.map(transform : (T) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(), transform)
}
@@ -1,20 +0,0 @@
package kotlin
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun <T, R, C: MutableCollection<in R>> Collection<T>.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
@@ -1,19 +0,0 @@
package kotlin
import java.util.*
//
// This file contains methods which are optimised for working on Collection / Array collections where the size
// could be used to implement a more optimal solution
//
// See [[GenerateStandardLib.kt]] for more details
//
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <T, R> Collection<T>.map(transform : (T) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
@@ -0,0 +1 @@
public class EmptyIterableException(val it : Iterable<*>) : RuntimeException("$it is empty")
@@ -1,7 +0,0 @@
package kotlin
import java.util.*
/** Copies all elements into a [[SortedSet]] */
public inline fun <T> Iterable<T>.toSortedSet() : SortedSet<T> = toCollection(TreeSet<T>())
@@ -1,110 +0,0 @@
package kotlin
import java.util.ArrayList
//
// This file contains methods which could have a lazy implementation for things like
// Iterator<T> or java.util.Iterator<T>
//
// 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 <T> Iterable<T>.filter(predicate: (T) -> Boolean) : List<T> = filterTo(ArrayList<T>(), predicate)
/**
* Returns a list containing all elements which do not match the given predicate
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNot
*/
public inline fun <T> Iterable<T>.filterNot(predicate: (T)-> Boolean) : List<T> = filterNotTo(ArrayList<T>(), predicate)
/**
* Returns a list containing all the non-*null* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt filterNotNull
*/
public inline fun <T> Iterable<T?>?.filterNotNull() : List<T> = filterNotNullTo<T, ArrayList<T>>(java.util.ArrayList<T>())
/**
* 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 <T, R> Iterable<T>.flatMap(transform: (T)-> Collection<R>) : Collection<R> = flatMapTo(ArrayList<R>(), 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 <T> Iterable<T>.plus(element: T): List<T> {
val list = toCollection(ArrayList<T>())
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 <T> Iterable<T>.plus(elements: Iterable<T>): List<T> {
val list = toCollection(ArrayList<T>())
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 <T> Iterable<T?>.requireNoNulls() : List<T> {
val list = ArrayList<T>()
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
} else {
list.add(element)
}
}
return list
}
/**
* Returns a list containing everything but the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt drop
*/
public inline fun <T> Iterable<T>.drop(n: Int): List<T> {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt dropWhile
*/
public inline fun <T> Iterable<T>.dropWhile(predicate: (T) -> Boolean): List<T> = dropWhileTo(ArrayList<T>(), predicate)
/**
* Returns a list containing the first *n* elements
*
* @includeFunctionBody ../../test/CollectionTest.kt take
*/
public inline fun <T> Iterable<T>.take(n: Int): List<T> {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*
* @includeFunctionBody ../../test/CollectionTest.kt takeWhile
*/
public inline fun <T> Iterable<T>.takeWhile(predicate: (T) -> Boolean): List<T> = takeWhileTo(ArrayList<T>(), predicate)
@@ -34,42 +34,14 @@ private fun <T> countTo(n: Int): (T) -> Boolean {
*
* Will throw an exception if there are no elements
*/
// TODO: Specify type of the exception
public inline fun <T> Iterable<T>.first() : T {
if (this is AbstractList<T>) {
return this.get(0)
if (this is List<T>) {
return this.first()
}
return this.iterator().next()
}
/**
* Get the last element in the collection.
*
* If base collection implements [[List]] interface, the combination of size() and get()
* methods will be used for getting last element. Otherwise, this method determines the
* last item by iterating through the all items.
*
* Will throw an exception if there are no elements.
*
* @includeFunctionBody ../../test/CollectionTest.kt last
*/
// TODO: Specify type of the exception
public fun <T> Iterable<T>.last() : T {
if (this is List<T>) {
return this.get(this.size() - 1)
}
val iterator = this.iterator()
var last : T = iterator.next()
while (iterator.hasNext()) {
last = iterator.next()
}
return last;
}
/**
* Checks if collection contains given item.
*
@@ -91,20 +63,6 @@ public fun <T> Iterable<T>.containsItem(item : T) : Boolean {
return false
}
/**
* Convert collection of arbitrary elements to a List of tuples of the index and the element
*
* @includeFunctionBody ../../test/ListTest.kt withIndices
*/
public fun <T> Iterable<T>.withIndices(): List<Pair<Int, T>> {
val answer = ArrayList<Pair<Int, T>>()
var nextIndex = 0
for (e in this) {
answer.add(Pair(nextIndex, e))
nextIndex++
}
return answer
}
public inline fun <T: Comparable<T>> MutableIterable<T>.sort() : List<T> {
val list = toCollection(ArrayList<T>())
@@ -1,25 +0,0 @@
package kotlin
import java.util.AbstractList
import java.util.Comparator
import java.util.ArrayList
// TODO this function is here as it breaks the JS compiler; lets move back to JLangIterablesSpecial when it works again :)
/**
* Copies all elements into a [[List]] and sorts it by value of compare_function(element)
*
* E.g. arrayList("two" to 2, "one" to 1).sortBy({it._2}) returns list sorted by second element of tuple
*
* @includeFunctionBody ../../test/CollectionTest.kt sortBy
*/
public inline fun <T, R: Comparable<in R>> Iterable<T>.sortBy(f: (T) -> R): List<T> {
val sortedList = toCollection(ArrayList<T>())
val sortBy: Comparator<T> = comparator<T> {(x: T, y: T) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
+76 -91
View File
@@ -5,19 +5,12 @@ import java.util.Collections
/**
* Returns an iterator which invokes the function to calculate the next value on each iteration until the function returns *null*
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt fibonacci
*/
public inline fun <T> iterate(nextFunction: () -> T?) : Iterator<T> = FunctionIterator(nextFunction)
public inline fun <T:Any> iterate(nextFunction: () -> T?) : Iterator<T> {
return FunctionIterator(nextFunction)
}
/**
* Returns an iterator over elements which match the given *predicate*
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange
*/
public inline fun <T> Iterator<T>.filter(predicate: (T) -> Boolean) : Iterator<T> = FilterIterator<T>(this, predicate)
private class FilterIterator<T>(val iterator : Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() {
class FilterIterator<T>(val iterator : Iterator<T>, val predicate: (T)-> Boolean) : AbstractIterator<T>() {
override protected fun computeNext(): Unit {
while (iterator.hasNext()) {
val next = iterator.next()
@@ -30,13 +23,7 @@ private class FilterIterator<T>(val iterator : Iterator<T>, val predicate: (T)->
}
}
/** Returns an iterator over elements which do not match the given *predicate* */
public inline fun <T> Iterator<T>.filterNot(predicate: (T) -> Boolean) : Iterator<T> = filter { !predicate(it) }
/** Returns an iterator over non-*null* elements */
public inline fun <T> Iterator<T?>?.filterNotNull() : Iterator<T> = FilterNotNullIterator(this)
private class FilterNotNullIterator<T>(val iterator : Iterator<T?>?) : AbstractIterator<T>() {
class FilterNotNullIterator<T:Any>(val iterator : Iterator<T?>?) : AbstractIterator<T>() {
override protected fun computeNext(): Unit {
if (iterator != null) {
while (iterator.hasNext()) {
@@ -51,14 +38,7 @@ private class FilterNotNullIterator<T>(val iterator : Iterator<T?>?) : AbstractI
}
}
/**
* Returns an iterator obtained by applying *transform*, a function transforming an object of type *T* into an object of type *R*
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt mapAndTakeWhileExtractTheTransformedElements
*/
public inline fun <T, R> Iterator<T>.map(transform: (T) -> R): Iterator<R> = MapIterator<T, R>(this, transform)
private class MapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -> R) : AbstractIterator<R>() {
class MapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -> R) : AbstractIterator<R>() {
override protected fun computeNext() : Unit {
if (iterator.hasNext()) {
setNext((transform)(iterator.next()))
@@ -68,14 +48,7 @@ private class MapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -
}
}
/**
* Returns an iterator over the concatenated results of transforming each element to one or more values
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt flatMapAndTakeExtractTheTransformedElements
*/
public inline fun <T, R> Iterator<T>.flatMap(transform: (T) -> Iterator<R>): Iterator<R> = FlatMapIterator<T, R>(this, transform)
private class FlatMapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -> Iterator<R>) : AbstractIterator<R>() {
class FlatMapIterator<T, R>(val iterator : Iterator<T>, val transform: (T) -> Iterator<R>) : AbstractIterator<R>() {
var transformed: Iterator<R> = iterate<R> { null }
override protected fun computeNext() : Unit {
@@ -94,63 +67,7 @@ private class FlatMapIterator<T, R>(val iterator : Iterator<T>, val transform: (
}
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt plus
*/
public inline fun <T> Iterator<T>.plus(element: T): Iterator<T> {
return CompositeIterator<T>(this, SingleIterator(element))
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt plusCollection
*/
public inline fun <T> Iterator<T>.plus(iterator: Iterator<T>): Iterator<T> {
return CompositeIterator<T>(this, iterator)
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt plusCollection
*/
public inline fun <T> Iterator<T>.plus(collection: Iterable<T>): Iterator<T> = plus(collection.iterator())
/**
* Returns an iterator containing all the non-*null* elements, lazily throwing an [[IllegalArgumentException]]
if there are any null elements
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt requireNoNulls
*/
public inline fun <T> Iterator<T?>.requireNoNulls(): Iterator<T> {
return map<T?, T>{
if (it == null) throw IllegalArgumentException("null element in iterator $this") else it
}
}
/**
* Returns an iterator restricted to the first *n* elements
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt takeExtractsTheFirstNElements
*/
public inline fun <T> Iterator<T>.take(n: Int): Iterator<T> {
var count = n
return takeWhile{ --count >= 0 }
}
/**
* Returns an iterator restricted to the first elements that match the given *predicate*
*
* @includeFunctionBody ../../test/iterators/IteratorsTest.kt filterAndTakeWhileExtractTheElementsWithinRange
*/
public inline fun <T> Iterator<T>.takeWhile(predicate: (T) -> Boolean): Iterator<T> = TakeWhileIterator<T>(this, predicate)
private class TakeWhileIterator<T>(val iterator: Iterator<T>, val predicate: (T) -> Boolean) : AbstractIterator<T>() {
class TakeWhileIterator<T>(val iterator: Iterator<T>, val predicate: (T) -> Boolean) : AbstractIterator<T>() {
override protected fun computeNext() : Unit {
if (iterator.hasNext()) {
val item = iterator.next()
@@ -162,3 +79,71 @@ private class TakeWhileIterator<T>(val iterator: Iterator<T>, val predicate: (T)
done()
}
}
/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */
class FunctionIterator<T:Any>(val nextFunction: () -> T?): AbstractIterator<T>() {
override protected fun computeNext(): Unit {
val next = (nextFunction)()
if (next == null) {
done()
} else {
setNext(next)
}
}
}
/** An [[Iterator]] which iterates over a number of iterators in sequence */
class CompositeIterator<T>(vararg iterators: Iterator<T>): AbstractIterator<T>() {
val iteratorsIter = iterators.iterator()
var currentIter: Iterator<T>? = null
override protected fun computeNext(): Unit {
while (true) {
if (currentIter == null) {
if (iteratorsIter.hasNext()) {
currentIter = iteratorsIter.next()
} else {
done()
return
}
}
val iter = currentIter
if (iter != null) {
if (iter.hasNext()) {
setNext(iter.next())
return
} else {
currentIter = null
}
}
}
}
}
/** A singleton [[Iterator]] which invokes once over a value */
class SingleIterator<T>(val value: T): AbstractIterator<T>() {
var first = true
override protected fun computeNext(): Unit {
if (first) {
first = false
setNext(value)
} else {
done()
}
}
}
class IndexIterator<T>(val iterator : Iterator<T>): Iterator<Pair<Int, T>> {
private var index : Int = 0
override fun next(): Pair<Int, T> {
return Pair(index++, iterator.next())
}
override fun hasNext(): Boolean {
return iterator.hasNext()
}
}
@@ -1,7 +1,6 @@
package kotlin
import kotlin.support.*
import java.util.Collections
/** Returns an iterator over elements that are instances of a given type *R* which is a subclass of *T* */
public inline fun <T, R: T> Iterator<T>.filterIsInstance(klass: Class<R>): Iterator<R> = FilterIsIterator<T,R>(this, klass)
+69
View File
@@ -0,0 +1,69 @@
package kotlin
/**
* Get the first element in the list or throws [[EmptyIterableException]] if list is empty.
*/
public inline fun <T> List<T>.first() : T {
return if (size() > 0) get(0) else throw EmptyIterableException(this)
}
/**
* Get the first element in the list or *null* if list is empty.
*/
public inline fun <T:Any> List<T>.firstOrNull() : T? {
return if (size() > 0) get(0) else null
}
/**
* Get the last element in the list or throws [[EmptyIterableException]] if list is empty.
*/
public inline fun <T> List<T>.last() : T {
val s = size()
return if (s > 0) get(s - 1) else throw EmptyIterableException(this)
}
/**
* Get the last element in the list or *null* if list is empty.
*/
public inline fun <T:Any> List<T>.lastOrNull() : T? {
val s = size()
return if (s > 0) get(s - 1) else null
}
public inline fun <T> List<T>.forEachWithIndex(operation : (Int, T) -> Unit) {
for (index in indices) {
operation(index, get(index))
}
}
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun <T, R> List<T>.foldRight(initial: R, operation: (T, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*/
public inline fun <T> List<T>.reduceRight(operation: (T, T) -> T) : T {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
+18
View File
@@ -115,3 +115,21 @@ public inline fun <K,V> Map<K,V>.toMap(map: MutableMap<K,V>): Map<K,V> {
map.putAll(this)
return map
}
/**
* Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <K,V,R> Map<K,V>.map(transform: (Map.Entry<K,V>) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/**
* Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/MapTest.kt mapValues
*/
public inline fun <K,V,R> Map<K,V>.mapValues(transform : (Map.Entry<K,V>) -> R): Map<K,R> {
return mapValuesTo(java.util.HashMap<K,R>(this.size), transform)
}
-19
View File
@@ -42,22 +42,3 @@ public inline fun Map<String, String>.toProperties(): Properties {
return answer
}
/**
* Returns a new List containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/CollectionTest.kt map
*/
public inline fun <K,V,R> Map<K,V>.map(transform: (Map.Entry<K,V>) -> R) : List<R> {
return mapTo(java.util.ArrayList<R>(this.size), transform)
}
/**
* Returns a new Map containing the results of applying the given *transform* function to each [[Map.Entry]] in this [[Map]]
*
* @includeFunctionBody ../../test/MapTest.kt mapValues
*/
public inline fun <K,V,R> Map<K,V>.mapValues(transform : (Map.Entry<K,V>) -> R): Map<K,R> {
return mapValuesTo(java.util.HashMap<K,R>(this.size), transform)
}
+6 -17
View File
@@ -6,7 +6,7 @@ import java.util.Comparator
* Helper method for implementing [[Comparable]] methods using a list of functions
* to calculate the values to compare
*/
inline fun <T> compareBy(a: T?, b: T?, vararg functions: T.() -> Any?): Int {
inline fun <T : Any> compareBy(a: T?, b: T?, vararg functions: T.() -> Comparable<*>?): Int {
require(functions.size > 0)
if (a === b) return 0
if (a == null) return - 1
@@ -25,34 +25,23 @@ inline fun <T> compareBy(a: T?, b: T?, vararg functions: T.() -> Any?): Int {
* they are compared via [[#equals()]] and if they are not the same then
* the [[#hashCode()]] method is used as the difference
*/
public inline fun <T> compareValues(a: T?, b: T?): Int {
public inline fun <T : Comparable<*>> compareValues(a: T?, b: T?): Int {
if (a === b) return 0
if (a == null) return - 1
if (b == null) return 1
if (a is Comparable<*>) {
return (a as Comparable<Any?>).compareTo(b)
}
if (a == b) {
return 0
}
if (a is Object && b is Object) {
val diff = a.hashCode() - b.hashCode()
return if (diff == 0) 1 else diff
} else {
// TODO???
return 1
}
return (a as Comparable<Any?>).compareTo(b)
}
/**
* Creates a comparator using the sequence of functions used to calculate a value to compare on
*/
public inline fun <T> comparator(vararg val functions: T.() -> Any?): Comparator<T> {
public inline fun <T> comparator(vararg val functions: T.() -> Comparable<*>?): Comparator<T> {
return FunctionComparator<T>(*functions)
}
private class FunctionComparator<T>(vararg val functions: T.() -> Any?): Comparator<T> {
private class FunctionComparator<T>(vararg val functions: T.() -> Comparable<*>?): Comparator<T> {
public override fun toString(): String {
return "FunctionComparator${functions.toList()}"
-11
View File
@@ -51,14 +51,3 @@ public inline fun <A,B> A.to(that: B): Pair<A, B> = Pair(this, that)
Run function f
*/
public inline fun <T> run(f: () -> T) : T = f()
/**
* A helper method for creating a [[Runnable]] from a function
*/
public inline fun runnable(action: ()-> Unit): Runnable {
return object: Runnable {
public override fun run() {
action()
}
}
}
@@ -47,3 +47,14 @@ public inline fun <T> callable(action: ()-> T): Callable<T> {
public override fun call() = action()
}
}
/**
* A helper method for creating a [[Runnable]] from a function
*/
public inline fun runnable(action: ()-> Unit): Runnable {
return object: Runnable {
public override fun run() {
action()
}
}
}
@@ -33,13 +33,13 @@ public abstract class AbstractIterator<T>: Iterator<T> {
override fun next(): T {
if (!hasNext()) throw NoSuchElementException()
state = State.NotReady
return next!!
return next as T
}
/** Returns the next element in the iteration without advancing the iteration */
fun peek(): T {
if (!hasNext()) throw NoSuchElementException()
return next!!;
return next as T;
}
private fun tryToComputeNext(): Boolean {
@@ -76,59 +76,4 @@ public abstract class AbstractIterator<T>: Iterator<T> {
}
}
/** An [[Iterator]] which invokes a function to calculate the next value in the iteration until the function returns *null* */
class FunctionIterator<T>(val nextFunction: () -> T?): AbstractIterator<T>() {
override protected fun computeNext(): Unit {
val next = (nextFunction)()
if (next == null) {
done()
} else {
setNext(next)
}
}
}
/** An [[Iterator]] which iterates over a number of iterators in sequence */
class CompositeIterator<T>(vararg iterators: Iterator<T>): AbstractIterator<T>() {
val iteratorsIter = iterators.iterator()
var currentIter: Iterator<T>? = null
override protected fun computeNext(): Unit {
while (true) {
if (currentIter == null) {
if (iteratorsIter.hasNext()) {
currentIter = iteratorsIter.next()
} else {
done()
return
}
}
val iter = currentIter
if (iter != null) {
if (iter.hasNext()) {
setNext(iter.next())
return
} else {
currentIter = null
}
}
}
}
}
/** A singleton [[Iterator]] which invokes once over a value */
class SingleIterator<T>(val value: T): AbstractIterator<T>() {
var first = true
override protected fun computeNext(): Unit {
if (first) {
first = false
setNext(value)
} else {
done()
}
}
}