Implement sum extension function for UArrays (KT-28779)
This commit is contained in:
committed by
Ilya Gorbunov
parent
690e35f11a
commit
7695b5e575
@@ -3735,3 +3735,99 @@ public inline fun UShortArray.sumByDouble(selector: (UShort) -> Double): Double
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUInt")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Array<out UInt>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfULong")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Array<out ULong>.sum(): ULong {
|
||||
var sum: ULong = 0uL
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUByte")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Array<out UByte>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUShort")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Array<out UShort>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UIntArray.sum(): UInt {
|
||||
return storage.sum().toUInt()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ULongArray.sum(): ULong {
|
||||
return storage.sum().toULong()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UByteArray.sum(): UInt {
|
||||
return sumBy { it.toUInt() }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the array.
|
||||
*/
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun UShortArray.sum(): UInt {
|
||||
return sumBy { it.toUInt() }
|
||||
}
|
||||
|
||||
|
||||
@@ -67,3 +67,59 @@ public fun Collection<UShort>.toUShortArray(): UShortArray {
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the collection.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUInt")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Iterable<UInt>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the collection.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfULong")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Iterable<ULong>.sum(): ULong {
|
||||
var sum: ULong = 0uL
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the collection.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUByte")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Iterable<UByte>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the collection.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUShort")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Iterable<UShort>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2010-2019 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the license/LICENSE.txt file.
|
||||
*/
|
||||
|
||||
@file:kotlin.jvm.JvmMultifileClass
|
||||
@file:kotlin.jvm.JvmName("USequencesKt")
|
||||
|
||||
package kotlin.sequences
|
||||
|
||||
//
|
||||
// NOTE: THIS FILE IS AUTO-GENERATED by the GenerateStandardLib.kt
|
||||
// See: https://github.com/JetBrains/kotlin/tree/master/libraries/stdlib
|
||||
//
|
||||
|
||||
import kotlin.random.*
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the sequence.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUInt")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Sequence<UInt>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the sequence.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfULong")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Sequence<ULong>.sum(): ULong {
|
||||
var sum: ULong = 0uL
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the sequence.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUByte")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Sequence<UByte>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the sum of all elements in the sequence.
|
||||
*
|
||||
* The operation is _terminal_.
|
||||
*/
|
||||
@kotlin.jvm.JvmName("sumOfUShort")
|
||||
@SinceKotlin("1.3")
|
||||
@ExperimentalUnsignedTypes
|
||||
public fun Sequence<UShort>.sum(): UInt {
|
||||
var sum: UInt = 0u
|
||||
for (element in this) {
|
||||
sum += element
|
||||
}
|
||||
return sum
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user