From 139aaa1616fd341a71f0f11adb4e8850639e228c Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Thu, 14 Jul 2011 15:32:37 -0700 Subject: [PATCH] Add is_empty, is_not_empty preds to std::ivec --- src/lib/ivec.rs | 20 ++++++++++++++++++++ src/test/run-pass/lib-ivec.rs | 23 +++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/src/lib/ivec.rs b/src/lib/ivec.rs index 6101163d32d4..25e1e0118e84 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 b71a9a545e61..528bfc9395f3 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: +