Reformat stdlib: collections

#KT-5558
This commit is contained in:
Ilya Gorbunov
2018-04-20 21:30:56 +03:00
parent bea94d1d59
commit ad43c0f4cb
34 changed files with 337 additions and 312 deletions
@@ -7,7 +7,7 @@ package kotlin.collections
/**
* Provides a skeletal implementation of the read-only [Collection] interface.
*
* @param E the type of elements contained in the collection. The collection is covariant on its element type.
* @param E the type of elements contained in the collection. The collection is covariant on its element type.
*/
@SinceKotlin("1.1")
public abstract class AbstractCollection<out E> protected constructor() : Collection<E> {
@@ -17,7 +17,7 @@ public abstract class AbstractCollection<out E> protected constructor() : Collec
override fun contains(element: @UnsafeVariance E): Boolean = any { it == element }
override fun containsAll(elements: Collection<@UnsafeVariance E>): Boolean =
elements.all { contains(it) } // use when js will support bound refs: elements.all(this::contains)
elements.all { contains(it) } // use when js will support bound refs: elements.all(this::contains)
override fun isEmpty(): Boolean = size == 0
@@ -17,7 +17,7 @@ private enum class State {
* A base class to simplify implementing iterators so that implementations only have to implement [computeNext]
* to implement the iterator, calling [done] when the iteration is complete.
*/
public abstract class AbstractIterator<T>: Iterator<T> {
public abstract class AbstractIterator<T> : Iterator<T> {
private var state = State.NotReady
private var nextValue: T? = null
@@ -16,7 +16,7 @@ package kotlin.collections
*
* @param K the type of map keys. The map is invariant on its key type.
* @param V the type of map values. The map is covariant on its value type.
*/
*/
@SinceKotlin("1.1")
public abstract class AbstractMap<K, out V> protected constructor() : Map<K, V> {
@@ -78,25 +78,29 @@ public abstract class AbstractMap<K, out V> protected constructor() : Map<K, V>
* Accessing this property first time creates a keys view from [entries].
* All subsequent accesses just return the created instance.
*/
private @kotlin.jvm.Volatile var _keys: Set<K>? = null
override val keys: Set<K> get() {
if (_keys == null) {
_keys = object : AbstractSet<K>() {
override operator fun contains(element: K): Boolean = containsKey(element)
override val keys: Set<K>
get() {
if (_keys == null) {
_keys = object : AbstractSet<K>() {
override operator fun contains(element: K): Boolean = containsKey(element)
override operator fun iterator(): Iterator<K> {
val entryIterator = entries.iterator()
return object : Iterator<K> {
override fun hasNext(): Boolean = entryIterator.hasNext()
override fun next(): K = entryIterator.next().key
override operator fun iterator(): Iterator<K> {
val entryIterator = entries.iterator()
return object : Iterator<K> {
override fun hasNext(): Boolean = entryIterator.hasNext()
override fun next(): K = entryIterator.next().key
}
}
}
override val size: Int get() = this@AbstractMap.size
override val size: Int get() = this@AbstractMap.size
}
}
return _keys!!
}
return _keys!!
}
@kotlin.jvm.Volatile
private var _keys: Set<K>? = null
override fun toString(): String = entries.joinToString(", ", "{", "}") { toString(it) }
@@ -110,25 +114,28 @@ public abstract class AbstractMap<K, out V> protected constructor() : Map<K, V>
* Accessing this property first time creates a values view from [entries].
* All subsequent accesses just return the created instance.
*/
private @kotlin.jvm.Volatile var _values: Collection<V>? = null
override val values: Collection<V> get() {
if (_values == null) {
_values = object : AbstractCollection<V>() {
override operator fun contains(element: @UnsafeVariance V): Boolean = containsValue(element)
override val values: Collection<V>
get() {
if (_values == null) {
_values = object : AbstractCollection<V>() {
override operator fun contains(element: @UnsafeVariance V): Boolean = containsValue(element)
override operator fun iterator(): Iterator<V> {
val entryIterator = entries.iterator()
return object : Iterator<V> {
override fun hasNext(): Boolean = entryIterator.hasNext()
override fun next(): V = entryIterator.next().value
override operator fun iterator(): Iterator<V> {
val entryIterator = entries.iterator()
return object : Iterator<V> {
override fun hasNext(): Boolean = entryIterator.hasNext()
override fun next(): V = entryIterator.next().value
}
}
}
override val size: Int get() = this@AbstractMap.size
override val size: Int get() = this@AbstractMap.size
}
}
return _values!!
}
return _values!!
}
@kotlin.jvm.Volatile
private var _values: Collection<V>? = null
private fun implFindEntry(key: K): Map.Entry<K, V>? = entries.firstOrNull { it.key == key }
@@ -10,7 +10,6 @@
package kotlin.collections
/**
* Returns a single list of all elements from all arrays in the given array.
* @sample samples.collections.Arrays.Transformations.flattenArray
@@ -53,7 +53,7 @@ internal object EmptyList : List<Nothing>, Serializable, RandomAccess {
internal fun <T> Array<out T>.asCollection(): Collection<T> = ArrayAsCollection(this, isVarargs = false)
private class ArrayAsCollection<T>(val values: Array<out T>, val isVarargs: Boolean): Collection<T> {
private class ArrayAsCollection<T>(val values: Array<out T>, val isVarargs: Boolean) : Collection<T> {
override val size: Int get() = values.size
override fun isEmpty(): Boolean = values.isEmpty()
override fun contains(element: T): Boolean = values.contains(element)
@@ -102,15 +102,15 @@ public inline fun <T> arrayListOf(): ArrayList<T> = ArrayList()
* Returns a new [MutableList] with the given elements.
* @sample samples.collections.Collections.Lists.mutableList
*/
public fun <T> mutableListOf(vararg elements: T): MutableList<T>
= if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
public fun <T> mutableListOf(vararg elements: T): MutableList<T> =
if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
/**
* Returns a new [ArrayList] with the given elements.
* @sample samples.collections.Collections.Lists.arrayList
*/
public fun <T> arrayListOf(vararg elements: T): ArrayList<T>
= if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
public fun <T> arrayListOf(vararg elements: T): ArrayList<T> =
if (elements.size == 0) ArrayList() else ArrayList(ArrayAsCollection(elements, isVarargs = true))
/**
* Returns a new read-only list either of single given element, if it is not null, or empty list if the element is null. The returned list is serializable (JVM).
@@ -214,7 +214,7 @@ internal fun <T> List<T>.optimizeReadOnlyList() = when (size) {
* @sample samples.collections.Collections.Lists.binarySearchOnComparable
* @sample samples.collections.Collections.Lists.binarySearchWithBoundaries
*/
public fun <T: Comparable<T>> List<T?>.binarySearch(element: T?, fromIndex: Int = 0, toIndex: Int = size): Int {
public fun <T : Comparable<T>> List<T?>.binarySearch(element: T?, fromIndex: Int = 0, toIndex: Int = size): Int {
rangeCheck(size, fromIndex, toIndex)
var low = fromIndex
@@ -287,8 +287,13 @@ public fun <T> List<T>.binarySearch(element: T, comparator: Comparator<in T>, fr
* so that the list (or the specified subrange of list) still remains sorted.
* @sample samples.collections.Collections.Lists.binarySearchByKey
*/
public inline fun <T, K : Comparable<K>> List<T>.binarySearchBy(key: K?, fromIndex: Int = 0, toIndex: Int = size, crossinline selector: (T) -> K?): Int =
binarySearch(fromIndex, toIndex) { compareValues(selector(it), key) }
public inline fun <T, K : Comparable<K>> List<T>.binarySearchBy(
key: K?,
fromIndex: Int = 0,
toIndex: Int = size,
crossinline selector: (T) -> K?
): Int =
binarySearch(fromIndex, toIndex) { compareValues(selector(it), key) }
// do not introduce this overload --- too rare
//public fun <T, K> List<T>.binarySearchBy(key: K, comparator: Comparator<K>, fromIndex: Int = 0, toIndex: Int = size(), selector: (T) -> K): Int =
@@ -47,7 +47,7 @@ public interface Grouping<T, out K> {
*/
@SinceKotlin("1.1")
public inline fun <T, K, R> Grouping<T, K>.aggregate(
operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
): Map<K, R> {
return aggregateTo(mutableMapOf<K, R>(), operation)
}
@@ -72,8 +72,8 @@ public inline fun <T, K, R> Grouping<T, K>.aggregate(
*/
@SinceKotlin("1.1")
public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.aggregateTo(
destination: M,
operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
destination: M,
operation: (key: K, accumulator: R?, element: T, first: Boolean) -> R
): M {
for (e in this.sourceIterator()) {
val key = keyOf(e)
@@ -102,11 +102,11 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.aggregateTo(
*/
@SinceKotlin("1.1")
public inline fun <T, K, R> Grouping<T, K>.fold(
initialValueSelector: (key: K, element: T) -> R,
operation: (key: K, accumulator: R, element: T) -> R
initialValueSelector: (key: K, element: T) -> R,
operation: (key: K, accumulator: R, element: T) -> R
): Map<K, R> =
@Suppress("UNCHECKED_CAST")
aggregate { key, acc, e, first -> operation(key, if (first) initialValueSelector(key, e) else acc as R, e) }
@Suppress("UNCHECKED_CAST")
aggregate { key, acc, e, first -> operation(key, if (first) initialValueSelector(key, e) else acc as R, e) }
/**
* Groups elements from the [Grouping] source by key and applies [operation] to the elements of each group sequentially,
@@ -131,12 +131,12 @@ public inline fun <T, K, R> Grouping<T, K>.fold(
*/
@SinceKotlin("1.1")
public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
destination: M,
initialValueSelector: (key: K, element: T) -> R,
operation: (key: K, accumulator: R, element: T) -> R
destination: M,
initialValueSelector: (key: K, element: T) -> R,
operation: (key: K, accumulator: R, element: T) -> R
): M =
@Suppress("UNCHECKED_CAST")
aggregateTo(destination) { key, acc, e, first -> operation(key, if (first) initialValueSelector(key, e) else acc as R, e) }
@Suppress("UNCHECKED_CAST")
aggregateTo(destination) { key, acc, e, first -> operation(key, if (first) initialValueSelector(key, e) else acc as R, e) }
/**
@@ -152,11 +152,11 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
*/
@SinceKotlin("1.1")
public inline fun <T, K, R> Grouping<T, K>.fold(
initialValue: R,
operation: (accumulator: R, element: T) -> R
initialValue: R,
operation: (accumulator: R, element: T) -> R
): Map<K, R> =
@Suppress("UNCHECKED_CAST")
aggregate { _, acc, e, first -> operation(if (first) initialValue else acc as R, e) }
@Suppress("UNCHECKED_CAST")
aggregate { _, acc, e, first -> operation(if (first) initialValue else acc as R, e) }
/**
* Groups elements from the [Grouping] source by key and applies [operation] to the elements of each group sequentially,
@@ -175,12 +175,12 @@ public inline fun <T, K, R> Grouping<T, K>.fold(
*/
@SinceKotlin("1.1")
public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
destination: M,
initialValue: R,
operation: (accumulator: R, element: T) -> R
destination: M,
initialValue: R,
operation: (accumulator: R, element: T) -> R
): M =
@Suppress("UNCHECKED_CAST")
aggregateTo(destination) { _, acc, e, first -> operation(if (first) initialValue else acc as R, e) }
@Suppress("UNCHECKED_CAST")
aggregateTo(destination) { _, acc, e, first -> operation(if (first) initialValue else acc as R, e) }
/**
@@ -199,12 +199,12 @@ public inline fun <T, K, R, M : MutableMap<in K, R>> Grouping<T, K>.foldTo(
*/
@SinceKotlin("1.1")
public inline fun <S, T : S, K> Grouping<T, K>.reduce(
operation: (key: K, accumulator: S, element: T) -> S
operation: (key: K, accumulator: S, element: T) -> S
): Map<K, S> =
aggregate { key, acc, e, first ->
@Suppress("UNCHECKED_CAST")
if (first) e else operation(key, acc as S, e)
}
aggregate { key, acc, e, first ->
@Suppress("UNCHECKED_CAST")
if (first) e else operation(key, acc as S, e)
}
/**
* Groups elements from the [Grouping] source by key and applies the reducing [operation] to the elements of each group
@@ -225,13 +225,13 @@ public inline fun <S, T : S, K> Grouping<T, K>.reduce(
*/
@SinceKotlin("1.1")
public inline fun <S, T : S, K, M : MutableMap<in K, S>> Grouping<T, K>.reduceTo(
destination: M,
operation: (key: K, accumulator: S, element: T) -> S
destination: M,
operation: (key: K, accumulator: S, element: T) -> S
): M =
aggregateTo(destination) { key, acc, e, first ->
@Suppress("UNCHECKED_CAST")
if (first) e else operation(key, acc as S, e)
}
aggregateTo(destination) { key, acc, e, first ->
@Suppress("UNCHECKED_CAST")
if (first) e else operation(key, acc as S, e)
}
/**
@@ -246,7 +246,7 @@ public inline fun <S, T : S, K, M : MutableMap<in K, S>> Grouping<T, K>.reduceTo
*/
@SinceKotlin("1.1")
public fun <T, K, M : MutableMap<in K, Int>> Grouping<T, K>.eachCountTo(destination: M): M =
foldTo(destination, 0) { acc, _ -> acc + 1 }
foldTo(destination, 0) { acc, _ -> acc + 1 }
/*
/**
@@ -4,6 +4,7 @@
*/
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")
package kotlin.collections
/**
@@ -42,23 +43,23 @@ private fun <T> Collection<T>.safeToConvertToSet() = size > 2 && this is ArrayLi
/** Converts this collection to a set, when it's worth so and it doesn't change contains method behavior. */
internal fun <T> Iterable<T>.convertToSetForSetOperationWith(source: Iterable<T>): Collection<T> =
when(this) {
is Set -> this
is Collection ->
when {
source is Collection && source.size < 2 -> this
else -> if (this.safeToConvertToSet()) toHashSet() else this
}
else -> toHashSet()
}
when (this) {
is Set -> this
is Collection ->
when {
source is Collection && source.size < 2 -> this
else -> if (this.safeToConvertToSet()) toHashSet() else this
}
else -> toHashSet()
}
/** Converts this collection to a set, when it's worth so and it doesn't change contains method behavior. */
internal fun <T> Iterable<T>.convertToSetForSetOperation(): Collection<T> =
when(this) {
is Set -> this
is Collection -> if (this.safeToConvertToSet()) toHashSet() else this
else -> toHashSet()
}
when (this) {
is Set -> this
is Collection -> if (this.safeToConvertToSet()) toHashSet() else this
else -> toHashSet()
}
/**
@@ -27,7 +27,7 @@ public fun <T> Iterator<T>.withIndex(): Iterator<IndexedValue<T>> = IndexingIter
* Performs the given [operation] on each element of this [Iterator].
* @sample samples.collections.Iterators.forEachIterator
*/
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit): Unit {
for (element in this) operation(element)
}
@@ -4,6 +4,7 @@
*/
@file:kotlin.jvm.JvmName("MapAccessorsKt")
package kotlin.collections
import kotlin.reflect.KProperty
@@ -18,8 +19,8 @@ import kotlin.internal.Exact
* @throws NoSuchElementException when the map doesn't contain value for the property name and doesn't provide an implicit default (see [withDefault]).
*/
@kotlin.internal.InlineOnly
public inline operator fun <V, V1: V> Map<in String, @Exact V>.getValue(thisRef: Any?, property: KProperty<*>): V1
= @Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V1)
public inline operator fun <V, V1 : V> Map<in String, @Exact V>.getValue(thisRef: Any?, property: KProperty<*>): V1 =
@Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V1)
/**
* Returns the value of the property for the given object from this mutable map.
@@ -31,15 +32,15 @@ public inline operator fun <V, V1: V> Map<in String, @Exact V>.getValue(thisRef:
*/
@kotlin.jvm.JvmName("getVar")
@kotlin.internal.InlineOnly
public inline operator fun <V, V1: V> MutableMap<in String, out @Exact V>.getValue(thisRef: Any?, property: KProperty<*>): V1
= @Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V1)
public inline operator fun <V, V1 : V> MutableMap<in String, out @Exact V>.getValue(thisRef: Any?, property: KProperty<*>): V1 =
@Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V1)
@Deprecated("Use getValue() with two type parameters instead")
@kotlin.jvm.JvmName("getVarContravariant")
@kotlin.internal.LowPriorityInOverloadResolution
@kotlin.internal.InlineOnly
public inline fun <V> MutableMap<in String, in V>.getValue(thisRef: Any?, property: KProperty<*>): V
= @Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V)
public inline fun <V> MutableMap<in String, in V>.getValue(thisRef: Any?, property: KProperty<*>): V =
@Suppress("UNCHECKED_CAST") (getOrImplicitDefault(property.name) as V)
/**
* Stores the value of the property for the given object in this mutable map.
@@ -33,10 +33,10 @@ internal fun <K, V> Map<K, V>.getOrImplicitDefault(key: K): V {
* When this map already has an implicit default value provided with a former call to [withDefault], it is being replaced by this call.
*/
public fun <K, V> Map<K, V>.withDefault(defaultValue: (key: K) -> V): Map<K, V> =
when (this) {
is MapWithDefault -> this.map.withDefault(defaultValue)
else -> MapWithDefaultImpl(this, defaultValue)
}
when (this) {
is MapWithDefault -> this.map.withDefault(defaultValue)
else -> MapWithDefaultImpl(this, defaultValue)
}
/**
* Returns a wrapper of this mutable map, having the implicit default value provided with the specified function [defaultValue].
@@ -48,26 +48,23 @@ public fun <K, V> Map<K, V>.withDefault(defaultValue: (key: K) -> V): Map<K, V>
*/
@kotlin.jvm.JvmName("withDefaultMutable")
public fun <K, V> MutableMap<K, V>.withDefault(defaultValue: (key: K) -> V): MutableMap<K, V> =
when (this) {
is MutableMapWithDefault -> this.map.withDefault(defaultValue)
else -> MutableMapWithDefaultImpl(this, defaultValue)
}
when (this) {
is MutableMapWithDefault -> this.map.withDefault(defaultValue)
else -> MutableMapWithDefaultImpl(this, defaultValue)
}
private interface MapWithDefault<K, out V>: Map<K, V> {
private interface MapWithDefault<K, out V> : Map<K, V> {
public val map: Map<K, V>
public fun getOrImplicitDefault(key: K): V
}
private interface MutableMapWithDefault<K, V>: MutableMap<K, V>, MapWithDefault<K, V> {
private interface MutableMapWithDefault<K, V> : MutableMap<K, V>, MapWithDefault<K, V> {
public override val map: MutableMap<K, V>
}
private class MapWithDefaultImpl<K, out V>(public override val map: Map<K,V>, private val default: (key: K) -> V) : MapWithDefault<K, V> {
private class MapWithDefaultImpl<K, out V>(public override val map: Map<K, V>, private val default: (key: K) -> V) : MapWithDefault<K, V> {
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
@@ -83,7 +80,7 @@ private class MapWithDefaultImpl<K, out V>(public override val map: Map<K,V>, pr
override fun getOrImplicitDefault(key: K): V = map.getOrElseNullable(key, { default(key) })
}
private class MutableMapWithDefaultImpl<K, V>(public override val map: MutableMap<K, V>, private val default: (key: K) -> V): MutableMapWithDefault<K, V> {
private class MutableMapWithDefaultImpl<K, V>(public override val map: MutableMap<K, V>, private val default: (key: K) -> V) : MutableMapWithDefault<K, V> {
override fun equals(other: Any?): Boolean = map.equals(other)
override fun hashCode(): Int = map.hashCode()
override fun toString(): String = map.toString()
+44 -45
View File
@@ -11,7 +11,7 @@ package kotlin.collections
private object EmptyMap : Map<Any?, Nothing>, Serializable {
private const val serialVersionUID: Long = 8246714829545688274
override fun equals(other: Any?): Boolean = other is Map<*,*> && other.isEmpty()
override fun equals(other: Any?): Boolean = other is Map<*, *> && other.isEmpty()
override fun hashCode(): Int = 0
override fun toString(): String = "{}"
@@ -48,7 +48,8 @@ public fun <K, V> emptyMap(): Map<K, V> = @Suppress("UNCHECKED_CAST") (EmptyMap
*
* @sample samples.collections.Maps.Instantiation.mapFromPairs
*/
public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> = if (pairs.size > 0) pairs.toMap(LinkedHashMap(mapCapacity(pairs.size))) else emptyMap()
public fun <K, V> mapOf(vararg pairs: Pair<K, V>): Map<K, V> =
if (pairs.size > 0) pairs.toMap(LinkedHashMap(mapCapacity(pairs.size))) else emptyMap()
/**
* Returns an empty read-only map.
@@ -80,8 +81,8 @@ public inline fun <K, V> mutableMapOf(): MutableMap<K, V> = LinkedHashMap()
* @sample samples.collections.Maps.Instantiation.mutableMapFromPairs
* @sample samples.collections.Maps.Instantiation.emptyMutableMap
*/
public fun <K, V> mutableMapOf(vararg pairs: Pair<K, V>): MutableMap<K, V>
= LinkedHashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
public fun <K, V> mutableMapOf(vararg pairs: Pair<K, V>): MutableMap<K, V> =
LinkedHashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
/**
* Returns an empty new [HashMap].
@@ -96,8 +97,7 @@ public inline fun <K, V> hashMapOf(): HashMap<K, V> = HashMap<K, V>()
*
* @sample samples.collections.Maps.Instantiation.hashMapFromPairs
*/
public fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V>
= HashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
public fun <K, V> hashMapOf(vararg pairs: Pair<K, V>): HashMap<K, V> = HashMap<K, V>(mapCapacity(pairs.size)).apply { putAll(pairs) }
/**
* Returns an empty new [LinkedHashMap].
@@ -116,8 +116,7 @@ public inline fun <K, V> linkedMapOf(): LinkedHashMap<K, V> = LinkedHashMap<K, V
*
* @sample samples.collections.Maps.Instantiation.linkedMapFromPairs
*/
public fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V>
= pairs.toMap(LinkedHashMap(mapCapacity(pairs.size)))
public fun <K, V> linkedMapOf(vararg pairs: Pair<K, V>): LinkedHashMap<K, V> = pairs.toMap(LinkedHashMap(mapCapacity(pairs.size)))
/**
* Calculate the initial capacity of a map, based on Guava's com.google.common.collect.Maps approach. This is equivalent
@@ -145,7 +144,7 @@ public inline fun <K, V> Map<out K, V>.isNotEmpty(): Boolean = !isEmpty()
* Returns the [Map] if its not `null`, or the empty [Map] otherwise.
*/
@kotlin.internal.InlineOnly
public inline fun <K, V> Map<K, V>?.orEmpty() : Map<K, V> = this ?: emptyMap()
public inline fun <K, V> Map<K, V>?.orEmpty(): Map<K, V> = this ?: emptyMap()
/**
* Checks if the map contains the given key.
@@ -153,14 +152,14 @@ public inline fun <K, V> Map<K, V>?.orEmpty() : Map<K, V> = this ?: emptyMap()
* This method allows to use the `x in map` syntax for checking whether an object is contained in the map.
*/
@kotlin.internal.InlineOnly
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.contains(key: K) : Boolean = containsKey(key)
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.contains(key: K): Boolean = containsKey(key)
/**
* Returns the value corresponding to the given [key], or `null` if such a key is not present in the map.
*/
@kotlin.internal.InlineOnly
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.get(key: K): V?
= @Suppress("UNCHECKED_CAST") (this as Map<K, V>).get(key)
public inline operator fun <@kotlin.internal.OnlyInputTypes K, V> Map<out K, V>.get(key: K): V? =
@Suppress("UNCHECKED_CAST") (this as Map<K, V>).get(key)
/**
* Allows to use the index operator for storing values in a mutable map.
@@ -176,8 +175,8 @@ public inline operator fun <K, V> MutableMap<K, V>.set(key: K, value: V): Unit {
* Allows to overcome type-safety restriction of `containsKey` that requires to pass a key of type `K`.
*/
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes K> Map<out K, *>.containsKey(key: K): Boolean
= @Suppress("UNCHECKED_CAST") (this as Map<K, *>).containsKey(key)
public inline fun <@kotlin.internal.OnlyInputTypes K> Map<out K, *>.containsKey(key: K): Boolean =
@Suppress("UNCHECKED_CAST") (this as Map<K, *>).containsKey(key)
/**
* Returns `true` if the map maps one or more keys to the specified [value].
@@ -197,8 +196,8 @@ public inline fun <K, @kotlin.internal.OnlyInputTypes V> Map<K, V>.containsValue
* Allows to overcome type-safety restriction of `remove` that requires to pass a key of type `K`.
*/
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap<out K, V>.remove(key: K): V?
= @Suppress("UNCHECKED_CAST") (this as MutableMap<K, V>).remove(key)
public inline fun <@kotlin.internal.OnlyInputTypes K, V> MutableMap<out K, V>.remove(key: K): V? =
@Suppress("UNCHECKED_CAST") (this as MutableMap<K, V>).remove(key)
/**
* Returns the key component of the map entry.
@@ -327,7 +326,7 @@ public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Array<out Pair<K, V>>): U
/**
* Puts all the elements of the given collection into this [MutableMap] with the first component in the pair being the key and the second the value.
*/
public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Iterable<Pair<K,V>>): Unit {
public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Iterable<Pair<K, V>>): Unit {
for ((key, value) in pairs) {
put(key, value)
}
@@ -336,7 +335,7 @@ public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Iterable<Pair<K,V>>): Uni
/**
* Puts all the elements of the given sequence into this [MutableMap] with the first component in the pair being the key and the second the value.
*/
public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Sequence<Pair<K,V>>): Unit {
public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Sequence<Pair<K, V>>): Unit {
for ((key, value) in pairs) {
put(key, value)
}
@@ -471,15 +470,15 @@ public fun <K, V> Iterable<Pair<K, V>>.toMap(): Map<K, V> {
/**
* Populates and returns the [destination] mutable map with key-value pairs from the given collection of pairs.
*/
public fun <K, V, M : MutableMap<in K, in V>> Iterable<Pair<K, V>>.toMap(destination: M): M
= destination.apply { putAll(this@toMap) }
public fun <K, V, M : MutableMap<in K, in V>> Iterable<Pair<K, V>>.toMap(destination: M): M =
destination.apply { putAll(this@toMap) }
/**
* Returns a new map containing all key-value pairs from the given array of pairs.
*
* The returned map preserves the entry iteration order of the original array.
*/
public fun <K, V> Array<out Pair<K, V>>.toMap(): Map<K, V> = when(size) {
public fun <K, V> Array<out Pair<K, V>>.toMap(): Map<K, V> = when (size) {
0 -> emptyMap()
1 -> mapOf(this[0])
else -> toMap(LinkedHashMap<K, V>(mapCapacity(size)))
@@ -488,8 +487,8 @@ public fun <K, V> Array<out Pair<K, V>>.toMap(): Map<K, V> = when(size) {
/**
* Populates and returns the [destination] mutable map with key-value pairs from the given array of pairs.
*/
public fun <K, V, M : MutableMap<in K, in V>> Array<out Pair<K, V>>.toMap(destination: M): M
= destination.apply { putAll(this@toMap) }
public fun <K, V, M : MutableMap<in K, in V>> Array<out Pair<K, V>>.toMap(destination: M): M =
destination.apply { putAll(this@toMap) }
/**
* Returns a new map containing all key-value pairs from the given sequence of pairs.
@@ -501,8 +500,8 @@ public fun <K, V> Sequence<Pair<K, V>>.toMap(): Map<K, V> = toMap(LinkedHashMap<
/**
* Populates and returns the [destination] mutable map with key-value pairs from the given sequence of pairs.
*/
public fun <K, V, M : MutableMap<in K, in V>> Sequence<Pair<K, V>>.toMap(destination: M): M
= destination.apply { putAll(this@toMap) }
public fun <K, V, M : MutableMap<in K, in V>> Sequence<Pair<K, V>>.toMap(destination: M): M =
destination.apply { putAll(this@toMap) }
/**
* Returns a new read-only map containing all key-value pairs from the original map.
@@ -528,8 +527,8 @@ public fun <K, V> Map<out K, V>.toMutableMap(): MutableMap<K, V> = LinkedHashMap
* Populates and returns the [destination] mutable map with key-value pairs from the given map.
*/
@SinceKotlin("1.1")
public fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.toMap(destination: M): M
= destination.apply { putAll(this@toMap) }
public fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.toMap(destination: M): M =
destination.apply { putAll(this@toMap) }
/**
* Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair].
@@ -537,8 +536,8 @@ public fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.toMap(destination: M
* The returned map preserves the entry iteration order of the original map.
* The [pair] is iterated in the end if it has a unique key.
*/
public operator fun <K, V> Map<out K, V>.plus(pair: Pair<K, V>): Map<K, V>
= if (this.isEmpty()) mapOf(pair) else LinkedHashMap(this).apply { put(pair.first, pair.second) }
public operator fun <K, V> Map<out K, V>.plus(pair: Pair<K, V>): Map<K, V> =
if (this.isEmpty()) mapOf(pair) else LinkedHashMap(this).apply { put(pair.first, pair.second) }
/**
* Creates a new read-only map by replacing or adding entries to this map from a given collection of key-value [pairs].
@@ -546,8 +545,8 @@ public operator fun <K, V> Map<out K, V>.plus(pair: Pair<K, V>): Map<K, V>
* The returned map preserves the entry iteration order of the original map.
* Those [pairs] with unique keys are iterated in the end in the order of [pairs] collection.
*/
public operator fun <K, V> Map<out K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V>
= if (this.isEmpty()) pairs.toMap() else LinkedHashMap(this).apply { putAll(pairs) }
public operator fun <K, V> Map<out K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V> =
if (this.isEmpty()) pairs.toMap() else LinkedHashMap(this).apply { putAll(pairs) }
/**
* Creates a new read-only map by replacing or adding entries to this map from a given array of key-value [pairs].
@@ -555,8 +554,8 @@ public operator fun <K, V> Map<out K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<
* The returned map preserves the entry iteration order of the original map.
* Those [pairs] with unique keys are iterated in the end in the order of [pairs] array.
*/
public operator fun <K, V> Map<out K, V>.plus(pairs: Array<out Pair<K, V>>): Map<K, V>
= if (this.isEmpty()) pairs.toMap() else LinkedHashMap(this).apply { putAll(pairs) }
public operator fun <K, V> Map<out K, V>.plus(pairs: Array<out Pair<K, V>>): Map<K, V> =
if (this.isEmpty()) pairs.toMap() else LinkedHashMap(this).apply { putAll(pairs) }
/**
* Creates a new read-only map by replacing or adding entries to this map from a given sequence of key-value [pairs].
@@ -564,8 +563,8 @@ public operator fun <K, V> Map<out K, V>.plus(pairs: Array<out Pair<K, V>>): Map
* The returned map preserves the entry iteration order of the original map.
* Those [pairs] with unique keys are iterated in the end in the order of [pairs] sequence.
*/
public operator fun <K, V> Map<out K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V>
= LinkedHashMap(this).apply { putAll(pairs) }.optimizeReadOnlyMap()
public operator fun <K, V> Map<out K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V> =
LinkedHashMap(this).apply { putAll(pairs) }.optimizeReadOnlyMap()
/**
* Creates a new read-only map by replacing or adding entries to this map from another [map].
@@ -573,8 +572,8 @@ public operator fun <K, V> Map<out K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<
* The returned map preserves the entry iteration order of the original map.
* Those entries of another [map] that are missing in this map are iterated in the end in the order of that [map].
*/
public operator fun <K, V> Map<out K, V>.plus(map: Map<out K, V>): Map<K, V>
= LinkedHashMap(this).apply { putAll(map) }
public operator fun <K, V> Map<out K, V>.plus(map: Map<out K, V>): Map<K, V> =
LinkedHashMap(this).apply { putAll(map) }
/**
@@ -623,8 +622,8 @@ public inline operator fun <K, V> MutableMap<in K, in V>.plusAssign(map: Map<K,
* The returned map preserves the entry iteration order of the original map.
*/
@SinceKotlin("1.1")
public operator fun <K, V> Map<out K, V>.minus(key: K): Map<K, V>
= this.toMutableMap().apply { minusAssign(key) }.optimizeReadOnlyMap()
public operator fun <K, V> Map<out K, V>.minus(key: K): Map<K, V> =
this.toMutableMap().apply { minusAssign(key) }.optimizeReadOnlyMap()
/**
* Returns a map containing all entries of the original map except those entries
@@ -633,8 +632,8 @@ public operator fun <K, V> Map<out K, V>.minus(key: K): Map<K, V>
* The returned map preserves the entry iteration order of the original map.
*/
@SinceKotlin("1.1")
public operator fun <K, V> Map<out K, V>.minus(keys: Iterable<K>): Map<K, V>
= this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
public operator fun <K, V> Map<out K, V>.minus(keys: Iterable<K>): Map<K, V> =
this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
/**
* Returns a map containing all entries of the original map except those entries
@@ -643,8 +642,8 @@ public operator fun <K, V> Map<out K, V>.minus(keys: Iterable<K>): Map<K, V>
* The returned map preserves the entry iteration order of the original map.
*/
@SinceKotlin("1.1")
public operator fun <K, V> Map<out K, V>.minus(keys: Array<out K>): Map<K, V>
= this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
public operator fun <K, V> Map<out K, V>.minus(keys: Array<out K>): Map<K, V> =
this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
/**
* Returns a map containing all entries of the original map except those entries
@@ -653,8 +652,8 @@ public operator fun <K, V> Map<out K, V>.minus(keys: Array<out K>): Map<K, V>
* The returned map preserves the entry iteration order of the original map.
*/
@SinceKotlin("1.1")
public operator fun <K, V> Map<out K, V>.minus(keys: Sequence<K>): Map<K, V>
= this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
public operator fun <K, V> Map<out K, V>.minus(keys: Sequence<K>): Map<K, V> =
this.toMutableMap().apply { minusAssign(keys) }.optimizeReadOnlyMap()
/**
* Removes the entry with the given [key] from this mutable map.
@@ -17,8 +17,8 @@ package kotlin.collections
* @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
*/
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.remove(element: T): Boolean
= @Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).remove(element)
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.remove(element: T): Boolean =
@Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).remove(element)
/**
* Removes all of this collection's elements that are also contained in the specified collection.
@@ -28,8 +28,8 @@ public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.r
* @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified.
*/
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.removeAll(elements: Collection<T>): Boolean
= @Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).removeAll(elements)
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.removeAll(elements: Collection<T>): Boolean =
@Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).removeAll(elements)
/**
* Retains only the elements in this collection that are contained in the specified collection.
@@ -39,8 +39,8 @@ public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.r
* @return `true` if any element was removed from the collection, `false` if the collection was not modified.
*/
@kotlin.internal.InlineOnly
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.retainAll(elements: Collection<T>): Boolean
= @Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).retainAll(elements)
public inline fun <@kotlin.internal.OnlyInputTypes T> MutableCollection<out T>.retainAll(elements: Collection<T>): Boolean =
@Suppress("UNCHECKED_CAST") (this as MutableCollection<T>).retainAll(elements)
/**
* Removes the element at the specified [index] from this list.
@@ -159,7 +159,7 @@ public fun <T> MutableIterable<T>.retainAll(predicate: (T) -> Boolean): Boolean
private fun <T> MutableIterable<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
var result = false
with (iterator()) {
with(iterator()) {
while (hasNext())
if (predicate(next()) == predicateResultToRemove) {
remove()
@@ -199,8 +199,7 @@ private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicat
removeAt(removeIndex)
return true
}
else {
} else {
return false
}
}
@@ -27,11 +27,12 @@ private class ReversedList<T>(private val delegate: MutableList<T>) : AbstractMu
delegate.add(reversePositionIndex(index), element)
}
}
private fun List<*>.reverseElementIndex(index: Int) =
if (index in 0..lastIndex) lastIndex - index else throw IndexOutOfBoundsException("Element index $index must be in range [${0..lastIndex}].")
if (index in 0..lastIndex) lastIndex - index else throw IndexOutOfBoundsException("Element index $index must be in range [${0..lastIndex}].")
private fun List<*>.reversePositionIndex(index: Int) =
if (index in 0..size) size - index else throw IndexOutOfBoundsException("Position index $index must be in range [${0..size}].")
if (index in 0..size) size - index else throw IndexOutOfBoundsException("Position index $index must be in range [${0..size}].")
/**
@@ -91,12 +91,13 @@ public fun <T, R> Sequence<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
* the specified [predicate].
*
* @param sendWhen If `true`, values for which the predicate returns `true` are returned. Otherwise,
* values for which the predicate returns `false` are returned
* values for which the predicate returns `false` are returned
*/
internal class FilteringSequence<T>(private val sequence: Sequence<T>,
private val sendWhen: Boolean = true,
private val predicate: (T) -> Boolean
) : Sequence<T> {
internal class FilteringSequence<T>(
private val sequence: Sequence<T>,
private val sendWhen: Boolean = true,
private val predicate: (T) -> Boolean
) : Sequence<T> {
override fun iterator(): Iterator<T> = object : Iterator<T> {
val iterator = sequence.iterator()
@@ -203,10 +204,11 @@ constructor(private val sequence: Sequence<T>) : Sequence<IndexedValue<T>> {
* values as soon as one of the underlying sequences stops returning values.
*/
internal class MergingSequence<T1, T2, V>
constructor(private val sequence1: Sequence<T1>,
private val sequence2: Sequence<T2>,
private val transform: (T1, T2) -> V
) : Sequence<V> {
constructor(
private val sequence1: Sequence<T1>,
private val sequence2: Sequence<T2>,
private val transform: (T1, T2) -> V
) : Sequence<V> {
override fun iterator(): Iterator<V> = object : Iterator<V> {
val iterator1 = sequence1.iterator()
val iterator2 = sequence2.iterator()
@@ -221,11 +223,11 @@ internal class MergingSequence<T1, T2, V>
}
internal class FlatteningSequence<T, R, E>
constructor(
private val sequence: Sequence<T>,
private val transformer: (T) -> R,
private val iterator: (R) -> Iterator<E>
) : Sequence<E> {
constructor(
private val sequence: Sequence<T>,
private val transformer: (T) -> R,
private val iterator: (R) -> Iterator<E>
) : Sequence<E> {
override fun iterator(): Iterator<E> = object : Iterator<E> {
val iterator = sequence.iterator()
var itemIterator: Iterator<E>? = null
@@ -273,16 +275,16 @@ internal interface DropTakeSequence<T> : Sequence<T> {
* A sequence that skips [startIndex] values from the underlying [sequence]
* and stops returning values right before [endIndex], i.e. stops at `endIndex - 1`
*/
internal class SubSequence<T> (
private val sequence: Sequence<T>,
private val startIndex: Int,
private val endIndex: Int
): Sequence<T>, DropTakeSequence<T> {
internal class SubSequence<T>(
private val sequence: Sequence<T>,
private val startIndex: Int,
private val endIndex: Int
) : Sequence<T>, DropTakeSequence<T> {
init {
require(startIndex >= 0) { "startIndex should be non-negative, but is $startIndex" }
require(endIndex >= 0) { "endIndex should be non-negative, but is $endIndex" }
require(endIndex >= startIndex) { "endIndex should be not less than startIndex, but was $endIndex < $startIndex"}
require(endIndex >= startIndex) { "endIndex should be not less than startIndex, but was $endIndex < $startIndex" }
}
private val count: Int get() = endIndex - startIndex
@@ -297,7 +299,7 @@ internal class SubSequence<T> (
// Shouldn't be called from constructor to avoid premature iteration
private fun drop() {
while(position < startIndex && iterator.hasNext()) {
while (position < startIndex && iterator.hasNext()) {
iterator.next()
position++
}
@@ -322,13 +324,13 @@ internal class SubSequence<T> (
* A sequence that returns at most [count] values from the underlying [sequence], and stops returning values
* as soon as that count is reached.
*/
internal class TakeSequence<T> (
internal class TakeSequence<T>(
private val sequence: Sequence<T>,
private val count: Int
) : Sequence<T>, DropTakeSequence<T> {
init {
require (count >= 0) { "count must be non-negative, but was $count." }
require(count >= 0) { "count must be non-negative, but was $count." }
}
override fun drop(n: Int): Sequence<T> = if (n >= count) emptySequence() else SubSequence(sequence, n, count)
@@ -356,9 +358,10 @@ internal class TakeSequence<T> (
* `true`, and stops returning values once the function returns `false` for the next element.
*/
internal class TakeWhileSequence<T>
constructor(private val sequence: Sequence<T>,
private val predicate: (T) -> Boolean
) : Sequence<T> {
constructor(
private val sequence: Sequence<T>,
private val predicate: (T) -> Boolean
) : Sequence<T> {
override fun iterator(): Iterator<T> = object : Iterator<T> {
val iterator = sequence.iterator()
var nextState: Int = -1 // -1 for unknown, 0 for done, 1 for continue
@@ -402,12 +405,12 @@ internal class TakeWhileSequence<T>
* A sequence that skips the specified number of values from the underlying [sequence] and returns
* all values after that.
*/
internal class DropSequence<T> (
private val sequence: Sequence<T>,
private val count: Int
internal class DropSequence<T>(
private val sequence: Sequence<T>,
private val count: Int
) : Sequence<T>, DropTakeSequence<T> {
init {
require (count >= 0) { "count must be non-negative, but was $count." }
require(count >= 0) { "count must be non-negative, but was $count." }
}
override fun drop(n: Int): Sequence<T> = DropSequence(sequence, count + n)
@@ -442,9 +445,10 @@ internal class DropSequence<T> (
* all values after that.
*/
internal class DropWhileSequence<T>
constructor(private val sequence: Sequence<T>,
private val predicate: (T) -> Boolean
) : Sequence<T> {
constructor(
private val sequence: Sequence<T>,
private val predicate: (T) -> Boolean
) : Sequence<T> {
override fun iterator(): Iterator<T> = object : Iterator<T> {
val iterator = sequence.iterator()
@@ -485,11 +489,11 @@ internal class DropWhileSequence<T>
}
}
internal class DistinctSequence<T, K>(private val source : Sequence<T>, private val keySelector : (T) -> K) : Sequence<T> {
internal class DistinctSequence<T, K>(private val source: Sequence<T>, private val keySelector: (T) -> K) : Sequence<T> {
override fun iterator(): Iterator<T> = DistinctIterator(source.iterator(), keySelector)
}
private class DistinctIterator<T, K>(private val source : Iterator<T>, private val keySelector : (T) -> K) : AbstractIterator<T>() {
private class DistinctIterator<T, K>(private val source: Iterator<T>, private val keySelector: (T) -> K) : AbstractIterator<T>() {
private val observed = HashSet<K>()
override fun computeNext() {
@@ -508,7 +512,7 @@ private class DistinctIterator<T, K>(private val source : Iterator<T>, private v
}
private class GeneratorSequence<T: Any>(private val getInitialValue: () -> T?, private val getNextValue: (T) -> T?): Sequence<T> {
private class GeneratorSequence<T : Any>(private val getInitialValue: () -> T?, private val getNextValue: (T) -> T?) : Sequence<T> {
override fun iterator(): Iterator<T> = object : Iterator<T> {
var nextItem: T? = null
var nextState: Int = -2 // -2 for initial unknown, -1 for next unknown, 0 for done, 1 for continue
@@ -553,7 +557,6 @@ public fun <T> Sequence<T>.constrainOnce(): Sequence<T> {
}
/**
* Returns a sequence which invokes the function to calculate the next value on each iteration until the function returns `null`.
*
@@ -601,6 +604,6 @@ public fun <T : Any> generateSequence(seed: T?, nextFunction: (T) -> T?): Sequen
*
* @sample samples.collections.Sequences.Building.generateSequenceWithLazySeed
*/
public fun <T: Any> generateSequence(seedFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> =
GeneratorSequence(seedFunction, nextFunction)
public fun <T : Any> generateSequence(seedFunction: () -> T?, nextFunction: (T) -> T?): Sequence<T> =
GeneratorSequence(seedFunction, nextFunction)
@@ -32,6 +32,7 @@ internal object EmptySet : Set<Nothing>, Serializable {
* @sample samples.collections.Collections.Sets.emptyReadOnlySet
*/
public fun <T> emptySet(): Set<T> = EmptySet
/**
* Returns a new read-only set with the given elements.
* Elements of the set are iterated in the order they were specified.
@@ -86,7 +86,7 @@ internal class MovingSubList<out E>(private val list: List<E>) : AbstractList<E>
*
* Buffer overflow is not allowed so [add] doesn't overwrite tail but raises an exception.
*/
private class RingBuffer<T>(val capacity: Int): AbstractList<T>(), RandomAccess {
private class RingBuffer<T>(val capacity: Int) : AbstractList<T>(), RandomAccess {
init {
require(capacity >= 0) { "ring buffer capacity should not be negative but it is $capacity" }
}
@@ -106,25 +106,25 @@ private class RingBuffer<T>(val capacity: Int): AbstractList<T>(), RandomAccess
fun isFull() = size == capacity
override fun iterator(): Iterator<T> = object : AbstractIterator<T>() {
private var count = size
private var index = startIndex
private var count = size
private var index = startIndex
override fun computeNext() {
if (count == 0) {
done()
} else {
@Suppress("UNCHECKED_CAST")
setNext(buffer[index] as T)
index = index.forward(1)
count--
}
override fun computeNext() {
if (count == 0) {
done()
} else {
@Suppress("UNCHECKED_CAST")
setNext(buffer[index] as T)
index = index.forward(1)
count--
}
}
}
@Suppress("UNCHECKED_CAST")
override fun <T> toArray(array: Array<T>): Array<T> {
val result: Array<T?> =
if (array.size < this.size) array.copyOf(this.size) else array as Array<T?>
if (array.size < this.size) array.copyOf(this.size) else array as Array<T?>
val size = this.size