Scoop API

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 shift
3 * later elements. Clearing releases references in occupied slots.
4 */
5public 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 = 0L
10 /**
11 * The current number of element slots visible through the list interface.
12 */
13 public override val size: Long get() = elementCount
15 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 + 1L
32 val capacity = storage.size
33 val nextCapacity =
34 if (capacity == 0L) {
35 8L
36 } else {
37 if (capacity > 4611686018427387903L) {
38 required
39 } else {
40 capacity * 2L
41 }
42 }
43 val old = storage
44 val count = elementCount
45 val replacement =
46 MutableArray<MaybeUninit<T>>(nextCapacity) { _ -> MaybeUninit<T>.uninit() }
47 var index = 0L
48 while (index < count) {
49 replacement[index] = old[index]
50 index += 1L
51 }
52 storage = replacement
53 }
55 /**
56 * Reads the element at a zero-based index; out-of-range indices throw
57 * [IndexOutOfBoundsException].
58 */
59 public override operator fun get(index: Long): T {
60 checkIndex(index)
61 @Unsafe
62 {
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 later
77 * elements.
78 */
79 public override fun add(value: T) {
80 val count = elementCount
81 ensureRoom()
82 storage[count] = MaybeUninit<T>.initialized(value)
83 elementCount = count + 1L
84 }
86 /**
87 * Appends an element or inserts it at the supplied index. Indexed insertion shifts later
88 * elements.
89 */
90 public override fun add(index: Long, value: T) {
91 val count = elementCount
92 if (index < 0L || index > count) {
93 throw IndexOutOfBoundsException()
94 }
95 ensureRoom()
96 val target = storage
97 var cursor = count
98 while (cursor > index) {
99 target[cursor] = target[cursor - 1L]
100 cursor = cursor - 1L
101 }
102 target[index] = MaybeUninit<T>.initialized(value)
103 elementCount = count + 1L
104 }
106 /**
107 * Removes and returns the indexed element, shifts later elements, and releases the vacated
108 * slot.
109 */
110 public override fun removeAt(index: Long): T {
111 checkIndex(index)
112 @Unsafe
113 {
114 val target = storage
115 val result = target[index].assumeInit()
116 val last = elementCount - 1L
117 var cursor = index
118 while (cursor < last) {
119 target[cursor] = target[cursor + 1L]
120 cursor = cursor + 1L
121 }
122 target[last] = MaybeUninit<T>.uninit()
123 elementCount = last
124 return result
125 }
126 }
128 /**
129 * Releases all occupied element slots and sets the size to zero.
130 */
131 public override fun clear() {
132 val target = storage
133 val count = elementCount
134 var cursor = 0L
135 while (cursor < count) {
136 target[cursor] = MaybeUninit<T>.uninit()
137 cursor = cursor + 1L
138 }
139 elementCount = 0L
140 }
142 /** Copies the initialized elements into independent immutable array slots. */
143 public fun toArray(): Array<T> {
144 val count = elementCount
145 val source = storage
146 return Array<T>(
147 count,
148 fun(index: Long): T {
149 @Unsafe
150 {
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 = elementCount
160 val source = storage
161 return MutableArray<T>(
162 count,
163 fun(index: Long): T {
164 @Unsafe
165 {
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 }
213@NoGC
214@Extern(name = "scoop_rt_allocation_overflow", abi = "scoop")
215internal fun coreAllocationOverflow()