2011-06-16 18:08:26 -05:00
|
|
|
// Interior vector utility functions.
|
|
|
|
|
|
|
|
import option::none;
|
|
|
|
import option::some;
|
|
|
|
|
|
|
|
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[T](&mutable T[] v, uint n);
|
|
|
|
fn ivec_on_heap[T](&T[] v) -> bool;
|
2011-06-16 19:06:24 -05:00
|
|
|
fn ivec_to_ptr[T](&T[] v) -> *T;
|
|
|
|
fn ivec_copy_from_buf[T](&mutable T[] v, *T ptr, uint count);
|
2011-06-16 18:08:26 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Reserves space for `n` elements in the given vector.
|
|
|
|
fn reserve[T](&mutable T[] v, uint n) {
|
|
|
|
rustrt::ivec_reserve(v, n);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn on_heap[T](&T[] v) -> bool {
|
|
|
|
ret rustrt::ivec_on_heap(v);
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:06:24 -05:00
|
|
|
fn to_ptr[T](&T[] v) -> *T {
|
|
|
|
ret rustrt::ivec_to_ptr(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
fn len[T](&T[] v) -> uint {
|
|
|
|
ret rusti::ivec_len(v);
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:47:28 -05:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-06-16 19:06:24 -05:00
|
|
|
mod unsafe {
|
|
|
|
fn copy_from_buf[T](&mutable T[] v, *T ptr, uint count) {
|
|
|
|
ret rustrt::ivec_copy_from_buf(v, ptr, count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|