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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
|
// Interior vector utility functions.
import option::none;
import option::some;
import uint::next_power_of_two;
import ptr::addr_of;
type operator2[T,U,V] = fn(&T, &U) -> V;
native "rust-intrinsic" mod rusti {
fn ivec_len[T](&T[] v) -> uint;
}
native "rust" mod rustrt {
fn ivec_reserve_shared[T](&mutable T[mutable?] v, uint n);
fn ivec_on_heap[T](&T[] v) -> uint;
fn ivec_to_ptr[T](&T[] v) -> *T;
fn ivec_copy_from_buf_shared[T](&mutable T[mutable?] v,
*T ptr, uint count);
}
/// Reserves space for `n` elements in the given vector.
fn reserve[T](&mutable T[mutable?] v, uint n) {
rustrt::ivec_reserve_shared(v, n);
}
fn on_heap[T](&T[] v) -> bool {
ret rustrt::ivec_on_heap(v) != 0u;
}
fn to_ptr[T](&T[] v) -> *T {
ret rustrt::ivec_to_ptr(v);
}
fn len[T](&T[mutable?] v) -> uint {
ret rusti::ivec_len(v);
}
type init_op[T] = fn(uint) -> T;
fn init_fn[T](&init_op[T] op, uint n_elts) -> T[] {
auto v = ~[];
reserve(v, n_elts);
let uint i = 0u;
while (i < n_elts) { v += ~[op(i)]; i += 1u; }
ret v;
}
// TODO: Remove me once we have slots.
fn init_fn_mut[T](&init_op[T] op, uint n_elts) -> T[mutable] {
auto v = ~[mutable];
reserve(v, n_elts);
let uint i = 0u;
while (i < n_elts) { v += ~[mutable op(i)]; i += 1u; }
ret v;
}
fn init_elt[T](&T t, uint n_elts) -> T[] {
auto v = ~[];
reserve(v, n_elts);
let uint i = 0u;
while (i < n_elts) { v += ~[t]; i += 1u; }
ret v;
}
// TODO: Remove me once we have slots.
fn init_elt_mut[T](&T t, uint n_elts) -> T[mutable] {
auto v = ~[mutable];
reserve(v, n_elts);
let uint i = 0u;
while (i < n_elts) { v += ~[mutable t]; i += 1u; }
ret v;
}
// Predicates
pred is_empty[T](&T[mutable?] v) -> bool {
// FIXME: This would be easier if we could just call len
for (T t in v) {
ret false;
}
ret true;
}
pred is_not_empty[T](&T[mutable?] v) -> bool {
ret !is_empty(v);
}
// Accessors
/// Returns the first element of a vector
fn head[T](&T[mutable?] v) : is_not_empty(v) -> T {
ret v.(0);
}
/// Returns all but the first element of a vector
fn tail[T](&T[mutable?] v) : is_not_empty(v) -> T[mutable?] {
ret slice(v, 1u, len(v));
}
/// Returns the last element of `v`.
fn last[T](&T[mutable?] v) -> option::t[T] {
if (len(v) == 0u) { ret none; }
ret some(v.(len(v) - 1u));
}
/// Returns a copy of the elements from [`start`..`end`) from `v`.
fn slice[T](&T[mutable?] v, uint start, uint end) -> T[] {
assert (start <= end);
assert (end <= len(v));
auto result = ~[];
reserve(result, end - start);
auto i = start;
while (i < end) { result += ~[v.(i)]; i += 1u; }
ret result;
}
// TODO: Remove me once we have slots.
fn slice_mut[T](&T[mutable?] v, uint start, uint end) -> T[mutable] {
assert (start <= end);
assert (end <= len(v));
auto result = ~[mutable];
reserve(result, end - start);
auto i = start;
while (i < end) { result += ~[mutable v.(i)]; i += 1u; }
ret result;
}
// Mutators
// TODO: Write this, unsafely, in a way that's not O(n).
fn pop[T](&mutable T[mutable?] v) -> T {
auto ln = len(v);
assert (ln > 0u);
ln -= 1u;
auto e = v.(ln);
v = slice(v, 0u, ln);
ret e;
}
// TODO: More.
// Appending
/// Expands the given vector in-place by appending `n` copies of `initval`.
fn grow[T](&mutable T[] v, uint n, &T initval) {
reserve(v, next_power_of_two(len(v) + n));
let uint i = 0u;
while (i < n) {
v += ~[initval];
i += 1u;
}
}
// TODO: Remove me once we have slots.
fn grow_mut[T](&mutable T[mutable] v, uint n, &T initval) {
reserve(v, next_power_of_two(len(v) + n));
let uint i = 0u;
while (i < n) {
v += ~[mutable initval];
i += 1u;
}
}
/// Calls `f` `n` times and appends the results of these calls to the given
/// vector.
fn grow_fn[T](&mutable T[] v, uint n, fn(uint)->T init_fn) {
reserve(v, next_power_of_two(len(v) + n));
let uint i = 0u;
while (i < n) {
v += ~[init_fn(i)];
i += 1u;
}
}
/// Sets the element at position `index` to `val`. If `index` is past the end
/// of the vector, expands the vector by replicating `initval` to fill the
/// intervening space.
fn grow_set[T](&mutable T[mutable] v, uint index, &T initval, &T val) {
if (index >= len(v)) { grow_mut(v, index - len(v) + 1u, initval); }
v.(index) = val;
}
// Functional utilities
fn map[T,U](fn(&T)->U f, &T[mutable?] v) -> U[] {
auto result = ~[];
reserve(result, len(v));
for (T elem in v) {
auto elem2 = elem; // satisfies alias checker
result += ~[f(elem2)];
}
ret result;
}
fn filter_map[T,U](fn(&T)->option::t[U] f, &T[mutable?] v) -> U[] {
auto result = ~[];
for (T elem in v) {
auto elem2 = elem; // satisfies alias checker
alt (f(elem2)) {
case (none) { /* no-op */ }
case (some(?result_elem)) { result += ~[result_elem]; }
}
}
ret result;
}
fn foldl[T,U](fn(&U,&T)->U p, &U z, &T[mutable?] v) -> U {
auto sz = len(v);
if (sz == 0u) { ret z; }
auto first = v.(0);
auto rest = slice(v, 1u, sz);
ret p(foldl[T,U](p, z, rest), first);
}
fn any[T](fn(&T)->bool f, &T[] v) -> bool {
for (T elem in v) { if (f(elem)) { ret true; } }
ret false;
}
fn all[T](fn(&T)->bool f, &T[] v) -> bool {
for (T elem in v) { if (!f(elem)) { ret false; } }
ret true;
}
fn member[T](&T x, &T[] v) -> bool {
for (T elt in v) { if (x == elt) { ret true; } }
ret false;
}
fn count[T](&T x, &T[mutable?] v) -> uint {
auto cnt = 0u;
for (T elt in v) { if (x == elt) { cnt += 1u; } }
ret cnt;
}
fn find[T](fn(&T) -> bool f, &T[] v) -> option::t[T] {
for (T elt in v) { if (f(elt)) { ret some[T](elt); } }
ret none[T];
}
mod unsafe {
type ivec_repr = rec(mutable uint fill,
mutable uint alloc,
*mutable ivec_heap_part heap_part);
type ivec_heap_part = rec(mutable uint fill);
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
ret rustrt::ivec_copy_from_buf_shared(v, ptr, count);
}
fn from_buf[T](*T ptr, uint bytes) -> T[] {
auto v = ~[];
copy_from_buf(v, ptr, bytes);
ret v;
}
fn set_len[T](&mutable T[] v, uint new_len) {
auto new_fill = new_len * sys::size_of[T]();
let *mutable ivec_repr stack_part =
::unsafe::reinterpret_cast(addr_of(v));
if ((*stack_part).fill == 0u) {
(*(*stack_part).heap_part).fill = new_fill; // On heap.
} else {
(*stack_part).fill = new_fill; // On stack.
}
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// compile-command: "make -k -C .. 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
// End:
|