KT-54739 Return single empty inst from collection builders(all src-sets)

Fixed remaining builders, added additional checks to a test.

^KT-54739 fixed
This commit is contained in:
Filipp Zhinkin
2023-04-20 15:10:11 +02:00
committed by Space Team
parent 0c1d957711
commit cadf4f63b0
7 changed files with 125 additions and 12 deletions
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@@ -13,6 +13,9 @@ actual class ArrayList<E> private constructor(
private val backingList: ArrayList<E>?,
private val root: ArrayList<E>?
) : MutableList<E>, RandomAccess, AbstractMutableList<E>() {
private companion object {
private val Empty = ArrayList<Nothing>(0).also { it.isReadOnly = true }
}
actual constructor() : this(10)
@@ -28,7 +31,7 @@ actual class ArrayList<E> private constructor(
if (backingList != null) throw IllegalStateException() // just in case somebody casts subList to ArrayList
checkIsMutable()
isReadOnly = true
return this
return if (length > 0) this else Empty
}
override actual val size: Int
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@@ -52,7 +52,7 @@ actual class HashMap<K, V> private constructor(
internal fun build(): Map<K, V> {
checkIsMutable()
isReadOnly = true
return this
return if (size > 0) this else EmptyHolder.value()
}
override actual fun isEmpty(): Boolean = _size == 0
@@ -488,6 +488,15 @@ actual class HashMap<K, V> private constructor(
private fun computeShift(hashSize: Int): Int = hashSize.countLeadingZeroBits() + 1
}
internal object EmptyHolder {
val value_ = HashMap<Nothing, Nothing>(0).also { it.isReadOnly = true }
fun <K, V> value(): HashMap<K, V> {
@Suppress("UNCHECKED_CAST")
return value_ as HashMap<K, V>
}
}
internal open class Itr<K, V>(
internal val map: HashMap<K, V>
) {
@@ -1,5 +1,5 @@
/*
* Copyright 2010-2018 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* Copyright 2010-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
* that can be found in the LICENSE file.
*/
@@ -8,6 +8,9 @@ package kotlin.collections
actual class HashSet<E> internal constructor(
private val backing: HashMap<E, *>
) : MutableSet<E>, kotlin.native.internal.KonanSet<E>, AbstractMutableSet<E>() {
private companion object {
private val Empty = HashSet(HashMap.EmptyHolder.value<Nothing, Nothing>())
}
actual constructor() : this(HashMap<E, Nothing>())
@@ -23,7 +26,7 @@ actual class HashSet<E> internal constructor(
@PublishedApi
internal fun build(): Set<E> {
backing.build()
return this
return if (size > 0) this else Empty
}
override actual val size: Int get() = backing.size