From abb275775eb597dd286e564ceea57b9c94fa54f9 Mon Sep 17 00:00:00 2001 From: Abduqodiri Qurbonzoda Date: Tue, 29 Jan 2019 15:35:27 +0300 Subject: [PATCH] Implement map, flatMap, zip & groupBy extension functions for UArrays --- .../stdlib/common/src/generated/_UArrays.kt | 1012 +++++++++++++++++ .../test/collections/UnsignedArraysTest.kt | 114 +- .../kotlin-stdlib-runtime-merged.txt | 16 + .../src/templates/Generators.kt | 37 +- .../src/templates/Mapping.kt | 91 +- 5 files changed, 1211 insertions(+), 59 deletions(-) diff --git a/libraries/stdlib/common/src/generated/_UArrays.kt b/libraries/stdlib/common/src/generated/_UArrays.kt index 4c133d31ccc..2af2461d8ba 100644 --- a/libraries/stdlib/common/src/generated/_UArrays.kt +++ b/libraries/stdlib/common/src/generated/_UArrays.kt @@ -2523,6 +2523,638 @@ public inline fun ShortArray.toUShortArray(): UShortArray { return UShortArray(this.copyOf()) } +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.flatMap(transform: (UInt) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.flatMap(transform: (ULong) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.flatMap(transform: (UByte) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Returns a single list of all elements yielded from results of [transform] function being invoked on each element of original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.flatMap(transform: (UShort) -> Iterable): List { + return flatMapTo(ArrayList(), transform) +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UIntArray.flatMapTo(destination: C, transform: (UInt) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > ULongArray.flatMapTo(destination: C, transform: (ULong) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UByteArray.flatMapTo(destination: C, transform: (UByte) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Appends all elements yielded from results of [transform] function being invoked on each element of original array, to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UShortArray.flatMapTo(destination: C, transform: (UShort) -> Iterable): C { + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.groupBy(keySelector: (UInt) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.groupBy(keySelector: (ULong) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.groupBy(keySelector: (UByte) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and returns a map where each group key is associated with a list of corresponding elements. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.groupBy(keySelector: (UShort) -> K): Map> { + return groupByTo(LinkedHashMap>(), keySelector) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.groupBy(keySelector: (UInt) -> K, valueTransform: (UInt) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.groupBy(keySelector: (ULong) -> K, valueTransform: (ULong) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.groupBy(keySelector: (UByte) -> K, valueTransform: (UByte) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and returns a map where each group key is associated with a list of corresponding values. + * + * The returned map preserves the entry iteration order of the keys produced from the original array. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.groupBy(keySelector: (UShort) -> K, valueTransform: (UShort) -> V): Map> { + return groupByTo(LinkedHashMap>(), keySelector, valueTransform) +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> UIntArray.groupByTo(destination: M, keySelector: (UInt) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> ULongArray.groupByTo(destination: M, keySelector: (ULong) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> UByteArray.groupByTo(destination: M, keySelector: (UByte) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} + +/** + * Groups elements of the original array by the key returned by the given [keySelector] function + * applied to each element and puts to the [destination] map each group key associated with a list of corresponding elements. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupBy + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> UShortArray.groupByTo(destination: M, keySelector: (UShort) -> K): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> UIntArray.groupByTo(destination: M, keySelector: (UInt) -> K, valueTransform: (UInt) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> ULongArray.groupByTo(destination: M, keySelector: (ULong) -> K, valueTransform: (ULong) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> UByteArray.groupByTo(destination: M, keySelector: (UByte) -> K, valueTransform: (UByte) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Groups values returned by the [valueTransform] function applied to each element of the original array + * by the key returned by the given [keySelector] function applied to the element + * and puts to the [destination] map each group key associated with a list of corresponding values. + * + * @return The [destination] map. + * + * @sample samples.collections.Collections.Transformations.groupByKeysAndValues + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun >> UShortArray.groupByTo(destination: M, keySelector: (UShort) -> K, valueTransform: (UShort) -> V): M { + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.map(transform: (UInt) -> R): List { + return mapTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.map(transform: (ULong) -> R): List { + return mapTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.map(transform: (UByte) -> R): List { + return mapTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element in the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.map(transform: (UShort) -> R): List { + return mapTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.mapIndexed(transform: (index: Int, UInt) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.mapIndexed(transform: (index: Int, ULong) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.mapIndexed(transform: (index: Int, UByte) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} + +/** + * Returns a list containing the results of applying the given [transform] function + * to each element and its index in the original array. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.mapIndexed(transform: (index: Int, UShort) -> R): List { + return mapIndexedTo(ArrayList(size), transform) +} + +/** + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UIntArray.mapIndexedTo(destination: C, transform: (index: Int, UInt) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > ULongArray.mapIndexedTo(destination: C, transform: (index: Int, ULong) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UByteArray.mapIndexedTo(destination: C, transform: (index: Int, UByte) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Applies the given [transform] function to each element and its index in the original array + * and appends the results to the given [destination]. + * @param [transform] function that takes the index of an element and the element itself + * and returns the result of the transform applied to the element. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UShortArray.mapIndexedTo(destination: C, transform: (index: Int, UShort) -> R): C { + var index = 0 + for (item in this) + destination.add(transform(index++, item)) + return destination +} + +/** + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UIntArray.mapTo(destination: C, transform: (UInt) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > ULongArray.mapTo(destination: C, transform: (ULong) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UByteArray.mapTo(destination: C, transform: (UByte) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Applies the given [transform] function to each element of the original array + * and appends the results to the given [destination]. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun > UShortArray.mapTo(destination: C, transform: (UShort) -> R): C { + for (item in this) + destination.add(transform(item)) + return destination +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UIntArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun ULongArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UByteArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + +/** + * Returns a lazy [Iterable] of [IndexedValue] for each element of the original array. + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public fun UShortArray.withIndex(): Iterable> { + return IndexingIterable { iterator() } +} + /** * Returns `true` if all elements match the given [predicate]. * @@ -3983,6 +4615,386 @@ public inline fun UShortArray.sumByDouble(selector: (UShort) -> Double): Double return sum } +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UIntArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun ULongArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UByteArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UShortArray.zip(other: Array): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.zip(other: Array, transform: (a: UInt, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.zip(other: Array, transform: (a: ULong, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.zip(other: Array, transform: (a: UByte, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.zip(other: Array, transform: (a: UShort, b: R) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of pairs built from the elements of `this` collection and [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UIntArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` collection and [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun ULongArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` collection and [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UByteArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` collection and [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UShortArray.zip(other: Iterable): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] collection with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.zip(other: Iterable, transform: (a: UInt, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] collection with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.zip(other: Iterable, transform: (a: ULong, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] collection with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.zip(other: Iterable, transform: (a: UByte, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] collection with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.zip(other: Iterable, transform: (a: UShort, b: R) -> V): List { + val arraySize = size + val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) + var i = 0 + for (element in other) { + if (i >= arraySize) break + list.add(transform(this[i++], element)) + } + return list +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UIntArray.zip(other: UIntArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun ULongArray.zip(other: ULongArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UByteArray.zip(other: UByteArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of pairs built from the elements of `this` array and the [other] array with the same index. + * The returned list has length of the shortest collection. + * + * @sample samples.collections.Iterables.Operations.zipIterable + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +public infix fun UShortArray.zip(other: UShortArray): List> { + return zip(other) { t1, t2 -> t1 to t2 } +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest array. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UIntArray.zip(other: UIntArray, transform: (a: UInt, b: UInt) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest array. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun ULongArray.zip(other: ULongArray, transform: (a: ULong, b: ULong) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest array. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UByteArray.zip(other: UByteArray, transform: (a: UByte, b: UByte) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + +/** + * Returns a list of values built from the elements of `this` array and the [other] array with the same index + * using the provided [transform] function applied to each pair of elements. + * The returned list has length of the shortest array. + * + * @sample samples.collections.Iterables.Operations.zipIterableWithTransform + */ +@SinceKotlin("1.3") +@ExperimentalUnsignedTypes +@kotlin.internal.InlineOnly +public inline fun UShortArray.zip(other: UShortArray, transform: (a: UShort, b: UShort) -> V): List { + val size = minOf(size, other.size) + val list = ArrayList(size) + for (i in 0 until size) { + list.add(transform(this[i], other[i])) + } + return list +} + /** * Returns the sum of all elements in the array. */ diff --git a/libraries/stdlib/test/collections/UnsignedArraysTest.kt b/libraries/stdlib/test/collections/UnsignedArraysTest.kt index 8fbad81875a..2e1747f29cb 100644 --- a/libraries/stdlib/test/collections/UnsignedArraysTest.kt +++ b/libraries/stdlib/test/collections/UnsignedArraysTest.kt @@ -8,6 +8,7 @@ package test.collections import test.collections.behaviors.collectionBehavior import test.collections.behaviors.listBehavior +import test.collections.behaviors.iteratorBehavior import kotlin.test.* fun assertArrayContentEquals(expected: UIntArray, actual: UIntArray, message: String = "") { assertTrue(expected contentEquals actual, message) } @@ -39,7 +40,7 @@ class UnsignedArraysTest { assertEquals(index.toUByte(), initArray[index]) } } - + @Test fun ushortArrayInit() { val zeroArray = UShortArray(42) @@ -53,7 +54,7 @@ class UnsignedArraysTest { assertEquals(index.toUShort(), initArray[index]) } } - + @Test fun uintArrayInit() { val zeroArray = UIntArray(42) @@ -67,7 +68,7 @@ class UnsignedArraysTest { assertEquals(index.toUInt(), initArray[index]) } } - + @Test fun ulongArrayInit() { val zeroArray = ULongArray(42) @@ -332,11 +333,10 @@ class UnsignedArraysTest { @Test fun sumByDouble() { - // TODO: .toInt().toDouble() -> .toDouble() when conversion from unsigned primitives to Double gets implemented. - assertEquals(3.0, ubyteArrayOf(0, 1, 2).sumByDouble { it.toInt().toDouble() }) - assertEquals(1.0, ushortArrayOf(0, 1, 2).sumByDouble { (it % 2u).toInt().toDouble() }) - assertEquals(0.0, uintArrayOf(0, 2, 4).sumByDouble { (it % 2u).toInt().toDouble() }) - assertEquals(6.0, ulongArrayOf(2, 3, 4).sumByDouble { (it - 1u).toInt().toDouble() }) + assertEquals(3.0, ubyteArrayOf(0, 1, 2).sumByDouble { it.toDouble() }) + assertEquals(1.0, ushortArrayOf(0, 1, 2).sumByDouble { (it % 2u).toDouble() }) + assertEquals(0.0, uintArrayOf(0, 2, 4).sumByDouble { (it % 2u).toDouble() }) + assertEquals(6.0, ulongArrayOf(2, 3, 4).sumByDouble { (it - 1u).toDouble() }) } @Test @@ -352,14 +352,16 @@ class UnsignedArraysTest { assertEquals(genericArray.toList(), ulongArray.toList()) } - @Test fun reversed() { + @Test + fun reversed() { expect(listOf(3u, 2u, 1u)) { uintArrayOf(1u, 2u, 3u).reversed() } expect(listOf(3u, 2u, 1u)) { ubyteArrayOf(1u, 2u, 3u).reversed() } expect(listOf(3u, 2u, 1u)) { ushortArrayOf(1u, 2u, 3u).reversed() } expect(listOf(3u, 2u, 1u)) { ulongArrayOf(1u, 2u, 3u).reversed() } } - @Test fun reversedArray() { + @Test + fun reversedArray() { assertArrayContentEquals(uintArrayOf(3u, 2u, 1u), uintArrayOf(1u, 2u, 3u).reversedArray()) assertArrayContentEquals(ubyteArrayOf(3u, 2u, 1u), ubyteArrayOf(1u, 2u, 3u).reversedArray()) assertArrayContentEquals(ushortArrayOf(3u, 2u, 1u), ushortArrayOf(1u, 2u, 3u).reversedArray()) @@ -729,6 +731,96 @@ class UnsignedArraysTest { expect(2u) { ushortArrayOf(0, 1, 2).singleOrNull { it == 2.toUShort() } } expect(1u) { uintArrayOf(0, 1, 2).singleOrNull { it % 2u == 1u } } expect(null) { uintArrayOf().singleOrNull() } - expect(null) { ulongArrayOf(0, 1, 2).singleOrNull() { it % 2uL == 0uL } } + expect(null) { ulongArrayOf(0, 1, 2).singleOrNull { it % 2uL == 0uL } } } + + @Test + fun map() { + assertEquals(listOf(), ubyteArrayOf().map { it }) + assertEquals(listOf(1, 2, 3), ushortArrayOf(1, 2, 3).map { it }) + assertEquals(listOf(2, 4, 6), uintArrayOf(1, 2, 3).map { 2u * it }) + assertEquals(listOf(0, 0, 0), ulongArrayOf(1, 2, 3).map { 0uL }) + } + + @Test + fun mapIndexed() { + assertEquals(listOf(), ubyteArrayOf().mapIndexed { _, e -> e }) + assertEquals(listOf(1, 2, 3), ushortArrayOf(1, 2, 3).mapIndexed { _, e -> e }) + assertEquals(listOf(0, 1, 2), uintArrayOf(1, 2, 3).mapIndexed { index, _ -> index }) + assertEquals(listOf(0, 0, 0), ulongArrayOf(1, 2, 3).mapIndexed { _, _ -> 0 }) + } + + @Test + fun groupBy() { + assertEquals(mapOf(), ubyteArrayOf().groupBy { k -> k }) + assertEquals( + mapOf( + 1.toUShort() to listOf(1), + 2.toUShort() to listOf(2), + 3.toUShort() to listOf(3) + ), + ushortArrayOf(1, 2, 3).groupBy { k -> k } + ) + assertEquals( + mapOf( + 0.toUInt() to listOf("2"), + 1.toUInt() to listOf("1", "3") + ), + uintArrayOf(1, 2, 3).groupBy({ k -> k % 2 }, { v -> v.toString() }) + ) + assertEquals( + mapOf( + 0 to listOf(0, 0, 0) + ), + ulongArrayOf(1, 2, 3).groupBy({ 0 }, { 0 }) + ) + } + + @Test + fun flatMap() { + assertEquals(listOf(), ubyteArrayOf().flatMap { listOf(it) }) + assertEquals(listOf(1, 2, 3), ushortArrayOf(1, 2, 3).flatMap { listOf(it) }) + assertEquals(listOf(1, 1, 2, 2, 3, 3), uintArrayOf(1, 2, 3).flatMap { listOf(it, it) }) + assertEquals(listOf(), ulongArrayOf(1, 2, 3).flatMap { listOf() }) + } + + @Test + fun withIndex() { + fun assertIterableContentEquals(expected: Iterable, actual: Iterable) { + compare(expected.iterator(), actual.iterator()) { iteratorBehavior() } + } + + assertIterableContentEquals(listOf(), ubyteArrayOf().withIndex()) + assertIterableContentEquals( + listOf( + IndexedValue(0, 1.toUShort()), + IndexedValue(1, 2.toUShort()), + IndexedValue(2, 3.toUShort()) + ), + ushortArrayOf(1, 2, 3).withIndex() + ) + assertEquals(IndexedValue(1, 2.toUInt()), uintArrayOf(1, 2, 3).withIndex().minBy { it.value % 2 }) + assertIterableContentEquals(listOf(0, 1, 2), ulongArrayOf(1, 2, 3).withIndex().map { it.index }) + } + + @Test + fun zip() { + assertEquals(listOf(), ubyteArrayOf().zip(ubyteArrayOf())) + assertEquals( + listOf( + 1.toUShort() to 1.toUShort(), + 2.toUShort() to 2.toUShort() + ), + ushortArrayOf(1, 2, 3).zip(ushortArrayOf(1, 2)) + ) + assertEquals( + listOf("1a", "2b", "3c"), + uintArrayOf(1, 2, 3).zip(arrayOf("a", "b", "c", "d")) { a, b -> a.toString() + b } + ) + assertEquals( + listOf(11, 12, 13), + ulongArrayOf(1, 2, 3).zip(listOf(10, 10, 10)) { a, b -> a + b } + ) + } + } \ No newline at end of file diff --git a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt index 091d75a2319..dbff3a18daa 100644 --- a/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt +++ b/libraries/tools/binary-compatibility-validator/reference-public-api/kotlin-stdlib-runtime-merged.txt @@ -2399,6 +2399,22 @@ public final class kotlin/collections/UArraysKt { public static final fun toUIntArray ([Lkotlin/UInt;)[I public static final fun toULongArray ([Lkotlin/ULong;)[J public static final fun toUShortArray ([Lkotlin/UShort;)[S + public static final fun withIndex--ajY-9A ([I)Ljava/lang/Iterable; + public static final fun withIndex-GBYM_sE ([B)Ljava/lang/Iterable; + public static final fun withIndex-QwZRm1k ([J)Ljava/lang/Iterable; + public static final fun withIndex-rL5Bavg ([S)Ljava/lang/Iterable; + public static final fun zip-C-E_24M ([I[Ljava/lang/Object;)Ljava/util/List; + public static final fun zip-F7u83W8 ([JLjava/lang/Iterable;)Ljava/util/List; + public static final fun zip-HwE9HBo ([ILjava/lang/Iterable;)Ljava/util/List; + public static final fun zip-JGPC0-M ([SLjava/lang/Iterable;)Ljava/util/List; + public static final fun zip-JQknh5Q ([BLjava/lang/Iterable;)Ljava/util/List; + public static final fun zip-ctEhBpI ([I[I)Ljava/util/List; + public static final fun zip-f7H3mmw ([J[Ljava/lang/Object;)Ljava/util/List; + public static final fun zip-kdPth3s ([B[B)Ljava/util/List; + public static final fun zip-mazbYpA ([S[S)Ljava/util/List; + public static final fun zip-nl983wc ([B[Ljava/lang/Object;)Ljava/util/List; + public static final fun zip-uaTIQ5s ([S[Ljava/lang/Object;)Ljava/util/List; + public static final fun zip-us8wMrg ([J[J)Ljava/util/List; } public abstract class kotlin/collections/UByteIterator : java/util/Iterator, kotlin/jvm/internal/markers/KMappedMarker { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt index 633ce923d36..145ad8418b8 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Generators.kt @@ -10,6 +10,15 @@ import templates.SequenceClass.* object Generators : TemplateGroupBase() { + init { + defaultBuilder { + specialFor(ArraysOfUnsigned) { + since("1.3") + annotation("@ExperimentalUnsignedTypes") + } + } + } + val f_plusElement = fn("plusElement(element: T)") { include(Iterables, Collections, Sets, Sequences) } builder { @@ -993,8 +1002,11 @@ object Generators : TemplateGroupBase() { } val f_zip_transform = fn("zip(other: Iterable, transform: (a: T, b: R) -> V)") { - include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { + inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { """ Returns a list of values built from the elements of `this` ${f.collection} and the [other] collection with the same index @@ -1006,7 +1018,6 @@ object Generators : TemplateGroupBase() { typeParam("R") typeParam("V") returns("List") - inline() body { """ val first = iterator() @@ -1018,7 +1029,7 @@ object Generators : TemplateGroupBase() { return list """ } - body(ArraysOfObjects, ArraysOfPrimitives) { + body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ val arraySize = size val list = ArrayList(minOf(other.collectionSizeOrDefault(10), arraySize)) @@ -1033,8 +1044,11 @@ object Generators : TemplateGroupBase() { } val f_zip_array_transform = fn("zip(other: Array, transform: (a: T, b: R) -> V)") { - include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { + inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { """ Returns a list of values built from the elements of `this` ${f.collection} and the [other] array with the same index @@ -1046,7 +1060,6 @@ object Generators : TemplateGroupBase() { typeParam("R") typeParam("V") returns("List") - inline() body { """ val arraySize = other.size @@ -1059,7 +1072,7 @@ object Generators : TemplateGroupBase() { return list """ } - body(ArraysOfObjects, ArraysOfPrimitives) { + body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { """ val size = minOf(size, other.size) val list = ArrayList(size) @@ -1073,8 +1086,11 @@ object Generators : TemplateGroupBase() { } val f_zip_sameArray_transform = fn("zip(other: SELF, transform: (a: T, b: T) -> V)") { - include(ArraysOfPrimitives) + include(ArraysOfPrimitives, ArraysOfUnsigned) } builder { + inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { """ Returns a list of values built from the elements of `this` array and the [other] array with the same index @@ -1085,7 +1101,6 @@ object Generators : TemplateGroupBase() { sample("samples.collections.Iterables.Operations.zipIterableWithTransform") typeParam("V") returns("List") - inline() body { """ val size = minOf(size, other.size) @@ -1149,7 +1164,7 @@ object Generators : TemplateGroupBase() { val f_zip = fn("zip(other: Iterable)") { - include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { infix(true) doc { @@ -1188,7 +1203,7 @@ object Generators : TemplateGroupBase() { } val f_zip_array = fn("zip(other: Array)") { - include(Iterables, ArraysOfObjects, ArraysOfPrimitives) + include(Iterables, ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) } builder { infix(true) doc { @@ -1208,7 +1223,7 @@ object Generators : TemplateGroupBase() { } val f_zip_sameArray = fn("zip(other: SELF)") { - include(ArraysOfPrimitives) + include(ArraysOfPrimitives, ArraysOfUnsigned) } builder { infix(true) doc { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt index 50a5872e12b..f1a958b7d52 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Mapping.kt @@ -19,12 +19,16 @@ object Mapping : TemplateGroupBase() { else sequenceClassification(intermediate, stateless) } + specialFor(ArraysOfUnsigned) { + since("1.3") + annotation("@ExperimentalUnsignedTypes") + } } } val f_withIndex = fn("withIndex()") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { doc { "Returns a ${if (f == Sequences) f.mapResult else "lazy [Iterable]"} of [IndexedValue] for each ${f.element} of the original ${f.collection}." @@ -42,9 +46,10 @@ object Mapping : TemplateGroupBase() { val f_mapIndexed = fn("mapIndexed(transform: (index: Int, T) -> R)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { """ @@ -59,7 +64,7 @@ object Mapping : TemplateGroupBase() { body(Iterables) { "return mapIndexedTo(ArrayList(collectionSizeOrDefault(10)), transform)" } - body(ArraysOfObjects, ArraysOfPrimitives) { + body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) { "return mapIndexedTo(ArrayList(size), transform)" } body(CharSequences) { @@ -76,9 +81,10 @@ object Mapping : TemplateGroupBase() { val f_map = fn("map(transform: (T) -> R)") { includeDefault() - include(Maps, CharSequences) + include(Maps, CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { """ @@ -91,7 +97,7 @@ object Mapping : TemplateGroupBase() { body(Iterables) { "return mapTo(ArrayList(collectionSizeOrDefault(10)), transform)" } - body(ArraysOfObjects, ArraysOfPrimitives, Maps) { + body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, Maps) { "return mapTo(ArrayList(size), transform)" } body(CharSequences) { @@ -162,9 +168,10 @@ object Mapping : TemplateGroupBase() { val f_mapTo = fn("mapTo(destination: C, transform: (T) -> R)") { includeDefault() - include(Maps, CharSequences) + include(Maps, CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { """ @@ -178,18 +185,19 @@ object Mapping : TemplateGroupBase() { body { """ - for (item in this) - destination.add(transform(item)) - return destination + for (item in this) + destination.add(transform(item)) + return destination """ } } val f_mapIndexedTo = fn("mapIndexedTo(destination: C, transform: (index: Int, T) -> R)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { """ @@ -206,10 +214,10 @@ object Mapping : TemplateGroupBase() { body { fun checkOverflow(value: String) = if (f == Sequences || f == Iterables) "checkIndexOverflow($value)" else value """ - var index = 0 - for (item in this) - destination.add(transform(${checkOverflow("index++")}, item)) - return destination + var index = 0 + for (item in this) + destination.add(transform(${checkOverflow("index++")}, item)) + return destination """ } } @@ -260,9 +268,10 @@ object Mapping : TemplateGroupBase() { val f_flatMap = fn("flatMap(transform: (T) -> Iterable)") { includeDefault() - include(Maps, CharSequences) + include(Maps, CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { "Returns a single list of all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}." } typeParam("R") @@ -283,9 +292,11 @@ object Mapping : TemplateGroupBase() { val f_flatMapTo = fn("flatMapTo(destination: C, transform: (T) -> Iterable)") { includeDefault() - include(Maps, CharSequences) + include(Maps, CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { "Appends all elements yielded from results of [transform] function being invoked on each ${f.element} of original ${f.collection}, to the given [destination]." } specialFor(Sequences) { signature("flatMapTo(destination: C, transform: (T) -> Sequence)") @@ -295,20 +306,21 @@ object Mapping : TemplateGroupBase() { returns("C") body { """ - for (element in this) { - val list = transform(element) - destination.addAll(list) - } - return destination + for (element in this) { + val list = transform(element) + destination.addAll(list) + } + return destination """ } } val f_groupBy_key = fn("groupBy(keySelector: (T) -> K)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } doc { """ @@ -327,9 +339,10 @@ object Mapping : TemplateGroupBase() { val f_groupByTo_key = fn("groupByTo(destination: M, keySelector: (T) -> K)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } typeParam("K") typeParam("M : MutableMap>") @@ -346,21 +359,23 @@ object Mapping : TemplateGroupBase() { returns("M") body { """ - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(element) - } - return destination + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(element) + } + return destination """ } } val f_groupBy_key_value = fn("groupBy(keySelector: (T) -> K, valueTransform: (T) -> V)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + doc { """ Groups values returned by the [valueTransform] function applied to each ${f.element} of the original ${f.collection} @@ -381,9 +396,11 @@ object Mapping : TemplateGroupBase() { val f_groupByTo_key_value = fn("groupByTo(destination: M, keySelector: (T) -> K, valueTransform: (T) -> V)") { includeDefault() - include(CharSequences) + include(CharSequences, ArraysOfUnsigned) } builder { inline() + specialFor(ArraysOfUnsigned) { inlineOnly() } + typeParam("K") typeParam("V") typeParam("M : MutableMap>") @@ -402,12 +419,12 @@ object Mapping : TemplateGroupBase() { returns("M") body { """ - for (element in this) { - val key = keySelector(element) - val list = destination.getOrPut(key) { ArrayList() } - list.add(valueTransform(element)) - } - return destination + for (element in this) { + val key = keySelector(element) + val list = destination.getOrPut(key) { ArrayList() } + list.add(valueTransform(element)) + } + return destination """ } }