All Downloads are FREE. Search and download functionalities are using the official Maven repository.

kotlin.collections.MutableCollections.kt Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")

package kotlin.collections

import java.util.*

/**
 * Checks if all elements in the specified collection are contained in this collection.
 *
 * Allows to overcome type-safety restriction of `containsAll` that requires to pass a collection of type `Collection`.
 */
public fun <@kotlin.internal.OnlyInputTypes T> Collection.containsAll(elements: Collection): Boolean = this.containsAll(elements)

@Suppress("NOTHING_TO_INLINE")
@Deprecated("Collections have incompatible types. Upcast either to Collection if you're sure.", ReplaceWith("containsAll(elements)"))
public inline fun Collection<*>.containsAllRaw(elements: Collection): Boolean = containsAll(elements)

@Deprecated("Collections have incompatible types. Upcast either to Collection if you're sure.", ReplaceWith("containsAll(elements)"))
@kotlin.jvm.JvmName("containsAllOfAny")
@kotlin.internal.LowPriorityInOverloadResolution
public fun  Collection.containsAll(elements: Collection): Boolean = containsAll(elements)

/**
 * Removes a single instance of the specified element from this
 * collection, if it is present.
 *
 * Allows to overcome type-safety restriction of `remove` that requires to pass an element of type `E`.
 *
 * @return `true` if the element has been successfully removed; `false` if it was not present in the collection.
 */
public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.remove(element: T): Boolean = (this as MutableCollection).remove(element)

@Suppress("NOTHING_TO_INLINE")
@Deprecated("Collection and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("remove(element as Any?)"))
public inline fun  MutableCollection.removeRaw(element: Any?): Boolean = remove(element)

@Deprecated("Collection and element have incompatible types. Upcast element to Any? if you're sure.", ReplaceWith("remove(element as T)"))
@kotlin.jvm.JvmName("removeAny")
@kotlin.internal.LowPriorityInOverloadResolution
public fun  MutableCollection.remove(element: T): Boolean = remove(element)

/**
 * Removes all of this collection's elements that are also contained in the specified collection.

 * Allows to overcome type-safety restriction of `removeAll` that requires to pass a collection of type `Collection`.
 *
 * @return `true` if any of the specified elements was removed from the collection, `false` if the collection was not modified.
 */
public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.removeAll(elements: Collection): Boolean = (this as MutableCollection).removeAll(elements)

@Suppress("NOTHING_TO_INLINE")
@Deprecated("Collections have incompatible types. Upcast elements to Collection if you're sure.", ReplaceWith("removeAll(elements)"))
public inline fun  MutableCollection.removeAllRaw(elements: Collection): Boolean = removeAll(elements)

@Deprecated("Collections have incompatible types. Upcast elements to Collection if you're sure.", ReplaceWith("removeAll(elements)"))
@kotlin.jvm.JvmName("removeAllOfAny")
@kotlin.internal.LowPriorityInOverloadResolution
public fun  MutableCollection.removeAll(elements: Collection): Boolean = removeAll(elements)

/**
 * Retains only the elements in this collection that are contained in the specified collection.
 *
 * Allows to overcome type-safety restriction of `retailAll` that requires to pass a collection of type `Collection`.
 *
 * @return `true` if any element was removed from the collection, `false` if the collection was not modified.
 */
public fun <@kotlin.internal.OnlyInputTypes T> MutableCollection.retainAll(elements: Collection): Boolean = (this as MutableCollection).retainAll(elements)

@Suppress("NOTHING_TO_INLINE")
@Deprecated("Collections have incompatible types. Upcast elements to Collection if you're sure.", ReplaceWith("retainAll(elements)"))
public inline fun  MutableCollection.retainAllRaw(elements: Collection): Boolean = retainAll(elements)

@Deprecated("Collections have incompatible types. Upcast elements to Collection if you're sure.", ReplaceWith("retainAll(elements)"))
@kotlin.jvm.JvmName("retainAllOfAny")
@kotlin.internal.LowPriorityInOverloadResolution
public fun  MutableCollection.retainAll(elements: Collection): Boolean = retainAll(elements as Collection)


@Deprecated("Use operator 'get' instead", ReplaceWith("this[index]"))
public fun CharSequence.charAt(index: Int): Char = this[index]

@Deprecated("Use property 'size' instead", ReplaceWith("size"))
public inline fun Collection<*>.size() = size

@Deprecated("Use property 'size' instead", ReplaceWith("size"))
public inline fun Map<*, *>.size() = size

@Deprecated("Use property 'key' instead", ReplaceWith("key"))
public fun  Map.Entry.getKey(): K = key


@Deprecated("Use property 'value' instead.", ReplaceWith("value"))
public fun  Map.Entry.getValue(): V = value

@Deprecated("Use 'removeAt' instead.", ReplaceWith("removeAt(index)"))
public fun  MutableList.remove(index: Int): E = removeAt(index)




@Deprecated("Use property 'length' instead.", ReplaceWith("length"))
public fun CharSequence.length(): Int = length

@Deprecated("Map and key have incompatible types. Upcast key to Any? if you're sure.", ReplaceWith("get(key as K)"))
@kotlin.internal.LowPriorityInOverloadResolution
public inline operator fun  Map.get(key: K): V? = get(key)

@Deprecated("Map and key have incompatible types. Upcast key to Any? if you're sure.", ReplaceWith("containsKey(key as K)"))
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun  Map.containsKey(key: K): Boolean = containsKey(key)

@Deprecated("Map and value have incompatible types. Upcast value to Any? if you're sure.", ReplaceWith("containsValue(value as V)"))
@kotlin.internal.LowPriorityInOverloadResolution
public inline fun  Map.containsValue(value: V): Boolean = containsValue(value as Any?)

@Deprecated("Use property 'keys' instead.", ReplaceWith("keys"))
public inline fun  Map.keySet(): Set = keys

@kotlin.jvm.JvmName("mutableKeys")
@Deprecated("Use property 'keys' instead.", ReplaceWith("keys"))
public fun  MutableMap.keySet(): MutableSet = keys

@Deprecated("Use property 'entries' instead.", ReplaceWith("entries"))
public inline fun  Map.entrySet(): Set> = entries

@kotlin.jvm.JvmName("mutableEntrySet")
@Deprecated("Use property 'entries' instead.", ReplaceWith("entries"))
public fun  MutableMap.entrySet(): MutableSet> = entries

@Deprecated("Use property 'values' instead.", ReplaceWith("values"))
public inline fun  Map.values(): Collection = values

@kotlin.jvm.JvmName("mutableValues")
@Deprecated("Use property 'values' instead.", ReplaceWith("values"))
public fun  MutableMap.values(): MutableCollection = values

/**
 * Adds the specified [element] to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(element: T) {
    this.add(element)
}

/**
 * Adds all elements of the given [elements] collection to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(elements: Iterable) {
    this.addAll(elements)
}

/**
 * Adds all elements of the given [elements] array to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(elements: Array) {
    this.addAll(elements)
}

/**
 * Adds all elements of the given [elements] sequence to this mutable collection.
 */
public operator fun  MutableCollection.plusAssign(elements: Sequence) {
    this.addAll(elements)
}

/**
 * Removes a single instance of the specified [element] from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(element: T) {
    this.remove(element)
}

/**
 * Removes all elements contained in the given [elements] collection from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(elements: Iterable) {
    this.removeAll(elements)
}

/**
 * Removes all elements contained in the given [elements] array from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(elements: Array) {
    this.removeAll(elements)
}

/**
 * Removes all elements contained in the given [elements] sequence from this mutable collection.
 */
public operator fun  MutableCollection.minusAssign(elements: Sequence) {
    this.removeAll(elements)
}

/**
 * Adds all elements of the given [elements] collection to this [MutableCollection].
 */
public fun  MutableCollection.addAll(elements: Iterable): Boolean {
    when (elements) {
        is Collection -> return addAll(elements)
        else -> {
            var result: Boolean = false
            for (item in elements)
                if (add(item)) result = true
            return result
        }
    }
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.addAll(elements: Iterable) {
    when (elements) {
        is Collection -> addAll(elements)
        else -> for (item in elements) add(item)
    }
}

/**
 * Adds all elements of the given [elements] sequence to this [MutableCollection].
 */
public fun  MutableCollection.addAll(elements: Sequence): Boolean {
    var result: Boolean = false
    for (item in elements) {
        if (add(item)) result = true
    }
    return result
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.addAll(elements: Sequence) {
    for (item in elements) add(item)
}

/**
 * Adds all elements of the given [elements] array to this [MutableCollection].
 */
public fun  MutableCollection.addAll(elements: Array): Boolean {
    return addAll(elements.asList())
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.addAll(elements: Array) {
    addAll(elements.asList())
}

/**
 * Removes all elements from this [MutableIterable] that match the given [predicate].
 */
public fun  MutableIterable.removeAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, true)

/**
 * Retains only elements of this [MutableIterable] that match the given [predicate].
 */
public fun  MutableIterable.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)

private fun  MutableIterable.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
    var result = false
    with (iterator()) {
        while (hasNext())
            if (predicate(next()) == predicateResultToRemove) {
                remove()
                result = true
            }
    }
    return result
}

/**
 * Removes all elements from this [MutableList] that match the given [predicate].
 */
public fun  MutableList.removeAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, true)

/**
 * Retains only elements of this [MutableList] that match the given [predicate].
 */
public fun  MutableList.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)

private fun  MutableList.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
    var writeIndex: Int = 0
    for (readIndex in 0..lastIndex) {
        val element = this[readIndex]
        if (predicate(element) == predicateResultToRemove)
            continue

        if (writeIndex != readIndex)
            this[writeIndex] = element

        writeIndex++
    }
    if (writeIndex < size) {
        for (removeIndex in lastIndex downTo writeIndex)
            removeAt(removeIndex)

        return true
    }
    else {
        return false
    }
}

/**
 * Removes all elements from this [MutableCollection] that are also contained in the given [elements] collection.
 */
public fun  MutableCollection.removeAll(elements: Iterable): Boolean {
    return removeAll(elements.convertToSetForSetOperationWith(this))
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.removeAll(elements: Iterable) {
    removeAll(elements.convertToSetForSetOperationWith(this))
}

/**
 * Removes all elements from this [MutableCollection] that are also contained in the given [elements] sequence.
 */
public fun  MutableCollection.removeAll(elements: Sequence): Boolean {
    val set = elements.toHashSet()
    return set.isNotEmpty() && removeAll(set)
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.removeAll(elements: Sequence) {
    val set = elements.toHashSet()
    if (set.isNotEmpty())
        removeAll(set)
}

/**
 * Removes all elements from this [MutableCollection] that are also contained in the given [elements] array.
 */
public fun  MutableCollection.removeAll(elements: Array): Boolean {
    return elements.isNotEmpty() && removeAll(elements.toHashSet())
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.removeAll(elements: Array) {
    if (elements.isNotEmpty())
        removeAll(elements.toHashSet())
//    else
//        removeAll(emptyList())
}

/**
 * Retains only elements of this [MutableCollection] that are contained in the given [elements] collection.
 */
public fun  MutableCollection.retainAll(elements: Iterable): Boolean {
    return retainAll(elements.convertToSetForSetOperationWith(this))
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.retainAll(elements: Iterable) {
    retainAll(elements.convertToSetForSetOperationWith(this))
}

/**
 * Retains only elements of this [MutableCollection] that are contained in the given [elements] array.
 */
public fun  MutableCollection.retainAll(elements: Array): Boolean {
    if (elements.isNotEmpty())
        return retainAll(elements.toHashSet())
    else
        return retainNothing()
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.retainAll(elements: Array) {
    if (elements.isNotEmpty())
        retainAll(elements.toHashSet())
    else
        clear()
//        retainAll(emptyList())
}

/**
 * Retains only elements of this [MutableCollection] that are contained in the given [elements] sequence.
 */
public fun  MutableCollection.retainAll(elements: Sequence): Boolean {
    val set = elements.toHashSet()
    if (set.isNotEmpty())
        return retainAll(set)
    else
        return retainNothing()
}

@Deprecated("Provided for binary compatibility", level = DeprecationLevel.HIDDEN)
public fun  MutableCollection.retainAll(elements: Sequence) {
    val set = elements.toHashSet()
    if (set.isNotEmpty())
        retainAll(set)
    else
        clear()
}

private fun MutableCollection<*>.retainNothing(): Boolean {
    val result = isNotEmpty()
    clear()
    return result
}

/**
 * Sorts elements in the list in-place according to their natural sort order.
 * */
public fun > MutableList.sort(): Unit {
    if (size > 1) java.util.Collections.sort(this)
}

/**
 *  Sorts elements in the list in-place according to order specified with [comparator].
 */
public fun  MutableList.sortWith(comparator: Comparator): Unit {
    if (size > 1) java.util.Collections.sort(this, comparator)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy