Move ArrayDeque.newCapacity to AbstractList

This commit is contained in:
Abduqodiri Qurbonzoda
2023-05-30 21:54:19 +03:00
committed by Space Team
parent d8cfc79d92
commit 2ef50f8c34
6 changed files with 67 additions and 53 deletions
@@ -133,6 +133,19 @@ public abstract class AbstractList<out E> protected constructor() : AbstractColl
}
}
private const val maxArraySize = Int.MAX_VALUE - 8
/** [oldCapacity] and [minCapacity] must be non-negative. */
internal fun newCapacity(oldCapacity: Int, minCapacity: Int): Int {
// overflow-conscious
var newCapacity = oldCapacity + (oldCapacity shr 1)
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity
if (newCapacity - maxArraySize > 0)
newCapacity = if (minCapacity > maxArraySize) Int.MAX_VALUE else maxArraySize
return newCapacity
}
internal fun orderedHashCode(c: Collection<*>): Int {
var hashCode = 1
for (e in c) {
@@ -63,7 +63,7 @@ public class ArrayDeque<E> : AbstractMutableList<E> {
return
}
val newCapacity = newCapacity(elementData.size, minCapacity)
val newCapacity = AbstractList.newCapacity(elementData.size, minCapacity)
copyElements(newCapacity)
}
@@ -560,18 +560,7 @@ public class ArrayDeque<E> : AbstractMutableList<E> {
internal companion object {
private val emptyElementData = emptyArray<Any?>()
private const val maxArraySize = Int.MAX_VALUE - 8
private const val defaultMinCapacity = 10
internal fun newCapacity(oldCapacity: Int, minCapacity: Int): Int {
// overflow-conscious
var newCapacity = oldCapacity + (oldCapacity shr 1)
if (newCapacity - minCapacity < 0)
newCapacity = minCapacity
if (newCapacity - maxArraySize > 0)
newCapacity = if (minCapacity > maxArraySize) Int.MAX_VALUE else maxArraySize
return newCapacity
}
}
// For testing only