-
Notifications
You must be signed in to change notification settings - Fork 160
/
range_switch_case.cpp
84 lines (80 loc) · 2.07 KB
/
range_switch_case.cpp
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// # Range switch case
//
// http://stackoverflow.com/questions/9432226/how-do-i-select-a-range-of-values-in-a-switch-statement/42331563#42331563
#include "common.hpp"
int main() {
// Test case.
const std::map<int, int> result{
{-1, -1},
{ 0, 0},
{ 1, 0},
{ 2, 2},
{ 3, 2},
{ 4, 2},
{ 5, 5},
{ 6, 5},
{ 7, 7},
};
// Raw lambda map.
{
int x;
const std::map<int,std::function<void()>> m{
{0, [&](){
x = -1;
}},
{2, [&](){
x = 0;
}},
{5, [&](){
x = 2;
}},
{7, [&](){
x = 5;
}}
};
const auto end = m.end();
for (auto i = -1; i < 8; ++i) {
auto it = m.upper_bound(i);
if (it == end) {
x = 7;
} else {
it->second();
}
assert(x == result.at(i));
}
}
// How to use the range pattern from a method.
// The key is to static initialize the lambda map.
{
struct RangeSwitch {
int method(int x) {
static const std::map<int,std::function<void(int&)>> m{
{0, [&](int &x){
x = -1;
}},
{2, [&](int &x){
x = 0;
}},
{5, [&](int &x){
x = 2;
}},
{7, [&](int &x){
x = 5;
}}
};
static const auto end = m.end();
auto it = m.upper_bound(x);
if (it == end) {
x = 7;
} else {
it->second(x);
}
return x;
}
};
RangeSwitch rangeSwitch;
for (auto i = -1; i < 8; ++i) {
assert(rangeSwitch.method(i) == result.at(i));
}
}
}