From 1113cdd5559ff1af12e4c6b7af57208a408ce010 Mon Sep 17 00:00:00 2001 From: Nikolay Igotti Date: Thu, 2 Mar 2017 17:33:19 +0300 Subject: [PATCH] Few more functions. --- runtime/src/main/kotlin/kotlin/Arrays.kt | 293 ++++++++++++++++++++++- 1 file changed, 285 insertions(+), 8 deletions(-) diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index 04043ac6b6c..ea27f670507 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -340,6 +340,78 @@ private class BooleanIteratorImpl(val collection: BooleanArray) : BooleanIterato // This part is from generated _Arrays.kt. +/** + * 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 all elements match the given [predicate]. + */ +public inline fun ByteArray.all(predicate: (Byte) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun ShortArray.all(predicate: (Short) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun IntArray.all(predicate: (Int) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun LongArray.all(predicate: (Long) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun FloatArray.all(predicate: (Float) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun DoubleArray.all(predicate: (Double) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun BooleanArray.all(predicate: (Boolean) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + +/** + * Returns `true` if all elements match the given [predicate]. + */ +public inline fun CharArray.all(predicate: (Char) -> Boolean): Boolean { + for (element in this) if (!predicate(element)) return false + return true +} + /** * Returns `true` if array has at least one element. */ @@ -356,14 +428,6 @@ public inline fun Array.any(predicate: (T) -> Boolean): Boolean { return false } -/** - * 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 [element] is found in the array. */ @@ -427,6 +491,12 @@ public operator fun CharArray.contains(element: Char): Boolean { return indexOf(element) >= 0 } +/** + * Returns the number of elements in this array. + */ +@kotlin.internal.InlineOnly +public inline fun Array.count(): Int = this.size + /** * Returns an element at the given [index] or throws an [IndexOutOfBoundsException] if the [index] is out of bounds of this array. */ @@ -499,6 +569,213 @@ public inline fun CharArray.elementAt(index: Int): Char { return get(index) } +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun IntArray.fold(initial: R, operation: (acc: R, Int) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun LongArray.fold(initial: R, operation: (acc: R, Long) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun FloatArray.fold(initial: R, operation: (acc: R, Float) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun DoubleArray.fold(initial: R, operation: (acc: R, Double) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun BooleanArray.fold(initial: R, operation: (acc: R, Boolean) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Accumulates value starting with [initial] value and applying [operation] from left to right to current accumulator value and each element. + */ +public inline fun CharArray.fold(initial: R, operation: (acc: R, Char) -> R): R { + var accumulator = initial + for (element in this) accumulator = operation(accumulator, element) + return accumulator +} + +/** + * Performs the given [action] on each element. + */ +public inline fun Array.forEach(action: (T) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun ByteArray.forEach(action: (Byte) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun ShortArray.forEach(action: (Short) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun IntArray.forEach(action: (Int) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun LongArray.forEach(action: (Long) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun FloatArray.forEach(action: (Float) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun DoubleArray.forEach(action: (Double) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun BooleanArray.forEach(action: (Boolean) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element. + */ +public inline fun CharArray.forEach(action: (Char) -> Unit): Unit { + for (element in this) action(element) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun Array.forEachIndexed(action: (index: Int, T) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun ByteArray.forEachIndexed(action: (index: Int, Byte) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun ShortArray.forEachIndexed(action: (index: Int, Short) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun IntArray.forEachIndexed(action: (index: Int, Int) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun LongArray.forEachIndexed(action: (index: Int, Long) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun FloatArray.forEachIndexed(action: (index: Int, Float) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun DoubleArray.forEachIndexed(action: (index: Int, Double) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun BooleanArray.forEachIndexed(action: (index: Int, Boolean) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + +/** + * Performs the given [action] on each element, providing sequential index with the element. + * @param [action] function that takes the index of an element and the element itself + * and performs the desired action on the element. + */ +public inline fun CharArray.forEachIndexed(action: (index: Int, Char) -> Unit): Unit { + var index = 0 + for (item in this) action(index++, item) +} + /** * Returns first index of [element], or -1 if the array does not contain element. */