Do not use indexed access for lists not supporting RandomAccess
Add RandomAccess specialization.
This commit is contained in:
@@ -550,8 +550,12 @@ public fun <T> Iterable<T>.drop(n: Int): List<T> {
|
|||||||
return emptyList()
|
return emptyList()
|
||||||
list = ArrayList<T>(resultSize)
|
list = ArrayList<T>(resultSize)
|
||||||
if (this is List<T>) {
|
if (this is List<T>) {
|
||||||
for (item in listIterator(n)) {
|
if (this is RandomAccess) {
|
||||||
list.add(item)
|
for (index in n..size - 1)
|
||||||
|
list.add(this[index])
|
||||||
|
} else {
|
||||||
|
for (item in listIterator(n))
|
||||||
|
list.add(item)
|
||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -177,6 +177,9 @@ public fun <T> MutableList<T>.removeAll(predicate: (T) -> Boolean): Boolean = fi
|
|||||||
public fun <T> MutableList<T>.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)
|
public fun <T> MutableList<T>.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)
|
||||||
|
|
||||||
private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
|
private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
|
||||||
|
if (this !is RandomAccess)
|
||||||
|
return (this as MutableIterable<T>).filterInPlace(predicate, predicateResultToRemove)
|
||||||
|
|
||||||
var writeIndex: Int = 0
|
var writeIndex: Int = 0
|
||||||
for (readIndex in 0..lastIndex) {
|
for (readIndex in 0..lastIndex) {
|
||||||
val element = this[readIndex]
|
val element = this[readIndex]
|
||||||
|
|||||||
@@ -49,8 +49,12 @@ fun filtering(): List<GenericFunction> {
|
|||||||
|
|
||||||
list = ArrayList<T>(resultSize)
|
list = ArrayList<T>(resultSize)
|
||||||
if (this is List<T>) {
|
if (this is List<T>) {
|
||||||
for (item in listIterator(n)) {
|
if (this is RandomAccess) {
|
||||||
list.add(item)
|
for (index in n..size - 1)
|
||||||
|
list.add(this[index])
|
||||||
|
} else {
|
||||||
|
for (item in listIterator(n))
|
||||||
|
list.add(item)
|
||||||
}
|
}
|
||||||
return list
|
return list
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user