generated._Arrays.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-stdlib Show documentation
Show all versions of kotlin-stdlib Show documentation
Kotlin Standard Library for JVM
package kotlin
//
// NOTE THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
//
import java.util.*
/**
* Returns *true* if all elements match the given *predicate*
*/
public inline fun Array.all(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (!predicate(element)) return false
return true
}
/**
* Returns *true* if any elements match the given *predicate*
*/
public inline fun Array.any(predicate: (T) -> Boolean) : Boolean {
for (element in this) if (predicate(element)) return true
return false
}
/**
* 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 fun Array.appendString(buffer: Appendable, separator: String = ", ", prefix: String ="", postfix: String = "", limit: Int = -1, truncated: String = "...") : Unit {
buffer.append(prefix)
var count = 0
for (element in this) {
if (++count > 1) buffer.append(separator)
if (limit < 0 || count <= limit) {
val text = if (element == null) "null" else element.toString()
buffer.append(text)
} else break
}
if (limit >= 0 && count > limit) buffer.append(truncated)
buffer.append(postfix)
}
/**
* Returns the number of elements which match the given *predicate*
*/
public inline fun Array.count(predicate: (T) -> Boolean) : Int {
var count = 0
for (element in this) if (predicate(element)) count++
return count
}
/**
* Returns a list containing everything but the first *n* elements
*/
public fun Array.drop(n: Int) : List {
return dropWhile(countTo(n))
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun Array.dropWhile(predicate: (T) -> Boolean) : List {
return dropWhileTo(ArrayList(), predicate)
}
/**
* Returns a list containing the everything but the first elements that satisfy the given *predicate*
*/
public inline fun > Array.dropWhileTo(result: L, predicate: (T) -> Boolean) : L {
var start = true
for (element in this) {
if (start && predicate(element)) {
// ignore
} else {
start = false
result.add(element)
}
}
return result
}
/**
* Returns a list containing all elements which match the given *predicate*
*/
public inline fun Array.filter(predicate: (T) -> Boolean) : List {
return filterTo(ArrayList(), predicate)
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun Array.filterNot(predicate: (T) -> Boolean) : List {
return filterNotTo(ArrayList(), predicate)
}
/**
* Returns a list containing all the non-*null* elements
*/
public fun Array.filterNotNull() : List {
return filterNotNullTo>(ArrayList())
}
/**
* Filters all non-*null* elements into the given list
*/
public fun > Array.filterNotNullTo(result: C) : C {
for (element in this) if (element != null) result.add(element)
return result
}
/**
* Returns a list containing all elements which do not match the given *predicate*
*/
public inline fun > Array.filterNotTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (!predicate(element)) result.add(element)
return result
}
/**
* Filters all elements which match the given predicate into the given list
*/
public inline fun > Array.filterTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element)
return result
}
/**
* Returns the first element which matches the given *predicate* or *null* if none matched
*/
public inline fun Array.find(predicate: (T) -> Boolean) : T? {
for (element in this) if (predicate(element)) return element
return null
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single list
*/
public inline fun Array.flatMap(transform: (T)-> Iterable) : List {
return flatMapTo(ArrayList(), transform)
}
/**
* Returns the result of transforming each element to one or more values which are concatenated together into a single collection
*/
public inline fun > Array.flatMapTo(result: C, transform: (T) -> Iterable) : C {
for (element in this) {
val list = transform(element)
for (r in list) result.add(r)
}
return result
}
/**
* Folds all elements from from left to right with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun Array.fold(initial: R, operation: (R, T) -> R) : R {
var answer = initial
for (element in this) answer = operation(answer, element)
return answer
}
/**
* Folds all elements from right to left with the *initial* value to perform the operation on sequential pairs of elements
*/
public inline fun Array.foldRight(initial: R, operation: (T, R) -> R) : R {
var r = initial
var index = size - 1
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Performs the given *operation* on each element
*/
public inline fun Array.forEach(operation: (T) -> Unit) : Unit {
for (element in this) operation(element)
}
/**
* Groups the elements in the collection into a new [[Map]] using the supplied *toKey* function to calculate the key to group the elements by
*/
public inline fun Array.groupBy(toKey: (T) -> K) : Map> {
return groupByTo(HashMap>(), toKey)
}
public inline fun Array.groupByTo(result: MutableMap>, toKey: (T) -> K) : Map> {
for (element in this) {
val key = toKey(element)
val list = result.getOrPut(key) { ArrayList() }
list.add(element)
}
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun Array.indexOf(item: T) : Int {
if (item == null) {
for (i in indices) {
if (this[i] == null) {
return i
}
}
} else {
for (i in indices) {
if (item == this[i]) {
return i
}
}
}
return -1
}
/**
* 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 fun Array.makeString(separator: String = ", ", prefix: String = "", postfix: String = "", limit: Int = -1, truncated: String = "...") : String {
val buffer = StringBuilder()
appendString(buffer, separator, prefix, postfix, limit, truncated)
return buffer.toString()
}
/**
* Returns a new List containing the results of applying the given *transform* function to each element in this collection
*/
public inline fun Array.map(transform : (T) -> R) : List {
return mapTo(ArrayList(), transform)
}
/**
* Transforms each element of this collection with the given *transform* function and
* adds each return value to the given *results* collection
*/
public inline fun > Array.mapTo(result: C, transform : (T) -> R) : C {
for (item in this)
result.add(transform(item))
return result
}
/**
* Returns the largest element or null if there are no elements
*/
public fun > Array.max() : T? {
if (isEmpty()) return null
var max = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (max < e) max = e
}
return max
}
/**
* Returns the first element yielding the largest value of the given function or null if there are no elements
*/
public inline fun , T: Any> Array.maxBy(f: (T) -> R) : T? {
if (isEmpty()) return null
var maxElem = this[0]
var maxValue = f(maxElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = f(e)
if (maxValue < v) {
maxElem = e
maxValue = v
}
}
return maxElem
}
/**
* Returns the smallest element or null if there are no elements
*/
public fun > Array.min() : T? {
if (isEmpty()) return null
var min = this[0]
for (i in 1..lastIndex) {
val e = this[i]
if (min > e) min = e
}
return min
}
/**
* Returns the first element yielding the smallest value of the given function or null if there are no elements
*/
public inline fun , T: Any> Array.minBy(f: (T) -> R) : T? {
if (size == 0) return null
var minElem = this[0]
var minValue = f(minElem)
for (i in 1..lastIndex) {
val e = this[i]
val v = f(e)
if (minValue > v) {
minElem = e
minValue = v
}
}
return minElem
}
/**
* Partitions this collection into a pair of collections
*/
public inline fun Array.partition(predicate: (T) -> Boolean) : Pair, List> {
val first = ArrayList()
val second = ArrayList()
for (element in this) {
if (predicate(element)) {
first.add(element)
} else {
second.add(element)
}
}
return Pair(first, second)
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following collection
*/
public fun Array.plus(collection: Iterable) : List {
return plus(collection.iterator())
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the given element at the end
*/
public fun Array.plus(element: T) : List {
val answer = ArrayList()
toCollection(answer)
answer.add(element)
return answer
}
/**
* Creates an [[Iterator]] which iterates over this iterator then the following iterator
*/
public fun Array.plus(iterator: Iterator) : List {
val answer = ArrayList()
toCollection(answer)
for (element in iterator) {
answer.add(element)
}
return answer
}
/**
* Applies binary operation to all elements of iterable, going from left to right.
* Similar to fold function, but uses the first element as initial value
*/
public inline fun Array.reduce(operation: (T, T) -> T) : T {
val iterator = this.iterator()
if (!iterator.hasNext()) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var result: T = iterator.next() //compiler doesn't understand that result will initialized anyway
while (iterator.hasNext()) {
result = operation(result, iterator.next())
}
return result
}
/**
* Applies binary operation to all elements of iterable, going from right to left.
* Similar to foldRight function, but uses the last element as initial value
*/
public inline fun Array.reduceRight(operation: (T, T) -> T) : T {
var index = size - 1
if (index < 0) {
throw UnsupportedOperationException("Empty iterable can't be reduced")
}
var r = get(index--)
while (index >= 0) {
r = operation(get(index--), r)
}
return r
}
/**
* Returns a original Iterable containing all the non-*null* elements, throwing an [[IllegalArgumentException]] if there are any null elements
*/
public fun Array.requireNoNulls() : Array {
for (element in this) {
if (element == null) {
throw IllegalArgumentException("null element found in $this")
}
}
return this as Array
}
/**
* Reverses the order the elements into a list
*/
public fun Array.reverse() : List {
val list = toCollection(ArrayList())
Collections.reverse(list)
return list
}
/**
* 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.second}) returns list sorted by second element of pair
*/
public inline fun > Array.sortBy(f: (T) -> R) : List {
val sortedList = toCollection(ArrayList())
val sortBy: Comparator = comparator {(x: T, y: T) ->
val xr = f(x)
val yr = f(y)
xr.compareTo(yr)
}
java.util.Collections.sort(sortedList, sortBy)
return sortedList
}
/**
* Returns a list containing the first *n* elements
*/
public fun Array.take(n: Int) : List {
return takeWhile(countTo(n))
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun Array.takeWhile(predicate: (T) -> Boolean) : List {
return takeWhileTo(ArrayList(), predicate)
}
/**
* Returns a list containing the first elements that satisfy the given *predicate*
*/
public inline fun > Array.takeWhileTo(result: C, predicate: (T) -> Boolean) : C {
for (element in this) if (predicate(element)) result.add(element) else break
return result
}
/**
* Copies all elements into the given collection
*/
public fun > Array.toCollection(result: C) : C {
for (element in this) result.add(element)
return result
}
/**
* Copies all elements into a [[LinkedList]]
*/
public fun Array.toLinkedList() : LinkedList {
return toCollection(LinkedList())
}
/**
* Copies all elements into a [[List]]
*/
public fun Array.toList() : List {
return toCollection(ArrayList())
}
/**
* Copies all elements into a [[Set]]
*/
public fun Array.toSet() : Set {
return toCollection(LinkedHashSet())
}
/**
* Copies all elements into a [[SortedSet]]
*/
public fun Array.toSortedSet() : SortedSet {
return toCollection(TreeSet())
}
/**
* Returns an iterator of Pairs(index, data)
*/
public fun Array.withIndices() : Iterator> {
return IndexIterator(iterator())
}
/**
* Sums up the elements
*/
public fun Array.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array.sum() : Int {
return fold(0, {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array.sum() : Long {
return fold(0.toLong(), {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array.sum() : Float {
return fold(0.toFloat(), {a,b -> a+b})
}
/**
* Sums up the elements
*/
public fun Array.sum() : Double {
return fold(0.0, {a,b -> a+b})
}