[K/N] Use AbstractList.newCapacity in HashMap.ensureCapacity
This commit is contained in:
committed by
Space Team
parent
2ef50f8c34
commit
af7965d996
@@ -196,11 +196,10 @@ internal class MapBuilder<K, V> private constructor(
|
|||||||
&& gaps >= this.capacity / 4 // at least 25% of current capacity is occupied by gaps
|
&& gaps >= this.capacity / 4 // at least 25% of current capacity is occupied by gaps
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureCapacity(capacity: Int) {
|
private fun ensureCapacity(minCapacity: Int) {
|
||||||
if (capacity < 0) throw OutOfMemoryError() // overflow
|
if (minCapacity < 0) throw OutOfMemoryError() // overflow
|
||||||
if (capacity > this.capacity) {
|
if (minCapacity > this.capacity) {
|
||||||
var newSize = this.capacity * 3 / 2
|
val newSize = AbstractList.newCapacity(this.capacity, minCapacity)
|
||||||
if (capacity > newSize) newSize = capacity
|
|
||||||
keysArray = keysArray.copyOfUninitializedElements(newSize)
|
keysArray = keysArray.copyOfUninitializedElements(newSize)
|
||||||
valuesArray = valuesArray?.copyOfUninitializedElements(newSize)
|
valuesArray = valuesArray?.copyOfUninitializedElements(newSize)
|
||||||
presenceArray = presenceArray.copyOf(newSize)
|
presenceArray = presenceArray.copyOf(newSize)
|
||||||
|
|||||||
@@ -193,11 +193,10 @@ actual class HashMap<K, V> private constructor(
|
|||||||
&& gaps >= this.capacity / 4 // at least 25% of current capacity is occupied by gaps
|
&& gaps >= this.capacity / 4 // at least 25% of current capacity is occupied by gaps
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun ensureCapacity(capacity: Int) {
|
private fun ensureCapacity(minCapacity: Int) {
|
||||||
if (capacity < 0) throw OutOfMemoryError() // overflow
|
if (minCapacity < 0) throw OutOfMemoryError() // overflow
|
||||||
if (capacity > this.capacity) {
|
if (minCapacity > this.capacity) {
|
||||||
var newSize = this.capacity * 3 / 2
|
val newSize = AbstractList.newCapacity(this.capacity, minCapacity)
|
||||||
if (capacity > newSize) newSize = capacity
|
|
||||||
keysArray = keysArray.copyOfUninitializedElements(newSize)
|
keysArray = keysArray.copyOfUninitializedElements(newSize)
|
||||||
valuesArray = valuesArray?.copyOfUninitializedElements(newSize)
|
valuesArray = valuesArray?.copyOfUninitializedElements(newSize)
|
||||||
presenceArray = presenceArray.copyOf(newSize)
|
presenceArray = presenceArray.copyOf(newSize)
|
||||||
|
|||||||
Reference in New Issue
Block a user