[K/N] Use AbstractList.newCapacity in HashMap.ensureCapacity

This commit is contained in:
Abduqodiri Qurbonzoda
2023-05-30 22:20:47 +03:00
committed by Space Team
parent 2ef50f8c34
commit af7965d996
2 changed files with 8 additions and 10 deletions
@@ -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
}
private fun ensureCapacity(capacity: Int) {
if (capacity < 0) throw OutOfMemoryError() // overflow
if (capacity > this.capacity) {
var newSize = this.capacity * 3 / 2
if (capacity > newSize) newSize = capacity
private fun ensureCapacity(minCapacity: Int) {
if (minCapacity < 0) throw OutOfMemoryError() // overflow
if (minCapacity > this.capacity) {
val newSize = AbstractList.newCapacity(this.capacity, minCapacity)
keysArray = keysArray.copyOfUninitializedElements(newSize)
valuesArray = valuesArray?.copyOfUninitializedElements(newSize)
presenceArray = presenceArray.copyOf(newSize)