[K/N] Use iterator in AbstractMutableList.indexOf/lastIndexOf
Instead of access by index. To avoid performance issues when the subclass `get(index)` has non-constant time complexity. It also aligns behavior with JVM.
This commit is contained in:
committed by
Space Team
parent
60cafc6623
commit
3be613ced8
@@ -67,23 +67,9 @@ public actual abstract class AbstractMutableList<E> protected actual constructor
|
||||
|
||||
actual override fun contains(element: E): Boolean = indexOf(element) >= 0
|
||||
|
||||
actual override fun indexOf(element: E): Int {
|
||||
for (index in 0..lastIndex) {
|
||||
if (get(index) == element) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
actual override fun indexOf(element: E): Int = indexOfFirst { it == element }
|
||||
|
||||
actual override fun lastIndexOf(element: E): Int {
|
||||
for (index in lastIndex downTo 0) {
|
||||
if (get(index) == element) {
|
||||
return index
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
actual override fun lastIndexOf(element: E): Int = indexOfLast { it == element }
|
||||
|
||||
actual override fun listIterator(): MutableListIterator<E> = listIterator(0)
|
||||
actual override fun listIterator(index: Int): MutableListIterator<E> = ListIteratorImpl(index)
|
||||
|
||||
Reference in New Issue
Block a user