Provide minus and minusAssign for iterables, collections, sets, sequences.
removeAll and retainAll special case optimizations. #KT-3721 Fixed #KT-7466 Fixed
This commit is contained in:
@@ -381,6 +381,140 @@ public fun <T, R, V> Sequence<T>.merge(sequence: Sequence<R>, transform: (T, R)
|
||||
return MergingSequence(this, sequence, transform)
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements of the original collection except the elements contained in the given [array].
|
||||
*/
|
||||
public fun <T> Iterable<T>.minus(array: Array<out T>): List<T> {
|
||||
if (array.isEmpty()) return this.toList()
|
||||
val other = array.toHashSet()
|
||||
return this.filterNot { it in other }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence containing all elements of original sequence except the elements contained in the given [array].
|
||||
*/
|
||||
public fun <T> Sequence<T>.minus(array: Array<out T>): Sequence<T> {
|
||||
if (array.isEmpty()) return this
|
||||
return object: Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val other = array.toHashSet()
|
||||
return this@minus.filterNot { it in other }.iterator()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set containing all elements of the original set except the elements contained in the given [array].
|
||||
*/
|
||||
public fun <T> Set<T>.minus(array: Array<out T>): Set<T> {
|
||||
val result = LinkedHashSet<T>(this)
|
||||
result.removeAll(array)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements of the original collection except the elements contained in the given [collection].
|
||||
*/
|
||||
public fun <T> Iterable<T>.minus(collection: Iterable<T>): List<T> {
|
||||
val other = collection.convertToSetForSetOperationWith(this)
|
||||
if (other.isEmpty())
|
||||
return this.toList()
|
||||
return this.filterNot { it in other }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence containing all elements of original sequence except the elements contained in the given [collection].
|
||||
*/
|
||||
public fun <T> Sequence<T>.minus(collection: Iterable<T>): Sequence<T> {
|
||||
return object: Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val other = collection.convertToSetForSetOperation()
|
||||
if (other.isEmpty())
|
||||
return this@minus.iterator()
|
||||
else
|
||||
return this@minus.filterNot { it in other }.iterator()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set containing all elements of the original set except the elements contained in the given [collection].
|
||||
*/
|
||||
public fun <T> Set<T>.minus(collection: Iterable<T>): Set<T> {
|
||||
val other = collection.convertToSetForSetOperationWith(this)
|
||||
if (other.isEmpty())
|
||||
return this.toSet()
|
||||
if (other is Set)
|
||||
return this.filterNotTo(LinkedHashSet<T>()) { it in other }
|
||||
val result = LinkedHashSet<T>(this)
|
||||
result.removeAll(other)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements of the original collection without the first occurrence of the given [element].
|
||||
*/
|
||||
public fun <T> Iterable<T>.minus(element: T): List<T> {
|
||||
val result = ArrayList<T>(collectionSizeOrDefault(10))
|
||||
var removed = false
|
||||
return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence containing all elements of the original sequence without the first occurrence of the given [element].
|
||||
*/
|
||||
public fun <T> Sequence<T>.minus(element: T): Sequence<T> {
|
||||
return object: Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
var removed = false
|
||||
return this@minus.filter { if (!removed && it == element) { removed = true; false } else true }.iterator()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set containing all elements of the original set except the given [element].
|
||||
*/
|
||||
public fun <T> Set<T>.minus(element: T): Set<T> {
|
||||
val result = LinkedHashSet<T>(mapCapacity(size()))
|
||||
var removed = false
|
||||
return this.filterTo(result) { if (!removed && it == element) { removed = true; false } else true }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list containing all elements of the original collection except the elements contained in the given [sequence].
|
||||
*/
|
||||
public fun <T> Iterable<T>.minus(sequence: Sequence<T>): List<T> {
|
||||
val other = sequence.toHashSet()
|
||||
if (other.isEmpty())
|
||||
return this.toList()
|
||||
return this.filterNot { it in other }
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a sequence containing all elements of original sequence except the elements contained in the given [sequence].
|
||||
*/
|
||||
public fun <T> Sequence<T>.minus(sequence: Sequence<T>): Sequence<T> {
|
||||
return object: Sequence<T> {
|
||||
override fun iterator(): Iterator<T> {
|
||||
val other = sequence.toHashSet()
|
||||
if (other.isEmpty())
|
||||
return this@minus.iterator()
|
||||
else
|
||||
return this@minus.filterNot { it in other }.iterator()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a set containing all elements of the original set except the elements contained in the given [sequence].
|
||||
*/
|
||||
public fun <T> Set<T>.minus(sequence: Sequence<T>): Set<T> {
|
||||
val result = LinkedHashSet<T>(this)
|
||||
result.removeAll(sequence)
|
||||
return result
|
||||
}
|
||||
|
||||
/**
|
||||
* Splits original collection into pair of collections,
|
||||
* where *first* collection contains elements for which [predicate] yielded `true`,
|
||||
|
||||
Reference in New Issue
Block a user