-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSetsunaShiki.h
81 lines (50 loc) · 2.2 KB
/
SetsunaShiki.h
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
#pragma once
#include <utility>
#include <algorithm>
#define Auto decltype(auto)
namespace SetsunaShiki {
//
template <class RandomAccessIterator>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last, RandomAccessIterator const change);
template <class RandomAccessIterator, class Compare>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last, RandomAccessIterator const change, Compare comp);
//
template <class RandomAccessIterator, class Compare> class Private
{
private:
static void InsertLower(RandomAccessIterator b, RandomAccessIterator e)
{
auto v = std::move(b[0]);
while (b-- != e) b[1] = std::move(b[0]);
b[1] = std::move(v);
}
static void InsertUpper(RandomAccessIterator b, RandomAccessIterator e)
{
auto v = std::move(b[0]);
while (++b != e) b[-1] = std::move(b[0]);
b[-1] = std::move(v);
}
public:
static void Sort(RandomAccessIterator const first, RandomAccessIterator const last, RandomAccessIterator const change, Compare comp)
{
if (std::distance(first, last) > 1){
if (std::distance(first, change) > 0 && comp(change[0], change[-1])){
InsertLower(change, std::upper_bound(first, change, *change, comp));
} else if (std::distance(change+1, last) > 0 && comp(change[1], change[0])){
InsertUpper(change, std::lower_bound(change+1, last, *change, comp));
}
}
}
};
template <class RandomAccessIterator>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last, RandomAccessIterator const change)
{
SetsunaShiki::sort(first, last, change, std::less<typename std::iterator_traits<RandomAccessIterator>::value_type>());
}
template <class RandomAccessIterator, class Compare>
inline void sort(RandomAccessIterator const first, RandomAccessIterator const last, RandomAccessIterator const change, Compare comp)
{
SetsunaShiki::Private<RandomAccessIterator, Compare>::Sort(first, last, change, comp);
}
}
#undef Auto