-
Notifications
You must be signed in to change notification settings - Fork 16
/
array.effekt
215 lines (187 loc) · 5.62 KB
/
array.effekt
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
module array
import effekt
import exception
import list
/**
* A mutable 0-indexed fixed-sized array.
*/
extern type Array[T]
/**
* Allocates a new array of size `size`, keeping its values _undefined_.
* Prefer using `array` constructor instead to ensure that values are defined.
*/
extern global def allocate[T](size: Int): Array[T] =
js "(new Array(${size}))"
chez "(make-vector ${size})" // creates an array filled with 0s on CS
llvm """
%z = call %Pos @c_array_new(%Int %size)
ret %Pos %z
"""
/**
* Creates a new Array of size `size` filled with the value `init`
*/
extern global def array[T](size: Int, init: T): Array[T] =
ml "Array.array (${size}, ${init})"
default {
val arr = allocate[T](size);
each(0, size) { i =>
unsafeSet(arr, i, init)
};
arr
}
/**
* Converts a List `list` to an Array
*/
def fromList[T](list: List[T]): Array[T] = {
val listSize = list.size();
val arr = allocate(listSize);
foreachIndex(list) { (i, head) =>
arr.unsafeSet(i, head)
}
return arr;
}
/**
* Gets the length of the array in constant time.
*/
extern pure def size[T](arr: Array[T]): Int =
js "${arr}.length"
chez "(vector-length ${arr})"
ml "Array.length ${arr}"
llvm """
%z = call %Int @c_array_size(%Pos %arr)
ret %Int %z
"""
/**
* Gets the element of the `arr` at given `index` in constant time.
* Unchecked Precondition: `index` is in bounds (0 ≤ index < arr.size)
*
* Prefer using `get` instead.
*/
extern global def unsafeGet[T](arr: Array[T], index: Int): T =
js "${arr}[${index}]"
chez "(vector-ref ${arr} ${index})"
ml "Array.sub (${arr}, ${index})"
llvm """
%z = call %Pos @c_array_get(%Pos %arr, %Int %index)
ret %Pos %z
"""
extern js """
function array$set(arr, index, value) {
arr[index] = value;
return $effekt.unit
}
"""
/**
* Sets the element of the `arr` at given `index` to `value` in constant time.
* Unchecked Precondition: `index` is in bounds (0 ≤ index < arr.size)
*
* Prefer using `set` instead.
*/
extern global def unsafeSet[T](arr: Array[T], index: Int, value: T): Unit =
js "array$set(${arr}, ${index}, ${value})"
chez "(begin (vector-set! ${arr} ${index} ${value}) #f)"
ml "Array.update (${arr}, ${index}, ${value})"
llvm """
%z = call %Pos @c_array_set(%Pos %arr, %Int %index, %Pos %value)
ret %Pos %z
"""
/**
* Creates a copy of `arr`
*/
def copy[T](arr: Array[T]): Array[T] = {
with on[OutOfBounds].default { <> }; // should not happen
val len = arr.size;
val newArray = allocate[T](len);
copy[T](arr, 0, newArray, 0, len);
newArray
}
/**
* Copies `length`-many elements from `from` to `to`
* starting at `start` (in `from`) and `offset` (in `to`)
*/
def copy[T](from: Array[T], start: Int, to: Array[T], offset: Int, length: Int): Unit / Exception[OutOfBounds] = {
val startValid = start >= 0 && start + length <= from.size
val offsetValid = offset >= 0 && offset + length <= to.size
def go(i: Int, j: Int, length: Int): Unit =
if (length >= 0) {
to.unsafeSet(j, from.unsafeGet(i))
go(i + 1, j + 1, length - 1)
}
if (startValid && offsetValid) go(start, offset, length)
else do raise(OutOfBounds(), "Array index out of bounds, when copying")
}
// Derived operations:
/**
* Gets the element of the `arr` at given `index` in constant time,
* throwing an `Exception[OutOfBounds]` unless `0 ≤ index < arr.size`.
*/
def get[T](arr: Array[T], index: Int): T / Exception[OutOfBounds] =
if (index >= 0 && index < arr.size) arr.unsafeGet(index)
else do raise(OutOfBounds(), "Array index out of bounds: " ++ show(index))
/**
* Sets the element of the `arr` at given `index` to `value` in constant time,
* throwing an `Exception[OutOfBounds]` unless `0 ≤ index < arr.size`.
*/
def set[T](arr: Array[T], index: Int, value: T): Unit / Exception[OutOfBounds] =
if (index >= 0 && index < arr.size) unsafeSet(arr, index, value)
else do raise(OutOfBounds(), "Array index out of bounds: " ++ show(index))
/**
* Builds a new Array of size `size` from a computation `index` which gets an index
* and returns a value that will be on that position in the resulting array
*/
def build[T](size: Int) { index: Int => T }: Array[T] = {
val arr = allocate[T](size);
each(0, size) { i =>
unsafeSet(arr, i, index(i))
};
arr
}
// Utility functions:
def toList[T](arr: Array[T]): List[T] = {
var i = arr.size - 1;
var l = Nil[T]()
while (i >= 0) {
l = Cons(arr.unsafeGet(i), l)
i = i - 1
}
l
}
def foreach[T](arr: Array[T]){ action: T => Unit / Control }: Unit = {
each(0, arr.size) { i =>
val x: T = arr.unsafeGet(i)
action(x)
}
}
def foreachIndex[T](arr: Array[T]){ action: (Int, T) => Unit / Control }: Unit = {
each(0, arr.size) { i =>
val x: T = arr.unsafeGet(i)
action(i, x)
}
}
def sum(list: Array[Int]): Int = {
var acc = 0
list.foreach { x =>
acc = acc + x
}
acc
}
// Show Instances
// --------------
def show[A](arr: Array[A]) { showA: A => String }: String = {
var output = "Array("
val lastIndex = arr.size - 1
arr.foreachIndex { (index, a) =>
if (index == lastIndex) output = output ++ showA(a)
else output = output ++ showA(a) ++ ", "
}
output = output ++ ")"
output
}
def show(l: Array[Int]): String = show(l) { e => show(e) }
def show(l: Array[Double]): String = show(l) { e => show(e) }
def show(l: Array[Bool]): String = show(l) { e => show(e) }
def show(l: Array[String]): String = show(l) { e => e }
def println(l: Array[Int]): Unit = println(show(l))
def println(l: Array[Double]): Unit = println(show(l))
def println(l: Array[Bool]): Unit = println(show(l))
def println(l: Array[String]): Unit = println(show(l))