Implemented indexOfFirst/Last(predicate). #KT-6577 Fixed
This commit is contained in:
committed by
Ilya Ryzhenkov
parent
38a4f86db2
commit
c8094e7587
@@ -1162,6 +1162,324 @@ public fun <T> Stream<T>.indexOf(element: T): Int {
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> Array<out T>.indexOfFirst(predicate: (T) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun BooleanArray.indexOfFirst(predicate: (Boolean) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun ByteArray.indexOfFirst(predicate: (Byte) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun CharArray.indexOfFirst(predicate: (Char) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun DoubleArray.indexOfFirst(predicate: (Double) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun FloatArray.indexOfFirst(predicate: (Float) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun IntArray.indexOfFirst(predicate: (Int) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun LongArray.indexOfFirst(predicate: (Long) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun ShortArray.indexOfFirst(predicate: (Short) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.indexOfFirst(predicate: (T) -> Boolean): Int {
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
if (predicate(item))
|
||||
return index
|
||||
index++
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> List<T>.indexOfFirst(predicate: (T) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> Stream<T>.indexOfFirst(predicate: (T) -> Boolean): Int {
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
if (predicate(item))
|
||||
return index
|
||||
index++
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the first element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun String.indexOfFirst(predicate: (Char) -> Boolean): Int {
|
||||
for (index in indices) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> Array<out T>.indexOfLast(predicate: (T) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun BooleanArray.indexOfLast(predicate: (Boolean) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun ByteArray.indexOfLast(predicate: (Byte) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun CharArray.indexOfLast(predicate: (Char) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun DoubleArray.indexOfLast(predicate: (Double) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun FloatArray.indexOfLast(predicate: (Float) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun IntArray.indexOfLast(predicate: (Int) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun LongArray.indexOfLast(predicate: (Long) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun ShortArray.indexOfLast(predicate: (Short) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> Iterable<T>.indexOfLast(predicate: (T) -> Boolean): Int {
|
||||
var lastIndex = -1
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
if (predicate(item))
|
||||
lastIndex = index
|
||||
index++
|
||||
}
|
||||
return lastIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> List<T>.indexOfLast(predicate: (T) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun <T> Stream<T>.indexOfLast(predicate: (T) -> Boolean): Int {
|
||||
var lastIndex = -1
|
||||
var index = 0
|
||||
for (item in this) {
|
||||
if (predicate(item))
|
||||
lastIndex = index
|
||||
index++
|
||||
}
|
||||
return lastIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns index of the last element matching the given [predicate], or -1 if the collection does not contain such element
|
||||
*/
|
||||
public inline fun String.indexOfLast(predicate: (Char) -> Boolean): Int {
|
||||
for (index in indices.reversed()) {
|
||||
if (predicate(this[index])) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the last element.
|
||||
* @throws NoSuchElementException if the collection is empty.
|
||||
@@ -1644,26 +1962,6 @@ public fun <T> Iterable<T>.lastIndexOf(element: T): Int {
|
||||
return lastIndex
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last index of *element*, or -1 if the collection does not contain element
|
||||
*/
|
||||
public fun <T> List<T>.lastIndexOf(element: T): Int {
|
||||
if (element == null) {
|
||||
for (index in indices.reverse()) {
|
||||
if (this[index] == null) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
} else {
|
||||
for (index in indices.reverse()) {
|
||||
if (element == this[index]) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns last index of *element*, or -1 if the collection does not contain element
|
||||
*/
|
||||
|
||||
@@ -165,7 +165,37 @@ class ArraysTest {
|
||||
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) }
|
||||
expect(0) { array(null, "dog", null).indexOf(null : String?)}
|
||||
|
||||
expect(-1) { array("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { array("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { array("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { array("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
|
||||
expect(-1) { streamOf("cat", "dog", "bird").indexOfFirst { it.contains("p") } }
|
||||
expect(0) { streamOf("cat", "dog", "bird").indexOfFirst { it.startsWith('c') } }
|
||||
expect(1) { streamOf("cat", "dog", "bird").indexOfFirst { it.startsWith('d') } }
|
||||
expect(2) { streamOf("cat", "dog", "bird").indexOfFirst { it.endsWith('d') } }
|
||||
}
|
||||
|
||||
test fun lastIndexOf() {
|
||||
expect(-1) { array("cat", "dog", "bird").lastIndexOf("mouse") }
|
||||
expect(0) { array("cat", "dog", "bird").lastIndexOf("cat") }
|
||||
expect(1) { array("cat", "dog", "bird").lastIndexOf("dog") }
|
||||
expect(2) { array(null, "dog", null).lastIndexOf(null : String?)}
|
||||
expect(3) { array("cat", "dog", "bird", "dog").lastIndexOf("dog") }
|
||||
|
||||
expect(-1) { array("cat", "dog", "bird").indexOfLast { it.contains("p") } }
|
||||
expect(0) { array("cat", "dog", "bird").indexOfLast { it.startsWith('c') } }
|
||||
expect(2) { array("cat", "dog", "cap", "bird").indexOfLast { it.startsWith('c') } }
|
||||
expect(2) { array("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
|
||||
expect(3) { array("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
|
||||
|
||||
expect(-1) { streamOf("cat", "dog", "bird").indexOfLast { it.contains("p") } }
|
||||
expect(0) { streamOf("cat", "dog", "bird").indexOfLast { it.startsWith('c') } }
|
||||
expect(2) { streamOf("cat", "dog", "cap", "bird").indexOfLast { it.startsWith('c') } }
|
||||
expect(2) { streamOf("cat", "dog", "bird").indexOfLast { it.endsWith('d') } }
|
||||
expect(3) { streamOf("cat", "dog", "bird", "red").indexOfLast { it.endsWith('d') } }
|
||||
}
|
||||
|
||||
test fun plus() {
|
||||
|
||||
@@ -1,8 +1,9 @@
|
||||
package test.collections
|
||||
|
||||
import org.junit.Test
|
||||
import java.util.ArrayList
|
||||
import java.util.LinkedHashSet
|
||||
import kotlin.test.*
|
||||
import java.util.*
|
||||
|
||||
fun <T> iterableOf(vararg items: T): Iterable<T> = IterableWrapper(items.toList())
|
||||
|
||||
@@ -35,6 +36,18 @@ abstract class OrderedIterableTests<T : Iterable<String>>(data: T, empty: T) : I
|
||||
expect(-1) { data.lastIndexOf("zap") }
|
||||
}
|
||||
|
||||
Test fun indexOfFirst() {
|
||||
expect(-1) { data.indexOfFirst { it.contains("p") } }
|
||||
expect(0) { data.indexOfFirst { it.startsWith('f') } }
|
||||
expect(-1) { empty.indexOfFirst { it.startsWith('f') } }
|
||||
}
|
||||
|
||||
Test fun indexOfLast() {
|
||||
expect(-1) { data.indexOfLast { it.contains("p") } }
|
||||
expect(1) { data.indexOfLast { it.length() == 3 } }
|
||||
expect(-1) { empty.indexOfLast { it.startsWith('f') } }
|
||||
}
|
||||
|
||||
Test fun elementAt() {
|
||||
expect("foo") { data.elementAt(0) }
|
||||
expect("bar") { data.elementAt(1) }
|
||||
|
||||
Reference in New Issue
Block a user