/
MutableCollections.kt
333 lines (289 loc) · 11 KB
/
MutableCollections.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
@file:kotlin.jvm.JvmMultifileClass
@file:kotlin.jvm.JvmName("CollectionsKt")
package kotlin.collections
/**
* 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.
*/
@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)
/**
* 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<E>`.
*
* @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)
/**
* Retains only the elements in this collection that are contained in the specified collection.
*
* Allows to overcome type-safety restriction of `retainAll` that requires to pass a collection of type `Collection<E>`.
*
* @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)
/**
* Removes the element at the specified [index] from this list.
* In Kotlin one should use the [MutableList.removeAt] function instead.
*/
@Deprecated("Use removeAt(index) instead.", ReplaceWith("removeAt(index)"), level = DeprecationLevel.ERROR)
@kotlin.internal.InlineOnly
public inline fun <T> MutableList<T>.remove(index: Int): T = removeAt(index)
@Deprecated("Use sortWith(comparator) instead.", ReplaceWith("this.sortWith(comparator)"), level = DeprecationLevel.ERROR)
@JvmVersion
@kotlin.internal.InlineOnly
@Suppress("UNUSED_PARAMETER")
public inline fun <T> MutableList<T>.sort(comparator: Comparator<in T>): Unit = throw NotImplementedError()
@Deprecated("Use sortWith(Comparator(comparison)) instead.", ReplaceWith("this.sortWith(Comparator(comparison))"), level = DeprecationLevel.ERROR)
@JvmVersion
@kotlin.internal.InlineOnly
@Suppress("UNUSED_PARAMETER")
public inline fun <T> MutableList<T>.sort(comparison: (T, T) -> Int): Unit = throw NotImplementedError()
/**
* Adds the specified [element] to this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(element: T) {
this.add(element)
}
/**
* Adds all elements of the given [elements] collection to this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(elements: Iterable<T>) {
this.addAll(elements)
}
/**
* Adds all elements of the given [elements] array to this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(elements: Array<T>) {
this.addAll(elements)
}
/**
* Adds all elements of the given [elements] sequence to this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.plusAssign(elements: Sequence<T>) {
this.addAll(elements)
}
/**
* Removes a single instance of the specified [element] from this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(element: T) {
this.remove(element)
}
/**
* Removes all elements contained in the given [elements] collection from this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(elements: Iterable<T>) {
this.removeAll(elements)
}
/**
* Removes all elements contained in the given [elements] array from this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(elements: Array<T>) {
this.removeAll(elements)
}
/**
* Removes all elements contained in the given [elements] sequence from this mutable collection.
*/
@kotlin.internal.InlineOnly
public inline operator fun <T> MutableCollection<in T>.minusAssign(elements: Sequence<T>) {
this.removeAll(elements)
}
/**
* Adds all elements of the given [elements] collection to this [MutableCollection].
*/
public fun <T> MutableCollection<in T>.addAll(elements: Iterable<T>): Boolean {
when (elements) {
is Collection -> return addAll(elements)
else -> {
var result: Boolean = false
for (item in elements)
if (add(item)) result = true
return result
}
}
}
/**
* Adds all elements of the given [elements] sequence to this [MutableCollection].
*/
public fun <T> MutableCollection<in T>.addAll(elements: Sequence<T>): Boolean {
var result: Boolean = false
for (item in elements) {
if (add(item)) result = true
}
return result
}
/**
* Adds all elements of the given [elements] array to this [MutableCollection].
*/
public fun <T> MutableCollection<in T>.addAll(elements: Array<out T>): Boolean {
return addAll(elements.asList())
}
/**
* Removes all elements from this [MutableIterable] that match the given [predicate].
*/
public fun <T> MutableIterable<T>.removeAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, true)
/**
* Retains only elements of this [MutableIterable] that match the given [predicate].
*/
public fun <T> MutableIterable<T>.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)
private fun <T> MutableIterable<T>.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 <T> MutableList<T>.removeAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, true)
/**
* Retains only elements of this [MutableList] that match the given [predicate].
*/
public fun <T> MutableList<T>.retainAll(predicate: (T) -> Boolean): Boolean = filterInPlace(predicate, false)
private fun <T> MutableList<T>.filterInPlace(predicate: (T) -> Boolean, predicateResultToRemove: Boolean): Boolean {
if (this !is RandomAccess)
return (this as MutableIterable<T>).filterInPlace(predicate, predicateResultToRemove)
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 <T> MutableCollection<in T>.removeAll(elements: Iterable<T>): Boolean {
return removeAll(elements.convertToSetForSetOperationWith(this))
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] sequence.
*/
public fun <T> MutableCollection<in T>.removeAll(elements: Sequence<T>): Boolean {
val set = elements.toHashSet()
return set.isNotEmpty() && removeAll(set)
}
/**
* Removes all elements from this [MutableCollection] that are also contained in the given [elements] array.
*/
public fun <T> MutableCollection<in T>.removeAll(elements: Array<out T>): Boolean {
return elements.isNotEmpty() && removeAll(elements.toHashSet())
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [elements] collection.
*/
public fun <T> MutableCollection<in T>.retainAll(elements: Iterable<T>): Boolean {
return retainAll(elements.convertToSetForSetOperationWith(this))
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [elements] array.
*/
public fun <T> MutableCollection<in T>.retainAll(elements: Array<out T>): Boolean {
if (elements.isNotEmpty())
return retainAll(elements.toHashSet())
else
return retainNothing()
}
/**
* Retains only elements of this [MutableCollection] that are contained in the given [elements] sequence.
*/
public fun <T> MutableCollection<in T>.retainAll(elements: Sequence<T>): Boolean {
val set = elements.toHashSet()
if (set.isNotEmpty())
return retainAll(set)
else
return retainNothing()
}
private fun MutableCollection<*>.retainNothing(): Boolean {
val result = isNotEmpty()
clear()
return result
}
/**
* Sorts elements in the list in-place according to their natural sort order.
*/
@kotlin.jvm.JvmVersion
public fun <T : Comparable<T>> MutableList<T>.sort(): Unit {
if (size > 1) java.util.Collections.sort(this)
}
/**
* Sorts elements in the list in-place according to the order specified with [comparator].
*/
@kotlin.jvm.JvmVersion
public fun <T> MutableList<T>.sortWith(comparator: Comparator<in T>): Unit {
if (size > 1) java.util.Collections.sort(this, comparator)
}
/**
* Fills the list with the provided [value].
*
* Each element in the list gets replaced with the [value].
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
@SinceKotlin("1.2")
public inline fun <T> MutableList<T>.fill(value: T) {
java.util.Collections.fill(this, value)
}
/**
* Randomly shuffles elements in this mutable list.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
@SinceKotlin("1.2")
public inline fun <T> MutableList<T>.shuffle() {
java.util.Collections.shuffle(this)
}
/**
* Randomly shuffles elements in this mutable list using the specified [random] instance as the source of randomness.
*/
@kotlin.jvm.JvmVersion
@kotlin.internal.InlineOnly
@SinceKotlin("1.2")
public inline fun <T> MutableList<T>.shuffle(random: java.util.Random) {
java.util.Collections.shuffle(this, random)
}
/**
* Returns a new list with the elements of this list randomly shuffled.
*/
@kotlin.jvm.JvmVersion
@SinceKotlin("1.2")
public fun <T> Iterable<T>.shuffled(): List<T> = toMutableList().apply { shuffle() }
/**
* Returns a new list with the elements of this list randomly shuffled
* using the specified [random] instance as the source of randomness.
*/
@kotlin.jvm.JvmVersion
@SinceKotlin("1.2")
public fun <T> Iterable<T>.shuffled(random: java.util.Random): List<T> = toMutableList().apply { shuffle(random) }