From 10a7e83461ac62b8d51981c3cfaeac584700417f Mon Sep 17 00:00:00 2001 From: "Keith A. Lewis" Date: Sat, 1 Jun 2024 17:01:46 -0400 Subject: [PATCH] pair --- fms_iterable.h | 29 +++++++++++++++++++++++++++++ fms_iterable.t.cpp | 14 ++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/fms_iterable.h b/fms_iterable.h index c2b03a9..cd1874b 100644 --- a/fms_iterable.h +++ b/fms_iterable.h @@ -1271,6 +1271,35 @@ namespace fms::iterable { return delta(i, [](T a, T b) { return std::min(b - a, 0); }); } + template + class pair { + I i; + J j; + public: + using iterator_category = std::input_iterator_tag; + using value_type = std::pair; + + pair(I i, J j) + : i(i), j(j) + { } + + explicit operator bool() const + { + return i and j; + } + value_type operator*() const + { + return { *i, *j }; + } + pair& operator++() + { + ++i; + ++j; + + return *this; + } + }; + } // namespace fms::iterable #define FMS_ITERABLE_OPERATOR(X) \ diff --git a/fms_iterable.t.cpp b/fms_iterable.t.cpp index e7af33e..df88316 100644 --- a/fms_iterable.t.cpp +++ b/fms_iterable.t.cpp @@ -695,6 +695,19 @@ int test_exp() { return 0; } +int test_pair() +{ + { + pair p(iota(1), iota(2)); + assert(p); + assert(*p == std::make_pair(1,2)); + + ++p; + assert(p); + assert(*p == std::make_pair(2, 3)); + } + return 0; +} int main() { @@ -721,6 +734,7 @@ int main() test_delta(); test_call(); test_exp(); + test_pair(); return 0; }