Implement map, flatMap, zip & groupBy extension functions for UArrays
This commit is contained in:
committed by
Ilya Gorbunov
parent
503264b996
commit
abb275775e
@@ -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
|
||||
"""
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user