about summary refs log tree commit diff
diff options
context:
space:
mode:
authorBrian Anderson <banderson@mozilla.com>2011-07-14 15:32:37 -0700
committerBrian Anderson <banderson@mozilla.com>2011-07-14 17:13:12 -0700
commit139aaa1616fd341a71f0f11adb4e8850639e228c (patch)
tree9db094c6e50cfe149d5487ba0c38ec89b61732f9
parent8afb1a7c639d2d2959cdc79c544fac7dd5730897 (diff)
Add is_empty, is_not_empty preds to std::ivec
-rw-r--r--src/lib/ivec.rs20
-rw-r--r--src/test/run-pass/lib-ivec.rs23
2 files changed, 43 insertions, 0 deletions
diff --git a/src/lib/ivec.rs b/src/lib/ivec.rs
index 6101163d32d..25e1e0118e8 100644
--- a/src/lib/ivec.rs
+++ b/src/lib/ivec.rs
@@ -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:
diff --git a/src/test/run-pass/lib-ivec.rs b/src/test/run-pass/lib-ivec.rs
index b71a9a545e6..528bfc9395f 100644
--- a/src/test/run-pass/lib-ivec.rs
+++ b/src/test/run-pass/lib-ivec.rs
@@ -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:
+