From 16a3540ea4257a19b9bfd9d5300a280e18b423a1 Mon Sep 17 00:00:00 2001 From: Behdad Esfahbod Date: Wed, 15 May 2019 20:48:20 -0700 Subject: [PATCH] [algs] Add hb_bind0 and hb_bind1 --- src/hb-algs.hh | 54 ++++++++++++++++++++++++++++++++++++++++++++++++ src/test-algs.cc | 7 +++++++ 2 files changed, 61 insertions(+) diff --git a/src/hb-algs.hh b/src/hb-algs.hh index f10e27939..17296e17e 100644 --- a/src/hb-algs.hh +++ b/src/hb-algs.hh @@ -119,6 +119,60 @@ struct } HB_FUNCOBJ (hb_invoke); +enum class hb_bind_pos_t +{ + _0, + _1, +}; +template +struct hb_binder_t +{ + hb_binder_t (Appl a, V v) : a (a), v (v) {} + + template auto + operator () (Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl), + hb_declval (V), + hb_declval (Ts)...)) + { + return hb_invoke (hb_forward (a), + hb_forward (v), + hb_forward (ds)...); + } + template auto + operator () (T0&& d0, Ts&& ...ds) -> decltype (hb_invoke (hb_declval (Appl), + hb_declval (T0), + hb_declval (V), + hb_declval (Ts)...)) + { + return hb_invoke (hb_forward (a), + hb_forward (d0), + hb_forward (v), + hb_forward (ds)...); + } + + private: + hb_reference_wrapper a; + V v; +}; +struct +{ + template auto + operator () (Appl&& a, V&& v) const HB_AUTO_RETURN + (( hb_binder_t (a, v) )) +} +HB_FUNCOBJ (hb_bind0); +struct +{ + template auto + operator () (Appl&& a, V&& v) const HB_AUTO_RETURN + (( hb_binder_t (a, v) )) +} +HB_FUNCOBJ (hb_bind1); + struct { private: diff --git a/src/test-algs.cc b/src/test-algs.cc index c4610d9e7..de96bde14 100644 --- a/src/test-algs.cc +++ b/src/test-algs.cc @@ -77,5 +77,12 @@ main (int argc, char **argv) xp = hb_pair_t (nullptr, 1); xp = hb_pair_t (nullptr, 1); + assert (3 == hb_bind0 (hb_min, 3) (4)); + assert (3 == hb_bind0 (hb_min, 4) (3)); + + auto M0 = hb_bind1 (hb_max, 0); + assert (M0 (-2) == 0); + assert (M0 (+2) == 2); + return 0; }