Skip to content

Commit

Permalink
pair
Browse files Browse the repository at this point in the history
  • Loading branch information
keithalewis committed Jun 1, 2024
1 parent b2ac562 commit 10a7e83
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 0 deletions.
29 changes: 29 additions & 0 deletions fms_iterable.h
Original file line number Diff line number Diff line change
Expand Up @@ -1271,6 +1271,35 @@ namespace fms::iterable {
return delta(i, [](T a, T b) { return std::min<T>(b - a, 0); });
}

template<input I, input J>
class pair {
I i;
J j;
public:
using iterator_category = std::input_iterator_tag;
using value_type = std::pair<typename I::value_type, typename J::value_type>;

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) \
Expand Down
14 changes: 14 additions & 0 deletions fms_iterable.t.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -721,6 +734,7 @@ int main()
test_delta();
test_call();
test_exp();
test_pair();

return 0;
}

0 comments on commit 10a7e83

Please sign in to comment.