Implement map, flatMap, zip & groupBy extension functions for UArrays

This commit is contained in:
Abduqodiri Qurbonzoda
2019-01-29 15:35:27 +03:00
committed by Ilya Gorbunov
parent 503264b996
commit abb275775e
5 changed files with 1211 additions and 59 deletions
File diff suppressed because it is too large Load Diff
@@ -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<UByte>(3u, 2u, 1u)) { ubyteArrayOf(1u, 2u, 3u).reversed() }
expect(listOf<UShort>(3u, 2u, 1u)) { ushortArrayOf(1u, 2u, 3u).reversed() }
expect(listOf<ULong>(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<UShort>(1, 2, 3), ushortArrayOf(1, 2, 3).map { it })
assertEquals(listOf<UInt>(2, 4, 6), uintArrayOf(1, 2, 3).map { 2u * it })
assertEquals(listOf<ULong>(0, 0, 0), ulongArrayOf(1, 2, 3).map { 0uL })
}
@Test
fun mapIndexed() {
assertEquals(listOf(), ubyteArrayOf().mapIndexed { _, e -> e })
assertEquals(listOf<UShort>(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<UShort>(1),
2.toUShort() to listOf<UShort>(2),
3.toUShort() to listOf<UShort>(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<UShort>(1, 2, 3), ushortArrayOf(1, 2, 3).flatMap { listOf(it) })
assertEquals(listOf<UInt>(1, 1, 2, 2, 3, 3), uintArrayOf(1, 2, 3).flatMap { listOf(it, it) })
assertEquals(listOf(), ulongArrayOf(1, 2, 3).flatMap { listOf<ULong>() })
}
@Test
fun withIndex() {
fun <T> assertIterableContentEquals(expected: Iterable<T>, actual: Iterable<T>) {
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<ULong>(11, 12, 13),
ulongArrayOf(1, 2, 3).zip(listOf<ULong>(10, 10, 10)) { a, b -> a + b }
)
}
}
@@ -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 {
@@ -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<R>, 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<V>")
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<V>(minOf(other.collectionSizeOrDefault(10), arraySize))
@@ -1033,8 +1044,11 @@ object Generators : TemplateGroupBase() {
}
val f_zip_array_transform = fn("zip(other: Array<out R>, 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<V>")
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<V>(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<V>")
inline()
body {
"""
val size = minOf(size, other.size)
@@ -1149,7 +1164,7 @@ object Generators : TemplateGroupBase() {
val f_zip = fn("zip(other: Iterable<R>)") {
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<out R>)") {
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 {
@@ -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<R>(collectionSizeOrDefault(10)), transform)"
}
body(ArraysOfObjects, ArraysOfPrimitives) {
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned) {
"return mapIndexedTo(ArrayList<R>(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<R>(collectionSizeOrDefault(10)), transform)"
}
body(ArraysOfObjects, ArraysOfPrimitives, Maps) {
body(ArraysOfObjects, ArraysOfPrimitives, ArraysOfUnsigned, Maps) {
"return mapTo(ArrayList<R>(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<R>)") {
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<R>)") {
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<R>)")
@@ -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<in K, MutableList<T>>")
@@ -346,21 +359,23 @@ object Mapping : TemplateGroupBase() {
returns("M")
body {
"""
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<T>() }
list.add(element)
}
return destination
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<T>() }
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<in K, MutableList<V>>")
@@ -402,12 +419,12 @@ object Mapping : TemplateGroupBase() {
returns("M")
body {
"""
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<V>() }
list.add(valueTransform(element))
}
return destination
for (element in this) {
val key = keySelector(element)
val list = destination.getOrPut(key) { ArrayList<V>() }
list.add(valueTransform(element))
}
return destination
"""
}
}