kotlin.collections.Sets.kt Maven / Gradle / Ivy
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("SetsKt")
package kotlin.collections
import java.io.Serializable
import java.util.*
internal object EmptySet : Set, Serializable {
private const val serialVersionUID: Long = 3406603774387020532
override fun equals(other: Any?): Boolean = other is Set<*> && other.isEmpty()
override fun hashCode(): Int = 0
override fun toString(): String = "[]"
override val size: Int get() = 0
override fun isEmpty(): Boolean = true
override fun contains(element: Nothing): Boolean = false
override fun containsAll(elements: Collection): Boolean = elements.isEmpty()
override fun iterator(): Iterator = EmptyIterator
private fun readResolve(): Any = EmptySet
}
/** Returns an empty read-only set. The returned set is serializable (JVM). */
public fun emptySet(): Set = EmptySet
/**
* Returns a new read-only set with the given elements.
* Elements of the set are iterated in the order they were specified.
* The returned set is serializable (JVM).
*/
public fun setOf(vararg elements: T): Set = if (elements.size > 0) elements.toSet() else emptySet()
/** Returns an empty read-only set. The returned set is serializable (JVM). */
@kotlin.internal.InlineOnly
public inline fun setOf(): Set = emptySet()
/**
* Returns a new [MutableSet] with the given elements.
* Elements of the set are iterated in the order they were specified.
*/
public fun mutableSetOf(vararg elements: T): MutableSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))
/** Returns a new [HashSet] with the given elements. */
public fun hashSetOf(vararg elements: T): HashSet = elements.toCollection(HashSet(mapCapacity(elements.size)))
/**
* Returns a new [LinkedHashSet] with the given elements.
* Elements of the set are iterated in the order they were specified.
*/
public fun linkedSetOf(vararg elements: T): LinkedHashSet = elements.toCollection(LinkedHashSet(mapCapacity(elements.size)))
/** Returns this Set if it's not `null` and the empty set otherwise. */
@kotlin.internal.InlineOnly
public inline fun Set?.orEmpty(): Set = this ?: emptySet()
/* Not available on platform: JS */
/* Not available on platform: JS */
/* Not available on platform: JS */
internal fun Set.optimizeReadOnlySet() = when (size) {
0 -> emptySet()
1 -> setOf(iterator().next())
else -> this
}