/
concPropUtils.kt
43 lines (34 loc) · 1.28 KB
/
concPropUtils.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
package net.aquadc.properties.internal
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater
internal inline fun <T, V> AtomicReferenceFieldUpdater<T, V>.update(zis: T, update: (V) -> V) {
var prev: V
var next: V
do {
prev = get(zis)
next = update(prev)
} while (!compareAndSet(zis, prev, next))
}
internal inline fun <reified T> Array<T>.with(lastElement: T): Array<T> {
val size = size
val new = arrayOfNulls<T>(size + 1)
System.arraycopy(this, 0, new, 0, size)
new[size] = lastElement
return new as Array<T> // if T is not nullable, this array element type won't be nullable too
}
internal inline fun <reified T> Array<T>.copyOfWithout(idx: Int): Array<T> {
val oldSize = size
val new = arrayOfNulls<T>(oldSize-1)
System.arraycopy(this, 0, new, 0, idx)
System.arraycopy(this, idx+1, new, idx, oldSize-idx-1)
return new as Array<T> // same here
}
internal inline fun <reified T> Array<T>.withoutNulls(): Array<T> {
val nulls = count { it == null }
if (nulls == 0) return this
val newSize = size - nulls
if (newSize == 0) return emptyArray()
val newArray = arrayOfNulls<T>(size - nulls)
var destPos = 0
forEach { if (it != null) newArray[destPos++] = it }
return newArray as Array<T>
}