-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsort.h
More file actions
110 lines (89 loc) · 2.62 KB
/
sort.h
File metadata and controls
110 lines (89 loc) · 2.62 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
/*
Copyright (C) 2017-2026 Topological Manifold
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <algorithm>
#include <type_traits>
namespace ns
{
namespace sort_implementation
{
template <typename T>
void swap_sort(T& a, T& b)
{
if (b < a)
{
std::swap(a, b);
}
}
}
template <typename T>
requires (std::tuple_size_v<std::remove_reference_t<T>> >= 6)
T&& sort(T&& v)
{
std::sort(v.begin(), v.end());
return std::forward<T>(v);
}
template <typename T>
requires (std::tuple_size_v<std::remove_reference_t<T>> == 1)
T&& sort(T&& v)
{
return std::forward<T>(v);
}
template <typename T>
requires (std::tuple_size_v<std::remove_reference_t<T>> == 2)
T&& sort(T&& v)
{
namespace impl = sort_implementation;
impl::swap_sort(v[0], v[1]);
return std::forward<T>(v);
}
template <typename T>
requires (std::tuple_size_v<std::remove_reference_t<T>> == 3)
T&& sort(T&& v)
{
namespace impl = sort_implementation;
impl::swap_sort(v[0], v[1]);
impl::swap_sort(v[0], v[2]);
impl::swap_sort(v[1], v[2]);
return std::forward<T>(v);
}
template <typename T>
requires (std::tuple_size_v<std::remove_reference_t<T>> == 4)
T&& sort(T&& v)
{
namespace impl = sort_implementation;
impl::swap_sort(v[0], v[1]);
impl::swap_sort(v[2], v[3]);
impl::swap_sort(v[0], v[2]);
impl::swap_sort(v[1], v[3]);
impl::swap_sort(v[1], v[2]);
return std::forward<T>(v);
}
template <typename T>
requires (std::tuple_size_v<std::remove_reference_t<T>> == 5)
T&& sort(T&& v)
{
namespace impl = sort_implementation;
impl::swap_sort(v[0], v[1]);
impl::swap_sort(v[2], v[3]);
impl::swap_sort(v[1], v[3]);
impl::swap_sort(v[3], v[4]);
impl::swap_sort(v[0], v[1]);
impl::swap_sort(v[2], v[3]);
impl::swap_sort(v[0], v[2]);
impl::swap_sort(v[1], v[3]);
impl::swap_sort(v[1], v[2]);
return std::forward<T>(v);
}
}