Add is_empty, is_not_empty preds to std::ivec

This commit is contained in:
Brian Anderson 2011-07-14 15:32:37 -07:00
parent 8afb1a7c63
commit 139aaa1616
2 changed files with 43 additions and 0 deletions

View file

@ -72,6 +72,18 @@ fn init_elt_mut[T](&T t, uint n_elts) -> T[mutable] {
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
@ -247,3 +259,11 @@ mod unsafe {
}
}
// 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:

View file

@ -84,6 +84,16 @@ fn test_init_elt() {
assert (v.(5) == 20u);
}
fn test_is_empty() {
assert ivec::is_empty[int](~[]);
assert !ivec::is_empty(~[0]);
}
fn test_is_not_empty() {
assert ivec::is_not_empty(~[0]);
assert !ivec::is_not_empty[int](~[]);
}
fn test_last() {
auto n = ivec::last(~[]);
assert (n == none);
@ -240,6 +250,10 @@ fn main() {
test_reserve_and_on_heap();
test_unsafe_ptrs();
// Predicates
test_is_empty();
test_is_not_empty();
// Accessors
test_init_fn();
test_init_elt();
@ -261,3 +275,12 @@ fn main() {
test_any_and_all();
}
// 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: