1
2
3
4 mutable data vector a =
5 Vector {
6
7
8 default: int -> a;
9
10
11 size: int;
12
13
14
15
16
17
18
19 table: array (option a)
20 }
21
22 val minimum_length =
23 16
24
25 val new_length (length: int, size: int) : int =
26
27
28 let length =
29 max (
30 minimum_length,
31 (array::above (length, size))
32 )
33 in
34 if size <= length then
35 length
36 else if size <= array::max_length then
37 size
38 else
39
40 fail
41
42 val init [a] (size: int, default: int -> a, f: int -> a) : vector a =
43 let length = new_length (0, size) in
44 Vector {
45 default = default;
46 size = size;
47 table = array::init (Up, length, fun (i: int) : option a =
48 if i < size then some (f i) else None
49 )
50 }
51
52 val grab [a] (consumes r: array a, default: int -> a) : vector a =
53
54
55 array::transform (Up, r, fun (_: int, consumes x: a) : option a = some x);
56 Vector {
57 default = default;
58 size = array::length r;
59 table = r
60 }
61
62 val list2vector [a] (consumes xs: list::list a, default: int -> a) : vector a =
63 grab (list::list2array xs, default)
64
65 val create [a] (default: int -> a) : vector a =
66 init (0, default, fun (i : int) : a = fail)
67
68 val size [a] (v: vector a) : int =
69 v.size
70
71 val resize [a] (v: vector a, new_size: int) : () =
72 if new_size < 0 then
73 fail;
74 let old_size = v.size in
75 if new_size <> old_size then begin
76
77 v.size <- new_size;
78 let table = v.table in
79 if new_size < old_size then
80
81
82
83
84
85
86 array::iter_segment (Up, new_size, old_size, fun (k: int | table @ array (option a)) : () =
87 array::set (table, k, None)
88 )
89 else begin
90
91 v.size <- new_size;
92 let old_length = array::length table in
93 if new_size > old_length then begin
94 let new_length = new_length (old_length, new_size) in
95
96
97
98
99
100 let table' = array::create [None] (new_length, None) in
101 array::steal [option a] (table, table', 0);
102 v.table <- table'
103 end;
104
105 array::iter_segment (Up, old_size, new_size, fun (k: int | v @ vector a) : () =
106 array::set (v.table, k, some (v.default k))
107 )
108 end
109 end
110
111 val shrink [a] (v: vector a, delta: int) : () =
112 if delta < 0 then
113 fail;
114 resize (v, size v - delta)
115
116 val pop [a] (v: vector a) : () =
117 resize (v, size v - 1)
118
119 val get [a] duplicable a => (v: vector a, i: int) : a =
120 if i >= 0 && i < v.size then
121 option::force (array::get (v.table, i))
122 else
123 fail
124
125 val set [a] (v: vector a, i: int, consumes x: a) : () =
126 if i >= 0 && i < v.size then
127 array::set (v.table, i, some x)
128 else
129 fail
130
131 val push [a] (v: vector a, consumes x: a) : () =
132 let i = size v in
133 resize (v, i + 1);
134 array::set (v.table, i, some x)
135
136 val last [a] duplicable a => (v: vector a) : a =
137 get (v, size v - 1)
138
139 val sort [a, p : perm] duplicable a => (v: vector a, cmp: (a, a | p) -> int | p) : () =
140
141 let cmp (x: option a, y: option a | p) : int =
142 cmp (option::force x, option::force y)
143 in
144
145
146 array::sort_initial_segment (cmp, v.table, v.size)
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