From 678123141128724af2b0eca0bb2688840ff8b24b Mon Sep 17 00:00:00 2001 From: Jon Renner Date: Thu, 26 Dec 2013 13:48:58 +0800 Subject: [PATCH] KT-4347 add Array extension indexOf to stdlib --- libraries/stdlib/src/generated/_Arrays.kt | 20 +++++++++ .../stdlib/src/generated/_BooleanArrays.kt | 12 ++++++ libraries/stdlib/src/generated/_ByteArrays.kt | 12 ++++++ libraries/stdlib/src/generated/_CharArrays.kt | 12 ++++++ .../stdlib/src/generated/_DoubleArrays.kt | 12 ++++++ .../stdlib/src/generated/_FloatArrays.kt | 12 ++++++ libraries/stdlib/src/generated/_IntArrays.kt | 12 ++++++ libraries/stdlib/src/generated/_LongArrays.kt | 12 ++++++ .../stdlib/src/generated/_ShortArrays.kt | 12 ++++++ libraries/stdlib/test/ArraysJVMTest.kt | 41 ++++++++++++++++++ libraries/stdlib/test/ArraysTest.kt | 8 ++++ .../kotlin-stdlib-gen/src/templates/Arrays.kt | 43 +++++++++++++++++++ 12 files changed, 208 insertions(+) diff --git a/libraries/stdlib/src/generated/_Arrays.kt b/libraries/stdlib/src/generated/_Arrays.kt index c013faef20b..6885247e85f 100644 --- a/libraries/stdlib/src/generated/_Arrays.kt +++ b/libraries/stdlib/src/generated/_Arrays.kt @@ -198,6 +198,26 @@ public inline fun Array.groupByTo(result: MutableMap Array.indexOf(item: T) : Int { + if (item == null) { + for (i in indices) { + if (this[i] == null) { + return i + } + } + } else { + for (i in indices) { + if (item == this[i]) { + return i + } + } + } + return -1 +} + /** * Creates a string from all the elements separated using the *separator* and using the given *prefix* and *postfix* if supplied. * If a collection could be huge you can specify a non-negative value of *limit* which will only show a subset of the collection then it will diff --git a/libraries/stdlib/src/generated/_BooleanArrays.kt b/libraries/stdlib/src/generated/_BooleanArrays.kt index 0b061b1b506..ae012f75959 100644 --- a/libraries/stdlib/src/generated/_BooleanArrays.kt +++ b/libraries/stdlib/src/generated/_BooleanArrays.kt @@ -183,6 +183,18 @@ public inline fun BooleanArray.groupByTo(result: MutableMap ByteArray.groupByTo(result: MutableMap CharArray.groupByTo(result: MutableMap DoubleArray.groupByTo(result: MutableMap FloatArray.groupByTo(result: MutableMap IntArray.groupByTo(result: MutableMap> return result } +/** + * Returns first index of item, or -1 if the array does not contain item + */ +public fun IntArray.indexOf(item: Int) : Int { + for (i in indices) { + if (item == this[i]) { + return i + } + } + return -1 +} + /** * Returns true if the array is empty */ diff --git a/libraries/stdlib/src/generated/_LongArrays.kt b/libraries/stdlib/src/generated/_LongArrays.kt index 09a01e56d48..26595284b07 100644 --- a/libraries/stdlib/src/generated/_LongArrays.kt +++ b/libraries/stdlib/src/generated/_LongArrays.kt @@ -183,6 +183,18 @@ public inline fun LongArray.groupByTo(result: MutableMap ShortArray.groupByTo(result: MutableMap(1000000000000, 2000000000000).sum() } expect(3.0.toFloat()) { array(1.0.toFloat(), 2.0.toFloat()).sum() } } + + test fun indexOf() { + expect(-1) { array("cat", "dog", "bird").indexOf("mouse") } + expect(0) { array("cat", "dog", "bird").indexOf("cat") } + expect(1) { array("cat", "dog", "bird").indexOf("dog") } + expect(2) { array("cat", "dog", "bird").indexOf("bird") } + expect(0) { array(null, "dog", null).indexOf(null)} + } /* TODO FIXME ASAP: These currently fail on JS due to missing upto() method on numbers diff --git a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt index 1d93c7b698d..aa8b7841136 100644 --- a/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt +++ b/libraries/tools/kotlin-stdlib-gen/src/templates/Arrays.kt @@ -25,5 +25,48 @@ fun arrays(): List { } } + templates add f("indexOf(item: T)") { + absentFor(PrimitiveArrays) + isInline = false + doc = "Returns first index of item, or -1 if the array does not contain item" + returns("Int") + body { + """ + if (item == null) { + for (i in indices) { + if (this[i] == null) { + return i + } + } + } else { + for (i in indices) { + if (item == this[i]) { + return i + } + } + } + return -1 + """ + } + } + + // implementation for PrimitiveArrays is separate from Arrays, because they cannot hold null elements + templates add f("indexOf(item: T)") { + absentFor(Arrays) + isInline = false + doc = "Returns first index of item, or -1 if the array does not contain item" + returns("Int") + body { + """ + for (i in indices) { + if (item == this[i]) { + return i + } + } + return -1 + """ + } + } + return templates.sort() } \ No newline at end of file