Skip to content

Commit 1d35c5f

Browse files
add swap.hpp
1 parent 8d46abc commit 1d35c5f

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

mlib.vcxproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,7 @@
168168
<ClInclude Include="string_parse.hpp" />
169169
<ClInclude Include="sub_pack.hpp" />
170170
<ClInclude Include="sub_tuple.hpp" />
171+
<ClInclude Include="swap.hpp" />
171172
<ClInclude Include="times.hpp" />
172173
<ClInclude Include="trait.hpp" />
173174
<ClInclude Include="transform.hpp" />

mlib.vcxproj.filters

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,9 @@
159159
<ClInclude Include="tuple_equal.hpp">
160160
<Filter>Header Files</Filter>
161161
</ClInclude>
162+
<ClInclude Include="swap.hpp">
163+
<Filter>Header Files</Filter>
164+
</ClInclude>
162165
</ItemGroup>
163166
<ItemGroup>
164167
<ClCompile Include="Main.cpp">

swap.hpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
#include<cstddef> // std::size_t
2+
#include<utility> // std::index_sequence
3+
#include<tuple> // std::tuple
4+
5+
#include"get_nth_element.hpp"
6+
7+
namespace mlib
8+
{
9+
// a class to hold compile time data
10+
template<auto... Ts>
11+
struct container
12+
{
13+
template<auto X>
14+
constexpr auto get()
15+
{
16+
return mlib::get_nth_element<X>(Ts...);
17+
}
18+
};
19+
20+
// this is a compile time parameter that is easy to pass into things
21+
template<auto X>
22+
struct c_p__
23+
{
24+
};
25+
26+
// when condition(value) returns TrueValue, else returns FalseValue
27+
template<auto value, auto condition, auto other_condition, auto TrueValue, auto SecondTrueValue, auto FalseValue>
28+
struct when_
29+
{
30+
constexpr auto operator()()
31+
{
32+
if constexpr (condition(value))
33+
{
34+
return TrueValue;
35+
}
36+
if constexpr (other_condition(value))
37+
{
38+
return SecondTrueValue;
39+
}
40+
else
41+
{
42+
return FalseValue(c_p__<value>{});
43+
}
44+
}
45+
};
46+
47+
// this function swaps Index and Other index then returns the tuple.
48+
template<auto Index, auto OtherIndex, auto... Ts>
49+
constexpr auto swap() // -> container
50+
{
51+
return[&]<std::size_t... indexes>(std::index_sequence<indexes...>)
52+
{
53+
return container < (when_ < indexes, [=](auto x) {return x == Index; }, [=](auto x) {return x == OtherIndex; }, mlib::get_nth_element<OtherIndex>(Ts...), mlib::get_nth_element<Index>(Ts...), [=]<auto X>(c_p__<X>) { return mlib::get_nth_element<X>(Ts...); } > {}())... > {};
54+
}(std::make_index_sequence<sizeof...(Ts)>{});
55+
}
56+
} // namespace mlib

0 commit comments

Comments
 (0)