[stdlib] Explicit visibility and return types: Collections

This commit is contained in:
Ilya Gorbunov
2023-11-12 02:02:06 +01:00
committed by Space Team
parent 0dd61c9f81
commit aa4419b7e3
23 changed files with 124 additions and 83 deletions
@@ -6,7 +6,7 @@
package kotlin.collections package kotlin.collections
/** /**
* Returns a *typed* array containing all of the elements of this collection. * Returns a *typed* array containing all the elements of this collection.
* *
* Allocates an array of runtime type `T` having its size equal to the size of this collection * Allocates an array of runtime type `T` having its size equal to the size of this collection
* and populates the array with the elements of this collection. * and populates the array with the elements of this collection.
@@ -5,12 +5,12 @@
package kotlin.collections package kotlin.collections
expect class ArrayList<E> : MutableList<E>, RandomAccess { public expect class ArrayList<E> : MutableList<E>, RandomAccess {
/** /**
* Creates a new empty [ArrayList]. * Creates a new empty [ArrayList].
*/ */
constructor() public constructor()
/** /**
* Creates a new empty [ArrayList] with the specified initial capacity. * Creates a new empty [ArrayList] with the specified initial capacity.
@@ -24,17 +24,17 @@ expect class ArrayList<E> : MutableList<E>, RandomAccess {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
constructor(initialCapacity: Int) public constructor(initialCapacity: Int)
/** /**
* Creates a new [ArrayList] filled with the elements of the specified collection. * Creates a new [ArrayList] filled with the elements of the specified collection.
* *
* The iteration order of elements in the created list is the same as in the specified collection. * The iteration order of elements in the created list is the same as in the specified collection.
*/ */
constructor(elements: Collection<E>) public constructor(elements: Collection<E>)
fun trimToSize() public fun trimToSize()
fun ensureCapacity(minCapacity: Int) public fun ensureCapacity(minCapacity: Int)
// From List // From List
@@ -5,28 +5,66 @@
package kotlin.collections package kotlin.collections
expect interface RandomAccess /**
* Marker interface indicating that the [List] implementation supports fast indexed access.
*/
public expect interface RandomAccess
/** /**
* Returns the array if it's not `null`, or an empty array otherwise. * Returns the array if it's not `null`, or an empty array otherwise.
* @sample samples.collections.Arrays.Usage.arrayOrEmpty * @sample samples.collections.Arrays.Usage.arrayOrEmpty
*/ */
expect inline fun <reified T> Array<out T>?.orEmpty(): Array<out T> public expect inline fun <reified T> Array<out T>?.orEmpty(): Array<out T>
/**
* Returns a *typed* array containing all the elements of this collection.
*
* Allocates an array of runtime type `T` having its size equal to the size of this collection
* and populates the array with the elements of this collection.
* @sample samples.collections.Collections.Collections.collectionToTypedArray
*/
public expect inline fun <reified T> Collection<T>.toTypedArray(): Array<T>
expect inline fun <reified T> Collection<T>.toTypedArray(): Array<T> /**
* Fills the list with the provided [value].
*
* Each element in the list gets replaced with the [value].
*/
@SinceKotlin("1.2") @SinceKotlin("1.2")
expect fun <T> MutableList<T>.fill(value: T): Unit public expect fun <T> MutableList<T>.fill(value: T): Unit
/**
* Randomly shuffles elements in this list.
*
* See: https://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#The_modern_algorithm
*/
@SinceKotlin("1.2") @SinceKotlin("1.2")
expect fun <T> MutableList<T>.shuffle(): Unit public expect fun <T> MutableList<T>.shuffle(): Unit
/**
* Returns a new list with the elements of this collection randomly shuffled.
*/
@SinceKotlin("1.2") @SinceKotlin("1.2")
expect fun <T> Iterable<T>.shuffled(): List<T> public expect fun <T> Iterable<T>.shuffled(): List<T>
expect fun <T : Comparable<T>> MutableList<T>.sort(): Unit /**
expect fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit * Sorts elements in the list in-place according to their natural sort order.
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*
* @sample samples.collections.Collections.Sorting.sortMutableList
*/
public expect fun <T : Comparable<T>> MutableList<T>.sort(): Unit
/**
* Sorts elements in the list in-place according to the order specified with [comparator].
*
* The sort is _stable_. It means that equal elements preserve their order relative to each other after sorting.
*
* @sample samples.collections.Collections.Sorting.sortMutableListWith
*/
public expect fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit
// from Grouping.kt // from Grouping.kt
@@ -5,11 +5,11 @@
package kotlin.collections package kotlin.collections
expect class HashMap<K, V> : MutableMap<K, V> { public expect class HashMap<K, V> : MutableMap<K, V> {
/** /**
* Creates a new empty [HashMap]. * Creates a new empty [HashMap].
*/ */
constructor() public constructor()
/** /**
* Creates a new empty [HashMap] with the specified initial capacity. * Creates a new empty [HashMap] with the specified initial capacity.
@@ -23,7 +23,7 @@ expect class HashMap<K, V> : MutableMap<K, V> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
constructor(initialCapacity: Int) public constructor(initialCapacity: Int)
/** /**
* Creates a new empty [HashMap] with the specified initial capacity and load factor. * Creates a new empty [HashMap] with the specified initial capacity and load factor.
@@ -39,12 +39,12 @@ expect class HashMap<K, V> : MutableMap<K, V> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
constructor(initialCapacity: Int, loadFactor: Float) public constructor(initialCapacity: Int, loadFactor: Float)
/** /**
* Creates a new [HashMap] filled with the contents of the specified [original] map. * Creates a new [HashMap] filled with the contents of the specified [original] map.
*/ */
constructor(original: Map<out K, V>) public constructor(original: Map<out K, V>)
// From Map // From Map
@@ -5,11 +5,11 @@
package kotlin.collections package kotlin.collections
expect class HashSet<E> : MutableSet<E> { public expect class HashSet<E> : MutableSet<E> {
/** /**
* Creates a new empty [HashSet]. * Creates a new empty [HashSet].
*/ */
constructor() public constructor()
/** /**
* Creates a new empty [HashSet] with the specified initial capacity. * Creates a new empty [HashSet] with the specified initial capacity.
@@ -23,7 +23,7 @@ expect class HashSet<E> : MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
constructor(initialCapacity: Int) public constructor(initialCapacity: Int)
/** /**
* Creates a new empty [HashSet] with the specified initial capacity and load factor. * Creates a new empty [HashSet] with the specified initial capacity and load factor.
@@ -39,12 +39,12 @@ expect class HashSet<E> : MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
constructor(initialCapacity: Int, loadFactor: Float) public constructor(initialCapacity: Int, loadFactor: Float)
/** /**
* Creates a new [HashSet] filled with the elements of the specified collection. * Creates a new [HashSet] filled with the elements of the specified collection.
*/ */
constructor(elements: Collection<E>) public constructor(elements: Collection<E>)
// From Set // From Set
@@ -5,11 +5,11 @@
package kotlin.collections package kotlin.collections
expect class LinkedHashMap<K, V> : MutableMap<K, V> { public expect class LinkedHashMap<K, V> : MutableMap<K, V> {
/** /**
* Creates a new empty [LinkedHashMap]. * Creates a new empty [LinkedHashMap].
*/ */
constructor() public constructor()
/** /**
* Creates a new empty [LinkedHashMap] with the specified initial capacity. * Creates a new empty [LinkedHashMap] with the specified initial capacity.
@@ -23,7 +23,7 @@ expect class LinkedHashMap<K, V> : MutableMap<K, V> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
constructor(initialCapacity: Int) public constructor(initialCapacity: Int)
/** /**
* Creates a new empty [LinkedHashMap] with the specified initial capacity and load factor. * Creates a new empty [LinkedHashMap] with the specified initial capacity and load factor.
@@ -39,14 +39,14 @@ expect class LinkedHashMap<K, V> : MutableMap<K, V> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
constructor(initialCapacity: Int, loadFactor: Float) public constructor(initialCapacity: Int, loadFactor: Float)
/** /**
* Creates a new [LinkedHashMap] filled with the contents of the specified [original] map. * Creates a new [LinkedHashMap] filled with the contents of the specified [original] map.
* *
* The iteration order of entries in the created map is the same as in the [original] map. * The iteration order of entries in the created map is the same as in the [original] map.
*/ */
constructor(original: Map<out K, V>) public constructor(original: Map<out K, V>)
// From Map // From Map
@@ -5,11 +5,11 @@
package kotlin.collections package kotlin.collections
expect class LinkedHashSet<E> : MutableSet<E> { public expect class LinkedHashSet<E> : MutableSet<E> {
/** /**
* Creates a new empty [LinkedHashSet]. * Creates a new empty [LinkedHashSet].
*/ */
constructor() public constructor()
/** /**
* Creates a new empty [LinkedHashSet] with the specified initial capacity. * Creates a new empty [LinkedHashSet] with the specified initial capacity.
@@ -23,7 +23,7 @@ expect class LinkedHashSet<E> : MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
constructor(initialCapacity: Int) public constructor(initialCapacity: Int)
/** /**
* Creates a new empty [LinkedHashSet] with the specified initial capacity and load factor. * Creates a new empty [LinkedHashSet] with the specified initial capacity and load factor.
@@ -39,14 +39,14 @@ expect class LinkedHashSet<E> : MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
constructor(initialCapacity: Int, loadFactor: Float) public constructor(initialCapacity: Int, loadFactor: Float)
/** /**
* Creates a new [LinkedHashSet] filled with the elements of the specified collection. * Creates a new [LinkedHashSet] filled with the elements of the specified collection.
* *
* The iteration order of elements in the created set is the same as in the specified collection. * The iteration order of elements in the created set is the same as in the specified collection.
*/ */
constructor(elements: Collection<E>) public constructor(elements: Collection<E>)
// From Set // From Set
@@ -17,7 +17,7 @@ import kotlin.js.arrayBufferIsView
public actual inline fun <T> Array<out T>?.orEmpty(): Array<out T> = this ?: emptyArray<T>() public actual inline fun <T> Array<out T>?.orEmpty(): Array<out T> = this ?: emptyArray<T>()
/** /**
* Returns a *typed* array containing all of the elements of this collection. * Returns a *typed* array containing all the elements of this collection.
* *
* Allocates an array of runtime type `T` having its size equal to the size of this collection * Allocates an array of runtime type `T` having its size equal to the size of this collection
* and populates the array with the elements of this collection. * and populates the array with the elements of this collection.
@@ -120,7 +120,7 @@ public actual fun <T> MutableList<T>.fill(value: T): Unit {
public actual fun <T> MutableList<T>.shuffle(): Unit = shuffle(Random) public actual fun <T> MutableList<T>.shuffle(): Unit = shuffle(Random)
/** /**
* Returns a new list with the elements of this list randomly shuffled. * Returns a new list with the elements of this collection randomly shuffled.
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() } public actual fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() }
@@ -225,7 +225,7 @@ internal actual fun checkCountOverflow(count: Int): Int {
* JS map and set implementations do not make use of capacities or load factors. * JS map and set implementations do not make use of capacities or load factors.
*/ */
@PublishedApi @PublishedApi
internal actual fun mapCapacity(expectedSize: Int) = expectedSize internal actual fun mapCapacity(expectedSize: Int): Int = expectedSize
/** /**
* Checks a collection builder function capacity argument. * Checks a collection builder function capacity argument.
@@ -161,7 +161,7 @@ public actual open class ArrayList<E> internal constructor(private var array: Ar
actual override fun lastIndexOf(element: E): Int = array.lastIndexOf(element) actual override fun lastIndexOf(element: E): Int = array.lastIndexOf(element)
override fun toString() = arrayToString(array) override fun toString(): String = arrayToString(array)
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
override fun <T> toArray(array: Array<T>): Array<T> { override fun <T> toArray(array: Array<T>): Array<T> {
@@ -32,7 +32,7 @@ public actual open class HashMap<K, V> : AbstractMutableMap<K, V>, MutableMap<K,
/** /**
* Creates a new empty [HashMap]. * Creates a new empty [HashMap].
*/ */
actual constructor() : this(InternalHashMap()) public actual constructor() : this(InternalHashMap())
/** /**
* Creates a new empty [HashMap] with the specified initial capacity and load factor. * Creates a new empty [HashMap] with the specified initial capacity and load factor.
@@ -48,7 +48,7 @@ public actual open class HashMap<K, V> : AbstractMutableMap<K, V>, MutableMap<K,
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
actual constructor(initialCapacity: Int, loadFactor: Float) : this(InternalHashMap(initialCapacity, loadFactor)) public actual constructor(initialCapacity: Int, loadFactor: Float) : this(InternalHashMap(initialCapacity, loadFactor))
/** /**
* Creates a new empty [HashMap] with the specified initial capacity. * Creates a new empty [HashMap] with the specified initial capacity.
@@ -62,12 +62,12 @@ public actual open class HashMap<K, V> : AbstractMutableMap<K, V>, MutableMap<K,
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f) public actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
/** /**
* Creates a new [HashMap] filled with the contents of the specified [original] map. * Creates a new [HashMap] filled with the contents of the specified [original] map.
*/ */
actual constructor(original: Map<out K, V>) : this(InternalHashMap(original)) public actual constructor(original: Map<out K, V>) : this(InternalHashMap(original))
actual override fun clear() { actual override fun clear() {
internalMap.clear() internalMap.clear()
@@ -92,7 +92,7 @@ public actual open class HashMap<K, V> : AbstractMutableMap<K, V>, MutableMap<K,
actual override val size: Int get() = internalMap.size actual override val size: Int get() = internalMap.size
actual override fun putAll(from: Map<out K, V>) = internalMap.putAll(from) actual override fun putAll(from: Map<out K, V>): Unit = internalMap.putAll(from)
} }
/** /**
@@ -31,12 +31,12 @@ public actual open class HashSet<E> : AbstractMutableSet<E>, MutableSet<E> {
/** /**
* Creates a new empty [HashSet]. * Creates a new empty [HashSet].
*/ */
actual constructor() : this(InternalHashMap()) public actual constructor() : this(InternalHashMap())
/** /**
* Creates a new [HashSet] filled with the elements of the specified collection. * Creates a new [HashSet] filled with the elements of the specified collection.
*/ */
actual constructor(elements: Collection<E>) : this(InternalHashMap(elements.size)) { public actual constructor(elements: Collection<E>) : this(InternalHashMap(elements.size)) {
for (element in elements) { for (element in elements) {
internalMap.put(element, true) internalMap.put(element, true)
} }
@@ -56,7 +56,7 @@ public actual open class HashSet<E> : AbstractMutableSet<E>, MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
actual constructor(initialCapacity: Int, loadFactor: Float) : this(InternalHashMap(initialCapacity, loadFactor)) public actual constructor(initialCapacity: Int, loadFactor: Float) : this(InternalHashMap(initialCapacity, loadFactor))
/** /**
* Creates a new empty [HashSet] with the specified initial capacity. * Creates a new empty [HashSet] with the specified initial capacity.
@@ -70,7 +70,7 @@ public actual open class HashSet<E> : AbstractMutableSet<E>, MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f) public actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
actual override fun add(element: E): Boolean { actual override fun add(element: E): Boolean {
return internalMap.put(element, true) == null return internalMap.put(element, true) == null
@@ -19,7 +19,7 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
/** /**
* Creates a new empty [LinkedHashMap]. * Creates a new empty [LinkedHashMap].
*/ */
actual constructor() : super() public actual constructor() : super()
/** /**
* Creates a new empty [LinkedHashMap] with the specified initial capacity. * Creates a new empty [LinkedHashMap] with the specified initial capacity.
@@ -33,7 +33,7 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
actual constructor(initialCapacity: Int) : super(initialCapacity) public actual constructor(initialCapacity: Int) : super(initialCapacity)
/** /**
* Creates a new empty [LinkedHashMap] with the specified initial capacity and load factor. * Creates a new empty [LinkedHashMap] with the specified initial capacity and load factor.
@@ -49,14 +49,14 @@ public actual open class LinkedHashMap<K, V> : HashMap<K, V>, MutableMap<K, V> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
actual constructor(initialCapacity: Int, loadFactor: Float) : super(initialCapacity, loadFactor) public actual constructor(initialCapacity: Int, loadFactor: Float) : super(initialCapacity, loadFactor)
/** /**
* Creates a new [LinkedHashMap] filled with the contents of the specified [original] map. * Creates a new [LinkedHashMap] filled with the contents of the specified [original] map.
* *
* The iteration order of entries in the created map is the same as in the [original] map. * The iteration order of entries in the created map is the same as in the [original] map.
*/ */
actual constructor(original: Map<out K, V>) : super(original) public actual constructor(original: Map<out K, V>) : super(original)
internal constructor(internalMap: InternalMap<K, V>) : super(internalMap) internal constructor(internalMap: InternalMap<K, V>) : super(internalMap)
@@ -18,14 +18,14 @@ public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
/** /**
* Creates a new empty [LinkedHashSet]. * Creates a new empty [LinkedHashSet].
*/ */
actual constructor() : super() public actual constructor() : super()
/** /**
* Creates a new [LinkedHashSet] filled with the elements of the specified collection. * Creates a new [LinkedHashSet] filled with the elements of the specified collection.
* *
* The iteration order of elements in the created set is the same as in the specified collection. * The iteration order of elements in the created set is the same as in the specified collection.
*/ */
actual constructor(elements: Collection<E>) : super(elements) public actual constructor(elements: Collection<E>) : super(elements)
/** /**
* Creates a new empty [LinkedHashSet] with the specified initial capacity and load factor. * Creates a new empty [LinkedHashSet] with the specified initial capacity and load factor.
@@ -41,7 +41,7 @@ public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
actual constructor(initialCapacity: Int, loadFactor: Float) : super(initialCapacity, loadFactor) public actual constructor(initialCapacity: Int, loadFactor: Float) : super(initialCapacity, loadFactor)
/** /**
* Creates a new empty [LinkedHashSet] with the specified initial capacity. * Creates a new empty [LinkedHashSet] with the specified initial capacity.
@@ -55,7 +55,7 @@ public actual open class LinkedHashSet<E> : HashSet<E>, MutableSet<E> {
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f) public actual constructor(initialCapacity: Int) : this(initialCapacity, 1.0f)
internal constructor(internalMap: InternalMap<E, Boolean>) : super(internalMap) internal constructor(internalMap: InternalMap<E, Boolean>) : super(internalMap)
@@ -25,7 +25,7 @@ public actual inline fun <reified T> Array<out T>?.orEmpty(): Array<out T> = thi
public inline fun ByteArray.toString(charset: Charset): String = String(this, charset) public inline fun ByteArray.toString(charset: Charset): String = String(this, charset)
/** /**
* Returns a *typed* array containing all of the elements of this collection. * Returns a *typed* array containing all the elements of this collection.
* *
* Allocates an array of runtime type `T` having its size equal to the size of this collection * Allocates an array of runtime type `T` having its size equal to the size of this collection
* and populates the array with the elements of this collection. * and populates the array with the elements of this collection.
@@ -63,7 +63,7 @@ public inline fun <T> java.util.Enumeration<T>.toList(): List<T> = java.util.Col
/** /**
* Returns a new list with the elements of this list randomly shuffled. * Returns a new list with the elements of this collection randomly shuffled.
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() } public actual fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() }
@@ -7,6 +7,9 @@
package kotlin.collections package kotlin.collections
/**
* Marker interface indicating that the [List] implementation supports fast indexed access.
*/
@SinceKotlin("1.1") public actual typealias RandomAccess = java.util.RandomAccess @SinceKotlin("1.1") public actual typealias RandomAccess = java.util.RandomAccess
@@ -5,7 +5,7 @@
package kotlin.collections package kotlin.collections
actual class ArrayList<E> actual constructor(initialCapacity: Int) : MutableList<E>, RandomAccess, AbstractMutableList<E>() { public actual class ArrayList<E> public actual constructor(initialCapacity: Int) : MutableList<E>, RandomAccess, AbstractMutableList<E>() {
private var backing = arrayOfUninitializedElements<E>(initialCapacity) private var backing = arrayOfUninitializedElements<E>(initialCapacity)
private var length = 0 private var length = 0
private var isReadOnly = false private var isReadOnly = false
@@ -16,14 +16,14 @@ actual class ArrayList<E> actual constructor(initialCapacity: Int) : MutableList
/** /**
* Creates a new empty [ArrayList]. * Creates a new empty [ArrayList].
*/ */
actual constructor() : this(10) public actual constructor() : this(10)
/** /**
* Creates a new [ArrayList] filled with the elements of the specified collection. * Creates a new [ArrayList] filled with the elements of the specified collection.
* *
* The iteration order of elements in the created list is the same as in the specified collection. * The iteration order of elements in the created list is the same as in the specified collection.
*/ */
actual constructor(elements: Collection<E>) : this(elements.size) { public actual constructor(elements: Collection<E>) : this(elements.size) {
addAll(elements) addAll(elements)
} }
@@ -37,7 +37,7 @@ actual class ArrayList<E> actual constructor(initialCapacity: Int) : MutableList
override actual val size: Int override actual val size: Int
get() = length get() = length
override actual fun isEmpty() = length == 0 override actual fun isEmpty(): Boolean = length == 0
override actual fun get(index: Int): E { override actual fun get(index: Int): E {
AbstractList.checkElementIndex(index, length) AbstractList.checkElementIndex(index, length)
@@ -154,13 +154,13 @@ actual class ArrayList<E> actual constructor(initialCapacity: Int) : MutableList
return backing.copyOfRange(fromIndex = 0, toIndex = length) as Array<Any?> return backing.copyOfRange(fromIndex = 0, toIndex = length) as Array<Any?>
} }
actual fun trimToSize() { public actual fun trimToSize() {
registerModification() registerModification()
if (length < backing.size) if (length < backing.size)
backing = backing.copyOfUninitializedElements(length) backing = backing.copyOfUninitializedElements(length)
} }
final actual fun ensureCapacity(minCapacity: Int) { public final actual fun ensureCapacity(minCapacity: Int) {
if (minCapacity <= backing.size) return if (minCapacity <= backing.size) return
registerModification() registerModification()
ensureCapacityInternal(minCapacity) ensureCapacityInternal(minCapacity)
@@ -96,7 +96,7 @@ public actual fun <T> MutableList<T>.shuffle(): Unit {
} }
/** /**
* Returns a new list with the elements of this list randomly shuffled. * Returns a new list with the elements of this collection randomly shuffled.
*/ */
@SinceKotlin("1.2") @SinceKotlin("1.2")
public actual fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() } public actual fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() }
@@ -9,7 +9,7 @@ import kotlin.native.concurrent.isFrozen
import kotlin.native.FreezingIsDeprecated import kotlin.native.FreezingIsDeprecated
@OptIn(FreezingIsDeprecated::class) @OptIn(FreezingIsDeprecated::class)
actual class HashMap<K, V> private constructor( public actual class HashMap<K, V> private constructor(
// keys in insert order // keys in insert order
private var keysArray: Array<K>, private var keysArray: Array<K>,
// values in insert order, allocated only when actually used, always null in pure HashSet // values in insert order, allocated only when actually used, always null in pure HashSet
@@ -52,7 +52,7 @@ actual class HashMap<K, V> private constructor(
/** /**
* Creates a new empty [HashMap]. * Creates a new empty [HashMap].
*/ */
actual constructor() : this(INITIAL_CAPACITY) public actual constructor() : this(INITIAL_CAPACITY)
/** /**
* Creates a new empty [HashMap] with the specified initial capacity. * Creates a new empty [HashMap] with the specified initial capacity.
@@ -66,7 +66,7 @@ actual class HashMap<K, V> private constructor(
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
actual constructor(initialCapacity: Int) : this( public actual constructor(initialCapacity: Int) : this(
arrayOfUninitializedElements(initialCapacity), arrayOfUninitializedElements(initialCapacity),
null, null,
IntArray(initialCapacity), IntArray(initialCapacity),
@@ -77,7 +77,7 @@ actual class HashMap<K, V> private constructor(
/** /**
* Creates a new [HashMap] filled with the contents of the specified [original] map. * Creates a new [HashMap] filled with the contents of the specified [original] map.
*/ */
actual constructor(original: Map<out K, V>) : this(original.size) { public actual constructor(original: Map<out K, V>) : this(original.size) {
putAll(original) putAll(original)
} }
@@ -95,7 +95,7 @@ actual class HashMap<K, V> private constructor(
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
actual constructor(initialCapacity: Int, loadFactor: Float) : this(initialCapacity) { public actual constructor(initialCapacity: Int, loadFactor: Float) : this(initialCapacity) {
require(loadFactor > 0) { "Non-positive load factor: $loadFactor" } require(loadFactor > 0) { "Non-positive load factor: $loadFactor" }
} }
@@ -770,4 +770,4 @@ internal class HashMapEntrySet<K, V> internal constructor(
} }
// This hash map keeps insertion order. // This hash map keeps insertion order.
actual typealias LinkedHashMap<K, V> = HashMap<K, V> public actual typealias LinkedHashMap<K, V> = HashMap<K, V>
@@ -5,7 +5,7 @@
package kotlin.collections package kotlin.collections
actual class HashSet<E> internal constructor( public actual class HashSet<E> internal constructor(
private val backing: HashMap<E, *> private val backing: HashMap<E, *>
) : MutableSet<E>, kotlin.native.internal.KonanSet<E>, AbstractMutableSet<E>() { ) : MutableSet<E>, kotlin.native.internal.KonanSet<E>, AbstractMutableSet<E>() {
private companion object { private companion object {
@@ -15,7 +15,7 @@ actual class HashSet<E> internal constructor(
/** /**
* Creates a new empty [HashSet]. * Creates a new empty [HashSet].
*/ */
actual constructor() : this(HashMap<E, Nothing>()) public actual constructor() : this(HashMap<E, Nothing>())
/** /**
* Creates a new empty [HashSet] with the specified initial capacity. * Creates a new empty [HashSet] with the specified initial capacity.
@@ -29,12 +29,12 @@ actual class HashSet<E> internal constructor(
* *
* @throws IllegalArgumentException if [initialCapacity] is negative. * @throws IllegalArgumentException if [initialCapacity] is negative.
*/ */
actual constructor(initialCapacity: Int) : this(HashMap<E, Nothing>(initialCapacity)) public actual constructor(initialCapacity: Int) : this(HashMap<E, Nothing>(initialCapacity))
/** /**
* Creates a new [HashSet] filled with the elements of the specified collection. * Creates a new [HashSet] filled with the elements of the specified collection.
*/ */
actual constructor(elements: Collection<E>) : this(elements.size) { public actual constructor(elements: Collection<E>) : this(elements.size) {
addAll(elements) addAll(elements)
} }
@@ -52,7 +52,7 @@ actual class HashSet<E> internal constructor(
* *
* @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive. * @throws IllegalArgumentException if [initialCapacity] is negative or [loadFactor] is non-positive.
*/ */
actual constructor(initialCapacity: Int, loadFactor: Float) : this(HashMap<E, Nothing>(initialCapacity, loadFactor)) public actual constructor(initialCapacity: Int, loadFactor: Float) : this(HashMap<E, Nothing>(initialCapacity, loadFactor))
@PublishedApi @PublishedApi
internal fun build(): Set<E> { internal fun build(): Set<E> {
@@ -68,7 +68,7 @@ actual class HashSet<E> internal constructor(
@Deprecated("This function is not supposed to be used directly.") @Deprecated("This function is not supposed to be used directly.")
@DeprecatedSinceKotlin(warningSince = "1.9") // TODO: advance to HIDDEN eventually @DeprecatedSinceKotlin(warningSince = "1.9") // TODO: advance to HIDDEN eventually
override fun getElement(element: E): E? = backing.getKey(element) override fun getElement(element: E): E? = backing.getKey(element)
override actual fun clear() = backing.clear() override actual fun clear(): Unit = backing.clear()
override actual fun add(element: E): Boolean = backing.addKey(element) >= 0 override actual fun add(element: E): Boolean = backing.addKey(element) >= 0
override actual fun remove(element: E): Boolean = backing.removeKey(element) >= 0 override actual fun remove(element: E): Boolean = backing.removeKey(element) >= 0
override actual fun iterator(): MutableIterator<E> = backing.keysIterator() override actual fun iterator(): MutableIterator<E> = backing.keysIterator()
@@ -90,4 +90,4 @@ actual class HashSet<E> internal constructor(
} }
// This hash set keeps insertion order. // This hash set keeps insertion order.
actual typealias LinkedHashSet<V> = HashSet<V> public actual typealias LinkedHashSet<V> = HashSet<V>
@@ -33,7 +33,7 @@ internal actual fun <K, V> Map<out K, V>.toSingletonMap(): Map<K, V>
* Native map and set implementations do not make use of capacities or load factors. * Native map and set implementations do not make use of capacities or load factors.
*/ */
@PublishedApi @PublishedApi
internal actual fun mapCapacity(expectedSize: Int) = expectedSize internal actual fun mapCapacity(expectedSize: Int): Int = expectedSize
/** /**
* Returns a new read-only map, mapping only the specified key to the * Returns a new read-only map, mapping only the specified key to the
@@ -528,7 +528,7 @@ public class ArrayDeque<E> : AbstractMutableList<E> {
size = 0 size = 0
} }
@Suppress("NOTHING_TO_OVERRIDE") @Suppress("NOTHING_TO_OVERRIDE", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") // different visibility inherited from the base class
override fun <T> toArray(array: Array<T>): Array<T> { override fun <T> toArray(array: Array<T>): Array<T> {
@Suppress("UNCHECKED_CAST") @Suppress("UNCHECKED_CAST")
val dest = (if (array.size >= size) array else arrayOfNulls(array, size)) as Array<Any?> val dest = (if (array.size >= size) array else arrayOfNulls(array, size)) as Array<Any?>
@@ -545,7 +545,7 @@ public class ArrayDeque<E> : AbstractMutableList<E> {
return terminateCollectionToArray(size, dest) as Array<T> return terminateCollectionToArray(size, dest) as Array<T>
} }
@Suppress("NOTHING_TO_OVERRIDE") @Suppress("NOTHING_TO_OVERRIDE", "NO_EXPLICIT_VISIBILITY_IN_API_MODE") // different visibility inherited from the base class
override fun toArray(): Array<Any?> { override fun toArray(): Array<Any?> {
return toArray(arrayOfNulls<Any?>(size)) return toArray(arrayOfNulls<Any?>(size))
} }
@@ -6,7 +6,7 @@
package kotlin.collections package kotlin.collections
/** /**
* Returns a *typed* array containing all of the elements of this collection. * Returns a *typed* array containing all the elements of this collection.
* *
* Allocates an array of runtime type `T` having its size equal to the size of this collection * Allocates an array of runtime type `T` having its size equal to the size of this collection
* and populates the array with the elements of this collection. * and populates the array with the elements of this collection.