From bdbfdc92b58d5c9f8654e430075dab543d1ba394 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Tue, 7 May 2019 22:52:43 -0700 Subject: [PATCH] [iter] Add value and projection to hb_all/any/none Allows for eg, checking all values equal 2: hb_all (it, 2). --- src/hb-iter.hh | 24 ++++++++++++++++++------ src/test-iter.cc | 10 +++++++++- 2 files changed, 27 insertions(+), 7 deletions(-) diff --git a/src/hb-iter.hh b/src/hb-iter.hh index 74cfbc8..192347d 100644 --- a/src/hb-iter.hh +++ b/src/hb-iter.hh @@ -649,11 +649,15 @@ HB_FUNCOBJ (hb_unzip); struct { template - bool operator () (Iterable&& c) const + bool operator () (Iterable&& c, + Val v = true, + Proj&& f = hb_identity) const { for (auto it = hb_iter (c); it; ++it) - if (!*it) + if (!((Val) hb_get (hb_forward (f), *it) == v)) return false; return true; } @@ -662,11 +666,15 @@ HB_FUNCOBJ (hb_all); struct { template - bool operator () (Iterable&& c) const + bool operator () (Iterable&& c, + Val v = true, + Proj&& f = hb_identity) const { for (auto it = hb_iter (c); it; ++it) - if (*it) + if (((Val) hb_get (hb_forward (f), *it) == v)) return true; return false; } @@ -675,11 +683,15 @@ HB_FUNCOBJ (hb_any); struct { template - bool operator () (Iterable&& c) const + bool operator () (Iterable&& c, + Val v = true, + Proj&& f = hb_identity) const { for (auto it = hb_iter (c); it; ++it) - if (*it) + if (((Val) hb_get (hb_forward (f), *it) == v)) return false; return true; } diff --git a/src/test-iter.cc b/src/test-iter.cc index bd99f83..70952f3 100644 --- a/src/test-iter.cc +++ b/src/test-iter.cc @@ -140,6 +140,7 @@ main (int argc, char **argv) test_iterable (v); hb_set_t st; + st << 1 << 15 << 43; test_iterable (st); hb_sorted_array_t sa; (void) static_cast, hb_sorted_array_t::item_t>&> (sa); @@ -162,7 +163,14 @@ main (int argc, char **argv) test_iterator_non_default_constructable (hb_iter (st) | hb_filter ()); test_iterator_non_default_constructable (hb_iter (st) | hb_map (hb_identity)); - hb_any (st); + assert (true == hb_all (st)); + assert (false == hb_all (st, 42u)); + assert (true == hb_any (st)); + assert (false == hb_any (st, 14)); + assert (true == hb_any (st, 15)); + assert (false == hb_none (st)); + assert (false == hb_none (st, 15)); + assert (true == hb_none (st, 17)); hb_array_t> pa; pa->as_array (); -- 2.7.4