Do not use indexed access for lists not supporting RandomAccess

Add RandomAccess specialization.
This commit is contained in:
Ilya Gorbunov
2016-04-22 20:54:40 +03:00
parent f4f82656f7
commit 09c1ff1233
3 changed files with 15 additions and 4 deletions
@@ -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]