scoop:scoop.core:0.1.0
src/array_list.scoop
Selected production source · src/array_list.scoop
1
/**2
* A growable indexed list. Appending grows storage geometrically; insertion and removal shift3
* later elements. Clearing releases references in occupied slots.4
*/5
public class ArrayList<T> public constructor(initialCapacity: Long = 0L): MutableList<T> {6
private var storage: MutableArray<MaybeUninit<T>> =7
MutableArray<MaybeUninit<T>>(initialCapacity) { _ -> MaybeUninit<T>.uninit() }8
private var elementCount: Long = 0L10
/**11
* The current number of element slots visible through the list interface.12
*/13
public override val size: Long get() = elementCount15
private fun checkIndex(index: Long) {16
if (index < 0L || index >= elementCount) {17
throw IndexOutOfBoundsException()18
}19
}21
private fun ensureRoom() {22
if (elementCount == storage.size) {23
grow()24
}25
}27
private fun grow() {28
if (elementCount == 9223372036854775807L) {29
coreAllocationOverflow()30
}31
val required = elementCount + 1L32
val capacity = storage.size33
val nextCapacity =34
if (capacity == 0L) {35
8L36
} else {37
if (capacity > 4611686018427387903L) {38
required39
} else {40
capacity * 2L41
}42
}43
val old = storage44
val count = elementCount45
val replacement =46
MutableArray<MaybeUninit<T>>(nextCapacity) { _ -> MaybeUninit<T>.uninit() }47
var index = 0L48
while (index < count) {49
replacement[index] = old[index]50
index += 1L51
}52
storage = replacement53
}55
/**56
* Reads the element at a zero-based index; out-of-range indices throw57
* [IndexOutOfBoundsException].58
*/59
public override operator fun get(index: Long): T {60
checkIndex(index)61
@Unsafe62
{63
return storage[index].assumeInit()64
}65
}67
/**68
* Replaces an existing element; out-of-range indices throw [IndexOutOfBoundsException].69
*/70
public override operator fun set(index: Long, value: T) {71
checkIndex(index)72
storage[index] = MaybeUninit<T>.initialized(value)73
}75
/**76
* Appends an element or inserts it at the supplied index. Indexed insertion shifts later77
* elements.78
*/79
public override fun add(value: T) {80
val count = elementCount81
ensureRoom()82
storage[count] = MaybeUninit<T>.initialized(value)83
elementCount = count + 1L84
}86
/**87
* Appends an element or inserts it at the supplied index. Indexed insertion shifts later88
* elements.89
*/90
public override fun add(index: Long, value: T) {91
val count = elementCount92
if (index < 0L || index > count) {93
throw IndexOutOfBoundsException()94
}95
ensureRoom()96
val target = storage97
var cursor = count98
while (cursor > index) {99
target[cursor] = target[cursor - 1L]100
cursor = cursor - 1L101
}102
target[index] = MaybeUninit<T>.initialized(value)103
elementCount = count + 1L104
}106
/**107
* Removes and returns the indexed element, shifts later elements, and releases the vacated108
* slot.109
*/110
public override fun removeAt(index: Long): T {111
checkIndex(index)112
@Unsafe113
{114
val target = storage115
val result = target[index].assumeInit()116
val last = elementCount - 1L117
var cursor = index118
while (cursor < last) {119
target[cursor] = target[cursor + 1L]120
cursor = cursor + 1L121
}122
target[last] = MaybeUninit<T>.uninit()123
elementCount = last124
return result125
}126
}128
/**129
* Releases all occupied element slots and sets the size to zero.130
*/131
public override fun clear() {132
val target = storage133
val count = elementCount134
var cursor = 0L135
while (cursor < count) {136
target[cursor] = MaybeUninit<T>.uninit()137
cursor = cursor + 1L138
}139
elementCount = 0L140
}142
/** Copies the initialized elements into independent immutable array slots. */143
public fun toArray(): Array<T> {144
val count = elementCount145
val source = storage146
return Array<T>(147
count,148
fun(index: Long): T {149
@Unsafe150
{151
return source[index].assumeInit()152
}153
}154
)155
}157
/** Copies the initialized elements into independent mutable array slots. */158
public fun toMutableArray(): MutableArray<T> {159
val count = elementCount160
val source = storage161
return MutableArray<T>(162
count,163
fun(index: Long): T {164
@Unsafe165
{166
return source[index].assumeInit()167
}168
}169
)170
}172
/**173
* Traverses the elements in index order and remains exhausted after reaching the end.174
*/175
public override operator fun iterator(): Iterator<T> = ListIterator<T>(this, 0L, false)177
/**178
* Builds element-wise inspection and serialization adapters for this container.179
*/180
public companion object {181
/**182
* Builds a sequence inspector using the supplied element inspector.183
*/184
public fun inspector(element: ValueInspector<T>): ValueInspector<ArrayList<T>> =185
ArrayListInspector<T>(element)187
/**188
* Builds a sequence encoder using the supplied element encoder.189
*/190
public fun encoder(element: ValueEncoder<T>): ValueEncoder<ArrayList<T>> =191
ArrayListEncoder<T>(element)193
/**194
* Builds a sequence decoder using the supplied element decoder.195
*/196
public fun decoder(element: ValueDecoder<T>): ValueDecoder<ArrayList<T>> =197
ArrayListDecoder<T>(element)199
override interface ValueEncoder<ArrayList<T>> where T.Companion: ValueEncoder<T> {200
/** Encodes logical elements using the element companion. */201
public override fun encode(value: ArrayList<T>, encoder: Encoder): Unit =202
encodeList(value, T.Companion, encoder)203
}205
override interface ValueDecoder<ArrayList<T>> where T.Companion: ValueDecoder<T> {206
/** Decodes a new list using the element companion. */207
public override fun decode(decoder: Decoder): ArrayList<T> =208
decodeSequence(decoder, T.Companion)209
}210
}211
}213
@NoGC214
@Extern(name = "scoop_rt_allocation_overflow", abi = "scoop")215
internal fun coreAllocationOverflow()