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
*/