-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpairwise.cpp
More file actions
295 lines (231 loc) · 9.75 KB
/
pairwise.cpp
File metadata and controls
295 lines (231 loc) · 9.75 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
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
// Logicwise
// Copyright (c) 2026 Frog Singing (@frog-singing)
// SPDX-License-Identifier: MIT
#include <logicwise.h>
#include <variant> // std::variant, std::visit
#include <array> // std::array
#include <span> // std::span
#include <concepts> // std::same_as
#include <type_traits> // std::is_same
#include <utility> // std::move
#include <iostream> // std::cout, std::boolalpha, std::endl
// If this file compiles successfully, then all logical assertions have passed.
namespace logicwise::test
{
struct spring; struct summer; struct autumn; struct winter;
template<typename Season> struct season_trait;
template<> struct season_trait<spring> { using next_season = summer; };
template<> struct season_trait<summer> { using next_season = autumn; };
template<> struct season_trait<autumn> { using next_season = winter; };
template<> struct season_trait<winter> { using next_season = spring; };
template<typename ThisSeason>
struct season
{
using next_season = typename season_trait<ThisSeason>::next_season;
int year;
};
struct spring : season<spring>
{ constexpr int year_of_next_season() const noexcept { return year; } };
struct summer : season<summer>
{ constexpr int year_of_next_season() const noexcept { return year; } };
struct autumn : season<autumn>
{ constexpr int year_of_next_season() const noexcept { return year; } };
struct winter : season<winter>
{ constexpr int year_of_next_season() const noexcept { return year + 1; } };
//--------------------------------------------------------------------------------
template<typename T1, typename T2>
struct is_same_with_operator_bool
{
constexpr operator bool() const noexcept { return false; }
};
template<typename T>
struct is_same_with_operator_bool<T, T>
{
constexpr operator bool() const noexcept { return true; }
};
template<auto S1, auto S2>
struct is_same_year_with_value
{
static constexpr bool value{ S1.year == S2.year };
};
template<auto S1, auto S2>
struct is_same_year_with_opeartor_bool
{
constexpr operator bool() const noexcept { return S1.year == S2.year; }
};
//--------------------------------------------------------------------------------
enum struct plan_choice { travel, create, rest, study, work, celebrate };
template<auto SeasonInstance> struct seasonal_plan { plan_choice plan; };
//--------------------------------------------------------------------------------
enum struct difficulty_level { easy, medium, hard };
template<auto SeasonInstance>
struct seasonal_challenge : seasonal_plan<SeasonInstance>
{
difficulty_level difficulty;
constexpr seasonal_challenge(seasonal_plan<SeasonInstance> p, difficulty_level d)
: seasonal_plan<SeasonInstance>{ p }, difficulty{ d } {}
};
template<auto SeasonInstance>
seasonal_challenge(seasonal_plan<SeasonInstance>, difficulty_level)
-> seasonal_challenge<SeasonInstance>;
}
int main()
{
using namespace logicwise;
using namespace logicwise::quantifier;
using namespace logicwise::arrangement; // check this out in logicwise/arrangement.h
using namespace logicwise::wrapper;
using namespace logicwise::container; // for to_variant_array
using namespace logicwise::test;
// type wrapper --------------------------------------------------------------------------------
static_assert(
rangewise<none_of, combination_pair>
::in<type_list<spring, summer, autumn, winter>>()
.satisfies([] <typename S1, typename S2> { return std::same_as<S1, S2>; }),
"type wrapper: distinct seasons via lambda"
);
static_assert(
rangewise<none_of, combination_pair>
::in<type_list<spring, summer, autumn, winter>>()
.satisfies<std::is_same>(),
"type wrapper: distinct seasons via trait with value"
);
static_assert(
rangewise<none_of, combination_pair>
::in<type_list<spring, summer, autumn, winter>>()
.satisfies<is_same_with_operator_bool>(),
"type wrapper: distinct seasons via trait with operator bool"
);
static_assert(
rangewise<all_of, linear_adjacent_pair>
::in<type_list<spring, summer, autumn>>()
.satisfies([] <typename S1, typename S2> { return std::same_as<typename S1::next_season, S2>; }),
"type wrapper: season flow"
);
// try to add more following seasons
static_assert(
rangewise<all_of, circular_adjacent_pair>
::in<type_list<autumn, winter, spring, summer>>()
.satisfies([] <typename S1, typename S2> { return std::same_as<typename S1::next_season, S2>; }),
"type wrapper: season loop"
);
// try to use type_list<autumn, winter, spring, summer, autumn, winter, spring, summer> instead
static_assert(
rangewise<exactly<1>, combination_pair>
::in<type_list<winter, autumn, summer, spring>>()
.satisfies([] <typename S1, typename S2> { return std::same_as<typename S1::next_season, S2>; }),
"type wrapper: one-way matching"
);
static_assert(
rangewise<exactly<4>, permutation_pair>
::in<type_list<winter, autumn, summer, spring>>()
.satisfies([] <typename S1, typename S2> { return std::same_as<typename S1::next_season, S2>; }),
"type wrapper: two-way matching"
);
// value wrapper --------------------------------------------------------------------------------
static_assert(
rangewise<all_of, linear_adjacent_pair>
::in<value_list<spring{ 2026 }, summer{ 2026 }, autumn{ 2026 }, winter{ 2026 }>> ()
.satisfies([] <auto S1, auto S2> { return S1.year == S2.year; }),
"value wrapper: seasons in the same year via lambda"
);
static_assert(
rangewise<all_of, linear_adjacent_pair>
::in<value_list<spring{ 2026 }, summer{ 2026 }, autumn{ 2026 }, winter{ 2026 }>> ()
.satisfies<is_same_year_with_value>(),
"value wrapper: seasons in the same year via trait with value"
);
static_assert(
rangewise<all_of, linear_adjacent_pair>
::in<value_list<spring{ 2026 }, summer{ 2026 }, autumn{ 2026 }, winter{ 2026 }>> ()
.satisfies<is_same_year_with_opeartor_bool>(),
"value wrapper: seasons in the same year via trait with operator bool"
);
static constexpr auto abs = [] (int x) { return x >= 0 ? x : -x; };
static_assert(
rangewise<all_of, combination_pair>
::in<value_list<spring{ 2022 }, spring{ 2024 }, spring{ 2026 }, spring{ 2028 }>> ()
.satisfies([] <auto S1, auto S2> { return abs(S1.year - S2.year) < 10; }),
"value wrapper: springs within a decade"
);
static constexpr auto one_way_adjacency = [] <auto S1, auto S2> {
return std::same_as<typename decltype(S1)::next_season, decltype(S2)> &&
S1.year_of_next_season() == S2.year;
};
static_assert(
rangewise<all_of, linear_adjacent_pair>
::in<value_list<autumn{ 2026 }, winter{ 2026 }, spring{ 2027 }, summer{ 2027 }>> ()
.satisfies(one_way_adjacency),
"value wrapper: season instance flow"
);
static_assert(
rangewise<none_of, permutation_pair>
::in<value_list<summer{ 2027 }, winter{ 2026 }, summer{ 2026 }, winter{ 2025 }>> ()
.satisfies(one_way_adjacency),
"value wrapper: scattered season instances"
);
// compile-time vector-like container --------------------------------------------------------------------------------
static constexpr seasonal_plan<spring{ 2026 }> my_plan_pool_for_spring_2026[4]
{
{ plan_choice::travel }, { plan_choice::rest }, { plan_choice::study }, { plan_choice::work }
};
using value_list_of_seasonal_plan_2026 = value_list<
my_plan_pool_for_spring_2026[0],
seasonal_plan<summer{ 2026 }>{ plan_choice::study },
seasonal_plan<autumn{ 2026 }>{ plan_choice::create },
seasonal_plan<winter{ 2026 }>{ plan_choice::celebrate }
>;
static constexpr auto seasonal_plan_2026 = value_list_of_seasonal_plan_2026{};
static_assert(
rangewise<none_of, combination_pair>
::in(std::move(my_plan_pool_for_spring_2026))
.satisfies([] (auto&& p1, auto&& p2) { return p1.plan == p2.plan; }),
"compile-time container: distinct plan choices"
);
static_assert(
rangewise<none_of, linear_adjacent_pair>
::in(to_variant_array(seasonal_plan_2026))
.satisfies([] (auto&& v1, auto&& v2) {
return std::visit([] (auto&& p1, auto&& p2) { return p1.plan == p2.plan; }, v1, v2);
}),
"compile-time container: discontinuous seasonal plans"
);
// runtime vector-like container --------------------------------------------------------------------------------
std::cout << std::boolalpha;
std::array<std::variant<
seasonal_challenge<spring{ 2026 }>,
seasonal_challenge<summer{ 2026 }>,
seasonal_challenge<autumn{ 2026 }>,
seasonal_challenge<winter{ 2026 }>
>, 4> seasonal_challenge_2026
{
seasonal_challenge{ get<0>(seasonal_plan_2026), difficulty_level::easy }, // travel in spring
seasonal_challenge{ get<1>(seasonal_plan_2026), difficulty_level::easy }, // study in summer
seasonal_challenge{ get<2>(seasonal_plan_2026), difficulty_level::medium }, // create in autumn
seasonal_challenge{ get<3>(seasonal_plan_2026), difficulty_level::easy } // celebrate in winter
};
// studying is hard instead of easy
std::visit([] (auto&& challenge) {
challenge.difficulty = difficulty_level::hard;
}, seasonal_challenge_2026[1]);
bool most_challenges_not_hard =
rangewise<quantifier::at_least<3>, arrangement::element>
::in(std::span{ seasonal_challenge_2026 })
.satisfies([] (auto&& variant) {
return std::visit([] (auto&& challenge) {
return challenge.difficulty != difficulty_level::hard;
}, variant);
});
std::cout << "most challenges not hard: " << most_challenges_not_hard << std::endl;
const auto& const_ref_of_seasonal_challenge_2026 = seasonal_challenge_2026;
bool no_continuous_easy_challenges =
rangewise<none_of, linear_adjacent_pair>
::in(const_ref_of_seasonal_challenge_2026)
.satisfies([] (auto&& v1, auto&& v2) {
return std::visit([] (auto&& c1, auto&& c2) {
return c1.difficulty == difficulty_level::easy && c2.difficulty == difficulty_level::easy;
}, v1, v2);
});
std::cout << "no continuous easy challenges: " << no_continuous_easy_challenges << std::endl;
std::cout << std::endl << "Hello Logic!" << std::endl;
}