2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
import option::none;
|
|
|
|
import option::some;
|
|
|
|
import util::orb;
|
2011-03-21 19:21:35 -05:00
|
|
|
|
2011-08-11 22:26:37 -05:00
|
|
|
export vbuf;
|
|
|
|
export empty_mut;
|
|
|
|
export init_fn;
|
|
|
|
export init_fn_mut;
|
|
|
|
export init_elt;
|
|
|
|
export init_elt_mut;
|
|
|
|
export buf;
|
|
|
|
export len;
|
|
|
|
export print_debug_info;
|
|
|
|
export slice;
|
|
|
|
export pop;
|
|
|
|
export push;
|
|
|
|
export foldl;
|
|
|
|
export reversed;
|
|
|
|
export member;
|
|
|
|
export map;
|
|
|
|
export map2;
|
|
|
|
export filter_map;
|
|
|
|
export empty;
|
|
|
|
export position;
|
|
|
|
export position_pred;
|
|
|
|
export reverse;
|
|
|
|
|
2011-05-12 10:24:54 -05:00
|
|
|
type vbuf = rustrt::vbuf;
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
type operator2[T, U, V] = fn(&T, &U) -> V ;
|
2010-11-09 17:35:40 -06:00
|
|
|
|
2011-04-28 19:15:32 -05:00
|
|
|
type array[T] = vec[mutable? T];
|
|
|
|
|
2010-06-23 23:03:09 -05:00
|
|
|
native "rust" mod rustrt {
|
2010-09-22 17:44:13 -05:00
|
|
|
type vbuf;
|
2011-07-27 07:19:39 -05:00
|
|
|
fn vec_buf[T](v: vec[T], offset: uint) -> vbuf;
|
|
|
|
fn vec_len[T](v: vec[T]) -> uint;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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](v: vec[T], n: uint);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The T in vec_alloc[T, U] is the type of the vec to allocate. The
|
|
|
|
* U is the type of an element in the vec. So to allocate a vec[U] we
|
|
|
|
* want to invoke this as vec_alloc[vec[U], U].
|
|
|
|
*/
|
|
|
|
fn vec_alloc[T, U](n_elts: uint) -> vec[U];
|
|
|
|
fn vec_alloc_mut[T, U](n_elts: uint) -> vec[mutable U];
|
|
|
|
fn refcount[T](v: vec[T]) -> uint;
|
|
|
|
fn vec_print_debug_info[T](v: vec[T]);
|
|
|
|
fn vec_from_vbuf[T](v: vbuf, n_elts: uint) -> vec[T];
|
|
|
|
fn unsafe_vec_to_mut[T](v: vec[T]) -> vec[mutable T];
|
|
|
|
}
|
|
|
|
|
|
|
|
fn alloc[T](n_elts: uint) -> vec[T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_alloc[vec[T], T](n_elts);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn alloc_mut[T](n_elts: uint) -> vec[mutable T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_alloc_mut[vec[mutable T], T](n_elts);
|
2011-03-18 15:53:49 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn refcount[T](v: array[T]) -> uint {
|
|
|
|
let r = rustrt::refcount[T](v);
|
|
|
|
if r == dbg::const_refcount { ret r; } else { ret r - 1u; }
|
2010-09-03 18:25:56 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn vec_from_vbuf[T](v: vbuf, n_elts: uint) -> vec[T] {
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_from_vbuf[T](v, n_elts);
|
2011-03-24 19:21:09 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-04-15 19:44:52 -05:00
|
|
|
// FIXME: Remove me; this is a botch to get around rustboot's bad typechecker.
|
2011-06-15 13:19:50 -05:00
|
|
|
fn empty[T]() -> vec[T] { ret alloc[T](0u); }
|
|
|
|
|
2011-04-15 19:44:52 -05:00
|
|
|
|
|
|
|
// FIXME: Remove me; this is a botch to get around rustboot's bad typechecker.
|
2011-06-15 13:19:50 -05:00
|
|
|
fn empty_mut[T]() -> vec[mutable T] { ret alloc_mut[T](0u); }
|
2011-04-15 19:44:52 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
type init_op[T] = fn(uint) -> T ;
|
2010-07-05 16:42:12 -05:00
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn init_fn[@T](op: &init_op[T], n_elts: uint) -> vec[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let v: vec[T] = alloc[T](n_elts);
|
|
|
|
let i: uint = 0u;
|
|
|
|
while i < n_elts { v += [op(i)]; i += 1u; }
|
2010-09-22 17:44:13 -05:00
|
|
|
ret v;
|
2010-07-05 16:42:12 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn init_fn_mut[@T](op: &init_op[T], n_elts: uint) -> vec[mutable T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let v: vec[mutable T] = alloc_mut[T](n_elts);
|
|
|
|
let i: uint = 0u;
|
|
|
|
while i < n_elts { v += [mutable op(i)]; i += 1u; }
|
2011-03-18 15:53:49 -05:00
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
2011-06-28 20:46:46 -05:00
|
|
|
// init_elt: creates and returns a vector of length n_elts, filled with
|
|
|
|
// that many copies of element t.
|
2011-07-29 14:58:52 -05:00
|
|
|
fn init_elt[@T](t: &T, n_elts: uint) -> vec[T] {
|
2010-09-22 17:44:13 -05:00
|
|
|
/**
|
|
|
|
* FIXME (issue #81): should be:
|
|
|
|
*
|
|
|
|
* fn elt_op[T](&T x, uint i) -> T { ret x; }
|
|
|
|
* let init_op[T] inner = bind elt_op[T](t, _);
|
|
|
|
* ret init_fn[T](inner, n_elts);
|
|
|
|
*/
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
let v: vec[T] = alloc[T](n_elts);
|
|
|
|
let i: uint = n_elts;
|
|
|
|
while i > 0u { i -= 1u; v += [t]; }
|
2010-09-22 17:44:13 -05:00
|
|
|
ret v;
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn init_elt_mut[@T](t: &T, n_elts: uint) -> vec[mutable T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let v: vec[mutable T] = alloc_mut[T](n_elts);
|
|
|
|
let i: uint = n_elts;
|
|
|
|
while i > 0u { i -= 1u; v += [mutable t]; }
|
2011-03-18 15:53:49 -05:00
|
|
|
ret v;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn buf[T](v: array[T]) -> vbuf { ret rustrt::vec_buf[T](v, 0u); }
|
2010-08-19 19:37:22 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn len[T](v: array[T]) -> uint { ret rustrt::vec_len[T](v); }
|
2010-06-23 23:03:09 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn len_set[T](v: array[T], n: uint) { rustrt::vec_len_set[T](v, n); }
|
2010-08-04 14:59:48 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn buf_off[T](v: array[T], offset: uint) -> vbuf {
|
2011-06-15 13:19:50 -05:00
|
|
|
assert (offset < len[T](v));
|
2011-05-12 10:24:54 -05:00
|
|
|
ret rustrt::vec_buf[T](v, offset);
|
2010-06-23 23:03:09 -05:00
|
|
|
}
|
2010-06-29 01:18:51 -05:00
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn print_debug_info[T](v: array[T]) { rustrt::vec_print_debug_info[T](v); }
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-06-25 00:17:17 -05:00
|
|
|
// FIXME: typestate precondition (list is non-empty)
|
2011-03-21 19:21:35 -05:00
|
|
|
// Returns the last element of v.
|
2011-07-29 14:58:52 -05:00
|
|
|
fn last[@T](v: array[T]) -> option::t[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let l = len[T](v);
|
|
|
|
if l == 0u { ret none[T]; }
|
2011-03-21 19:21:35 -05:00
|
|
|
ret some[T](v.(l - 1u));
|
|
|
|
}
|
|
|
|
|
2011-05-02 19:47:24 -05:00
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
// Returns elements from [start..end) from v.
|
2011-07-29 14:58:52 -05:00
|
|
|
fn slice[@T](v: array[T], start: uint, end: uint) -> vec[T] {
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (start <= end);
|
|
|
|
assert (end <= len[T](v));
|
2011-07-27 07:19:39 -05:00
|
|
|
let result = alloc[T](end - start);
|
|
|
|
let i: uint = start;
|
|
|
|
while i < end { result += [v.(i)]; i += 1u; }
|
2010-09-22 17:44:13 -05:00
|
|
|
ret result;
|
2010-07-11 16:51:02 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-06-03 18:14:29 -05:00
|
|
|
// FIXME: Should go away eventually.
|
2011-07-29 14:58:52 -05:00
|
|
|
fn slice_mut[@T](v: array[T], start: uint, end: uint) -> vec[mutable T] {
|
2011-06-03 18:14:29 -05:00
|
|
|
assert (start <= end);
|
|
|
|
assert (end <= len[T](v));
|
2011-07-27 07:19:39 -05:00
|
|
|
let result = alloc_mut[T](end - start);
|
|
|
|
let i: uint = start;
|
|
|
|
while i < end { result += [mutable v.(i)]; i += 1u; }
|
2011-06-03 18:14:29 -05:00
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn shift[@T](v: &mutable array[T]) -> T {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ln = len[T](v);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (ln > 0u);
|
2011-07-27 07:19:39 -05:00
|
|
|
let e = v.(0);
|
2011-03-16 16:58:02 -05:00
|
|
|
v = slice[T](v, 1u, ln);
|
|
|
|
ret e;
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn pop[@T](v: &mutable array[T]) -> T {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ln = len[T](v);
|
2011-05-02 19:47:24 -05:00
|
|
|
assert (ln > 0u);
|
2011-03-16 16:58:02 -05:00
|
|
|
ln -= 1u;
|
2011-07-27 07:19:39 -05:00
|
|
|
let e = v.(ln);
|
2011-03-16 16:58:02 -05:00
|
|
|
v = slice[T](v, 0u, ln);
|
|
|
|
ret e;
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn top[@T](v: &array[T]) -> T {
|
2011-07-27 07:19:39 -05:00
|
|
|
let ln = len[T](v);
|
2011-06-06 17:48:36 -05:00
|
|
|
assert (ln > 0u);
|
2011-06-15 13:19:50 -05:00
|
|
|
ret v.(ln - 1u);
|
2011-06-06 17:48:36 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn push[@T](v: &mutable array[T], t: &T) { v += [t]; }
|
2010-10-22 13:46:04 -05:00
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn unshift[@T](v: &mutable array[T], t: &T) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rs = alloc[T](len[T](v) + 1u);
|
2011-06-24 10:55:02 -05:00
|
|
|
rs += [t];
|
|
|
|
rs += v;
|
|
|
|
v = rs;
|
2010-10-22 13:46:04 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn grow[@T](v: &mutable array[T], n: uint, initval: &T) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = n;
|
|
|
|
while i > 0u { i -= 1u; v += [initval]; }
|
2010-06-29 01:18:51 -05:00
|
|
|
}
|
2010-07-05 16:42:12 -05:00
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn grow_set[@T](v: &mutable vec[mutable T], index: uint, initval: &T,
|
2011-07-27 07:19:39 -05:00
|
|
|
val: &T) {
|
|
|
|
let length = vec::len(v);
|
|
|
|
if index >= length { grow(v, index - length + 1u, initval); }
|
2011-04-15 19:44:52 -05:00
|
|
|
v.(index) = val;
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn grow_init_fn[@T](v: &mutable array[T], n: uint, init_fn: fn() -> T ) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = n;
|
|
|
|
while i > 0u { i -= 1u; v += [init_fn()]; }
|
2011-05-19 18:05:35 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn grow_init_fn_set[@T](v: &mutable array[T], index: uint, init_fn: fn() -> T,
|
2011-07-27 07:19:39 -05:00
|
|
|
val: &T) {
|
|
|
|
let length = vec::len(v);
|
|
|
|
if index >= length { grow_init_fn(v, index - length + 1u, init_fn); }
|
2011-05-19 18:05:35 -05:00
|
|
|
v.(index) = val;
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn map[@T, @U](f: &fn(&T) -> U , v: &vec[T]) -> vec[U] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rs: vec[U] = alloc[U](len[T](v));
|
|
|
|
for ve: T in v { rs += [f(ve)]; }
|
2011-06-24 10:55:02 -05:00
|
|
|
ret rs;
|
2011-05-23 20:50:33 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn filter_map[@T, @U](f: &fn(&T) -> option::t[U] , v: &vec[T]) -> vec[U] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rs: vec[U] = [];
|
|
|
|
for ve: T in v { alt f(ve) { some(elt) { rs += [elt]; } none. { } } }
|
2011-06-24 10:55:02 -05:00
|
|
|
ret rs;
|
2010-07-05 16:42:12 -05:00
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn map2[@T, @U, @V](f: &operator2[T, U, V], v0: &vec[T], v1: &vec[U])
|
|
|
|
-> vec[V] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let v0_len = len[T](v0);
|
|
|
|
if v0_len != len[U](v1) { fail; }
|
|
|
|
let u: vec[V] = alloc[V](v0_len);
|
|
|
|
let i = 0u;
|
|
|
|
while i < v0_len { u += [f({ v0.(i) }, { v1.(i) })]; i += 1u; }
|
2010-11-09 17:35:40 -06:00
|
|
|
ret u;
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn find[@T](f: fn(&T) -> bool , v: &vec[T]) -> option::t[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
for elt: T in v { if f(elt) { ret some[T](elt); } }
|
2011-03-24 14:12:04 -05:00
|
|
|
ret none[T];
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn position[@T](x: &T, v: &array[T]) -> option::t[uint] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = 0u;
|
|
|
|
while i < len(v) { if x == v.(i) { ret some[uint](i); } i += 1u; }
|
2011-06-20 19:26:17 -05:00
|
|
|
ret none[uint];
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn position_pred[T](f: fn(&T) -> bool , v: &vec[T]) -> option::t[uint] {
|
|
|
|
let i: uint = 0u;
|
|
|
|
while i < len(v) { if f(v.(i)) { ret some[uint](i); } i += 1u; }
|
2011-06-20 19:26:17 -05:00
|
|
|
ret none[uint];
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn member[T](x: &T, v: &array[T]) -> bool {
|
2011-07-28 05:01:45 -05:00
|
|
|
for elt: T in v { if x == elt { ret true; } }
|
2011-06-09 03:58:31 -05:00
|
|
|
ret false;
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn count[T](x: &T, v: &array[T]) -> uint {
|
|
|
|
let cnt = 0u;
|
|
|
|
for elt: T in v { if x == elt { cnt += 1u; } }
|
2011-06-10 09:39:09 -05:00
|
|
|
ret cnt;
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn foldl[@T, @U](p: fn(&U, &T) -> U , z: &U, v: &vec[T]) -> U {
|
2011-07-27 07:19:39 -05:00
|
|
|
let sz = len[T](v);
|
|
|
|
if sz == 0u {
|
2011-03-24 14:12:04 -05:00
|
|
|
ret z;
|
2011-06-15 13:19:50 -05:00
|
|
|
} else {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rest = slice[T](v, 1u, sz);
|
2011-06-15 13:19:50 -05:00
|
|
|
ret p(foldl[T, U](p, z, rest), v.(0));
|
2011-03-24 14:12:04 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn unzip[@T, @U](v: &vec[{_0: T, _1: U}]) -> {_0: vec[T], _1: vec[U]} {
|
2011-07-27 07:19:39 -05:00
|
|
|
let sz = len(v);
|
|
|
|
if sz == 0u {
|
|
|
|
ret {_0: alloc[T](0u), _1: alloc[U](0u)};
|
2011-06-15 13:19:50 -05:00
|
|
|
} else {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rest = slice(v, 1u, sz);
|
|
|
|
let tl = unzip[T, U](rest);
|
|
|
|
let a = [v.(0)._0];
|
|
|
|
let b = [v.(0)._1];
|
|
|
|
ret {_0: a + tl._0, _1: b + tl._1};
|
2011-04-06 19:56:44 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-06-09 11:48:16 -05:00
|
|
|
// FIXME make the lengths being equal a constraint
|
2011-07-29 14:58:52 -05:00
|
|
|
fn zip[@T, @U](v: &vec[T], u: &vec[U]) -> vec[{_0: T, _1: U}] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let sz = len(v);
|
2011-07-26 07:06:02 -05:00
|
|
|
assert (sz == len(u));
|
2011-07-27 07:19:39 -05:00
|
|
|
if sz == 0u {
|
2011-07-26 07:06:02 -05:00
|
|
|
ret alloc(0u);
|
2011-06-15 13:19:50 -05:00
|
|
|
} else {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rest = zip(slice(v, 1u, sz), slice(u, 1u, sz));
|
|
|
|
vec::push(rest, {_0: v.(0), _1: u.(0)});
|
2011-06-09 11:48:16 -05:00
|
|
|
ret rest;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn or(v: &vec[bool]) -> bool {
|
|
|
|
let f = orb;
|
2011-05-17 13:41:41 -05:00
|
|
|
ret vec::foldl[bool, bool](f, false, v);
|
2011-04-06 19:56:44 -05:00
|
|
|
}
|
|
|
|
|
2011-07-27 07:19:39 -05:00
|
|
|
fn any[T](f: &fn(&T) -> bool , v: &vec[T]) -> bool {
|
|
|
|
for t: T in v { if f(t) { ret true; } }
|
2011-07-05 04:48:19 -05:00
|
|
|
ret false;
|
|
|
|
}
|
2011-07-27 07:19:39 -05:00
|
|
|
fn all[T](f: &fn(&T) -> bool , v: &vec[T]) -> bool {
|
|
|
|
for t: T in v { if !f(t) { ret false; } }
|
2011-07-05 04:48:19 -05:00
|
|
|
ret true;
|
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn clone[@T](v: &vec[T]) -> vec[T] { ret slice[T](v, 0u, len[T](v)); }
|
2011-04-13 17:34:10 -05:00
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn plus_option[@T](v: &mutable vec[T], o: &option::t[T]) {
|
2011-07-27 07:19:39 -05:00
|
|
|
alt o { none. { } some(x) { v += [x]; } }
|
2011-04-13 17:34:10 -05:00
|
|
|
}
|
|
|
|
|
2011-07-29 14:58:52 -05:00
|
|
|
fn cat_options[@T](v: &vec[option::t[T]]) -> vec[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rs: vec[T] = [];
|
|
|
|
for o: option::t[T] in v { alt o { none. { } some(t) { rs += [t]; } } }
|
2011-06-24 10:55:02 -05:00
|
|
|
ret rs;
|
Further work on typestate_check
Lots of work on typestate_check, seems to get a lot of the way
through checking the standard library.
* Added for, for_each, assign_op, bind, cast, put, check, break,
and cont. (I'm not sure break and cont are actually handled correctly.)
* Fixed side-effect bug in seq_preconds so that unioning the
preconditions of a sequence of statements or expressions
is handled correctly.
* Pass poststate correctly through a stmt_decl.
* Handle expr_ret and expr_fail properly (after execution of a ret
or fail, everything is true -- this is needed to handle ifs and alts
where one branch is a ret or fail)
* Fixed bug in set_prestate_ann where a thing that needed to be
mutated wasn't getting passed as an alias
* Fixed bug in how expr_alt was treated (zero is not the identity
for intersect, who knew, right?)
* Update logging to reflect log_err vs. log
* Fixed find_locals so as to return all local decls and exclude
function arguments.
* Make union_postconds work on an empty vector (needed to handle
empty blocks correctly)
* Added _vec.cat_options, which takes a list of option[T] to a list
of T, ignoring any Nones
* Added two test cases.
2011-04-20 14:11:01 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-04-15 19:44:52 -05:00
|
|
|
// TODO: Remove in favor of built-in "freeze" operation when it's implemented.
|
2011-07-29 14:58:52 -05:00
|
|
|
fn freeze[@T](v: vec[mutable T]) -> vec[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let result: vec[T] = [];
|
|
|
|
for elem: T in v { result += [elem]; }
|
2011-04-15 19:44:52 -05:00
|
|
|
ret result;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-19 18:43:46 -05:00
|
|
|
// Swaps two elements in a vector
|
2011-07-29 14:58:52 -05:00
|
|
|
fn swap[@T](v: &vec[mutable T], a: uint, b: uint) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let t: T = v.(a);
|
2011-05-19 18:43:46 -05:00
|
|
|
v.(a) = v.(b);
|
|
|
|
v.(b) = t;
|
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-19 18:43:46 -05:00
|
|
|
// In place vector reversal
|
2011-07-29 14:58:52 -05:00
|
|
|
fn reverse[@T](v: &vec[mutable T]) {
|
2011-07-27 07:19:39 -05:00
|
|
|
let i: uint = 0u;
|
|
|
|
let ln = len[T](v);
|
|
|
|
while i < ln / 2u { swap(v, i, ln - i - 1u); i += 1u; }
|
2011-05-19 18:43:46 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-19 18:43:46 -05:00
|
|
|
// Functional vector reversal. Returns a reversed copy of v.
|
2011-07-29 14:58:52 -05:00
|
|
|
fn reversed[@T](v: vec[T]) -> vec[T] {
|
2011-07-27 07:19:39 -05:00
|
|
|
let rs: vec[T] = [];
|
|
|
|
let i = len[T](v);
|
|
|
|
if i == 0u { ret rs; } else { i -= 1u; }
|
|
|
|
while i != 0u { push[T](rs, v.(i)); i -= 1u; }
|
2011-06-24 10:55:02 -05:00
|
|
|
push[T](rs, v.(0));
|
|
|
|
ret rs;
|
2011-05-19 18:43:46 -05:00
|
|
|
}
|
|
|
|
|
2011-06-15 13:19:50 -05:00
|
|
|
|
2011-05-20 20:36:35 -05:00
|
|
|
/// Truncates the vector to length `new_len`.
|
|
|
|
/// FIXME: This relies on a typechecker bug (covariance vs. invariance).
|
2011-07-29 14:58:52 -05:00
|
|
|
fn truncate[@T](v: &mutable vec[mutable? T], new_len: uint) {
|
2011-05-20 20:36:35 -05:00
|
|
|
v = slice[T](v, 0u, new_len);
|
|
|
|
}
|
2010-09-22 17:44:13 -05:00
|
|
|
// Local Variables:
|
|
|
|
// mode: rust;
|
|
|
|
// fill-column: 78;
|
|
|
|
// indent-tabs-mode: nil
|
|
|
|
// c-basic-offset: 4
|
|
|
|
// buffer-file-coding-system: utf-8-unix
|
2011-06-15 14:01:19 -05:00
|
|
|
// compile-command: "make -k -C $RBUILD 2>&1 | sed -e 's/\\/x\\//x:\\//g'";
|
2010-09-22 17:44:13 -05:00
|
|
|
// End:
|