Scoop API

scoop:scoop.core:0.1.0

src/arrays.scoop

Selected production source · src/arrays.scoop

1/**
2 * A fixed-size array with immutable element slots and zero-based Long indexing. Copy conversion
3 * creates independent slots while preserving element values or references.
4 */
5@Intrinsic("core_array")
6public class Array<T> : List<T> {
7 @Intrinsic("array_length")
8 private fun arrayLength(): Long
10 /**
11 * The current number of element slots visible through the list interface.
12 */
13 public override val size: Long get() = arrayLength()
15 @Intrinsic("array_get")
16 private fun arrayRead(index: Long): T
18 /**
19 * Reads the element at a zero-based index; out-of-range indices throw
20 * [IndexOutOfBoundsException].
21 */
22 public override operator fun get(index: Long): T = arrayRead(index)
24 /**
25 * Copies the elements into independent mutable array slots.
26 */
27 @Intrinsic("array_to_mutable")
28 public fun toMutableArray(): MutableArray<T>
30 /**
31 * Traverses the elements in index order and remains exhausted after reaching the end.
32 */
33 public override operator fun iterator(): Iterator<T> = ListIterator<T>(this, 0L, false)
35 /**
36 * Builds element-wise inspection and serialization adapters for this container.
37 */
38 public companion object {
39 /**
40 * Builds a sequence inspector using the supplied element inspector.
41 */
42 public fun inspector(element: ValueInspector<T>): ValueInspector<Array<T>> = ArrayInspector<T>(element)
44 /**
45 * Builds a sequence encoder using the supplied element encoder.
46 */
47 public fun encoder(element: ValueEncoder<T>): ValueEncoder<Array<T>> = ArrayEncoder<T>(element)
49 /**
50 * Builds a sequence decoder using the supplied element decoder.
51 */
52 public fun decoder(element: ValueDecoder<T>): ValueDecoder<Array<T>> = ArrayDecoder<T>(element)
54 override interface ValueEncoder<Array<T>> where T.Companion : ValueEncoder<T> {
55 /** Encodes elements in index order using the element companion. */
56 public override fun encode(value: Array<T>, encoder: Encoder): Unit =
57 encodeList(value, T.Companion, encoder)
58 }
60 override interface ValueDecoder<Array<T>> where T.Companion : ValueDecoder<T> {
61 /** Decodes a new array using the element companion. */
62 public override fun decode(decoder: Decoder): Array<T> =
63 decodeSequence(decoder, T.Companion).toArray()
64 }
65 }
68/**
69 * A fixed-size array with replaceable element slots and zero-based Long indexing. Use [ArrayList]
70 * when the number of elements must change.
71 */
72@Intrinsic("core_mutable_array")
73public class MutableArray<T> : List<T>, IndexSet<Long, T> {
74 @Intrinsic("mutable_array_length")
75 private fun arrayLength(): Long
77 /**
78 * The current number of element slots visible through the list interface.
79 */
80 public override val size: Long get() = arrayLength()
82 @Intrinsic("mutable_array_get")
83 private fun arrayRead(index: Long): T
85 /**
86 * Reads the element at a zero-based index; out-of-range indices throw
87 * [IndexOutOfBoundsException].
88 */
89 public override operator fun get(index: Long): T = arrayRead(index)
91 @Intrinsic("mutable_array_set")
92 private fun arrayWrite(index: Long, value: T)
94 /**
95 * Replaces an existing element; out-of-range indices throw [IndexOutOfBoundsException].
96 */
97 public override operator fun set(index: Long, value: T): Unit = arrayWrite(index, value)
99 /**
100 * Copies the elements into independent immutable array slots.
101 */
102 @Intrinsic("array_to_immutable")
103 public fun toArray(): Array<T>
105 /**
106 * Traverses the elements in index order and remains exhausted after reaching the end.
107 */
108 public override operator fun iterator(): Iterator<T> = ListIterator<T>(this, 0L, false)
110 /**
111 * Builds element-wise inspection and serialization adapters for this container.
112 */
113 public companion object {
114 /**
115 * Builds a sequence inspector using the supplied element inspector.
116 */
117 public fun inspector(element: ValueInspector<T>): ValueInspector<MutableArray<T>> = MutableArrayInspector<T>(element)
119 /**
120 * Builds a sequence encoder using the supplied element encoder.
121 */
122 public fun encoder(element: ValueEncoder<T>): ValueEncoder<MutableArray<T>> = MutableArrayEncoder<T>(element)
124 /**
125 * Builds a sequence decoder using the supplied element decoder.
126 */
127 public fun decoder(element: ValueDecoder<T>): ValueDecoder<MutableArray<T>> =
128 MutableArrayDecoder<T>(element)
130 override interface ValueEncoder<MutableArray<T>> where T.Companion : ValueEncoder<T> {
131 /** Encodes elements in index order using the element companion. */
132 public override fun encode(value: MutableArray<T>, encoder: Encoder): Unit =
133 encodeList(value, T.Companion, encoder)
134 }
136 override interface ValueDecoder<MutableArray<T>> where T.Companion : ValueDecoder<T> {
137 /** Decodes a new mutable array using the element companion. */
138 public override fun decode(decoder: Decoder): MutableArray<T> =
139 decodeSequence(decoder, T.Companion).toMutableArray()
140 }
141 }