-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy patharray-inf.sml
More file actions
38 lines (30 loc) · 910 Bytes
/
array-inf.sml
File metadata and controls
38 lines (30 loc) · 910 Bytes
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
structure ArrayInf :> ARRAY_INF =
struct
type 'a array = 'a Array.array ref * 'a
fun array x = (ref (Array.array (0, x)), x)
fun sub ((ar, x), i) =
Array.sub (!ar, i)
handle Subscript =>
if i < 0 then
raise Subscript
else
x
fun update ((ar, x), i, y) =
Array.update (!ar, i, y)
handle Subscript =>
if i < 0 then
raise Subscript
else
let
val n = Int.max (i+1, Array.length (!ar) * 2)
val a = Array.array (n, x)
in
Array.copy {di=0, src= !ar, dst=a};
ar := a;
Array.update (a, i, y)
end
fun erase (ar, x) =
ar := Array.array (0, x)
fun isEmpty (ar, _) =
Array.length (!ar) = 0
end