@@ -77,30 +77,7 @@ extern global def unsafeSet[T](arr: Array[T], index: Int, value: T): Unit =
7777 ret %Pos %z
7878 """
7979
80- /// Creates a copy of `arr`
81- def copy[T](arr: Array[T]): Array[T] = {
82- with on[OutOfBounds].default { <> }; // should not happen
83- val len = arr.size;
84- val newArray = allocate[T](len);
85- copy[T](arr, 0, newArray, 0, len);
86- newArray
87- }
8880
89- /// Copies `length`-many elements from `from` to `to`
90- /// starting at `start` (in `from`) and `offset` (in `to`)
91- def copy[T](from: Array[T], start: Int, to: Array[T], offset: Int, length: Int): Unit / Exception[OutOfBounds] = {
92- val startValid = start >= 0 && start + length <= from.size
93- val offsetValid = offset >= 0 && offset + length <= to.size
94-
95- def go(i: Int, j: Int, length: Int): Unit =
96- if (length > 0) {
97- to.unsafeSet(j, from.unsafeGet(i))
98- go(i + 1, j + 1, length - 1)
99- }
100-
101- if (startValid && offsetValid) go(start, offset, length)
102- else do raise(OutOfBounds(), "Array index out of bounds, when copying")
103- }
10481
10582// Derived operations:
10683
@@ -126,6 +103,44 @@ def build[T](size: Int) { index: Int => T }: Array[T] = {
126103 arr
127104}
128105
106+ def resize[T](source: Array[T], size: Int): Array[T] = {
107+ val target = allocate(size)
108+ val n = min(source.size, target.size)
109+ def go(i: Int): Array[T] =
110+ if (i < n) {
111+ target.unsafeSet(i, source.unsafeGet(i))
112+ go(i + 1)
113+ } else {
114+ target
115+ }
116+ go(0)
117+ }
118+
119+ /**
120+ * Creates a copy of `arr`
121+ */
122+ def copy[T](array: Array[T]): Array[T] =
123+ array.resize(array.size)
124+
125+ /**
126+ * Copies `length`-many elements from `from` to `to`
127+ * starting at `start` (in `from`) and `offset` (in `to`)
128+ */
129+ def copy[T](from: Array[T], start: Int, to: Array[T], offset: Int, length: Int): Unit / Exception[OutOfBounds] = {
130+ val startValid = start >= 0 && start + length <= from.size
131+ val offsetValid = offset >= 0 && offset + length <= to.size
132+
133+ def go(i: Int, j: Int, length: Int): Unit =
134+ if (length > 0) {
135+ to.unsafeSet(j, from.unsafeGet(i))
136+ go(i + 1, j + 1, length - 1)
137+ }
138+
139+ if (startValid && offsetValid) go(start, offset, length)
140+ else do raise(OutOfBounds(), "Array index out of bounds, when copying")
141+ }
142+
143+
129144// Utility functions:
130145
131146def toList[T](arr: Array[T]): List[T] = {
@@ -140,26 +155,22 @@ def toList[T](arr: Array[T]): List[T] = {
140155
141156def foreach[T](arr: Array[T]){ action: T => Unit }: Unit =
142157 each(0, arr.size) { i =>
143- val x: T = arr.unsafeGet(i)
144- action(x)
158+ action(arr.unsafeGet(i))
145159 }
146160
147161def foreach[T](arr: Array[T]){ action: (T) {Control} => Unit }: Unit =
148162 each(0, arr.size) { (i) {label} =>
149- val x: T = arr.unsafeGet(i)
150- action(x) {label}
163+ action(arr.unsafeGet(i)) {label}
151164 }
152165
153166def foreachIndex[T](arr: Array[T]){ action: (Int, T) => Unit }: Unit =
154167 each(0, arr.size) { i =>
155- val x: T = arr.unsafeGet(i)
156- action(i, x)
168+ action(i, arr.unsafeGet(i))
157169 }
158170
159171def foreachIndex[T](arr: Array[T]){ action: (Int, T) {Control} => Unit }: Unit =
160172 each(0, arr.size) { (i) {label} =>
161- val x: T = arr.unsafeGet(i)
162- action(i, x) {label}
173+ action(i, arr.unsafeGet(i)) {label}
163174 }
164175
165176def sum(list: Array[Int]): Int = {
0 commit comments