More of stdlib. (#183)
This commit is contained in:
+1
-1
@@ -37,5 +37,5 @@ val PropertyDescriptor.backingField: PropertyDescriptor?
|
||||
}
|
||||
|
||||
fun DeclarationDescriptor.deepPrint() {
|
||||
this!!.accept(DeepPrintVisitor(PrintVisitor()), 0)
|
||||
this.accept(DeepPrintVisitor(PrintVisitor()), 0)
|
||||
}
|
||||
|
||||
+2
@@ -1334,6 +1334,8 @@ internal class CodeGeneratorVisitor(val context: Context) : IrElementVisitorVoid
|
||||
|
||||
private fun genInstanceOf(obj: LLVMValueRef, type: KotlinType): LLVMValueRef {
|
||||
val dstDescriptor = TypeUtils.getClassDescriptor(type) // Get class descriptor for dst type.
|
||||
// Reified parameters are not yet supported.
|
||||
assert(dstDescriptor != null)
|
||||
val dstTypeInfo = codegen.typeInfoValue(dstDescriptor!!) // Get TypeInfo for dst type.
|
||||
val srcObjInfoPtr = codegen.bitcast(codegen.kObjHeaderPtr, obj) // Cast src to ObjInfoPtr.
|
||||
val args = listOf(srcObjInfoPtr, dstTypeInfo) // Create arg list.
|
||||
|
||||
@@ -370,7 +370,11 @@ KInt Kotlin_Float_bits (KFloat a) {
|
||||
return alias.i;
|
||||
}
|
||||
|
||||
//--- Double ------------------------------------------------------------------//
|
||||
KBoolean Kotlin_Float_isNaN (KFloat a) { return isnan(a); }
|
||||
KBoolean Kotlin_Float_isInfinite (KFloat a) { return isinf(a); }
|
||||
KBoolean Kotlin_Float_isFinite (KFloat a) { return isfinite(a); }
|
||||
|
||||
//--- Double ------------------------------------------------------------------//
|
||||
|
||||
KInt Kotlin_Double_compareTo_Byte (KDouble a, KByte b) { if (a == b) return 0; return (a < b) ? -1 : 1; }
|
||||
KInt Kotlin_Double_compareTo_Short (KDouble a, KShort b) { if (a == b) return 0; return (a < b) ? -1 : 1; }
|
||||
@@ -435,4 +439,8 @@ KLong Kotlin_Double_bits (KDouble a) {
|
||||
return alias.l;
|
||||
}
|
||||
|
||||
KBoolean Kotlin_Double_isNaN (KDouble a) { return isnan(a); }
|
||||
KBoolean Kotlin_Double_isInfinite (KDouble a) { return isinf(a); }
|
||||
KBoolean Kotlin_Double_isFinite (KDouble a) { return isfinite(a); }
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -31,6 +31,31 @@ public annotation class Used
|
||||
*/
|
||||
public annotation class FixmeInner
|
||||
|
||||
/**
|
||||
* Need to be fixed because of reification support.
|
||||
*/
|
||||
public annotation class FixmeReified
|
||||
|
||||
/**
|
||||
* Need to be fixed because of sorting support.
|
||||
*/
|
||||
public annotation class FixmeSorting
|
||||
|
||||
/**
|
||||
* Need to be fixed because of specialization support.
|
||||
*/
|
||||
public annotation class FixmeSpecialization
|
||||
|
||||
/**
|
||||
* Need to be fixed because of sequences support.
|
||||
*/
|
||||
public annotation class FixmeSequences
|
||||
|
||||
/**
|
||||
* Need to be fixed because of variance support.
|
||||
*/
|
||||
public annotation class FixmeVariance
|
||||
|
||||
/**
|
||||
* Need to be fixed.
|
||||
*/
|
||||
|
||||
@@ -6,7 +6,8 @@ package kotlin
|
||||
*/
|
||||
//@Target(CLASS, ANNOTATION_CLASS, PROPERTY, FIELD, LOCAL_VARIABLE, VALUE_PARAMETER,
|
||||
// CONSTRUCTOR, FUNCTION, PROPERTY_GETTER, PROPERTY_SETTER, TYPE, EXPRESSION, FILE, TYPEALIAS)
|
||||
@Target(AnnotationTarget.TYPE, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION)
|
||||
@Target(AnnotationTarget.TYPE, AnnotationTarget.VALUE_PARAMETER, AnnotationTarget.EXPRESSION,
|
||||
AnnotationTarget.LOCAL_VARIABLE, AnnotationTarget.FUNCTION)
|
||||
//@Retention(SOURCE)
|
||||
public annotation class Suppress(vararg val names: String)
|
||||
|
||||
|
||||
@@ -48,13 +48,6 @@ private class IteratorImpl<T>(val collection: Array<T>) : Iterator<T> {
|
||||
|
||||
fun <T> arrayOf(vararg elements: T) : Array<out T> = elements
|
||||
|
||||
public fun <T, C : MutableCollection<in T>> Array<out T>.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <T> Array<T>.plus(elements: Array<T>): Array<T> {
|
||||
val result = copyOfUninitializedElements(this.size + elements.size)
|
||||
|
||||
@@ -488,14 +488,6 @@ public inline fun <T> Array<out T>.lastOrNull(predicate: (T) -> Boolean): T? {
|
||||
return null
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Array<out T>.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the range of valid indices for the array.
|
||||
*/
|
||||
@@ -592,4 +584,300 @@ public fun Array<out Double>.sum(): Double {
|
||||
return sum
|
||||
}
|
||||
|
||||
// From _Arrays.kt.
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Array<out T>.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun BooleanArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.isEmpty(): Boolean {
|
||||
return size == 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Array<out T>.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ByteArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun ShortArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun IntArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun LongArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun FloatArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun DoubleArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun BooleanArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns `true` if the array is not empty.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun CharArray.isNotEmpty(): Boolean {
|
||||
return !isEmpty()
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <T, C : MutableCollection<in T>> Array<out T>.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Byte>> ByteArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Short>> ShortArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Int>> IntArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Long>> LongArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Float>> FloatArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Double>> DoubleArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Boolean>> BooleanArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Appends all elements to the given [destination] collection.
|
||||
*/
|
||||
public fun <C : MutableCollection<in Char>> CharArray.toCollection(destination: C): C {
|
||||
for (item in this) {
|
||||
destination.add(item)
|
||||
}
|
||||
return destination
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun <T> Array<out T>.toHashSet(): HashSet<T> {
|
||||
return toCollection(HashSet<T>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun ByteArray.toHashSet(): HashSet<Byte> {
|
||||
return toCollection(HashSet<Byte>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun ShortArray.toHashSet(): HashSet<Short> {
|
||||
return toCollection(HashSet<Short>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun IntArray.toHashSet(): HashSet<Int> {
|
||||
return toCollection(HashSet<Int>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun LongArray.toHashSet(): HashSet<Long> {
|
||||
return toCollection(HashSet<Long>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun FloatArray.toHashSet(): HashSet<Float> {
|
||||
return toCollection(HashSet<Float>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun DoubleArray.toHashSet(): HashSet<Double> {
|
||||
return toCollection(HashSet<Double>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun BooleanArray.toHashSet(): HashSet<Boolean> {
|
||||
return toCollection(HashSet<Boolean>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a [HashSet] of all elements.
|
||||
*/
|
||||
public fun CharArray.toHashSet(): HashSet<Char> {
|
||||
return toCollection(HashSet<Char>(mapCapacity(size)))
|
||||
}
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package kotlin
|
||||
|
||||
import kotlin.comparisons.Comparator
|
||||
|
||||
/**
|
||||
* Classes which inherit from this interface have a defined total ordering between their instances.
|
||||
*/
|
||||
@@ -11,3 +13,6 @@ public interface Comparable<in T> {
|
||||
*/
|
||||
public operator fun compareTo(other: T): Int
|
||||
}
|
||||
|
||||
typealias Comparator<T> = kotlin.comparisons.Comparator<T>
|
||||
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* Returns `true` if the specified number is a
|
||||
* Not-a-Number (NaN) value, `false` otherwise.
|
||||
*/
|
||||
@SymbolName("Kotlin_Double_isNaN")
|
||||
external public fun Double.isNaN(): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if the specified number is a
|
||||
* Not-a-Number (NaN) value, `false` otherwise.
|
||||
*/
|
||||
@SymbolName("Kotlin_Float_isNaN")
|
||||
external public fun Float.isNaN(): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if this value is infinitely large in magnitude.
|
||||
*/
|
||||
@SymbolName("Kotlin_Double_isInfinite")
|
||||
external public fun Double.isInfinite(): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if this value is infinitely large in magnitude.
|
||||
*/
|
||||
@SymbolName("Kotlin_Float_isInfinite")
|
||||
external public fun Float.isInfinite(): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if the argument is a finite floating-point value; returns `false` otherwise (for `NaN` and infinity arguments).
|
||||
*/
|
||||
@SymbolName("Kotlin_Double_isFinite")
|
||||
external public fun Double.isFinite(): Boolean
|
||||
|
||||
/**
|
||||
* Returns `true` if the argument is a finite floating-point value; returns `false` otherwise (for `NaN` and infinity arguments).
|
||||
*/
|
||||
@SymbolName("Kotlin_Float_isFinite")
|
||||
external public fun Float.isFinite(): Boolean
|
||||
@@ -5,7 +5,7 @@ class ArrayList<E> private constructor(
|
||||
private var offset: Int,
|
||||
private var length: Int,
|
||||
private val backing: ArrayList<E>?
|
||||
) : MutableList<E> {
|
||||
) : MutableList<E>, RandomAccess {
|
||||
|
||||
constructor() : this(10)
|
||||
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -670,4 +670,7 @@ internal class HashMapEntrySet<K, V> internal constructor(
|
||||
@Suppress("UNCHECKED_CAST") // todo: get rid of unchecked cast here somehow
|
||||
return size == other.size && backing.containsAllEntries(other as Collection<Map.Entry<*, *>>)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// This hash map keeps insertion order.
|
||||
typealias LinkedHashMap<K, V> = HashMap<K, V>
|
||||
@@ -82,4 +82,7 @@ class HashSet<K> internal constructor(
|
||||
// ---------------------------- private ----------------------------
|
||||
|
||||
private fun contentEquals(other: Set<K>): Boolean = size == other.size && containsAll(other)
|
||||
}
|
||||
}
|
||||
|
||||
// This hash set keeps insertion order.
|
||||
typealias LinkedHashSet<V> = HashSet<V>
|
||||
@@ -0,0 +1,9 @@
|
||||
package kotlin.collections
|
||||
|
||||
/**
|
||||
* Data class representing a value from a collection or sequence, along with its index in that collection or sequence.
|
||||
*
|
||||
* @property value the underlying value.
|
||||
* @property index the index of the value in the collection or sequence.
|
||||
*/
|
||||
public data class IndexedValue<out T>(public val index: Int, public val value: T)
|
||||
@@ -0,0 +1,82 @@
|
||||
package kotlin.collections
|
||||
|
||||
/**
|
||||
* Given an [iterator] function constructs an [Iterable] instance that returns values through the [Iterator]
|
||||
* provided by that function.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Iterable(crossinline iterator: () -> Iterator<T>): Iterable<T> = object : Iterable<T> {
|
||||
override fun iterator(): Iterator<T> = iterator()
|
||||
}
|
||||
|
||||
/**
|
||||
* A wrapper over another [Iterable] (or any other object that can produce an [Iterator]) that returns
|
||||
* an indexing iterator.
|
||||
*/
|
||||
internal class IndexingIterable<out T>(private val iteratorFactory: () -> Iterator<T>) : Iterable<IndexedValue<T>> {
|
||||
override fun iterator(): Iterator<IndexedValue<T>> = IndexingIterator(iteratorFactory())
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns the size of this iterable if it is known, or `null` otherwise.
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun <T> Iterable<T>.collectionSizeOrNull(): Int? = if (this is Collection<*>) this.size else null
|
||||
|
||||
/**
|
||||
* Returns the size of this iterable if it is known, or the specified [default] value otherwise.
|
||||
*/
|
||||
@PublishedApi
|
||||
internal fun <T> Iterable<T>.collectionSizeOrDefault(default: Int): Int = if (this is Collection<*>) this.size else default
|
||||
|
||||
/** Returns true when it's safe to convert this collection to a set without changing contains method behavior. */
|
||||
private fun <T> Collection<T>.safeToConvertToSet() = size > 2 && this is ArrayList
|
||||
|
||||
/** 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()
|
||||
}
|
||||
|
||||
/** 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()
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a single list of all elements from all collections in the given collection.
|
||||
*/
|
||||
public fun <T> Iterable<Iterable<T>>.flatten(): List<T> {
|
||||
val result = ArrayList<T>()
|
||||
for (element in this) {
|
||||
result.addAll(element)
|
||||
}
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a pair of lists, where
|
||||
* *first* list is built from the first values of each pair from this collection,
|
||||
* *second* list is built from the second values of each pair from this collection.
|
||||
*/
|
||||
public fun <T, R> Iterable<Pair<T, R>>.unzip(): Pair<List<T>, List<R>> {
|
||||
val expectedSize = collectionSizeOrDefault(10)
|
||||
val listT = ArrayList<T>(expectedSize)
|
||||
val listR = ArrayList<R>(expectedSize)
|
||||
for (pair in this) {
|
||||
listT.add(pair.first)
|
||||
listR.add(pair.second)
|
||||
}
|
||||
return listT to listR
|
||||
}
|
||||
@@ -63,3 +63,31 @@ public abstract class BooleanIterator : Iterator<Boolean> {
|
||||
/** Returns the next value in the sequence without boxing. */
|
||||
public abstract fun nextBoolean(): Boolean
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the given iterator itself. This allows to use an instance of iterator in a `for` loop.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline operator fun <T> Iterator<T>.iterator(): Iterator<T> = this
|
||||
|
||||
/**
|
||||
* Returns an [Iterator] wrapping each value produced by this [Iterator] with the [IndexedValue],
|
||||
* containing value and it's index.
|
||||
*/
|
||||
public fun <T> Iterator<T>.withIndex(): Iterator<IndexedValue<T>> = IndexingIterator(this)
|
||||
|
||||
/**
|
||||
* Performs the given [operation] on each element of this [Iterator].
|
||||
*/
|
||||
public inline fun <T> Iterator<T>.forEach(operation: (T) -> Unit) : Unit {
|
||||
for (element in this) operation(element)
|
||||
}
|
||||
|
||||
/**
|
||||
* Iterator transforming original `iterator` into iterator of [IndexedValue], counting index from zero.
|
||||
*/
|
||||
internal class IndexingIterator<out T>(private val iterator: Iterator<T>) : Iterator<IndexedValue<T>> {
|
||||
private var index = 0
|
||||
final override fun hasNext(): Boolean = iterator.hasNext()
|
||||
final override fun next(): IndexedValue<T> = IndexedValue(index++, iterator.next())
|
||||
}
|
||||
@@ -97,8 +97,7 @@ internal fun mapCapacity(expectedSize: Int): Int {
|
||||
return Int.MAX_VALUE // any large value
|
||||
}
|
||||
|
||||
@Fixme
|
||||
private const val INT_MAX_POWER_OF_TWO: Int = 0x40000000 // Int.MAX_VALUE / 2 + 1
|
||||
private const val INT_MAX_POWER_OF_TWO: Int = Int.MAX_VALUE / 2 + 1
|
||||
|
||||
/** Returns `true` if this map is not empty. */
|
||||
@kotlin.internal.InlineOnly
|
||||
@@ -192,19 +191,14 @@ public inline fun <K, V> Map.Entry<K, V>.toPair(): Pair<K, V> = Pair(key, value)
|
||||
public inline fun <K, V> Map<K, V>.getOrElse(key: K, defaultValue: () -> V): V = get(key) ?: defaultValue()
|
||||
|
||||
|
||||
@Fixme
|
||||
internal inline fun <K, V> Map<K, V>.getOrElseNullable(key: K, defaultValue: () -> V): V {
|
||||
TODO()
|
||||
val value = get(key)
|
||||
if (value == null && !containsKey(key)) {
|
||||
return defaultValue()
|
||||
} else {
|
||||
return value as V
|
||||
}
|
||||
}
|
||||
// val value = get(key)
|
||||
// if (value == null && !containsKey(key)) {
|
||||
// return defaultValue()
|
||||
// } else {
|
||||
// return value as V
|
||||
// }
|
||||
//}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Returns the value for the given key. If the key is not found in the map, calls the [defaultValue] function,
|
||||
@@ -242,11 +236,9 @@ public inline operator fun <K, V> MutableMap<K, V>.iterator(): MutableIterator<M
|
||||
* Populates the given [destination] map with entries having the keys of this map and the values obtained
|
||||
* by applying the [transform] function to each entry in this [Map].
|
||||
*/
|
||||
@Fixme
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V, R, M : MutableMap<in K, in R>> Map<out K, V>.mapValuesTo(destination: M, transform: (Map.Entry<K, V>) -> R): M {
|
||||
TODO()
|
||||
// return entries.associateByTo(destination, { it.key }, transform)
|
||||
return entries.associateByTo(destination, { it.key }, transform)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -258,8 +250,7 @@ public inline fun <K, V, R, M : MutableMap<in K, in R>> Map<out K, V>.mapValuesT
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <K, V, R, M : MutableMap<in R, in V>> Map<out K, V>.mapKeysTo(destination: M, transform: (Map.Entry<K, V>) -> R): M {
|
||||
TODO()
|
||||
// return entries.associateByTo(destination, transform, { it.value })
|
||||
return entries.associateByTo(destination, transform, { it.value })
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -297,9 +288,12 @@ public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Sequence<Pair<K,V>>): Uni
|
||||
*
|
||||
* @sample samples.collections.Maps.Transforms.mapValues
|
||||
*/
|
||||
//public inline fun <K, V, R> Map<out K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R> {
|
||||
// return mapValuesTo(HashMap<K, R>(mapCapacity(size)), transform) // .optimizeReadOnlyMap()
|
||||
//}
|
||||
public inline fun <K, V, R> Map<out K, V>.mapValues(transform: (Map.Entry<K, V>) -> R): Map<K, R> {
|
||||
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
|
||||
return mapValuesTo(HashMap<K, R>(
|
||||
mapCapacity(size)),
|
||||
transform).optimizeReadOnlyMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a new Map with entries having the keys obtained by applying the [transform] function to each entry in this
|
||||
@@ -312,9 +306,12 @@ public fun <K, V> MutableMap<in K, in V>.putAll(pairs: Sequence<Pair<K,V>>): Uni
|
||||
*
|
||||
* @sample samples.collections.Maps.Transforms.mapKeys
|
||||
*/
|
||||
//public inline fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V> {
|
||||
// return mapKeysTo(HashMap<R, V>(mapCapacity(size)), transform) // .optimizeReadOnlyMap()
|
||||
//}
|
||||
public inline fun <K, V, R> Map<out K, V>.mapKeys(transform: (Map.Entry<K, V>) -> R): Map<R, V> {
|
||||
@Suppress("NON_PUBLIC_CALL_FROM_PUBLIC_INLINE")
|
||||
return mapKeysTo(HashMap<R, V>(
|
||||
mapCapacity(size)),
|
||||
transform).optimizeReadOnlyMap()
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a map containing all key-value pairs with keys matching the given [predicate].
|
||||
@@ -412,6 +409,7 @@ 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.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <K, V, M : MutableMap<in K, in V>> Iterable<Pair<K, V>>.toMap(destination: M): M {
|
||||
for (pair in this) {
|
||||
destination.put(pair.first, pair.second)
|
||||
@@ -434,6 +432,7 @@ 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.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <K, V, M : MutableMap<in K, in V>> Array<out Pair<K, V>>.toMap(destination: M): M {
|
||||
// = destination.apply { putAll(this@toMap) }
|
||||
for (pair in this) {
|
||||
@@ -452,6 +451,7 @@ public fun <K, V> Sequence<Pair<K, V>>.toMap(): Map<K, V> = toMap(HashMap<K, V>(
|
||||
/**
|
||||
* Populates and returns the [destination] mutable map with key-value pairs from the given sequence of pairs.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <K, V, M : MutableMap<in K, in V>> Sequence<Pair<K, V>>.toMap(destination: M): M {
|
||||
for (pair in this) {
|
||||
destination.put(pair.first, pair.second)
|
||||
@@ -484,15 +484,15 @@ public fun <K, V> Map</*out */K, V>.toMutableMap(): MutableMap<K, V> = HashMap(t
|
||||
public fun <K, V, M : MutableMap<in K, in V>> Map<out K, V>.toMap(destination: M): M
|
||||
= destination.apply { putAll(this@toMap) }
|
||||
|
||||
// TODO: fix me, once have correct type variance in HashMap.
|
||||
/**
|
||||
* Creates a new read-only map by replacing or adding an entry to this map from a given key-value [pair].
|
||||
*
|
||||
* 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 HashMap(this).apply { put(pair.first, pair.second) }
|
||||
@FixmeVariance
|
||||
public operator fun <K, V> Map</*out */K, V>.plus(pair: Pair<K, V>): Map<K, V>
|
||||
= if (this.isEmpty()) mapOf(pair) else HashMap(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].
|
||||
@@ -500,8 +500,9 @@ 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.
|
||||
* 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 HashMap(this).apply { putAll(pairs) }
|
||||
@FixmeVariance
|
||||
public operator fun <K, V> Map</*out */K, V>.plus(pairs: Iterable<Pair<K, V>>): Map<K, V>
|
||||
= if (this.isEmpty()) pairs.toMap() else HashMap(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].
|
||||
@@ -509,8 +510,9 @@ 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.
|
||||
* 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 HashMap(this).apply { putAll(pairs) }
|
||||
@FixmeVariance
|
||||
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 HashMap(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].
|
||||
@@ -518,8 +520,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.
|
||||
* 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>
|
||||
// = HashMap(this).apply { putAll(pairs) }.optimizeReadOnlyMap()
|
||||
public operator fun <K, V> Map</*out */K, V>.plus(pairs: Sequence<Pair<K, V>>): Map<K, V>
|
||||
= HashMap(this).apply { putAll(pairs) }.optimizeReadOnlyMap()
|
||||
|
||||
/**
|
||||
* Creates a new read-only map by replacing or adding entries to this map from another [map].
|
||||
@@ -527,8 +529,9 @@ 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.
|
||||
* 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>
|
||||
// = HashMap(this).apply { putAll(map) }
|
||||
@FixmeVariance
|
||||
public operator fun <K, V> Map</*out */K, V>.plus(map: Map</*out */K, V>): Map<K, V>
|
||||
= HashMap(this).apply { putAll(map) }
|
||||
|
||||
/**
|
||||
* Appends or replaces the given [pair] in this mutable map.
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
package kotlin.collections
|
||||
|
||||
import kotlin.comparisons.*
|
||||
|
||||
/**
|
||||
* Removes a single instance of the specified element from this
|
||||
* collection, if it is present.
|
||||
@@ -171,11 +173,7 @@ public fun <T> MutableList<T>.removeAll(predicate: (T) -> Boolean): Boolean = fi
|
||||
*/
|
||||
public fun <T> MutableList<T>.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)
|
||||
|
||||
@Fixme
|
||||
private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
|
||||
TODO()
|
||||
}
|
||||
/* TODO: fix downTo, RandomAccess
|
||||
if (this !is RandomAccess)
|
||||
return (this as MutableIterable<T>).filterInPlace(predicate, predicateResultToRemove)
|
||||
|
||||
@@ -199,97 +197,68 @@ private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicat
|
||||
else {
|
||||
return false
|
||||
}
|
||||
} */
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] collection.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <T> MutableCollection<in T>.removeAll(elements: Iterable<T>): Boolean {
|
||||
// TODO: add convertToSetForSetOperationWith
|
||||
// return removeAll(elements.convertToSetForSetOperationWith(this))
|
||||
var removed = false
|
||||
for (e in elements) {
|
||||
removed = removed or remove(e)
|
||||
}
|
||||
return removed
|
||||
return removeAll(elements.convertToSetForSetOperationWith(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] sequence.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <T> MutableCollection<in T>.removeAll(elements: Sequence<T>): Boolean {
|
||||
// TODO: add toHashSet()
|
||||
//val set = elements.toHashSet()
|
||||
// return set.isNotEmpty() && removeAll(set)
|
||||
var removed = false
|
||||
for (e in elements) {
|
||||
removed = removed or remove(e)
|
||||
}
|
||||
return removed
|
||||
val set = elements.toHashSet()
|
||||
return set.isNotEmpty() && removeAll(set)
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] array.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <T> MutableCollection<in T>.removeAll(elements: Array<out T>): Boolean {
|
||||
// TODO: add toHashSet()
|
||||
// return elements.isNotEmpty() && removeAll(elements.toHashSet())
|
||||
var removed = false
|
||||
for (e in elements) {
|
||||
removed = removed or remove(e)
|
||||
}
|
||||
return removed
|
||||
return elements.isNotEmpty() && removeAll(elements.toHashSet())
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only elements of this [MutableCollection] that are contained in the given [elements] collection.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <T> MutableCollection<in T>.retainAll(elements: Iterable<T>): Boolean {
|
||||
TODO()
|
||||
//return retainAll(elements.convertToSetForSetOperationWith(this))
|
||||
return retainAll(elements.convertToSetForSetOperationWith(this))
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only elements of this [MutableCollection] that are contained in the given [elements] array.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <T> MutableCollection<in T>.retainAll(elements: Array<out T>): Boolean {
|
||||
TODO()
|
||||
//if (elements.isNotEmpty())
|
||||
// return retainAll(elements.toHashSet())
|
||||
//else
|
||||
// return retainNothing()
|
||||
if (elements.isNotEmpty())
|
||||
return retainAll(elements.toHashSet())
|
||||
else
|
||||
return retainNothing()
|
||||
}
|
||||
|
||||
/**
|
||||
* Retains only elements of this [MutableCollection] that are contained in the given [elements] sequence.
|
||||
*/
|
||||
@Fixme
|
||||
public fun <T> MutableCollection<in T>.retainAll(elements: Sequence<T>): Boolean {
|
||||
TODO()
|
||||
//val set = elements.toHashSet()
|
||||
//if (set.isNotEmpty())
|
||||
// return retainAll(set)
|
||||
//else
|
||||
// return retainNothing()
|
||||
val set = elements.toHashSet()
|
||||
if (set.isNotEmpty())
|
||||
return retainAll(set)
|
||||
else
|
||||
return retainNothing()
|
||||
}
|
||||
|
||||
@Fixme
|
||||
private fun MutableCollection<*>.retainNothing(): Boolean {
|
||||
TODO()
|
||||
//val result = isNotEmpty()
|
||||
//clear()
|
||||
//return result
|
||||
val result = isNotEmpty()
|
||||
clear()
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Sorts elements in the list in-place according to their natural sort order.
|
||||
*/
|
||||
@Fixme
|
||||
@FixmeSorting
|
||||
public fun <T : Comparable<T>> MutableList<T>.sort(): Unit {
|
||||
TODO()
|
||||
//if (size > 1) java.util.Collections.sort(this)
|
||||
@@ -298,6 +267,8 @@ public fun <T : Comparable<T>> MutableList<T>.sort(): Unit {
|
||||
/**
|
||||
* Sorts elements in the list in-place according to the order specified with [comparator].
|
||||
*/
|
||||
//public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
//if (size > 1) java.util.Collections.sort(this, comparator)
|
||||
//}
|
||||
@FixmeSorting
|
||||
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
|
||||
TODO()
|
||||
//if (size > 1) java.util.Collections.sort(this, comparator)
|
||||
}
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
package kotlin.collections
|
||||
|
||||
public interface RandomAccess
|
||||
@@ -0,0 +1,300 @@
|
||||
package kotlin.comparisons
|
||||
|
||||
interface Comparator<T> {
|
||||
fun compare(a: T, b: T): Int
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two values using the specified functions [selectors] to calculate the result of the comparison.
|
||||
* The functions are called sequentially, receive the given values [a] and [b] and return [Comparable]
|
||||
* objects. As soon as the [Comparable] instances returned by a function for [a] and [b] values do not
|
||||
* compare as equal, the result of that comparison is returned.
|
||||
*/
|
||||
public fun <T> compareValuesBy(a: T, b: T, vararg selectors: (T) -> Comparable<*>?): Int {
|
||||
require(selectors.size > 0)
|
||||
for (fn in selectors) {
|
||||
val v1 = fn(a)
|
||||
val v2 = fn(b)
|
||||
val diff = compareValues(v1, v2)
|
||||
if (diff != 0) return diff
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two values using the specified [selector] function to calculate the result of the comparison.
|
||||
* The function is applied to the given values [a] and [b] and return [Comparable] objects.
|
||||
* The result of comparison of these [Comparable] instances is returned.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> compareValuesBy(a: T, b: T, selector: (T) -> Comparable<*>?): Int {
|
||||
return compareValues(selector(a), selector(b))
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares two values using the specified [selector] function to calculate the result of the comparison.
|
||||
* The function is applied to the given values [a] and [b] and return objects of type K which are then being
|
||||
* compared with the given [comparator].
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, K> compareValuesBy(a: T, b: T, comparator: Comparator<in K>, selector: (T) -> K): Int {
|
||||
return comparator.compare(selector(a), selector(b))
|
||||
}
|
||||
|
||||
//// Not so useful without type inference for receiver of expression
|
||||
//// compareValuesWith(v1, v2, compareBy { it.prop1 } thenByDescending { it.prop2 })
|
||||
///**
|
||||
// * Compares two values using the specified [comparator].
|
||||
// */
|
||||
//@Suppress("NOTHING_TO_INLINE")
|
||||
//public inline fun <T> compareValuesWith(a: T, b: T, comparator: Comparator<T>): Int = comparator.compare(a, b)
|
||||
//
|
||||
|
||||
|
||||
/**
|
||||
* Compares two nullable [Comparable] values. Null is considered less than any value.
|
||||
*/
|
||||
public fun <T : Comparable<*>> compareValues(a: T?, b: T?): Int {
|
||||
if (a === b) return 0
|
||||
if (a == null) return -1
|
||||
if (b == null) return 1
|
||||
|
||||
@Suppress("UNCHECKED_CAST")
|
||||
return (a as Comparable<Any>).compareTo(b)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comparator using the sequence of functions to calculate a result of comparison.
|
||||
* The functions are called sequentially, receive the given values `a` and `b` and return [Comparable]
|
||||
* objects. As soon as the [Comparable] instances returned by a function for `a` and `b` values do not
|
||||
* compare as equal, the result of that comparison is returned from the [Comparator].
|
||||
*/
|
||||
public fun <T> compareBy(vararg selectors: (T) -> Comparable<*>?): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, *selectors)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Creates a comparator using the function to transform value to a [Comparable] instance for comparison.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> compareBy(crossinline selector: (T) -> Comparable<*>?): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, selector)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comparator using the [selector] function to transform values being compared and then applying
|
||||
* the specified [comparator] to compare transformed values.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, K> compareBy(comparator: Comparator<in K>, crossinline selector: (T) -> K): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int = compareValuesBy(a, b, comparator, selector)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a descending comparator using the function to transform value to a [Comparable] instance for comparison.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> compareByDescending(crossinline selector: (T) -> Comparable<*>?): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int = compareValuesBy(b, a, selector)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a descending comparator using the [selector] function to transform values being compared and then applying
|
||||
* the specified [comparator] to compare transformed values.
|
||||
*
|
||||
* Note that an order of [comparator] is reversed by this wrapper.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, K> compareByDescending(comparator: Comparator<in K>, crossinline selector: (T) -> K): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int = compareValuesBy(b, a, comparator, selector)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comparator comparing values after the primary comparator defined them equal. It uses
|
||||
* the function to transform value to a [Comparable] instance for comparison.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Comparator<T>.thenBy(crossinline selector: (T) -> Comparable<*>?): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@thenBy.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else compareValuesBy(a, b, selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a comparator comparing values after the primary comparator defined them equal. It uses
|
||||
* the [selector] function to transform values and then compares them with the given [comparator].
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, K> Comparator<T>.thenBy(comparator: Comparator<in K>, crossinline selector: (T) -> K): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@thenBy.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else compareValuesBy(a, b, comparator, selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a descending comparator using the primary comparator and
|
||||
* the function to transform value to a [Comparable] instance for comparison.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Comparator<T>.thenByDescending(crossinline selector: (T) -> Comparable<*>?): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@thenByDescending.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else compareValuesBy(b, a, selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a descending comparator comparing values after the primary comparator defined them equal. It uses
|
||||
* the [selector] function to transform values and then compares them with the given [comparator].
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T, K> Comparator<T>.thenByDescending(comparator: Comparator<in K>, crossinline selector: (T) -> K): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@thenByDescending.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else compareValuesBy(b, a, comparator, selector)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Creates a comparator using the primary comparator and function to calculate a result of comparison.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T> Comparator<T>.thenComparator(crossinline comparison: (T, T) -> Int): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@thenComparator.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else comparison(a, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines this comparator and the given [comparator] such that the latter is applied only
|
||||
* when the former considered values equal.
|
||||
*/
|
||||
public infix fun <T> Comparator<T>.then(comparator: Comparator<in T>): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@then.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else comparator.compare(a, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Combines this comparator and the given [comparator] such that the latter is applied only
|
||||
* when the former considered values equal.
|
||||
*/
|
||||
public infix fun <T> Comparator<T>.thenDescending(comparator: Comparator<in T>): Comparator<T> {
|
||||
return object : Comparator<T> {
|
||||
public override fun compare(a: T, b: T): Int {
|
||||
val previousCompare = this@thenDescending.compare(a, b)
|
||||
return if (previousCompare != 0) previousCompare else comparator.compare(b, a)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Not so useful without type inference for receiver of expression
|
||||
/**
|
||||
* Extends the given [comparator] of non-nullable values to a comparator of nullable values
|
||||
* considering `null` value less than any other value.
|
||||
*/
|
||||
public fun <T: Any> nullsFirst(comparator: Comparator<in T>): Comparator<T?> {
|
||||
return object: Comparator<T?> {
|
||||
override fun compare(a: T?, b: T?): Int {
|
||||
if (a === b) return 0
|
||||
if (a == null) return -1
|
||||
if (b == null) return 1
|
||||
return comparator.compare(a, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a comparator of nullable [Comparable] values
|
||||
* considering `null` value less than any other value.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T: Comparable<T>> nullsFirst(): Comparator<T?> = nullsFirst(naturalOrder())
|
||||
|
||||
/**
|
||||
* Extends the given [comparator] of non-nullable values to a comparator of nullable values
|
||||
* considering `null` value greater than any other value.
|
||||
*/
|
||||
public fun <T: Any> nullsLast(comparator: Comparator<in T>): Comparator<T?> {
|
||||
return object: Comparator<T?> {
|
||||
override fun compare(a: T?, b: T?): Int {
|
||||
if (a === b) return 0
|
||||
if (a == null) return 1
|
||||
if (b == null) return -1
|
||||
return comparator.compare(a, b)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides a comparator of nullable [Comparable] values
|
||||
* considering `null` value greater than any other value.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T: Comparable<T>> nullsLast(): Comparator<T?> = nullsLast(naturalOrder())
|
||||
|
||||
/**
|
||||
* Returns a comparator that compares [Comparable] objects in natural order.
|
||||
*/
|
||||
public fun <T: Comparable<T>> naturalOrder(): Comparator<T> = @Suppress("UNCHECKED_CAST") (NaturalOrderComparator as Comparator<T>)
|
||||
|
||||
/**
|
||||
* Returns a comparator that compares [Comparable] objects in reversed natural order.
|
||||
*/
|
||||
public fun <T: Comparable<T>> reverseOrder(): Comparator<T> = @Suppress("UNCHECKED_CAST") (ReverseOrderComparator as Comparator<T>)
|
||||
|
||||
/** Returns a comparator that imposes the reverse ordering of this comparator. */
|
||||
public fun <T> Comparator<T>.reversed(): Comparator<T> = when (this) {
|
||||
is ReversedComparator -> this.comparator
|
||||
NaturalOrderComparator -> @Suppress("UNCHECKED_CAST") (ReverseOrderComparator as Comparator<T>)
|
||||
ReverseOrderComparator -> @Suppress("UNCHECKED_CAST") (NaturalOrderComparator as Comparator<T>)
|
||||
else -> ReversedComparator(this)
|
||||
}
|
||||
|
||||
|
||||
private class ReversedComparator<T>(public val comparator: Comparator<T>): Comparator<T> {
|
||||
override fun compare(a: T, b: T): Int = comparator.compare(b, a)
|
||||
@Suppress("VIRTUAL_MEMBER_HIDDEN")
|
||||
fun reversed(): Comparator<T> = comparator
|
||||
}
|
||||
|
||||
private object NaturalOrderComparator : Comparator<Comparable<Any>> {
|
||||
override fun compare(a: Comparable<Any>, b: Comparable<Any>): Int = a.compareTo(b)
|
||||
@Suppress("VIRTUAL_MEMBER_HIDDEN")
|
||||
fun reversed(): Comparator<Comparable<Any>> = ReverseOrderComparator
|
||||
}
|
||||
|
||||
private object ReverseOrderComparator: Comparator<Comparable<Any>> {
|
||||
override fun compare(a: Comparable<Any>, b: Comparable<Any>): Int = b.compareTo(a)
|
||||
@Suppress("VIRTUAL_MEMBER_HIDDEN")
|
||||
fun reversed(): Comparator<Comparable<Any>> = NaturalOrderComparator
|
||||
}
|
||||
@@ -333,3 +333,12 @@ public fun Long.coerceIn(range: ClosedRange<Long>): Long {
|
||||
public fun IntProgression.reversed(): IntProgression {
|
||||
return IntProgression.fromClosedRange(last, first, -step)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a progression from this value down to the specified [to] value with the step -1.
|
||||
*
|
||||
* The [to] value has to be less than this value.
|
||||
*/
|
||||
public infix fun Int.downTo(to: Int): IntProgression {
|
||||
return IntProgression.fromClosedRange(this, to, -1)
|
||||
}
|
||||
|
||||
+19
@@ -21,3 +21,22 @@ public interface Sequence<out T> {
|
||||
*/
|
||||
public operator fun iterator(): Iterator<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* A sequence that supports drop(n) and take(n) operations
|
||||
*/
|
||||
internal interface DropTakeSequence<T> : Sequence<T> {
|
||||
fun drop(n: Int): Sequence<T>
|
||||
fun take(n: Int): Sequence<T>
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns an empty sequence.
|
||||
*/
|
||||
public fun <T> emptySequence(): Sequence<T> = EmptySequence
|
||||
|
||||
private object EmptySequence : Sequence<Nothing>, DropTakeSequence<Nothing> {
|
||||
override fun iterator(): Iterator<Nothing> = EmptyIterator
|
||||
override fun drop(n: Int) = EmptySequence
|
||||
override fun take(n: Int) = EmptySequence
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,7 @@
|
||||
package kotlin.text
|
||||
|
||||
interface Appendable {
|
||||
fun append(c: Char): Appendable
|
||||
fun append(csq: CharSequence?): Appendable
|
||||
fun append(csq: CharSequence?, start: Int, end: Int): Appendable
|
||||
}
|
||||
@@ -13,7 +13,7 @@ external fun toCharArray(string: String) : CharArray
|
||||
|
||||
class StringBuilder private constructor (
|
||||
private var array: CharArray
|
||||
) : CharSequence {
|
||||
) : CharSequence, Appendable {
|
||||
constructor() : this(10)
|
||||
|
||||
constructor(capacity: Int) : this(CharArray(capacity))
|
||||
@@ -57,11 +57,17 @@ class StringBuilder private constructor (
|
||||
}
|
||||
}
|
||||
|
||||
fun append(it: Char) {
|
||||
// Of Appenable.
|
||||
override fun append(c: Char) : Appendable {
|
||||
ensureExtraCapacity(1)
|
||||
array[length++] = it
|
||||
array[length++] = c
|
||||
return this
|
||||
}
|
||||
|
||||
override fun append(csq: CharSequence?): Appendable = TODO()
|
||||
|
||||
override fun append(csq: CharSequence?, start: Int, end: Int): Appendable = TODO()
|
||||
|
||||
fun append(it: CharArray) {
|
||||
ensureExtraCapacity(it.size)
|
||||
for (c in it)
|
||||
|
||||
@@ -0,0 +1,98 @@
|
||||
package kotlin
|
||||
|
||||
/**
|
||||
* Throws an [IllegalArgumentException] if the [value] is false.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failRequireWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun require(value: Boolean): Unit = require(value) { "Failed requirement." }
|
||||
|
||||
/**
|
||||
* Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is false.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failRequireWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun require(value: Boolean, lazyMessage: () -> Any): Unit {
|
||||
if (!value) {
|
||||
val message = lazyMessage()
|
||||
throw IllegalArgumentException(message.toString())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [IllegalArgumentException] if the [value] is null. Otherwise returns the not null value.
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T:Any> requireNotNull(value: T?): T = requireNotNull(value) { "Required value was null." }
|
||||
|
||||
/**
|
||||
* Throws an [IllegalArgumentException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
|
||||
* returns the not null value.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failRequireWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T:Any> requireNotNull(value: T?, lazyMessage: () -> Any): T {
|
||||
if (value == null) {
|
||||
val message = lazyMessage()
|
||||
throw IllegalArgumentException(message.toString())
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [IllegalStateException] if the [value] is false.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failCheckWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun check(value: Boolean): Unit = check(value) { "Check failed." }
|
||||
|
||||
/**
|
||||
* Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is false.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failCheckWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun check(value: Boolean, lazyMessage: () -> Any): Unit {
|
||||
if (!value) {
|
||||
val message = lazyMessage()
|
||||
throw IllegalStateException(message.toString())
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [IllegalStateException] if the [value] is null. Otherwise
|
||||
* returns the not null value.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failCheckWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T:Any> checkNotNull(value: T?): T = checkNotNull(value) { "Required value was null." }
|
||||
|
||||
/**
|
||||
* Throws an [IllegalStateException] with the result of calling [lazyMessage] if the [value] is null. Otherwise
|
||||
* returns the not null value.
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failCheckWithLazyMessage
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun <T:Any> checkNotNull(value: T?, lazyMessage: () -> Any): T {
|
||||
if (value == null) {
|
||||
val message = lazyMessage()
|
||||
throw IllegalStateException(message.toString())
|
||||
} else {
|
||||
return value
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Throws an [IllegalStateException] with the given [message].
|
||||
*
|
||||
* @sample samples.misc.Preconditions.failWithError
|
||||
*/
|
||||
@kotlin.internal.InlineOnly
|
||||
public inline fun error(message: Any): Nothing = throw IllegalStateException(message.toString())
|
||||
Reference in New Issue
Block a user