Fixes in ArrayList after review.
Provide better implementations for some methods of AbstractList. #KT-12386
This commit is contained in:
@@ -50,9 +50,14 @@ public abstract class AbstractList<E> protected constructor() : AbstractCollecti
|
|||||||
removeRange(0, size)
|
removeRange(0, size)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
override fun removeAll(elements: Collection<E>): Boolean = removeAll { it in elements }
|
||||||
|
override fun retainAll(elements: Collection<E>): Boolean = removeAll { it !in elements }
|
||||||
|
|
||||||
|
|
||||||
override fun iterator(): MutableIterator<E> = IteratorImpl()
|
override fun iterator(): MutableIterator<E> = IteratorImpl()
|
||||||
|
|
||||||
|
override fun contains(element: E): Boolean = indexOf(element) >= 0
|
||||||
|
|
||||||
override fun indexOf(element: E): Int {
|
override fun indexOf(element: E): Int {
|
||||||
for (index in 0..lastIndex) {
|
for (index in 0..lastIndex) {
|
||||||
if (get(index) == element) {
|
if (get(index) == element) {
|
||||||
|
|||||||
@@ -16,8 +16,6 @@
|
|||||||
|
|
||||||
package kotlin.collections
|
package kotlin.collections
|
||||||
|
|
||||||
//TODO: should be JS Array-like (https://developer.mozilla.org/en-US/docs/JavaScript/Guide/Predefined_Core_Objects#Working_with_Array-like_objects)
|
|
||||||
|
|
||||||
public open class ArrayList<E> internal constructor(private var array: Array<Any?>) : AbstractList<E>(), RandomAccess {
|
public open class ArrayList<E> internal constructor(private var array: Array<Any?>) : AbstractList<E>(), RandomAccess {
|
||||||
|
|
||||||
public constructor(capacity: Int = 0) : this(emptyArray()) {}
|
public constructor(capacity: Int = 0) : this(emptyArray()) {}
|
||||||
@@ -55,8 +53,12 @@ public open class ArrayList<E> internal constructor(private var array: Array<Any
|
|||||||
|
|
||||||
if (index == size) return addAll(elements)
|
if (index == size) return addAll(elements)
|
||||||
if (elements.isEmpty()) return false
|
if (elements.isEmpty()) return false
|
||||||
|
when (index) {
|
||||||
|
size -> return addAll(elements)
|
||||||
|
0 -> array = elements.toTypedArray<Any?>() + array
|
||||||
|
else -> array = array.copyOfRange(0, index).asDynamic().concat(elements.toTypedArray<Any?>(), array.copyOfRange(index, size))
|
||||||
|
}
|
||||||
|
|
||||||
array = array.copyOfRange(0, index) + elements.toTypedArray<Any?>() + array.copyOfRange(index, size)
|
|
||||||
modCount++
|
modCount++
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
@@ -64,7 +66,7 @@ public open class ArrayList<E> internal constructor(private var array: Array<Any
|
|||||||
override fun removeAt(index: Int): E {
|
override fun removeAt(index: Int): E {
|
||||||
rangeCheck(index)
|
rangeCheck(index)
|
||||||
modCount++
|
modCount++
|
||||||
return if (index == size)
|
return if (index == lastIndex)
|
||||||
array.asDynamic().pop()
|
array.asDynamic().pop()
|
||||||
else
|
else
|
||||||
array.asDynamic().splice(index, 1)[0]
|
array.asDynamic().splice(index, 1)[0]
|
||||||
@@ -91,7 +93,6 @@ public open class ArrayList<E> internal constructor(private var array: Array<Any
|
|||||||
modCount++
|
modCount++
|
||||||
}
|
}
|
||||||
|
|
||||||
override fun contains(element: E): Boolean = indexOf(element) >= 0
|
|
||||||
|
|
||||||
override fun indexOf(element: E): Int = array.indexOf(element)
|
override fun indexOf(element: E): Int = array.indexOf(element)
|
||||||
|
|
||||||
@@ -100,13 +101,12 @@ public open class ArrayList<E> internal constructor(private var array: Array<Any
|
|||||||
override fun toString() = arrayToString(array)
|
override fun toString() = arrayToString(array)
|
||||||
override fun toArray(): Array<Any?> = array.copyOf()
|
override fun toArray(): Array<Any?> = array.copyOf()
|
||||||
|
|
||||||
|
|
||||||
private fun rangeCheck(index: Int) = index.apply {
|
private fun rangeCheck(index: Int) = index.apply {
|
||||||
if (index !in array.indices)
|
checkElementIndex(index, size)
|
||||||
throw IndexOutOfBoundsException("Index: $index, Size: $size")
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun insertionRangeCheck(index: Int) = index.apply {
|
private fun insertionRangeCheck(index: Int) = index.apply {
|
||||||
if (index !in 0..size)
|
checkPositionIndex(index, size)
|
||||||
throw IndexOutOfBoundsException("Index: $index, Size: $size")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user