From 69d2cf5c47e2af8cc3ff744b7660eed1df3bd879 Mon Sep 17 00:00:00 2001 From: Ilya Matveev Date: Fri, 7 Apr 2017 17:25:16 +0700 Subject: [PATCH] stdlib: Add toList method for arrays of value types --- runtime/src/main/kotlin/kotlin/Arrays.kt | 160 +++++++++++++++++++++++ 1 file changed, 160 insertions(+) diff --git a/runtime/src/main/kotlin/kotlin/Arrays.kt b/runtime/src/main/kotlin/kotlin/Arrays.kt index 07ca5148857..3de12647d09 100644 --- a/runtime/src/main/kotlin/kotlin/Arrays.kt +++ b/runtime/src/main/kotlin/kotlin/Arrays.kt @@ -3206,3 +3206,163 @@ public inline fun CharArray.sumBy(selector: (Char) -> Int): Int { } return sum } + +/** + * Returns a [List] containing all elements. + */ +public fun ByteArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun ShortArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun IntArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun LongArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun FloatArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun DoubleArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun BooleanArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [List] containing all elements. + */ +public fun CharArray.toList(): List { + return when (size) { + 0 -> emptyList() + 1 -> listOf(this[0]) + else -> this.toMutableList() + } +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun ByteArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun ShortArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun IntArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun LongArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun FloatArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun DoubleArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun BooleanArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +} + +/** + * Returns a [MutableList] filled with all elements of this array. + */ +public fun CharArray.toMutableList(): MutableList { + val list = ArrayList(size) + for (item in this) list.add(item) + return list +}