2010-06-23 23:03:09 -05:00
|
|
|
import vbuf = rustrt.vbuf;
|
2010-07-05 16:42:12 -05:00
|
|
|
import op = util.operator;
|
2010-06-23 23:03:09 -05:00
|
|
|
|
|
|
|
native "rust" mod rustrt {
|
|
|
|
type vbuf;
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-08-04 14:59:48 -05:00
|
|
|
fn vec_buf[T](vec[T] v, uint offset) -> vbuf;
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
fn vec_len[T](vec[T] v) -> uint;
|
2010-08-19 19:37:22 -05:00
|
|
|
/**
|
|
|
|
* Sometimes we modify the vec internal data via vec_buf and need to update
|
|
|
|
* the vec's fill length accordingly.
|
|
|
|
*/
|
|
|
|
fn vec_len_set[T](vec[T] v, uint n);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The T in vec_alloc[T, U] is the type of the vec to allocate. The
|
2010-07-22 19:47:32 -05:00
|
|
|
* U is the type of an element in the vec. So to allocate a vec[U] we
|
2010-08-19 19:37:22 -05:00
|
|
|
* want to invoke this as vec_alloc[vec[U], U].
|
|
|
|
*/
|
2010-07-22 19:47:32 -05:00
|
|
|
fn vec_alloc[T, U](uint n_elts) -> vec[U];
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-07-13 16:23:49 -05:00
|
|
|
fn refcount[T](vec[T] v) -> uint;
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2010-08-12 15:11:49 -05:00
|
|
|
fn vec_print_debug_info[T](vec[T] v);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-07-05 16:42:12 -05:00
|
|
|
fn alloc[T](uint n_elts) -> vec[T] {
|
2010-07-22 19:47:32 -05:00
|
|
|
ret rustrt.vec_alloc[vec[T], T](n_elts);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2010-09-03 18:25:56 -05:00
|
|
|
fn refcount[T](vec[T] v) -> uint {
|
|
|
|
// -1 because calling this function incremented the refcount.
|
|
|
|
ret rustrt.refcount[T](v) - 1u;
|
|
|
|
}
|
|
|
|
|
2010-07-05 16:42:12 -05:00
|
|
|
type init_op[T] = fn(uint i) -> T;
|
|
|
|
|
|
|
|
fn init_fn[T](&init_op[T] op, uint n_elts) -> vec[T] {
|
2010-06-23 23:03:09 -05:00
|
|
|
let vec[T] v = alloc[T](n_elts);
|
2010-08-11 17:56:19 -05:00
|
|
|
let uint i = 0u;
|
|
|
|
while (i < n_elts) {
|
2010-07-05 16:42:12 -05:00
|
|
|
v += vec(op(i));
|
2010-08-11 17:56:19 -05:00
|
|
|
i += 1u;
|
2010-07-05 16:42:12 -05:00
|
|
|
}
|
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
|
|
|
fn init_elt[T](&T t, uint n_elts) -> vec[T] {
|
2010-07-22 14:45:58 -05:00
|
|
|
/**
|
|
|
|
* FIXME (issue #81): should be:
|
|
|
|
*
|
2010-08-03 20:06:31 -05:00
|
|
|
* fn elt_op[T](&T x, uint i) -> T { ret x; }
|
2010-07-22 14:45:58 -05:00
|
|
|
* let init_op[T] inner = bind elt_op[T](t, _);
|
|
|
|
* ret init_fn[T](inner, n_elts);
|
|
|
|
*/
|
2010-07-05 16:42:12 -05:00
|
|
|
let vec[T] v = alloc[T](n_elts);
|
|
|
|
let uint i = n_elts;
|
2010-07-27 21:21:51 -05:00
|
|
|
while (i > 0u) {
|
|
|
|
i -= 1u;
|
2010-06-23 23:03:09 -05:00
|
|
|
v += vec(t);
|
|
|
|
}
|
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
2010-08-19 19:37:22 -05:00
|
|
|
fn buf[T](vec[T] v) -> vbuf {
|
|
|
|
ret rustrt.vec_buf[T](v, 0u);
|
|
|
|
}
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
fn len[T](vec[T] v) -> uint {
|
|
|
|
ret rustrt.vec_len[T](v);
|
|
|
|
}
|
|
|
|
|
2010-08-19 19:37:22 -05:00
|
|
|
fn len_set[T](vec[T] v, uint n) {
|
|
|
|
rustrt.vec_len_set[T](v, n);
|
2010-08-04 14:59:48 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
fn buf_off[T](vec[T] v, uint offset) -> vbuf {
|
|
|
|
check (offset < len[T](v));
|
|
|
|
ret rustrt.vec_buf[T](v, offset);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2010-06-29 01:18:51 -05:00
|
|
|
|
2010-08-12 15:11:49 -05:00
|
|
|
fn print_debug_info[T](vec[T] v) {
|
|
|
|
rustrt.vec_print_debug_info[T](v);
|
|
|
|
}
|
|
|
|
|
2010-07-11 16:51:02 -05:00
|
|
|
// Returns elements from [start..end) from v.
|
|
|
|
fn slice[T](vec[T] v, int start, int end) -> vec[T] {
|
2010-08-12 15:18:10 -05:00
|
|
|
check (0 <= start);
|
|
|
|
check (start <= end);
|
|
|
|
check (end <= (len[T](v) as int));
|
2010-07-27 21:21:51 -05:00
|
|
|
auto result = alloc[T]((end - start) as uint);
|
2010-08-12 15:18:10 -05:00
|
|
|
let int i = start;
|
2010-07-11 16:51:02 -05:00
|
|
|
while (i < end) {
|
|
|
|
result += vec(v.(i));
|
|
|
|
i += 1;
|
|
|
|
}
|
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2010-08-03 20:10:18 -05:00
|
|
|
fn grow[T](&mutable vec[T] v, int n, &T initval) {
|
2010-06-29 01:18:51 -05:00
|
|
|
let int i = n;
|
|
|
|
while (i > 0) {
|
|
|
|
i -= 1;
|
|
|
|
v += vec(initval);
|
|
|
|
}
|
|
|
|
}
|
2010-07-05 16:42:12 -05:00
|
|
|
|
2010-08-03 20:18:19 -05:00
|
|
|
fn map[T, U](&op[T,U] f, &vec[T] v) -> vec[U] {
|
|
|
|
let vec[U] u = alloc[U](len[T](v));
|
2010-07-05 16:42:12 -05:00
|
|
|
for (T ve in v) {
|
2010-07-12 21:39:29 -05:00
|
|
|
u += vec(f(ve));
|
2010-07-05 16:42:12 -05:00
|
|
|
}
|
|
|
|
ret u;
|
|
|
|
}
|