[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:
Abduqodiri Qurbonzoda
2023-06-02 16:48:00 +03:00
committed by Space Team
parent 60cafc6623
commit 3be613ced8
2 changed files with 4 additions and 32 deletions
@@ -58,23 +58,9 @@ public actual abstract class AbstractMutableList<E> protected actual constructor
override actual fun contains(element: E): Boolean = indexOf(element) >= 0
override actual fun indexOf(element: E): Int {
for (index in 0..lastIndex) {
if (get(index) == element) {
return index
}
}
return -1
}
override actual fun indexOf(element: E): Int = indexOfFirst { it == element }
override actual fun lastIndexOf(element: E): Int {
for (index in lastIndex downTo 0) {
if (get(index) == element) {
return index
}
}
return -1
}
override actual fun lastIndexOf(element: E): Int = indexOfLast { it == element }
override actual fun listIterator(): MutableListIterator<E> = listIterator(0)
override actual fun listIterator(index: Int): MutableListIterator<E> = ListIteratorImpl(index)