KT-4347 add Array extension indexOf to stdlib

This commit is contained in:
Jon Renner
2013-12-26 13:48:58 +08:00
committed by Andrey Breslav
parent 0a552dbb04
commit 6781231411
12 changed files with 208 additions and 0 deletions
+20
View File
@@ -198,6 +198,26 @@ public inline fun <T, K> Array<out T>.groupByTo(result: MutableMap<K, MutableLis
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun <T> Array<out T>.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
@@ -183,6 +183,18 @@ public inline fun <K> BooleanArray.groupByTo(result: MutableMap<K, MutableList<B
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun BooleanArray.indexOf(item: Boolean) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
@@ -183,6 +183,18 @@ public inline fun <K> ByteArray.groupByTo(result: MutableMap<K, MutableList<Byte
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun ByteArray.indexOf(item: Byte) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
@@ -183,6 +183,18 @@ public inline fun <K> CharArray.groupByTo(result: MutableMap<K, MutableList<Char
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun CharArray.indexOf(item: Char) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
@@ -183,6 +183,18 @@ public inline fun <K> DoubleArray.groupByTo(result: MutableMap<K, MutableList<Do
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun DoubleArray.indexOf(item: Double) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
@@ -183,6 +183,18 @@ public inline fun <K> FloatArray.groupByTo(result: MutableMap<K, MutableList<Flo
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun FloatArray.indexOf(item: Float) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
@@ -183,6 +183,18 @@ public inline fun <K> IntArray.groupByTo(result: MutableMap<K, MutableList<Int>>
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
*/
@@ -183,6 +183,18 @@ public inline fun <K> LongArray.groupByTo(result: MutableMap<K, MutableList<Long
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun LongArray.indexOf(item: Long) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
@@ -183,6 +183,18 @@ public inline fun <K> ShortArray.groupByTo(result: MutableMap<K, MutableList<Sho
return result
}
/**
* Returns first index of item, or -1 if the array does not contain item
*/
public fun ShortArray.indexOf(item: Short) : Int {
for (i in indices) {
if (item == this[i]) {
return i
}
}
return -1
}
/**
* Returns true if the array is empty
*/
+41
View File
@@ -59,6 +59,47 @@ class ArraysJVMTest {
}
}
test fun indexOf() {
expect(-1) { byteArray(1, 2, 3) indexOf 0 }
expect(0) { byteArray(1, 2, 3) indexOf 1 }
expect(1) { byteArray(1, 2, 3) indexOf 2 }
expect(2) { byteArray(1, 2, 3) indexOf 3 }
expect(-1) { shortArray(1, 2, 3) indexOf 0 }
expect(0) { shortArray(1, 2, 3) indexOf 1 }
expect(1) { shortArray(1, 2, 3) indexOf 2 }
expect(2) { shortArray(1, 2, 3) indexOf 3 }
expect(-1) { intArray(1, 2, 3) indexOf 0 }
expect(0) { intArray(1, 2, 3) indexOf 1 }
expect(1) { intArray(1, 2, 3) indexOf 2 }
expect(2) { intArray(1, 2, 3) indexOf 3 }
expect(-1) { longArray(1, 2, 3) indexOf 0 }
expect(0) { longArray(1, 2, 3) indexOf 1 }
expect(1) { longArray(1, 2, 3) indexOf 2 }
expect(2) { longArray(1, 2, 3) indexOf 3 }
expect(-1) { floatArray(1.0f, 2.0f, 3.0f) indexOf 0f }
expect(0) { floatArray(1.0f, 2.0f, 3.0f) indexOf 1.0f }
expect(1) { floatArray(1.0f, 2.0f, 3.0f) indexOf 2.0f }
expect(2) { floatArray(1.0f, 2.0f, 3.0f) indexOf 3.0f }
expect(-1) { doubleArray(1.0, 2.0, 3.0) indexOf 0.0 }
expect(0) { doubleArray(1.0, 2.0, 3.0) indexOf 1.0 }
expect(1) { doubleArray(1.0, 2.0, 3.0) indexOf 2.0 }
expect(2) { doubleArray(1.0, 2.0, 3.0) indexOf 3.0 }
expect(-1) { charArray('a', 'b', 'c') indexOf 'z' }
expect(0) { charArray('a', 'b', 'c') indexOf 'a' }
expect(1) { charArray('a', 'b', 'c') indexOf 'b' }
expect(2) { charArray('a', 'b', 'c') indexOf 'c' }
expect(0) { booleanArray(true, false) indexOf true }
expect(1) { booleanArray(true, false) indexOf false }
expect(-1) { booleanArray(true) indexOf false }
}
test fun isEmpty() {
assertTrue(intArray().isEmpty())
assertFalse(intArray(1).isEmpty())
+8
View File
@@ -157,6 +157,14 @@ class ArraysTest {
//expect(3000000000000) { array<Long>(1000000000000, 2000000000000).sum() }
expect(3.0.toFloat()) { array<Float>(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