From e80af77c319df4f536954d224844103582c01987 Mon Sep 17 00:00:00 2001 From: Evgeny Gerashchenko Date: Fri, 6 Dec 2013 20:35:31 +0400 Subject: [PATCH] Added max() and min() for CharArrays. --- libraries/stdlib/src/generated/_CharArrays.kt | 28 +++++++++++++++++++ libraries/stdlib/test/ArraysJVMTest.kt | 2 ++ .../src/templates/Iterables.kt | 4 +-- 3 files changed, 32 insertions(+), 2 deletions(-) diff --git a/libraries/stdlib/src/generated/_CharArrays.kt b/libraries/stdlib/src/generated/_CharArrays.kt index 07b20d7f8d2..8276e4476f4 100644 --- a/libraries/stdlib/src/generated/_CharArrays.kt +++ b/libraries/stdlib/src/generated/_CharArrays.kt @@ -225,6 +225,20 @@ public inline fun > CharArray.mapTo(result: C, tra return result } +/** + * Returns the largest element or null if there are no elements + */ +public fun CharArray.max() : Char? { + if (isEmpty()) return null + + var max = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (max < e) max = e + } + return max +} + /** * Returns the first element yielding the largest value of the given function or null if there are no elements */ @@ -244,6 +258,20 @@ public inline fun > CharArray.maxBy(f: (Char) -> R) : Char? { return maxElem } +/** + * Returns the smallest element or null if there are no elements + */ +public fun CharArray.min() : Char? { + if (isEmpty()) return null + + var min = this[0] + for (i in 1..lastIndex) { + val e = this[i] + if (min > e) min = e + } + return min +} + /** * Returns the first element yielding the smallest value of the given function or null if there are no elements */ diff --git a/libraries/stdlib/test/ArraysJVMTest.kt b/libraries/stdlib/test/ArraysJVMTest.kt index 5c8d0c43b39..37a5c338a04 100644 --- a/libraries/stdlib/test/ArraysJVMTest.kt +++ b/libraries/stdlib/test/ArraysJVMTest.kt @@ -92,6 +92,7 @@ class ArraysJVMTest { expect(2, { shortArray(3, 2).min() }) expect(2.0.toFloat(), { floatArray(3.0.toFloat(), 2.0.toFloat()).min() }) expect(2.0, { doubleArray(2.0, 3.0).min() }) + expect('a', { charArray('a', 'b').min() }) } test fun max() { @@ -103,6 +104,7 @@ class ArraysJVMTest { expect(3, { shortArray(3, 2).max() }) expect(3.0.toFloat(), { floatArray(3.0.toFloat(), 2.0.toFloat()).max() }) expect(3.0, { doubleArray(2.0, 3.0).max() }) + expect('b', { charArray('a', 'b').max() }) } test fun minBy() { diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt index 61c0dd11957..0d966ce060e 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Iterables.kt @@ -149,7 +149,7 @@ fun iterables(): ArrayList { templates add f("min()") { doc = "Returns the smallest element or null if there are no elements" returns("T?") - absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251) + absentFor(PrimitiveType.Boolean) typeParam("T: Comparable") isInline = false Iterables.body { @@ -184,7 +184,7 @@ fun iterables(): ArrayList { templates add f("max()") { doc = "Returns the largest element or null if there are no elements" returns("T?") - absentFor(PrimitiveType.Boolean, PrimitiveType.Char)//currently there are no sane way to compare Char? with something (KT-4251) + absentFor(PrimitiveType.Boolean) typeParam("T: Comparable") isInline = false Iterables.body {