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()
|
||||
list = ArrayList<T>(resultSize)
|
||||
if (this is List<T>) {
|
||||
for (item in listIterator(n)) {
|
||||
list.add(item)
|
||||
if (this is RandomAccess) {
|
||||
for (index in n..size - 1)
|
||||
list.add(this[index])
|
||||
} else {
|
||||
for (item in listIterator(n))
|
||||
list.add(item)
|
||||
}
|
||||
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)
|
||||
|
||||
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
|
||||
for (readIndex in 0..lastIndex) {
|
||||
val element = this[readIndex]
|
||||
|
||||
Reference in New Issue
Block a user