forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquotientRemainder.cpp
More file actions
42 lines (36 loc) · 1.08 KB
/
quotientRemainder.cpp
File metadata and controls
42 lines (36 loc) · 1.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#define NO_SCALAR_REFERENCES_USED_IN_PATTERNS 1
#include "matchit.h"
#include <iostream>
constexpr auto qr = [](auto divisor)
{
using namespace matchit;
return [divisor](auto quotient, auto remainder)
{
return and_(app(_ / divisor, quotient), app(_ % divisor, remainder));
};
};
constexpr std::array<int32_t, 2> quoRem(int32_t dividend, int32_t divisor)
{
using namespace matchit;
Id<int32_t> q;
Id<int32_t> r;
return match(dividend)(
// clang-format off
pattern | qr(divisor)(q, r) = [&] { return std::array<int32_t, 2>{*q, *r}; },
pattern | _ = [&] { return std::array<int32_t, 2>{0, 0}; }
// clang-format on
);
}
// todo: make Id variants for Id<T> Id<T&> Id<T const&> Id<T&&>
constexpr auto qrResult1 = quoRem(12, 6);
static_assert(qrResult1[0] == 2);
static_assert(qrResult1[1] == 0);
constexpr auto qrResult2 = quoRem(12, -5);
static_assert(qrResult2[0] == -2);
static_assert(qrResult2[1] == 2);
int32_t main()
{
auto const result = quoRem(12, 7);
std::cout << result[0] << "\t" << result[1] << std::endl;
return 0;
}